COMET
  • Get Started
    • Quickstart Guide
    • Install and Use COMET
    • Get Started
  • Learn By Skill Level
    • Getting Started
    • Beginner
    • Intermediate - Econometrics
    • Intermediate - Geospatial
    • Advanced

    • Browse All
  • Learn By Class
    • Making Sense of Economic Data (ECON 226/227)
    • Econometrics I (ECON 325)
    • Econometrics II (ECON 326)
    • Statistics in Geography (GEOG 374)
  • Learn to Research
    • Learn How to Do a Project
  • Teach With COMET
    • Learn how to teach with Jupyter and COMET
    • Using COMET in the Classroom
    • See COMET presentations
  • Contribute
    • Install for Development
    • Write Self Tests
  • Launch COMET
    • Launch on JupyterOpen (with Data)
    • Launch on JupyterOpen (lite)
    • Launch on Syzygy
    • Launch on Colab
    • Launch Locally

    • Project Datasets
    • Github Repository
  • |
  • About
    • COMET Team
    • Copyright Information
  1. R Notebooks
  2. Exporting Regression Output (11)
  • Learn to Research


  • STATA Notebooks
    • Setting Up (1)
    • Working with Do-files (2)
    • STATA Essentials (3)
    • Locals and Globals (4)
    • Opening Datasets (5)
    • Creating Variables (6)
    • Within Group Analysis (7)
    • Combining Datasets (8)
    • Creating Meaningful Visuals (9)
    • Combining Graphs (10)
    • Conducting Regression Analysis (11)
    • Exporting Regression Output (12)
    • Dummy Variables and Interactions (13)
    • Good Regression Practices (14)
    • Panel Data Regression (15)
    • Difference in Differences (16)
    • Instrumental Variable Analysis (17)
    • STATA Workflow Guide (18)

  • R Notebooks
    • Setting Up (1)
    • Working with R Scripts (2)
    • R Essentials (3)
    • Opening Datasets (4)
    • Creating Variables (5)
    • Within Group Analysis (6)
    • Combining Datasets (7)
    • Creating Meaningful Visuals (8)
    • Combining Graphs (9)
    • Conducting Regression Analysis (10)
    • Exporting Regression Output (11)
    • Dummy Variables and Interactions (12)
    • Good Regression Practices (13)
    • Panel Data Regression (14)
    • Difference in Differences (15)
    • Instrumental Variable Analysis (16)
    • R Workflow Guide (17)

  • Pystata Notebooks
    • Setting Up (1)
    • Working with Do-files (2)
    • STATA Essentials (3)
    • Locals and Globals (4)
    • Opening Datasets (5)
    • Creating Variables (6)
    • Within Group Analysis (7)
    • Combining Datasets (8)
    • Creating Meaningful Visuals (9)
    • Combining Graphs (10)
    • Conducting Regression Analysis (11)
    • Exporting Regression Output (12)
    • Dummy Variables and Interactions (13)
    • Good Regression Practices (14)
    • Panel Data Regression (15)
    • Difference in Differences (16)
    • Instrumental Variable Analysis (17)
    • STATA Workflow Guide (18)

On this page

  • Prerequisites
  • Learning Outcomes
  • 11.1 Exporting Regression Output
  • 11.2 Plotting Regression Coefficients
  • 11.3 Wrap Up
  • 11.4 Wrap-up Table
  • Report an issue

Other Formats

  • Jupyter
  1. R Notebooks
  2. Exporting Regression Output (11)

11 - Exporting Regression Output

econ 490
r
regression
exporting
visualization
Here, we work on how to export our regression results. We introduce some packages to make our regression results look professional and to present our coefficients in a meaningful manner.
Author

Marina Adshade, Paul Corcuera, Giulia Lo Forte, Jane Platt

Published

29 May 2024

Prerequisites

  1. Run OLS Regressions.

Learning Outcomes

  1. Being able to export regression output in a table.
  2. Being able to plot regression coefficients in a graph.

11.1 Exporting Regression Output

When doing our project, presenting our results in a clear and organized manner is as important as obtaining the results themselves. R’s output is very clear on the computer display, but at some point we need to “move” it from R to our draft. In this module, we will see how to save a regression output in a table.

Once again, we will be using the fictional data set. Recall that this data is simulating information of workers in the years 1982-2012 in a fictional country where a training program was introduced in 2003 to boost their earnings.

Let’s start by loading our packages and opening the dataset.

# Loading in our packages
library(tidyverse)
library(haven)
library(IRdisplay)

# Open the data
fake_data <- read_dta("../econ490-stata/fake_data.dta")

Imagine we are interested in estimating a multivariate regression of the following form:

\[ \text{earnings}_{it} = \alpha + \beta_1 \text{age}_{it} + \beta_2 \text{sex}_i + \varepsilon_{it} \]

where \(\text{Earnings}_{it}\) is the logarithm of earnings of individual \(i\) at time \(t\), \(\text{Age}_{it}\) is the logarithm of age of individual \(i\) at time \(t\), and \(\text{Sex}_i\) is a dummy variable equal to one if the sex of individual \(i\) is female.

