如何在 R 中的循环中更改线性回归中的预测变量?

How do I change predictors in linear regression in loop in R?

如何在 R 中的循环中更改线性回归中的预测变量?

下面是一个示例以及错误。有人可以修复它吗?

# sample data 
mpg <- mpg

str(mpg)

# array of predictors
predictors <- c("hwy", "cty")

# loop over predictors
for (predictor in predictors) 
{
  # fit linear regression
  model <- lm(formula = predictor ~ displ + cyl,
              data = mpg)
  
  # summary of model
  summary(model)
}

错误

Error in model.frame.default(formula = predictor ~ displ + cyl, data = mpg,  : 
  variable lengths differ (found for 'displ')

我们可以使用pastereformulate。此外,由于它是一个 for 循环,因此创建一个对象来存储 summary

的输出
sumry_model <- vector('list', length(predictors))
names(sumry_model) <- predictors
for (predictor in predictors) {
  # fit linear regression
  model <- lm(reformulate(c("displ", "cyl"), response = predictor),
              data = mpg)
  # with paste
  # model <- lm(formula = paste0(predictor, "~ displ + cyl"), data = mpg)
  
  # summary of model
    sumry_model[[predictor]] <- summary(model)
}

-输出

> sumry_model
$hwy

Call:
lm(formula = reformulate(c("displ", "cyl"), response = predictor), 
    data = mpg)

Residuals:
    Min      1Q  Median      3Q     Max 
-7.5098 -2.1953 -0.2049  1.9023 14.9223 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  38.2162     1.0481  36.461  < 2e-16 ***
displ        -1.9599     0.5194  -3.773 0.000205 ***
cyl          -1.3537     0.4164  -3.251 0.001323 ** 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.759 on 231 degrees of freedom
Multiple R-squared:  0.6049,    Adjusted R-squared:  0.6014 
F-statistic: 176.8 on 2 and 231 DF,  p-value: < 2.2e-16


$cty

Call:
lm(formula = reformulate(c("displ", "cyl"), response = predictor), 
    data = mpg)

Residuals:
    Min      1Q  Median      3Q     Max 
-5.9276 -1.4750 -0.0891  1.0686 13.9261 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  28.2885     0.6876  41.139  < 2e-16 ***
displ        -1.1979     0.3408  -3.515 0.000529 ***
cyl          -1.2347     0.2732  -4.519 9.91e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.466 on 231 degrees of freedom
Multiple R-squared:  0.6671,    Adjusted R-squared:  0.6642 
F-statistic: 231.4 on 2 and 231 DF,  p-value: < 2.2e-16

这也可以作为多元响应来完成

summary(lm(cbind(hwy, cty) ~ displ + cyl, data = mpg))

或者如果我们想使用 predictors

summary(lm(as.matrix(mpg[predictors]) ~ displ + cyl, data = mpg))