Learning Objectives
Following this assignment students should be able to:
- Write formatted text and math equations in LaTeX
- Include Stata-produced figures and tables in a LaTeX document
- Create coefficient plots with
coefplot- Build specification charts for robustness analysis
- Produce publication-ready regression and summary statistics tables with
esttab
Reading
Topics
- LaTeX
- Descriptive statistics in tables
- Regression results in tables
- Regression results as graphics
Readings
- Learn LaTeX in 30 minutes by Overleaf
- Making regression tables simplified by Ben Jann (The Stata Journal, 2007)
- Using Graphs Instead of Tables in Political Science by Kastellec & Leoni (Perspectives on Politics, 2007)
Lecture Notes
Exercises
-
LaTeX Formatting and Math (10 pts)
In your
lastname.texfile in thesemester26Overleaf project, under\section*{Assignment 10}, write the following using proper LaTeX formatting. You do not need to run any Stata code for this exercise.1. Write a short paragraph (2–3 sentences) explaining what omitted variable bias is. Bold the term “omitted variable bias” and italicize the word “biased.”
2. Write the OLS regression equation in display math (using
\begin{equation}…\end{equation}):[Y_i = \beta_0 + \beta_1 X_i + \varepsilon_i]
Use
\beta, subscripts (_), and\varepsilon.3. Write the formula for the OLS estimator of $\hat{\beta}_1$ as a displayed equation:
[\hat{\beta}1 = \frac{\sum{i=1}^{n}(X_i - \bar{X})(Y_i - \bar{Y})}{\sum_{i=1}^{n}(X_i - \bar{X})^2}]
Use
\hat{},\bar{},\frac{}{},\sum_{i=1}^{n}, and superscripts (^).4. Write the formula for the standard error of the regression as a displayed equation:
[SE(\hat{\beta}) = \sqrt{\frac{1}{n} \sum_{i=1}^{n} (X_i - \bar{X})^2}]
Use
\sqrt{},\frac{}{},\sum_{i=1}^{n},\bar{}, and\hat{}.
-
Inserting a Stata Figure (10 pts)
Using
tenuredata.dta, create a graph in Stata and include it in your LaTeX document. Start by loadingtenuredata.dta.- Create a scatter plot of
yieldagainstq_f_ha(fertilizer per hectare) with a fitted line (lfit). Choose formatting option for the markers, line, and axis titles. - In your
lastname.texunder Assignment 10, include the graph. Give it the captionRice yield vs fertilizer applicationand label itfig:scatter_rice.
- Create a scatter plot of
-
Basic `esttab` Table (10 pts)
Using
tenuredata.dta, produce a formatted regression table.1. In your
lastname.tex, include the table using\input{10-rice-regs.tex}inside atableenvironment.- Run
reg yield q_f_ha lt_f_ha, vce(cluster panelid)a - Store results as
r1. -
Display the table with
esttab:esttab r1 using "$answ/10-esttab-basic-1.tex", replace /// se star(* 0.10 ** 0.05 *** 0.01) /// keep(q_f_ha lt_f_ha) /// label booktabs /// stats(N r2, labels("Observations" "R-squared") fmt(0 3)) - In your
lastname.tex, include the table using\input{10-esttab-basic-1.tex}inside atableenvironment.
2.Run and store three regressions, all with
vce(cluster panelid):- r2:
yieldonq_f_ha lt_f_ha i.irrig i.tenure - r3:
yieldonq_f_ha lt_f_ha i.irrig i.tenure i.site i.year - Display a three-column table with
esttaband export it to LaTeX:
esttab r1 r2 r3 using "$answ/10-esttab-basic-2.tex", replace /// se star(* 0.10 ** 0.05 *** 0.01) /// keep(q_f_ha lt_f_ha 1.irrig 1.tenure) /// order(q_f_ha lt_f_ha 1.irrig 1.tenure) /// label booktabs /// indicate("Site FE = *.site" "Year FE = *.year") /// stats(N r2, labels("Observations" "R-squared") fmt(0 3)) /// note("Clustered SEs at household level." /// "\sym{*} \(p<0.10\), \sym{**} \(p<0.05\), " /// "\sym{***} \(p<0.01\)")- In your
lastname.tex, include the table using\input{10-esttab-basic-2.tex}inside atableenvironment.
- Run
-
Multi-Column Table with Notes (20 pts)
Using
tenuredata.dta(rice observations only), produce a multi-column regression table and export it to LaTeX. Run and store three regressions, all withvce(cluster panelid):- r1:
yieldonq_f_ha lt_f_ha - r2:
yieldonq_f_ha lt_f_ha i.irrig i.tenure - r3:
yieldonq_f_ha lt_f_ha i.irrig i.tenure i.site i.year
Display a three-column table with
esttaband export it to LaTeX using theprehead,postfoot, andfragmentoptions to create grouped column headers and a detailed note:esttab r1 r2 r3 using "$answ/10-rice-regs.tex", replace /// b(3) se(3) /// keep(q_f_ha lt_f_ha 1.irrig 1.tenure) /// order(q_f_ha lt_f_ha 1.irrig 1.tenure) /// star(* 0.10 ** 0.05 *** 0.01) /// indicate("Site FE = *.site" "Year FE = *.year") /// stats(N r2, labels("Observations" "R-squared") fmt(0 3)) /// noobs booktabs nonum nomtitle collabels(none) /// nobaselevels nogaps fragment label /// prehead("\begin{tabular}{l*{3}{c}} " /// "\\[-1.8ex]\hline \hline \\[-1.8ex] " /// "& \multicolumn{1}{c}{Baseline} & " /// "\multicolumn{2}{c}{With Controls} \\ " /// "\cline{2-2} \cline{3-4} \\[-1.8ex] " /// "& \multicolumn{1}{c}{(1)} & " /// "\multicolumn{1}{c}{(2)} " /// "& \multicolumn{1}{c}{(3)} \\ \midrule") /// postfoot("\hline \hline \\[-1.8ex] " /// "\multicolumn{4}{p{0.8\linewidth}}{\small " /// "\noindent \textit{Note}: Dependent variable " /// "is rice yield in kg/ha. All models use " /// "OLS with standard errors clustered at the " /// "household level (in parentheses). " /// "* p$<$0.10, ** p$<$0.05, *** p$<$0.01.} " /// "\end{tabular}")In your
lastname.tex, include the table using\input{10-rice-regs.tex}inside atableenvironment. To get the***to render correctly, you’ll need to add\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}to your preamble.
- r1:
-
Summary Statistics Table (10 pts)
Using
tenuredata.dta(rice observations only), produce a summary statistics table.- First, run
estimates clearto clear any previously stored estimates. - Use
estpost sumto compute summary statistics for:yield,q_f_ha,lt_f_ha,area,irrig,tenure. -
Display the table with:
esttab, cells("count mean sd min max") /// noobs nonumber nomtitle /// title("Summary Statistics — Rice Parcels") /// label
- First, run
-
Basic `coefplot` (10 pts)
Using
tenuredata.dta, create a coefficient plot.- Run a regression of
yieldonq_f_ha,lt_f_ha,i.irrig, andi.tenure, with standard errors clustered at the household level:vce(cluster panelid). - Create a coefficient plot using
coefplot:- Drop the constant (
drop(_cons)) - Add a vertical reference line at zero (
xline(0)) - Relabel the coefficients to be more descriptive
- Add a
titleand anxtitle
- Drop the constant (
- Export the graph:
graph export "$answ/10-coefplot-basic.png", replace
- Run a regression of
-
Multi-Model `coefplot` (10 pts)
Using
tenuredata.dta, compare the fertilizer and labor coefficients across multiple specifications.- Run and store three regressions, all with
vce(cluster panelid):- m1:
yieldonq_f_ha lt_f_ha i.irrig i.tenure - m2:
yieldonq_f_ha lt_f_ha i.irrig i.tenure i.site - m3:
yieldonq_f_ha lt_f_ha i.irrig i.tenure i.site i.year
- m1:
-
Create a multi-model coefplot showing only
q_f_haandlt_f_ha:coefplot (m1, label("Baseline") ) /// (m2, label("+ Tenure/Irrig") ) /// (m3, label("+ Village/Year FE") ), /// keep(q_f_ha lt_f_ha) xline(0) /// xtitle("Coefficient size") - Export the graph:
graph export "$answ/10-coefplot-multi.png", replace - In your
lastname.tex, include the graph using\includegraphics[width=0.8\textwidth]{10-coefplot-multi.png}
- Run and store three regressions, all with
-
Specification Chart (15 pts)
Using
tenuredata.dta, build a specification chart that shows how the fertilizer coefficient varies across many specifications. You can do this by adapting the code from the lecture notes, replacing variable names and labels as necessary.- Use
postfileto loop over specifications varying the controls, fixed effects, and standard errors (uselnyas the dependent variable):- Controls (4 levels): (1) baseline (
lnf lnl lnp), (2) + plot characteristics (i.irrig i.tenure), (3) + assets (tractor carabao), (4) + HH characteristics (hhsize educhoh agehoh) - Fixed Effects (3 levels): (1) none, (2) site (
i.site), (3) site and year (i.site i.year) - Standard errors (2 levels): (1) default, (2) clustered at
panelid
- Controls (4 levels): (1) baseline (
- Store the coefficient on
lnf, its SE, and the 95% CI from each specification. - After the loop, load the results and create significance indicators:
- Sort by
betaand generateobs = _n. - Stack specification indicators for the bottom panel (controls, FEs, SEs) and adjust your dual y-axis plot to match the 24 regressions and the new categories. Use colors to distinguish significant positive (blue), significant negative (maroon), and not significant (black) results.
- Export:
graph export "$answ/10-spec-chart-rice.png", replace - In your
lastname.tex, include the graph using\includegraphics[width=0.8\textwidth]{10-spec-chart-rice.png}
- Use
-
Check That Your Code Runs (5 pts)
Sometimes you think you’re code runs, but it only actually works because of something else you did previously. To make sure it actually runs you should save your work and then run it in a clean environment.
Follow these steps in to make sure your code really runs:
-
Restart Stata by closing the instance of Stata that you have open.
-
When you reopen Stata, type in the command line
clear all. Or, better yet, putclear allat the top of your.dofile, after the preamble but before any commands. -
Rerun your entire homework assignment by clicking the
Executebutton to make sure it runs from start to finish and produces the expected results. -
Make sure that you saved your code with the name of the assignment number (i.e.,
assignment_01). You should see the file in your git repo on your machine. -
Make sure that your code will run on other computers (relevant for all assignments after the first assignment)
- No
cd. Use aproject.dofile instead to set directories - Use only relative paths in files other than the
project.dofile - Use
/not\for paths
- No
-
-
Challenge 10 (Challenge - 20 pts)
This challenge uses
tenuredata.dtato practice skills from all three lectures (LaTeX, coefplot, estout). You will produce a short results section with a summary statistics table and a coefficient plot — all integrated into your LaTeX document. Reload thetenuredata.dtaand typeestimates clear.1. Use
estpost sumto compute summary statistics for:yield,q_f_ha,lt_f_ha,area,irrig,tenure,educhoh,aghoh. Then export the table to$answ/10-challenge-sumstats.texusingesttabwithbooktabsandlabel,posthead,postfoot,nonum,nomtitle,nobaselevels,nogaps, andfragment.2. Create a multi-model coefplot comparing
q_f_haacross the four regressions withvce(cluster panelid). Then export:graph export "$answ/10-challenge-coefplot.png", replace- c1:
yieldonq_f_ha lt_f_ha - c2:
yieldonq_f_ha lt_f_ha i.irrig i.tenure - c3:
yieldonq_f_ha lt_f_ha i.irrig i.tenure educhoh agehoh - c4:
yieldonq_f_ha lt_f_ha i.irrig i.tenure educhoh agehoh i.site i.year
3. In your
lastname.texunder Assignment 10, create a short results section that includes:- A couple sentences discuss the summary statistics table and referencing the table (use
\ref{}) - The summary statistics table (
\input{10-challenge-sumstats.tex}) with a title using\caption{}and label using\label{} - A couple sentences discuss the coefficient plot and referencing the figure (use
\ref{}) - The coefficient plot (
\includegraphics{10-challenge-coefplot.png}) with a title using\caption{}and label using\label{} - Compile and verify everything renders correctly.
- c1:
Assignment submission checklist
Solutions
- LaTeX Formatting and Math
- Inserting a Stata Figure 1, 2
- Summary Statistics Table
- Basic
esttabTable 1, 2 - Multi-Column Table with Notes
- Basic
coefplot - Multi-Model
coefplot - Specification Chart
- Check That Your Code Runs
- Challenge 10 1, 2