adam.888
adam.888

Reputation: 7846

R linear regression on a dataframe of variable length

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

Answers (2)

Carlos V
Carlos V

Reputation: 101

This solves the problem in an efficient manner:

lm(formula(df))

Upvotes: 2

MrFlick
MrFlick

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

Related Questions