有没有一种自动的方法,在`stargazer`表格中显示回归F统计量的p值?

3

这似乎是一个基本问题,但在 stargazer 线性回归表中似乎没有自动显示 F 统计量的 p 值的方法。我知道手动计算它(或从 summary(reg_out) 中获取)并将其附加到表格中是可能的,但是否有自动执行的方式?以下是一个工作示例:

library(stargazer)
iris_reg<-lm(Petal.Length~Sepal.Length+Sepal.Width, data=iris)
stargazer(iris_reg, header=FALSE, single.row=TRUE, type="text")

enter image description here

生成的输出显示F统计量和自由度,但不显示F统计量的p值。
2个回答

2
我还没有找到一个完美的解决方案,但是有一种将其集成到stargazer中的方法。
#create function to extract p-value from f-stat source: https://gettinggeneticsdone.blogspot.com/2011/01/rstats-function-for-extracting-f-test-p.html 
#and extended by format.pval() to have the typical pvalue display e.g., <0.001

lmp <- function (modelobject) {
  if (class(modelobject) != "lm") stop("Not an object of class 'lm' ")
  f <- summary(modelobject)$fstatistic
  p <- pf(f[1],f[2],f[3],lower.tail=F)
  attributes(p) <- NULL
  return(format.pval(p, eps = .001, digits = 3))
}

这段文字涉及到IT技术方面的内容。建议使用 omit.stat=c("f") 命令去除 stargazer 中的 F-statistic,并使用 add.lines() 手动添加它。注意,默认情况下,add.lines() 会在统计数据前添加信息。如果需要更改顺序,可以使用 table.layout 命令,参考 https://rdrr.io/cran/stargazer/man/stargazer_table_layout_characters.html。请注意保留 html 标签。
iris_reg<-lm(Petal.Length~Sepal.Length+Sepal.Width, data=iris)
summary(iris_reg)

stargazer(iris_reg, 
          header=FALSE, 
          single.row=TRUE, 
          type="text", 
          add.lines=list(c("F Statistic (p-value)",lmp(iris_reg))), 
          omit.stat = c("f"),#add this to only have one F-statistic
          table.layout = "=ldc-tsa-n") # add F-statistic after statistic block

=================================================
                          Dependent variable:    
                      ---------------------------
                             Petal.Length        
-------------------------------------------------
Sepal.Length               1.776*** (0.064)      
Sepal.Width                -1.339*** (0.122)     
Constant                   -2.525*** (0.563)     
Observations                      150            
R2                               0.868           
Adjusted R2                      0.866           
Residual Std. Error        0.646 (df = 147)      
F Statistic (p-value)           <0.001           
-------------------------------------------------
Note:                 *p<0.1; **p<0.05; ***p<0.01

如果您想在同一个stargazer中使用多个模型,请使用lapply,如JWilliman的答案所示https://dev59.com/nJvga4cB1Zd3GeqP68Xd#64745465

iris_reg2<-list(
  lm(Petal.Length~Sepal.Length+Sepal.Width, data=iris),
  lm(Petal.Length~Sepal.Length+Sepal.Width+factor(Species), data=iris)
)

stargazer(iris_reg2, 
          header=FALSE, 
          single.row=TRUE, 
          type="text", 
          add.lines=list(c("F Statistic (p-value)",unlist(lapply(iris_reg2,lmp)))), #lapply creates a list by default
          omit.stat = c("f"),#add this to only have one F-statistic
          table.layout = "=ldc-tsa-n") # add F-statistic after statistic block

=============================================================
                                  Dependent variable:        
                          -----------------------------------
                                     Petal.Length            
-------------------------------------------------------------
Sepal.Length              1.776*** (0.064)  0.646*** (0.054) 
Sepal.Width               -1.339*** (0.122)  -0.041 (0.081)  
factor(Species)versicolor                   2.170*** (0.107) 
factor(Species)virginica                    3.049*** (0.123) 
Constant                  -2.525*** (0.563) -1.634*** (0.268)
Observations                     150               150       
R2                              0.868             0.975      
Adjusted R2                     0.866             0.974      
Residual Std. Error       0.646 (df = 147)  0.283 (df = 145) 
F Statistic (p-value)          <0.001            <0.001      
-------------------------------------------------------------
Note:                             *p<0.1; **p<0.05; ***p<0.01

谢谢Len1550提供了详细的解决方案!我最终采用了你的方法,它非常有效。 - user3204008

1
您可以使用report参数来显示p值,而不是标准误差。
library(stargazer)
stargazer(iris_reg, type="text",report=('vc*p'))



===============================================
                        Dependent variable:    
                    ---------------------------
                           Petal.Length        
-----------------------------------------------
Sepal.Length                 1.776***          
                             p = 0.000         
                                               
Sepal.Width                  -1.339***         
                             p = 0.000         
                                               
Constant                     -2.525***         
                            p = 0.00002        
                                               
-----------------------------------------------
Observations                    150            
R2                             0.868           
Adjusted R2                    0.866           
Residual Std. Error      0.646 (df = 147)      
F Statistic          481.997*** (df = 2; 147)  
===============================================
Note:               *p<0.1; **p<0.05; ***p<0.01

感谢Ronak的回复,但您提出的解决方案并没有回答我的问题,即F统计量的p值仍未显示。 - user3204008

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接