R 运行 顺序回归

R Run Regressions In Sequence

 set.seed(1)
   data=data.frame(
    
    student=1:5000,
    alfa =runif(5000), 
    bravo =runif(5000), 
    charlie =runif(5000), 
    delta =runif(5000), 
    echo =runif(5000), 
    foxtrot =runif(5000), 
    golf =runif(5000), 
    hotel =runif(5000), 
    india =runif(5000), 
    juliett =runif(5000), 
    kilo =runif(5000), 
    lima =runif(5000), 
    mike =runif(5000), 
    november =runif(5000), 
    oscar =runif(5000), 
    papa =runif(5000), 
    GROUP = sample(0:1, r=T))

##########################

COVS1 = c(golf, hotel, india)
COVS2 = c(juliett, kilo)
COVS3 = c(lima, mike, november, oscar, papa)


## OVERALL
lm(alpha ~ delta +  golf + hotel + india, data = data)
lm(alpha ~ delta +  golf + hotel + india + juliett + kilo, data = data)
lm(alpha ~ delta +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data = data)
lm(alpha ~ echo +  golf + hotel + india, data = data)
lm(alpha ~ echo +  golf + hotel + india + juliett + kilo, data = data)
lm(alpha ~ echo +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data = data)
lm(alpha ~ foxtrot +  golf + hotel + india, data = data)
lm(alpha ~ foxtrot +  golf + hotel + india + juliett + kilo, data = data)
lm(alpha ~ foxtrot +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data = data)
lm(bravo ~ delta +  golf + hotel + india, data = data)
lm(bravo ~ delta +  golf + hotel + india + juliett + kilo, data = data)
lm(bravo ~ delta +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data = data)
lm(bravo ~ echo +  golf + hotel + india, data = data)
lm(bravo ~ echo +  golf + hotel + india + juliett + kilo, data = data)
lm(bravo ~ echo +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data = data)
lm(bravo ~ foxtrot +  golf + hotel + india, data = data)
lm(bravo ~ foxtrot +  golf + hotel + india + juliett + kilo, data = data)
lm(bravo ~ foxtrot +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data = data)
lm(charlie ~ delta +  golf + hotel + india, data = data)
lm(charlie ~ delta +  golf + hotel + india + juliett + kilo, data = data)
lm(charlie ~ delta +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data = data)
lm(charlie ~ echo +  golf + hotel + india, data = data)
lm(charlie ~ echo +  golf + hotel + india + juliett + kilo, data = data)
lm(charlie ~ echo +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data = data)
lm(charlie ~ foxtrot +  golf + hotel + india, data = data)
lm(charlie ~ foxtrot +  golf + hotel + india + juliett + kilo, data = data)
lm(charlie ~ foxtrot +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data = data)

