R Stargazer 将p值归入一行。

问题描述 投票:1回答:1
stargazer(model1, model2, title = "Models", header=FALSE, 
          dep.var.labels.include = FALSE,
          column.labels = c("Count", "Percentage"),
          style = "ajs",
          report = "vcp*",
          single.row = TRUE)

这是我用stargazer创建回归表的代码。但是,p值仍然显示在系数估计值的下面。我如何让p值显示在系数估计值的旁边?

r stargazer
1个回答
5
投票

你可以用p值代替标准误差。将模型放入一个列表中,这允许你使用 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       

请注意,这也可以用 texreg 这可能看起来更干净一些,而且包装也保养得很好。

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
© www.soinside.com 2019 - 2024. All rights reserved.