Add overall intercept to stargazer table in R in panel data analysis fixed effects model

26 Views Asked by At

I wish to add the overall intercept of a fixed effects model ("within" panel data analysis). This is common practice in Stata and I wish to mimic the Stata output by including the overall intercept.

library(stargazer)
library(plm)

data("Hedonic", package = "plm")
mod_fe <- plm(mv ~ age + crim, data = Hedonic, index = "townid")
overallint <- within_intercept(mod_fe)
overallint # 10.25964

stargazer(mod_fe, type = "text")

                 Dependent variable:    
             ---------------------------
                         mv             
----------------------------------------
age                   -0.004***         
                       (0.001)          
                                        
crim                  -0.009***         
                       (0.002)          
                                        
----------------------------------------
Observations             506            
R2                      0.147           
Adjusted R2            -0.046           
F Statistic    35.431*** (df = 2; 412)  

Note:        *p<0.1; **p<0.05; ***p<0.01

I could not find any relevant information to add the intercept. The mod_fe object does not store the intercept so I could not pull it from there. The within_intercept function calculates the actual value.

1

There are 1 best solutions below

0
Helix123 On

Read the documentation for within_intercept (and the contained example) and you find that the function can output a model object with the intercept as well (and not only the intercept) with argument return.model = TRUE.

A fully worked example incl. stargazer output comparing a FE model with and without intercept is:

library(plm)
data("Hedonic", package = "plm")
mod_fe <- plm(mv ~ age + crim, data = Hedonic, index = "townid")
mod_fe_int <- within_intercept(mod_fe, return.model = TRUE)
stargazer::stargazer(mod_fe, mod_fe_int, type = "text")
#> 
#> ============================================================
#>                            Dependent variable:              
#>              -----------------------------------------------
#>                        mv                    transY         
#>                        (1)                     (2)          
#> ------------------------------------------------------------
#> age                 -0.004***               -0.004***       
#>                      (0.001)                 (0.001)        
#>                                                             
#> crim                -0.009***               -0.009***       
#>                      (0.002)                 (0.002)        
#>                                                             
#> Constant                                    10.260***       
#>                                              (0.049)        
#>                                                             
#> ------------------------------------------------------------
#> Observations           506                     506          
#> R2                    0.147                   0.147         
#> Adjusted R2          -0.046                   0.143         
#> F Statistic  35.431*** (df = 2; 412) 35.431*** (df = 2; 503)
#> ============================================================
#> Note:                            *p<0.1; **p<0.05; ***p<0.01

NB: the model object returned by within_intercept in this case identifies itself technically as a pooling model.