Search code examples
rregressionstargazer

In R is there a way to display the mean of the dependent variable in a regression summary


I've been using stargazer to display linear regression results (DID) specifically but there doesn't seem to be a command to allow me to find the mean of the dependent variable of the regression - say along with R^2 or a similar statistic.

I can't link the data - but here's one example of where I would like this stat.

mother_employ <- felm(paid_employment ~ treat + is_post + treated_group + age_dv + age_dv^2 + marital_status + educ_levels + nkids_dv + regions + white | year |0|pidp, subset = sex_dv == 2, data)

Below I display the regression results using stargazer - I've tried to put "mean" in the keep stat call without results

stargazer(employ_mother, title = "Mother Employment",
          font.size = "small", 
          align = TRUE,
          column.sep.width = "-15pt",keep.stat = c("adj.rsq","n"), keep = c("treat","is_post","treated_group","age_dv","age_dv^2","nkids_dv","white"), notes = "This Table Exludes Marital Status, Education Level, and Regions Covariates", type = "text")

My output is such:

Mother Employ
========================================================================================
                                         Dependent variable:                            
              --------------------------------------------------------------------------
                                           paid_employment                              
----------------------------------------------------------------------------------------
treat                                          0.082***                                 
                                               (0.013)                                  
                                                                                        
is_post1                                        0.011                                   
                                               (0.012)                                  
                                                                                        
treated_group                                 -0.352***                                 
                                               (0.010)                                  
                                                                                        
age_dv                                         0.008***                                 
                                               (0.0004)                                 
                                                                                        
nkids_dv                                      -0.061***                                 
                                               (0.004)                                  
                                                                                        
white                                          0.168***                                 
                                               (0.010)                                  
                                                                                        
----------------------------------------------------------------------------------------
Observations                                    51,528                                  
Adjusted R2                                     0.237                                   
========================================================================================
Note:                                                        *p<0.1; **p<0.05; ***p<0.01
              This Table Exludes Marital Status, Education Level, and Regions Covariates
> 

I want to display the mean of the dependent variable with R^2 is this possible?


Solution

  • This is possible using the add.lines argument of stargazer().

    library(stargazer)
    
    model_results <- lm(mpg ~ cyl + drat, mtcars)
    stargazer(model_results,
              title = "Cars",
              font.size = "small", 
              align = TRUE,
              column.sep.width = "-15pt",
              keep.stat = c("adj.rsq","n"),
              add.lines = list("Mean" = c("Mean", mean(mtcars$mpg))), 
              out = "test.txt")
    

    Gives us

    Cars
    
    ========================================
                     Dependent variable:    
                 ---------------------------
                             mpg            
    ----------------------------------------
    cyl                   -2.484***         
                           (0.447)          
                                            
    drat                    1.872           
                           (1.494)          
                                            
    Constant              28.725***         
                           (7.592)          
                                            
    ----------------------------------------
    Mean                  20.090625         
    Observations             32             
    Adjusted R2             0.722           
    ========================================
    Note:        *p<0.1; **p<0.05; ***p<0.01
    

    Alternatively, if we only want values that are used in the regression we can use.

    mean(model_results$data$mpg) # For models from lm()
    mean(model_results$response) # For models from felm()
    

    In place of mean(mtcars$mpg)