How to Create High Quality Tables in R

How to Create High Quality Tables in R, R’s stargazer package can be used to produce high-quality tables suitable for publishing.

This example demonstrates how to use the mtcars built-in R dataset to get started with the stargazer package.

Example: Utilizing R’s Stargazer Package

Initially, we can install and load the stargazer package using the code that follows:

install.packages('stargazer') 
library(stargazer)

After loading the package, we can create excellent tables by using the stargazer method.

The basic syntax used by this function is as follows:

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

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

Please take note that in the upcoming examples, we’ll be exporting text files with.txt extensions, but you can export HTML pages with.html extensions instead.

Business leader’s approach towards Data Science » finnstats

Usually, the stargazer function is used to generate two distinct kinds of tables:

a table with each variable’s summary statistics from a data frame

A table that presents a regression model’s findings in summary form

To generate a table that shows summary statistics for every variable in the mtcars data frame, we may utilize the following code:

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   
-------------------------------------------------------------

Take note that each variable in the mtcars data frame has a summary statistics table created for it.

To see the text file containing these summary statistics, we can alternatively go to the folder where we exported mtcars_data.txt.

A table summarizing a multiple linear regression model with mpg as the response variable and disp and hp as the predictor variables can also be made with the following code:

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 coefficients for each regression model term are included in the output near the bottom of the table, along with a number of summary statistics.

Predictive Modeling and Data Science » Data Science Tutorials

You may also like...

Leave a Reply

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

one × five =