Linear regression of same outcome, similar number

2019-02-19 02:38发布

I want to run linear regression for the same outcome and a number of covariates minus one covariate in each model. I have looked at the example on this page but could that did not provide what I wanted.

Sample data

a <- data.frame(y = c(30,12,18), x1 = c(7,6,9),  x2 = c(6,8,5),
                x3 = c(4,-2,-3), x4 = c(8,3,-3), x5 = c(4,-4,-2))
m1 <- lm(y ~ x1 + x4 + x5, data = a)    
m2 <- lm(y ~ x2 + x4 + x5, data = a)   
m3 <- lm(y ~ x3 + x4 + x5, data = a)

How could I run these models in a short way and and without repeating the same covariates again and again?

1条回答
霸刀☆藐视天下
2楼-- · 2019-02-19 03:36

Following this example you could do this:

lapply(1:3, function(i){
    lm(as.formula(sprintf("y ~ x%i + x4 + x5", i)), a)
})
查看更多
登录 后发表回答