Shortcut using lm() in R for formula

我的未来我决定 提交于 2019-12-08 04:00:36

问题


It is possible to use a shortcut for formula in lm()

m <- matrix(rnorm(100), ncol=5)
lm(m[,1] ~ m[,2:5]

here it would be the same as

lm(m[,1] ~ m[,2] + m[,3] + m[,4] + m[,5]

but in the case when variables are not of the same level (at least this is my assumption for now) this does not work and I get the error:

Error in model.frame.default(formula = hm[, 1] ~ hm[, 2:4], drop.unused.levels = TRUE) : 
  invalid type (list) for variable 'hm[, 2:4]'

Data (hm):

     N cor.distance switches  time
1   50   0.04707842        2 0.003
2  100  -0.10769441        2 0.004
3  200  -0.01278359        2 0.004
4  300   0.04229509        5 0.008
5  500  -0.04490092        6 0.010
6 1000   0.01939561        4 0.007

Is there some shortcut still possible to avoid having to write the long formula?


回答1:


Try lm(y ~ ., data) where . means "every other column in data besides y.

m <- matrix(rnorm(100), ncol =5)
m <- as.data.frame(m)
names(m) <- paste("m", 1:5, sep="")
lm(m1 ~., data=m)

You can reassign m to include only the columns you as the predictors

m <- m[ ,2:4]
lm(m1 ~ ., data=m)



回答2:


There is another one shortcut for the cases when a dependent variable is in the first column:

data <- data.frame(y = rnorm(10), x1 = rnorm(10), x2 = rnorm(10))
lm(data)


来源:https://stackoverflow.com/questions/28776699/r-using-predict-on-new-data-with-high-dimensionality

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!