{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 12 - Exporting Regression Output\n", "\n", "Marina Adshade, Paul Corcuera, Giulia Lo Forte, Jane Platt \n", "2024-05-29\n", "\n", "## Prerequisites\n", "\n", "1. Run OLS Regressions.\n", "\n", "## Learning Outcomes\n", "\n", "1. Being able to export regression output in a table.\n", "2. Being able to plot regression coefficients in a graph.\n", "\n", "## 12.1 Exporting Regression Output\n", "\n", "When doing our project, presenting our results in a clear and organized\n", "manner is as important as obtaining the results themselves. Stata’s\n", "output is very clear on the computer display, but at some point we need\n", "to “move” it from Stata to our draft. In this module, we will see how to\n", "save a regression output in a table.\n", "\n", "Once again, we will be using the fictional data set. Recall that this\n", "data is simulating information of workers in the years 1982-2012 in a\n", "fictional country where a training program was introduced in 2003 to\n", "boost their earnings.\n", "\n", "Let’s start by opening the dataset.\n", "\n", "``` {stata}\n", "* Load the dataset\n", "clear *\n", "*cd \"\"\n", "use \"fake_data.dta\", clear\n", "```\n", "\n", "Imagine we are interested in estimating a multivariate regression of the\n", "following form:\n", "\n", "$$\n", "\\text{earnings}_{it} = \\alpha + \\beta_1 \\text{age}_{it} + \\beta_2 \\text{sex}_i + \\varepsilon_{it}\n", "$$\n", "\n", "where $\\text{Earnings}_{it}$ is the logarithm of earnings of individual\n", "$i$ at time $t$, $\\text{Age}_{it}$ is the logarithm of age of individual\n", "$i$ at time $t$, and $\\text{Sex}_i$ is a dummy variable equal to one if\n", "the sex of individual $i$ is female.\n", "\n", "First, we create the variables we need.\n", "\n", "``` {stata}\n", "* Create the variables\n", "generate logearn = log(earnings)\n", "generate logage = log(age)\n", "generate sexdummy = 1 if sex == \"F\"\n", "replace sexdummy = 0 if missing(sexdummy)\n", "```\n", "\n", "Then, we can estimate our specification using the command `regress`. We\n", "have seen how to do it in [Module\n", "11](https://comet.arts.ubc.ca/docs/Research/econ490-stata/11_Linear_Reg.html).\n", "\n", "``` {stata}\n", "regress logearn logage sexdummy\n", "```\n", "\n", "There are different options available to export this table to another\n", "file. In this module, we will use `etable`, a command available by\n", "default in Stata 17 and subsequent versions.\n", "\n", "`etable` can take several options. In its simplest form, we just need to\n", "type `etable, export(filename)` after fitting a model to save a table in\n", "a file named *filename*. We can use files of Microsoft Word, Microsoft\n", "Excel, LATEX, Markdown, or PDF, but we need to specify the right\n", "extension.\n", "\n", "For example, let’s save our results in a Microsoft Word file named\n", "*table.docx*.\n", "\n", "``` {stata}\n", "regress logearn logage sexdummy\n", "etable, export(table.docx, replace)\n", "```\n", "\n", "A file named *table.docx* should appear in your folder. Notice that this\n", "worked, but our table does not have a very professional appearance yet.\n", "We can add more options to the command `etable` to make our results more\n", "clear and organized.\n", "\n", "Here are some of the options we can add:\n", "\n", "- we can add more statistics, such as the number of observations\n", " (*N*), the R$^2$ (*r2*), the adjusted R$^2$ (*r2_a*), and the F\n", " statistic (*F*), with the options `mstat(N)`, `mstat(r2)`,\n", " `mstat(r2_a)`, and `mstat(F)`;\n", "- we can add a title *titlename* with the option `title(titlename)`;\n", "- we can show the stars indicating the level of significance of our\n", " coefficients with the option `showstars` and add a footnote\n", " explaining them with `showstarsnote`;\n", "- for the coefficients, we can display the variable labels instead of\n", " their names by adding the option `varlabel`;\n", "- for the dependent variable, we can display its variable label\n", " instead of its name by adding the option `column(dvlabel)`;\n", "- we can show only some coefficients, by including them in\n", " `keep(coeffnames)`. For example, we can show only the coefficients\n", " for age and sex by adding the option `keep(logage sexdummy)`.\n", "\n", "Let’s try all of them in practice. Notice that now we add the option\n", "*replace* when we save the file because there is already a Microsoft\n", "Word file named *table.docx*: `export(table.docx, replace)`.\n", "\n", "``` {stata}\n", "* Add labels to variables\n", "label var logearn \"Earnings (ln)\"\n", "label var logage \"Age (ln)\"\n", "label var sexdummy \"Female\"\n", "\n", "* Run regression\n", "regress logearn logage sexdummy\n", "\n", "* Store results\n", "etable, export(table.docx, replace) mstat(N) mstat(r2_a) title(Earnings) showstars showstarsnote keep(logage sexdummy) varlabel column(dvlabel)\n", "```\n", "\n", "This is way nicer, but what if we want to show the results of multiple\n", "models in the same table?\n", "\n", "Suppose we want to first estimate a model with only *age* or only *sex*\n", "as an explanatory variable, and then a multivariate model encompassing\n", "both. In this case, we just need to store the results of each model\n", "using the command `estimates store`.\n", "\n", "In the example below, we store the three models in objects *model1*,\n", "*model2*, and *model3*.\n", "\n", "``` {stata}\n", "* Store first regression in model1\n", "regress logearn logage\n", "estimates store model1\n", "\n", "* Store second regression in model2\n", "regress logearn sexdummy\n", "estimates store model2\n", "\n", "* Store third regression in model3\n", "regress logearn logage sexdummy\n", "estimates store model3\n", "```\n", "\n", "Now, we can export all the objects in one single table by calling their\n", "names in the options `estimates()`.\n", "\n", "``` {stata}\n", "etable, estimates(model1 model2 model3) mstat(N) mstat(r2_a) showstars showstarsnote varlabel column(dvlabel) export(table.docx, replace)\n", "```\n", "\n", "## 12.2 Plotting Regression Coefficients\n", "\n", "Visual representations can be better than tables. Sometimes we need to\n", "plot our estimated coefficients and their confidence intervals.\n", "\n", "In Stata, this is easily done with command `coefplot`. The graphs\n", "obtained with `coefplot` are easy to customize. In its simplest use, we\n", "only need to run `coefplot` right after our regression.\n", "\n", "**Note:** You will need to install command `coefplot` from the SSC\n", "Archive the first time you use it on your local computer. To do so, type\n", "`ssc install coefplot`.\n", "\n", "Once again, let’s try it on our multivariate model. We can omit the\n", "constant by adding the option `drop(_cons)`. Remember to save the graph.\n", "\n", "``` {stata}\n", "regress logearn logage sexdummy\n", "coefplot, drop(_cons)\n", "graph export graph1.jpg, as(jpg) replace\n", "```\n", "\n", "Since it is a graph, we can add most of the options that we have seen in\n", "[Module\n", "9](https://comet.arts.ubc.ca/docs/Research/econ490-stata/09_Stata_Graphs.html).\n", "For example, we can change the color of the background from light blue\n", "to white with the option `graphregion(color(white))`.\n", "\n", "There are some options that are specific to `coefplot`. By default,\n", "confidence intervals are drawn at 95% significance levels. We can\n", "specify different and multiple levels in the option `levels()`. For\n", "example, we can show both the 95% and 99.9% confidence intervals with\n", "`levels(99.9 95)`.\n", "\n", "Additionally, we can use a vertical layout with the option `vertical`.\n", "\n", "Let’s apply these options to our example.\n", "\n", "``` {stata}\n", "regress logearn logage sexdummy\n", "coefplot, drop(_cons) graphregion(color(white)) levels(99.9 95) vertical\n", "graph export graph1.jpg, as(jpg) replace\n", "```\n", "\n", "## 12.3 Wrap Up\n", "\n", "We have learned in this module how to store regression output in a clear\n", "and organized manner using the command `etable` and how to plot\n", "regression coefficients using the command `coefplot`.\n", "\n", "Remember to check the Stata documentation when creating graphs and\n", "exporting tables. The documentation can be your best ally if you end up\n", "using it.\n", "\n", "## 12.4 Wrap-up Table\n", "\n", "| Command | Function |\n", "|--------------------------------|----------------------------------------|\n", "| `etable, export(filename)` | It exports the regression output to a file named *filename*. |\n", "| `coefplot` | It plots regression coefficients and their 95% confidence intervals. |\n", "\n", "## References\n", "\n", "[etable manual](https://www.stata.com/manuals/retable.pdf)
[How to\n", "use\n", "coefplot](https://repec.sowi.unibe.ch/stata/coefplot/getting-started.html)\n", "
" ], "id": "e4ef4267-2c56-47bb-b452-9362c8d21d03" } ], "nbformat": 4, "nbformat_minor": 5, "metadata": { "kernelspec": { "name": "python3", "display_name": "Python 3 (ipykernel)", "language": "python", "path": "/usr/local/share/jupyter/kernels/python3" }, "language_info": { "name": "python", "codemirror_mode": { "name": "ipython", "version": "3" }, "file_extension": ".py", "mimetype": "text/x-python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } } }