如何解决是否有自动显示“stargazer”表中回归 F 统计量的 p 值的方法?
这似乎是一个基本问题,但似乎没有自动显示 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")
结果输出显示 F 统计量和自由度,但不显示 F 统计量的 p 值。
解决方法
您可以使用 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
,
我还没有找到解决此问题的 stargazer 解决方案,而是将其集成到 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))
}
这个想法是使用 omit.stat=c("f")
从 stargazer 中删除 F-statistc,并使用 add.lines()
手动添加它。注意 add.lines()
默认添加在统计块之前。您可以使用 table.layout
更改顺序,请参阅 https://rdrr.io/cran/stargazer/man/stargazer_table_layout_characters.html
iris_reg<-lm(Petal.Length~Sepal.Length+Sepal.Width,data=iris)
summary(iris_reg)
stargazer(iris_reg,header=FALSE,single.row=TRUE,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
如果您想在同一个观星者中有多个模型,请使用 lapply
,如 JWilliman https://stackoverflow.com/a/64745465/11311931
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,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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。