Label using lm () in R for the formula

You can use the shortcut for the formula in lm()

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

here he will be the same as

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

but in the case when the variables do not have the same level (at least this is my assumption so far), this does not work, and I get an 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 any other shortcut to avoid having to write a long formula?

+6
source share
2 answers

Try lm(y ~ ., data) , where . means "every column in data except 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 columns as predictors

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

There is another shortcut for cases where the dependent variable is in the first column:

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

Source: https://habr.com/ru/post/948375/


All Articles