```
# Loading in our packages
library(tidyverse)
library(haven)
library(IRdisplay)
# Open the data
<- read_dta("../econ490-stata/fake_data.dta") fake_data
```

# ECON 490: Exporting Regression Output (11)

## 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 dataset. 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.

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 specification, 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.

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

```
<- lm(data=fake_data, log_earnings ~ log_age + sexdummy)
model1 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
<- lm(data=fake_data, log_earnings ~ log_age)
model1 <- lm(data=fake_data, log_earnings ~ sexdummy)
model2 <- lm(data=fake_data, log_earnings ~ log_age + sexdummy)
model3
# 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
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`

.

```
<- lm(data=fake_data, log_earnings ~ log_age + sexdummy)
model1 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 `etable`

and how to plot regression coefficients using the command `stargazer`

.

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.

Here is a summary of the new commands we have seen in this module.

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