Linear regression of the same result, the same number of covariates, and one unique covariate in each model

I want to run linear regression for the same result and number of covariances minus one covariate in each model. I looked at an example on this page , but could it provide not 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 time without repeating the same covariates over and over?

+3


source to share


1 answer


Following this example , you can do this:



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

      

+2


source







All Articles