First, we create the variables we need.

fake_data <- fake_data %>%
        mutate(log_earnings = log(earnings)) %>%
        mutate(log_age = log(age)) %>%
        mutate(sexdummy = as.factor(sex))

Then we can estimate our regression using the function lm. We have seen how to do it in Module 10.

summary(lm(data=fake_data, log_earnings ~ log_age + sexdummy))

There are different options available to export this table to another file. In this module, we will use stargazer.

stargazer can take several options. In its simplest form, we just need to type stargazer(modelname, type="filetype", output="filename") to save the results of the model modelname in a file of type filetype named filename. We can use text files, tex files, and html files.

For example, let’s save our results in a text file named table.txt. First, we have to call the stargazer library.

#uncomment this line to install the package! install.packages("stargazer")
library(stargazer)

Then, we can save our linear model in a object called model1 and use it as input of the stargazer function.

model1 <- lm(data=fake_data, log_earnings ~ log_age + sexdummy)
stargazer(model1, type="text", out="table.txt")

A file named table.txt should appear in your folder. Notice that this worked, but our table does not have a very professional appearance yet. We can add more options to the function stargazer to make our results more clear and organized.

Here are some of the options we can add:

  • we can align the numeric values within our table with option align=TRUE;
  • we can keep only selected statistics using keep.stat;
  • we can add a title titlename with the option title="titlename";
  • we can modify the labels of covariates in the regression table with the option covaraiate.labels;
  • we can show only some coefficients, by including them in keep(coeffnames). Similarly, we can omit some of the coefficients by including them in omit(coeffnames).

Let’s try all of them in practice. Let’s save again the same table, with the following modifications:

  • keep only the coefficients for log_age and sexdummy;
  • rename those coefficients;
  • keep only the statistics on number of observations and adjusted R\(^2\);
  • add a title.
stargazer(model1, type="text", out="table.txt", title="Earnings analysis", keep.stat=c("n","adj.rsq"), keep=c("log_age","sexdummy"), covariate.labels=c("Age (ln)", "Male"))

This is way nicer, but what if we want to show the results of multiple models in the same table?

Suppose we want to first estimate a model with only age or only sex as an explanatory variable, and then a multivariate model encompassing both. In this case, we only need to store the results of each model in a separate object and then add all of them as inputs of stargazer.

In the example below, we store the three models in objects model1, model2, and model3 before adding them as inputs of stargazer.

# Store regressions
model1 <- lm(data=fake_data, log_earnings ~ log_age)
model2 <- lm(data=fake_data, log_earnings ~ sexdummy)
model3 <- lm(data=fake_data, log_earnings ~ log_age + sexdummy)

# Create table
stargazer(model1, model2, model3, title="Comparison", align=TRUE, type="text", out="table.txt", keep.stat=c("n","adj.rsq"))

11.2 Plotting Regression Coefficients

Visual representations can be better than tables. Sometimes we need to plot our estimated coefficients and their confidence intervals.

In R, this is easily done with command coefplot. The graphs obtained with coefplot are easy to customize. In its simplest use, we only need to save our regression results in an object and then give that object as input of coefplot.

Once again, let’s try it on our multivariate model. The first thing to do, is to load the corresponding library.

# Load package
#uncomment this line to install the package! install.packages("coefplot")

library(coefplot)

Now we can save our estimated coefficients in an object named model1 and use it as input for the coefplot function. Note that we can omit the constant by adding the option intercept=FALSE.

model1 <- lm(data=fake_data, log_earnings ~ log_age + sexdummy)
coefplot(model1, intercept=FALSE)

We can customize our graph further by using options that are specific to coefplot. By default, R draws two confidence intervals: the first at one standard deviation from the coefficient, and the second at two standard deviations from the coefficient. We can modify them with the options innerCI and outerCI, respectively. By default, they are set to innerCI=1 and outerCI=2.

We can also change the color of the estimates and their confidence intervals with the option color.

Finally, we can display the estimated coefficients horizontally with the option horizontal=TRUE.

Let’s apply these options to our example and generate an horizontal plot with red objects and only one confidence interval at 1.5 standard deviations distance.

coefplot(model1, intercept=FALSE, horizontal=TRUE, color="red", innerCI=0, outerCI=1.5)

11.3 Wrap Up

We have learned in this module how to store regression output in a clear and organized manner using the command stargazer and how to plot regression coefficients using the command coefplot.

Remember to check the R documentation when creating graphs and exporting tables. The documentation can be your best ally if you end up using it.

11.4 Wrap-up Table

Command Function
stargazer(modelname, type="filetype", output="filename") It saves modelname in a file of type filetype named filename.
coefplot(modelname) It plots regression coefficients and two confidence intervals, one at 1 standard deviation and the other at 2 standard deviations distance.
Conducting Regression Analysis (10)
Dummy Variables and Interactions (12)
  • Creative Commons License. See details.
 
  • Report an issue
  • The COMET Project and the UBC Vancouver School of Economics are located on the traditional, ancestral and unceded territory of the xʷməθkʷəy̓əm (Musqueam) and Sḵwx̱wú7mesh (Squamish) peoples.