How to Use Stargazer Package in R (With Example)


You can use the stargazer package in R to create high quality tables that can be used in publications.

The following example shows how to get started with the stargazer package by using the built-in R dataset called mtcars.

Example: How to Use the stargazer Package in R

First, we can use the following code to install and load the stargazer package:

#install stargazer package
install.packages('stargazer')

#load stargazer package
library(stargazer)

Once we’ve loaded the package, we can use the stargazer function to produce high quality tables.

This function uses the following basic syntax:

stargazer(df, type=’text’, title=’my_title’, out=’my_data.txt’, …)

where:

  • df: Name of the data frame to use
  • type: Type of output to display
  • title: Title to show at top of table
  • out: Name of file to use when exporting table

Note that for the following examples we’ll use .txt to export text files but you can use .html if you’d like to export HTML files instead.

The stargazer function is typically used to create two different types of tables:

  • A table of summary statistics for each variable in a data frame
  • A table that summarizes the results of a regression model

We can use the following code to create a table that displays summary statistics for each variable in the mtcars data frame:

#create table that provide summary statistics of each variable in dataset
stargazer(mtcars, type='text', title='Summary Statistics', out='mtcars_data.txt')

Summary Statistics
=============================================================
Statistic N   Mean   St. Dev.  Min   Pctl(25) Pctl(75)  Max  
-------------------------------------------------------------
mpg       32 20.091   6.027     10     15.4     22.8     34  
cyl       32  6.188   1.786     4       4        8       8   
disp      32 230.722 123.939    71    120.8     326     472  
hp        32 146.688  68.563    52     96.5     180     335  
drat      32  3.597   0.535   2.760   3.080    3.920   4.930 
wt        32  3.217   0.978   1.513   2.581    3.610   5.424 
qsec      32 17.849   1.787   14.500  16.892   18.900  22.900
vs        32  0.438   0.504     0       0        1       1   
am        32  0.406   0.499     0       0        1       1   
gear      32  3.688   0.738     3       3        4       5   
carb      32  2.812   1.615     1       2        4       8   
-------------------------------------------------------------

Note that a table of summary statistics has been produced for each variable in the mtcars data frame.

If we’d like, we can also navigate to the location where we exported mtcars_data.txt to view the text file that contains these summary statistics.

We can also use the following code to create a table that summarizes a multiple linear regression model that uses mpg as the response variable and disp and hp as the predictor variables:

#fit regression model
fit <- lm(mpg ~ disp + hp, data=mtcars)

#create table that summarizes regression model
stargazer(fit, type='text', title='Regression Summary', out='mtcars_regression.txt')

Regression Summary
===============================================
                        Dependent variable:    
                    ---------------------------
                                mpg            
-----------------------------------------------
disp                         -0.030***         
                              (0.007)          
                                               
hp                            -0.025*          
                              (0.013)          
                                               
Constant                     30.736***         
                              (1.332)          
                                               
-----------------------------------------------
Observations                    32             
R2                             0.748           
Adjusted R2                    0.731           
Residual Std. Error       3.127 (df = 29)      
F Statistic           43.095*** (df = 2; 29)   
===============================================
Note:               *p<0.1; **p<0.05; ***p<0.01

The output displays the coefficients for each term in the regression model along with various summary statistics near the bottom of the table.

Note: You can find the complete documentation for the stargazer package here.

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Loop Through Column Names in R
How to Create an Empty Data Frame in R
How to Append Rows to a Data Frame in R

Leave a Reply

Your email address will not be published. Required fields are marked *