First: Thanks to everyone who develops R, maintains r-help, and participates
in the list :)  

This is a silly follow up question.

>From Andy Liaw:
> dat <- data.frame(y=rnorm(10), x1=rnorm(10), x2=rnorm(10))

(Silly question - if the answer is on the lm or formula help page I didn't
get it:)
Why does lm | formula treat dat[,1] slightly differently than dat$y?

I see what it is doing - I am curious as to why:)

> lm(dat$y ~ .,data = dat)

Call:
lm(formula = dat$y ~ ., data = dat)

Coefficients:
(Intercept)           x1           x2  
   -0.08754     -0.04456     -0.16905  

> lm(dat[,1] ~ .,data = dat)

Call:
lm(formula = dat[, 1] ~ ., data = dat)
Coefficients:
(Intercept)            y           x1           x2  
 -5.266e-17    1.000e+00    4.121e-17   -3.274e-17  


As Gabor Grothendieck pointed out:
lm(formula = dat[, 1] ~ ., data = dat[,-1])
works like
lm(formula = dat$y ~ ., data = dat)

Curious Minds Want to Know
Bob


-----Original Message-----
From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, March 24, 2004 12:51 PM
To: [EMAIL PROTECTED]
Subject: Re: [R] First Variable in lm

Christian Hoffmann <christian.hoffmann <at> wsl.ch> writes:

> 
> Hi all,
> 
> I just cannot think of how to do it:
> I want to take the first variable (column) of a data frame and regress 
> it against all other variables.
> 
> bla <- function (dat) {
>    reg <- lm(whateverthefirstofthevariablenamesis ~., data=dat)
>    return(reg)
> }

Andy has already given a particularly concise solution but if your
variable is not in first position then you could rearrange the 
order of the variables to allow his solution or use this which works 
for any specified position of the dependent variable:

data(longley)
lm( longley[,7] ~. , data = longley[,-7] )

______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to