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
Run OLS Regressions.
Learning Outcomes
Being able to export regression output in a table.
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 packageslibrary(tidyverse)
Warning: package 'tidyverse' was built under R version 4.4.3
Warning: package 'tibble' was built under R version 4.4.2
Warning: package 'tidyr' was built under R version 4.4.2
Warning: package 'readr' was built under R version 4.4.2
Warning: package 'purrr' was built under R version 4.4.3
Warning: package 'dplyr' was built under R version 4.4.3
Warning: package 'stringr' was built under R version 4.4.2
Warning: package 'forcats' was built under R version 4.4.2
Warning: package 'lubridate' was built under R version 4.4.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.0 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.1 ✔ tibble 3.2.1
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.0.4
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(haven)
Warning: package 'haven' was built under R version 4.4.2
library(IRdisplay)# Open the datafake_data <-read_dta("../econ490-stata/fake_data.dta")
Imagine we are interested in estimating a multivariate regression of the following form:
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.
Call:
lm(formula = log_earnings ~ log_age + sexdummy, data = fake_data)
Residuals:
Min 1Q Median 3Q Max
-7.1618 -0.7342 0.0056 0.7400 7.1448
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 9.422341 0.067795 138.98 <2e-16 ***
log_age 0.220008 0.018099 12.16 <2e-16 ***
sexdummyM 0.539926 0.007405 72.92 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 1.116 on 138135 degrees of freedom
Multiple R-squared: 0.04234, Adjusted R-squared: 0.04232
F-statistic: 3053 on 2 and 138135 DF, p-value: < 2.2e-16
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)
Please cite as:
Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
Then, we can save our linear model in a object called model1 and use it as input of the stargazer function.
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\);
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.
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.
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.
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.