逐步删除每个回归器

时间:2018-03-27 13:54:12

标签: r regression stargazer

有没有机会一次指定完整的模型然后只是一个接一个地删除回归量并用它生成一个漂亮的观星者表而不必一次又一次地写每个回归线?

data <- datasets::airquality
# Treating Month and Day as crosssectional and time fixed effects

re1 <- plm(data = data, Ozone~Solar.R+Wind+Temp,
       index=c("Month", "Day"), model="within", effect="twoways") # full model 
# this is the only regression line I actually want to write.  
# The other regressors should be automatically dropped one by one in subsequent regressions.

re2 <- plm(data = data, Ozone~Wind+Temp,
       index=c("Month", "Day"), model="within", effect="twoways") # first regressor dropped

re3 <- plm(data = data, Ozone~Solar.R+Temp,
       index=c("Month", "Day"), model="within", effect="twoways") # second regressor dropped

re4 <- plm(data = data, Ozone~Solar.R+Wind,
       index=c("Month", "Day"), model="within", effect="twoways") # third regressor dropped

stargazer(re1, re2, re3, re4, 
          type = "html", 
          title = "Dropped after one another",
          out="HopeThisWorks.html")

我已经查看了step()函数,但这并不是很有帮助,因为我不打算根据重要性或其他任何东西来删除。

3 个答案:

答案 0 :(得分:1)

 re1 <- plm(data = data, Ozone~Solar.R+Wind+Temp,
   index=c("Month", "Day"), model="within", effect="twoways") # full model 


 A=lapply(paste0(".~.-",c("Solar.R","Wind","Temp")),function(x)update(re1,as.formula(x)))
[[1]]

Model Formula: Ozone ~ Wind + Temp

Coefficients:
   Wind    Temp 
-2.6933  2.3735 


[[2]]

Model Formula: Ozone ~ Solar.R + Temp

Coefficients:
 Solar.R     Temp 
0.040986 2.782978 


[[3]]

Model Formula: Ozone ~ Solar.R + Wind

Coefficients:
  Solar.R      Wind 
 0.096607 -4.841992

现在能够在全球环境中访问它:使用

 list2env(setNames(A,paste0("re",seq_along(A)+1)),.GlobalEnv)

 stargazer(re1, re2, re3, re4, 
      type = "html", 
      title = "Dropped after one another",
      out="HopeThisWorks.html")

答案 1 :(得分:1)

这是一个更灵活的选择:

bene <- function(data = datasets::airquality,
                 depvar = "Ozone",
                 covariates = c("Wind","Temp","Solar.R")){

  funny <- function(id){
    covs <- ifelse(is.na(id),paste0(covariates,collapse = " + "),paste0(covariates[-id],collapse = " + "))

    model <- eval(parse(text=paste0("plm(data = data,",depvar," ~ ",covs,",index=c('Month', 'Day'), model='within', effect='twoways')")))

    return(model)
  }

  xx <- capture.output(stargazer::stargazer(purrr::map(c(NA,1:length(covariates)),funny),
                                            type = "html",
                                            out = paste0("results/model.html"),
                                            star.cutoffs = c(0.05,0.01,0.001),
                                            title = paste0(depvar)))

  models <- purrr::map(c(NA,1:length(covariates)),funny)
  map(models,function(x)print(summary(x)))
}

bene(data = datasets::airquality,
     depvar = "Ozone",
     covariates = c("Wind","Temp","Solar.R"))

答案 2 :(得分:0)

你可以使用(一个改编)这个(迄今为止只有逐步工作)的功能,它显示了观星者输出并将回归表保存为html文件。

stepwise_model <- function(data = "datasets::airquality",
                       depvar = "Ozone",
                       covariates = c("Wind","Temp","Solar.R")){


data_df <- eval(parse(text = data)) 


models <- c()
for(q in 1:length(covariates)){

  label <- paste0("mod_",depvar,"_",q)
  models[q] <- label
  cov <- paste0(covariates[1:q],collapse = " + ")

  eval(parse(text = paste0(label," <<- plm::plm(",depvar," ~ ",cov,",data = data_df,index=c('Month', 'Day'), model='within', effect='twoways')")))

  eval(parse(text  = paste0("print(summary(",label,"))")))
}

modellist <- eval(parse(text = paste0("list(",paste0(models,collapse = ","),")")))

xx <- capture.output(stargazer::stargazer(modellist ,
                                          type = "html",
                                          out = paste0("results/paper/models/mod_",depvar,".html"),
                                          star.cutoffs = c(0.05,0.01,0.001),
                                          title = paste0(depvar)))

}

stepwise_model()