## GROUP == 0
lm(alpha ~ delta +  golf + hotel + india, data ~ subset(data, GROUP == 0)
lm(alpha ~ delta +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 0)
lm(alpha ~ delta +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 0)
lm(alpha ~ echo +  golf + hotel + india, data ~ subset(data, GROUP == 0)
lm(alpha ~ echo +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 0)
lm(alpha ~ echo +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 0)
lm(alpha ~ foxtrot +  golf + hotel + india, data ~ subset(data, GROUP == 0)
lm(alpha ~ foxtrot +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 0)
lm(alpha ~ foxtrot +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 0)
lm(bravo ~ delta +  golf + hotel + india, data ~ subset(data, GROUP == 0)
lm(bravo ~ delta +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 0)
lm(bravo ~ delta +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 0)
lm(bravo ~ echo +  golf + hotel + india, data ~ subset(data, GROUP == 0)
lm(bravo ~ echo +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 0)
lm(bravo ~ echo +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 0)
lm(bravo ~ foxtrot +  golf + hotel + india, data ~ subset(data, GROUP == 0)
lm(bravo ~ foxtrot +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 0)
lm(bravo ~ foxtrot +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 0)
lm(charlie ~ delta +  golf + hotel + india, data ~ subset(data, GROUP == 0)
lm(charlie ~ delta +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 0)
lm(charlie ~ delta +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 0)
lm(charlie ~ echo +  golf + hotel + india, data ~ subset(data, GROUP == 0)
lm(charlie ~ echo +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 0)
lm(charlie ~ echo +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 0)
lm(charlie ~ foxtrot +  golf + hotel + india, data ~ subset(data, GROUP == 0)
lm(charlie ~ foxtrot +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 0)
lm(charlie ~ foxtrot +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 0)

## GROUP == 1
lm(alpha ~ delta +  golf + hotel + india, data ~ subset(data, GROUP == 1)
lm(alpha ~ delta +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 1)
lm(alpha ~ delta +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 1)
lm(alpha ~ echo +  golf + hotel + india, data ~ subset(data, GROUP == 1)
lm(alpha ~ echo +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 1)
lm(alpha ~ echo +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 1)
lm(alpha ~ foxtrot +  golf + hotel + india, data ~ subset(data, GROUP == 1)
lm(alpha ~ foxtrot +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 1)
lm(alpha ~ foxtrot +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 1)
lm(bravo ~ delta +  golf + hotel + india, data ~ subset(data, GROUP == 1)
lm(bravo ~ delta +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 1)
lm(bravo ~ delta +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 1)
lm(bravo ~ echo +  golf + hotel + india, data ~ subset(data, GROUP == 1)
lm(bravo ~ echo +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 1)
lm(bravo ~ echo +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 1)
lm(bravo ~ foxtrot +  golf + hotel + india, data ~ subset(data, GROUP == 1)
lm(bravo ~ foxtrot +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 1)
lm(bravo ~ foxtrot +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 1)
lm(charlie ~ delta +  golf + hotel + india, data ~ subset(data, GROUP == 1)
lm(charlie ~ delta +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 1)
lm(charlie ~ delta +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 1)
lm(charlie ~ echo +  golf + hotel + india, data ~ subset(data, GROUP == 1)
lm(charlie ~ echo +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 1)
lm(charlie ~ echo +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 1)
lm(charlie ~ foxtrot +  golf + hotel + india, data ~ subset(data, GROUP == 1)
lm(charlie ~ foxtrot +  golf + hotel + india + juliett + kilo, data ~ subset(data, GROUP == 1)
lm(charlie ~ foxtrot +  golf + hotel + india + juliett + kilo + lima + mike + november + oscar + papa, data ~ subset(data, GROUP == 1)

我想知道,如何使用某种函数 运行 所有这些回归? 基本上,回归在 COVS1 上回归 'alpha',然后在 COVS1 和 COVS2 上回归 'alpha',然后在 COVS1、COVS2 和 COVS3 上回归 'alpha'。这是针对整个数据完成的,然后分别针对 GROUP == 0 和 GROUP == 1 完成。重复相同的过程,将 'alpha' 替换为 'bravo' 然后 'charlie' 所以我只是希望 运行 所有这些,然后存储模型和系数估计以及所有输出的标准误差..

我们可以创建一个循环来执行此操作

COVS1 <- c('golf', 'hotel', 'india')
COVS2 <- c('juliett', 'kilo')
COVS3 <- c('lima', 'mike', 'november', 'oscar', 'papa')
    
COVS <- list(COVS1, COVS2, COVS3)
    
df1 <- expand.grid(resp = c('alfa', 'bravo', 'charlie'),
     pred = c('delta', 'echo', 'foxtrot'), stringsAsFactors = FALSE)
    
df1 <- df1[order(df1$resp),]



library(broom)
lst_mod <-  lapply(seq_along(COVS), function(i) 
      lapply(seq_along(COVS), function(j) {

      fmla <- reformulate(c(df1$pred[i], unlist(COVS[seq_len(j)])), 
            response = df1$resp[i])
      tidy(lm(fmla, data = data))
 }))

或者如果我们需要一个数据集

library(dplyr)
library(purrr)
out_dat <- lapply(seq_along(COVS), function(i) 
  map_dfr(seq_along(COVS), ~ {

  fmla <- reformulate(c(df1$pred[i], unlist(COVS[seq_len(.x)])), 
        response = df1$resp[i])
  tidy(lm(fmla, data = data)) %>% 
        mutate(response_variable = df1$resp[i], formula = list(fmla))
    })) %>%
       bind_rows

如果我们需要在数据的 subset 上应用 lm,则对数据进行子集化

dat0 <- subset(data, GROUP == 0)
dat1 <- subset(data, GROUP == 1)

并将 lm 中的 data 更改为 'dat0'、'dat1'