Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

matrix - Shortcut using lm() in R for formula

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...