【问题标题】:Drop each Regressor step by step逐步删除每个 Regressor
【发布时间】:2018-03-27 13:54:11
【问题描述】:

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

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() 函数,但这并没有太大帮助,因为我的目标不是根据重要性或其他任何东西下降。

【问题讨论】:

  • 如果不是按显着性或 AIC,您想如何选择要删除的回归量?逐步回归已经足够狡猾了,你想要一个更不可靠的版本吗?
  • 或者你想要每个组合?如果是这样,那么this is a duplicate
  • 我只想一次删除一个变量,以证明我感兴趣的解释变量的重要性和符号不依赖于包含或排除的任何其他变量。在我看来,这是一个透明度问题。这与决定保留或不保留哪些变量无关!
  • 您有一个非常具体且不常见的用例。我认为您不会找到现成的解决方案。我建议使用combn 获取您想要的变量组合,使用paste 构建公式,然后将模型放在一个列表中。

标签: r regression stargazer


【解决方案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")

【讨论】:

  • 非常感谢 - 这是一个很棒且有效的解决方案。正是我想要的
【解决方案2】:

这是一个更灵活的选择:

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"))

【讨论】:

    【解决方案3】:

    您可以使用(改编)这个(到目前为止只有逐步工作的)函数,该函数向您显示观星器的输出并将回归表保存为 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()
    

    【讨论】:

    • 谢谢你 - 但我真的需要代码,一次只删除一个变量
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 2016-04-30
    • 2016-01-27
    • 2018-11-26
    相关资源
    最近更新 更多