Dylan
Dylan

Reputation: 93

R Stargazer Getting p-values into one line

stargazer(model1, model2, title = "Models", header=FALSE, 
          dep.var.labels.include = FALSE,
          column.labels = c("Count", "Percentage"),
          style = "ajs",
          report = "vcp*",
          single.row = TRUE)

This is my code to create regression tables with stargazer. However, the p-value still shows up below the coefficient estimates. How do I get p-values to show up next to the coefficient estimates?

Upvotes: 1

Views: 1227

Answers (1)

jay.sf
jay.sf

Reputation: 73702

You may replace standard errors with p-values. Put models into a list, which allows you to use lapply.

model1 <- lm(mpg ~ hp, mtcars)
model2 <- lm(mpg ~ hp + cyl, mtcars)

model.lst <- list(model1, model2)

stargazer::stargazer(model.lst, title = "Models", header=FALSE, 
          dep.var.labels.include = FALSE,
          column.labels = c("Count", "Percentage"),
          style = "ajs",
          report = "vcs*",
          single.row = TRUE, type="text",
          se=lapply(model.lst, function(x) summary(x)$coef[,4]))
# Models
# =================================================================
#                             Count                Percentage      
#                               1                      2           
# -----------------------------------------------------------------
# hp                     -.068 (0.000)***         -.019 (.213)     
# cyl                                          -2.265 (0.000)***   
# Constant              30.099 (0.000)***      36.908 (0.000)***   
# Observations                  32                     32          
# R2                           .602                   .741         
# Adjusted R2                  .589                   .723         
# Residual Std. Error    3.863 (df = 30)        3.173 (df = 29)    
# F Statistic         45.460*** (df = 1; 30) 41.422*** (df = 2; 29)
# -----------------------------------------------------------------
# Notes:              *P < .05                                     
#                     **P < .01                                    
#                     ***P < .001       

Note, that this is also possible with texreg which might look a little bit cleaner and the package is well maintained.

texreg::screenreg(model.lst, single.row=TRUE, 
          reorder.coef=c(2:3, 1),
          custom.model.names=c("Count", "Percentage"),
          override.se=lapply(model.lst, function(x) summary(x)$coef[,4]),
          override.pvalues=lapply(model.lst, function(x) summary(x)$coef[,4]),
          digits=3
       )
# ===================================================
#              Count               Percentage        
# ---------------------------------------------------
# hp           -0.068 (0.000) ***  -0.019 (0.213)    
# cyl                              -2.265 (0.000) ***
# (Intercept)  30.099 (0.000) ***  36.908 (0.000) ***
# ---------------------------------------------------
# R^2           0.602               0.741            
# Adj. R^2      0.589               0.723            
# Num. obs.    32                  32                
# ===================================================
# *** p < 0.001; ** p < 0.01; * p < 0.05

Upvotes: 6

Related Questions