在括号中显示p值而不是SE

时间:2014-07-02 23:17:23

标签: r stargazer

使用stargazer包时,我想更改系数下括号中显示的值。默认情况下,程序包将输出标准错误。

如何在括号中包含实际的p值?

2 个答案:

答案 0 :(得分:7)

正如Stargazer Omit test statistics中所述,因为版本5.0 stargazer包含report参数,允许用户选择要报告的统计信息。因此,要显示p值而不是SE,您可以这样做:

require(stargazer)
linear.1 <- lm(rating ~ complaints + privileges + learning 
                        + raises + critical, data=attitude)
## put in the p-values rather than the se's
stargazer(linear.1, type="text", report=('vc*p'))

将输出:

> stargazer(linear.1,  type="text", report=('vc*p'))

========================================
                 Dependent variable:    
             ---------------------------
                       rating           
----------------------------------------
complaints            0.692***          
                     p = 0.0002         

privileges             -0.104           
                      p = 0.450         

learning                0.249           
                      p = 0.132         

raises                 -0.033           
                      p = 0.870         

critical                0.015           
                      p = 0.918         

Constant               11.010           
                      p = 0.357         

----------------------------------------
Observations             30             
R2                      0.715           
Adjusted R2             0.656           
F Statistic           12.060***         
========================================
Note:        *p<0.1; **p<0.05; ***p<0.01

这种方法使用se参数safer than,并且不会弄明星的重要性。

另见:

答案 1 :(得分:0)

没有简单的方法(除非 style 选项之一解决了这个问题)。但是您可以用 p 值替换标准误差并对 p 值进行硬编码,以便正确的星数显示如下。

library(stargazer)
#> 
#> Please cite as:
#>  Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
#>  R package version 5.2.2. https://CRAN.R-project.org/package=stargazer

models <- list()
models[[1]] <- lm(mpg ~ cyl + disp, data = mtcars)
models[[2]] <- lm(mpg ~ cyl + disp + wt, data = mtcars)

get_ts <- function(fm) {
  summary(fm)$coefficients[,3]
}

get_pvals <- function(fm) {
  summary(fm)$coefficients[,4]
}

ts <- lapply(models, get_ts)
pvals <- lapply(models, get_pvals)

stargazer(models, type = "text", report=('vc*s'), se = pvals, p = pvals)
#> 
#> =================================================================
#>                                  Dependent variable:             
#>                     ---------------------------------------------
#>                                          mpg                     
#>                              (1)                    (2)          
#> -----------------------------------------------------------------
#> cyl                        -1.587**              -1.785***       
#>                            (0.034)                (0.007)        
#>                                                                  
#> disp                       -0.021*                 0.007         
#>                            (0.054)                (0.533)        
#>                                                                  
#> wt                                               -3.636***       
#>                                                   (0.002)        
#>                                                                  
#> Constant                  34.661***              41.108***       
#>                            (0.000)                (0.000)        
#>                                                                  
#> -----------------------------------------------------------------
#> Observations                  32                     32          
#> R2                          0.760                  0.833         
#> Adjusted R2                 0.743                  0.815         
#> Residual Std. Error    3.055 (df = 29)        2.595 (df = 28)    
#> F Statistic         45.808*** (df = 2; 29) 46.424*** (df = 3; 28)
#> =================================================================
#> Note:                                 *p<0.1; **p<0.05; ***p<0.01

reprex package (v2.0.0) 于 2021 年 5 月 11 日创建