Open In App

How to Use Stargazer Package in R

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

Stargazer is a R package designed to create summary tables for statistical models. It provides an user-friendly interface to generate high quality HTML, LaTeX and ASCII text tables.

Stargazer simplifies the process of presenting the results by converting them into tabular form. These tables are commonly used in research papers, reports and presentations to present model coefficients, standard errors, significance levels and other relevant statistics in a concise and readable format.

Using Stargazer Function

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

  1. A table that explains summary statistics for each variable in a data frame.
  2. Another table that summarizes the results of a regression model.

Syntax:

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

where,

  • df: Variable representing the data frame to utilize
  • type: Format of output to exhibit
  • title: Heading to display atop the table
  • out: Designation for the file when exporting the table

1. Install and Load the Package

We can install and load the Stargazer package in our R environment using the following functions.

R
install.packages("stargazer")
library(stargazer)

2. Loading the Dataset

We will be using the mtcars dataset , which is an inbuilt datset in R and print some of its rows.

R
data(mtcars)
head(mtcars)

Output:

data
How to Use Stargazer Package in R

3. Check the Summary

We can check the summary of dataset using the summary() function from base R.

R
summary(mtcars)

Output:

summary
How to Use Stargazer Package in R

4. Create Summary Statistics Table Using Stargazer

We will now create summary statistics table using the stargazer function. The type specifies that the output format of the table should be text. title sets the title of the table as "Custom Summary Statistics"

R
stargazer(mtcars, type='text', title='Summary Statistics')
         

Output:

star_summary
How to Use Stargazer Package in R

5. Fit a Regression Model and exported to the specified file paths

We will create table that summarizes the new regression model. The lm() function is used to fit a linear regression model using the formula (mpg ~ wt + cyl) . Here the dependent variable mpg (miles per gallon) is regressed on the independent variables wt (weight) and cyl (number of cylinders).

R
# Fit regression model
fit <- lm(mpg ~ wt + cyl, data = mtcars)
# Create table that summarizes the new regression model
stargazer(fit, type = 'text', title = 'Regression Summary Table', 
           out = 'C:\\Users\\GFG19565\\Downloads\\R.txt')

Output:

lm_star
How to Use Stargazer Package in R

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

6. Checking the Exported Table

Now verify if the summary statistics table and the Regression summary table were successfully exported to the specified file paths.

1
Stargazer Package in R

We can see the file is exported to the specified file paths and when we click on this file we get data in text format that we have pass.

RNOTE
Stargazer Package in R

Summary tables Vs Stargazer Package tables

Aspect

Summary Tables

Stargazer Package Tables

Presentation

Basic statistical summaries in plain format

Professionally formatted, resembling academic papers

Customization

Limited options for basic formatting adjustments

Extensive customization options for titles, labels, etc.

Output Formats

Typically limited to plain text or basic formatting

Supports HTML, LaTeX, plain text, with various options

Integration with Tools

May require additional manual steps for integration

Seamless integration with tools like R Markdown

Aesthetics

Basic appearance, often lacking visual appeal

Neat and visually appealing, suitable for publications

The Stargazer package in R is a versatile and user-friendly tool for creating summary tables of statistical models. With its easy-to-use interface and various customization options, Stargazer simplifies the process of presenting model results effectively. Stargazer helps enhance the clarity and professionalism of our statistical analyses by generating neat and professional-looking tables in various formats.


Article Tags :

Explore