user3204008
user3204008

Reputation: 98

Is there an automatic way of displaying the p value of a regression F statistic in a `stargazer` table?

This seems to be a basic question, but there doesn't seem to be an automatic way of displaying the p value of the F statistic in a stargazer linear regression table. I know it must be possible to manually calculate it (or grab it from summary(reg_out)) and append it to the table, but is there an automatic way of doing that? Below is a working example:

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

The resulting output, displays the F statistic and degrees of freedom but not the p value of the F Statistic.

Upvotes: 3

Views: 828

Answers (2)

Len1550
Len1550

Reputation: 21

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

Upvotes: 2

Ronak Shah
Ronak Shah

Reputation: 389275

You can use report argument to display the p-value instead of standard error.

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

Upvotes: 1

Related Questions