Search code examples
rregressionstargazer

Drop each Regressor step by step


Is there any chance to specify the full model once and then just to drop regressors one after the other and producing a nice stargazer table with it without having to write every regression line again and again?

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

I have looked into the step() function but this isn't quite helping as I am not aiming to drop according to significance or anything.


Solution

  •  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
    

    Now to be able to access this in the global environment: use

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