# [R] using lm() with variable formula

Bert Gunter gunter.berton at gene.com
Thu May 17 18:10:11 CEST 2007

```... and note that if a matrix of responses is on the left of ~ , separate
regressions will be simultaneously fit to each of the columns of the matrix.
Note that this **is** in TFM -- ?lm.

Bert Gunter
Genentech Nonclinical Statistics

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Gabor Grothendieck
Sent: Thursday, May 17, 2007 8:22 AM
To: Chris Elsaesser
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] using lm() with variable formula

Try this:

lm(Sepal.Length ~., iris[1:3])

# or

cn <- c("Sepal.Length", "Sepal.Width", "Petal.Length")
lm(Sepal.Length ~., iris[cn])

On 5/17/07, Chris Elsaesser <chris.elsaesser at spadac.com> wrote:
> New to R; please excuse me if this is a dumb question.  I tried to RTFM;
> didn't help.
>
> I want to do a series of regressions over the columns in a data.frame,
> systematically varying the response variable and the the terms; and not
> necessarily including all the non-response columns.  In my case, the
> columns are time series. I don't know if that makes a difference; it
> does mean I have to call lag() to offset non-response terms. I can not
> assume a specific number of columns in the data.frame; might be 3, might
> be 20.
>
> My central problem is that the formula given to lm() is different each
> time.  For example, say a data.frame had columns with the following
> headings:  height, weight, BP (blood pressure), and Cals (calorie intake
> per time frame).  In that case, I'd need something like the following:
>
>        lm(height ~ weight + BP + Cals)
>        lm(height ~ weight + BP)
>        lm(height ~ weight + Cals)
>        lm(height ~ BP + Cals)
>        lm(weight ~ height + BP)
>        lm(weight ~ height + Cals)
>        etc.
>
> In general, I'll have to read the header to get the argument labels.
>
> Do I have to write several functions, each taking a different number of
> arguments?  I'd like to construct a string or list representing the
> varialbes in the formula and apply lm(), so to say  [I'm mainly a Lisp
> programmer where that part would be very simple. Anyone have a Lisp API
> for R? :-}]
>
> Thanks,
> chris
>
> Chris Elsaesser, PhD
> Principal Scientist, Machine Learning
> 7921 Jones Branch Dr. Suite 600
> McLean, VA 22102
>
> 703.371.7301 (m)
> 703.637.9421 (o)
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help