I haven't found a stargazer solution to this, but a way of integrating it into 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))
}
The idea is to remove the F-statistc from stargazer with omit.stat=c("f")
and add it manually with add.lines()
. Note add.lines()
is added before the statistics block by default. You can change the order with table.layout
see 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,
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
If you want to have multiple models in the same stargazer use lapply
as seen in the answer by 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,
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