Reputation: 7846
I can perform a linear regression on a dataframe of known length where y is the first column followed by the x variables:
a <-rnorm(20)
b <-rnorm(20)
c <-rnorm(20)
d <-rnorm(20)
e <-rnorm(20)
f <-rnorm(20)
df <- data.frame(a,b,c,d,e,f)
df
dlm <- lm(df[,c(1)]~df[,c(2)]+df[,c(3)]+df[,c(4)]+df[,c(5)])
dlm
However when dealing with many dataframes of variable column length (first column: y variable,other columns x variables), I tried:
dlm2 <- lm(df[,c(1)]~df[,c(-1)])
dlm2
but this did not work. I would be grateful for your help.
Upvotes: 1
Views: 302
Reputation: 206446
The correct syntax in this case would be
lm(a~., df)
the problem with your attempt is that df[,-1]
is a a data.frame (which is a list object). If you wanted that to work you just need to convert to a matrix
lm( df[,1] ~ as.matrix(df[,-1]) )
but this really isn't an efficient use of the formula syntax.
Upvotes: 2