具有简单线性回归的嵌套循环

Nested loop with simple linear regression

我知道有几篇关于此主题的帖子,请参阅此处 and here

但是,我感兴趣的不仅是一组预测变量,还有一组结果。请在下面查看我的代码尝试。

set.seed(42)
n <- 100
age <- runif(n, min=45, max=85)
sex <-  factor(sample(c('Male','Female'), n, rep=TRUE, prob=c(.6, .4)))
smoke <- factor(sample(c("Never", 'Former', 'Current'), n, rep=TRUE, prob=c(.25, .6, .15)))
bmi <- runif(n, min=16, max=45)
outcome1 <- rbinom(n = 100, size = 1, prob = 0.3)
outcome2 <- rbinom(n = 100, size = 1, prob = 0.13)
predictorlist<- list("age","sex", "bmi", "smoke")
outcomelist <- list("outcome1","outcome2")

for (i in predictorlist){ 
  for (j in outcomelist){ 
    model <- lm(paste("j ~", i[[1]])) 
    print(summary(model)) 
  } 
}

这样做时,我收到以下错误消息:

Error in model.frame.default(formula = paste("j ~", i[[1]]), drop.unused.levels = TRUE) : 
  variable lengths differ (found for 'age')

快速检查一下它们的长度就会发现一切都井井有条

> length(age)
[1] 100
> length(sex)
[1] 100
> length(bmi)
[1] 100
> length(smoke)
[1] 100
> length(outcome1)
[1] 100
> length(outcome2)
[1] 100

当然,非常感谢任何帮助。

my_models <- list()
n <- 1
for (i in 1:length(predictorlist)){ 
  for (j in 1:length(outcomelist)){ 
    formula <- paste(outcomelist[[j]], "~", predictorlist[[i]])
    out <- coefficients(summary(model))
    Coeff <- rownames(out)  
    rownames(out) <- NULL 
    model <- lm(formula)
    my_models[[n]]<-data.frame(Model=formula ,Coeff = Coeff,out )

    n <- n+1
  } 
}

do.call(rbind,my_models)

给予,

              Model       Coeff      Estimate  Std..Error       t.value     Pr...t..
1    outcome1 ~ age (Intercept)  5.551115e-16 0.085820144  6.468313e-15 1.0000000000
2    outcome1 ~ age smokeFormer  1.756757e-01 0.094117066  1.866566e+00 0.0649830089
3    outcome1 ~ age  smokeNever -1.638898e-16 0.131940938 -1.242145e-15 1.0000000000
4    outcome2 ~ age (Intercept)  4.997536e-01 0.239278415  2.088586e+00 0.0393369696
5    outcome2 ~ age         age -3.936905e-03 0.003567878 -1.103430e+00 0.2725424813
6    outcome1 ~ sex (Intercept)  3.600995e-01 0.188106966  1.914334e+00 0.0584945208
7    outcome1 ~ sex         age -3.487458e-03 0.002804861 -1.243362e+00 0.2167006313
8    outcome2 ~ sex (Intercept)  2.173913e-01 0.063533308  3.421690e+00 0.0009091606
9    outcome2 ~ sex     sexMale  4.186795e-02 0.086457881  4.842584e-01 0.6292829854
10   outcome1 ~ bmi (Intercept)  1.304348e-01 0.050088617  2.604080e+00 0.0106444776
11   outcome1 ~ bmi     sexMale -8.051530e-04 0.068161974 -1.181235e-02 0.9905993425
12   outcome2 ~ bmi (Intercept)  3.919620e-01 0.159188779  2.462246e+00 0.0155528798
13   outcome2 ~ bmi         bmi -4.967139e-03 0.005010602 -9.913259e-01 0.3239678274
14 outcome1 ~ smoke (Intercept)  1.085051e-01 0.125958776  8.614332e-01 0.3911023372
15 outcome1 ~ smoke         bmi  7.025988e-04 0.003964659  1.772154e-01 0.8597049249
16 outcome2 ~ smoke (Intercept)  3.333333e-01 0.111424058  2.991574e+00 0.0035189594
17 outcome2 ~ smoke smokeFormer -1.036036e-01 0.122196316 -8.478456e-01 0.3986112406
18 outcome2 ~ smoke  smokeNever -1.515152e-01 0.171304709 -8.844774e-01 0.3786256699