Linear regression of same outcome, similar number of covariates and one unique covariate in each model

半城伤御伤魂 提交于 2019-12-01 06:34:53

问题


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:


Following this example you could do this:

lapply(1:3, function(i){
    lm(as.formula(sprintf("y ~ x%i + x4 + x5", i)), a)
})


来源:https://stackoverflow.com/questions/30099236/linear-regression-of-same-outcome-similar-number-of-covariates-and-one-unique-c

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