library(tidyverse)
library(broom)
library(modelsummary)
library(juanr)Problem Set 1: OLS Review
Due: Tuesday, April 7 by 11:59 PM. Push your completed .qmd and rendered .html to your GitHub repository.
Format: Write a self-contained Quarto document (.qmd) that renders to HTML. Your document should read as a short report — include brief prose explaining your reasoning, not just code and output.
Libraries you will likely need:
Shared data. Questions 3–5 use the county-level elections data from the juanr package. Prepare the data as follows:
small_elections = elections |>
mutate(
per_dem_2020 = per_dem_2020 * 100,
income_10k = hh_income / 10000
) |>
select(name, state, per_dem_2020, income_10k,
white_pct = white, black_pct = black)Question 1: Your own data
Browse Data Is Plural and find a dataset that interests you. Download it and place it in a data/ folder in your class folder.
Describe the dataset briefly: what are the observations? What variables are available?
Identify an outcome variable (\(Y\)) and a treatment variable (\(X\)). State a hypothesis: how do you expect \(X\) to affect \(Y\), and why?
Identify at least one control variable (\(Z\)). Why would it be important to control for this variable?
Estimate a linear model using
lm(). Present the results in a publication-quality regression table usingmodelsummary().Interpret the coefficient on your treatment variable using the “comparison” framing from lecture.
Create a marginal effects plot. Add a confidence interval and a rug plot showing the distribution of your treatment variable.
Question 2: Deriving \(\hat{\beta}_0\)
This question is required only for students completing the methods field. All others may skip it.
In lecture, we derived the OLS estimator for \(\hat{\beta}_1\) by taking \(\partial SSR / \partial \beta_1 = 0\) and solving. Now do the same for \(\hat{\beta}_0\).
In Quarto, wrapping text in two $ like so: $3x + 4 = 2$ will trigger math mode. From there you could write “loosely” and then ask your AI tool to clean it up. Or write on paper and include an image in your repo!
Starting from the sum of squared residuals for the two-parameter model:
\[SSR(\beta_0, \beta_1) = \sum_{i=1}^n (Y_i - \beta_0 - \beta_1 X_i)^2\]
Take the partial derivative of SSR with respect to \(\beta_0\).
Hint: Remember the chain rule.
Set the result equal to zero and solve for \(\hat{\beta}_0\).
Hint: After simplifying, you should have a sum of three terms. From there, remember that \(\sum_{i=1}^n \beta_0 = n\beta_0\) and \(\sum_{i=1}^n Y_i = n\bar{Y}\).
Show that: \(\hat{\beta}_0 = \bar{Y} - \hat{\beta}_1 \bar{X}\)
What does the result tell us about where the regression line necessarily passes through?
Hint: Plug in \(X = \bar{X}\) and \(\hat{\beta}_0 = \bar{Y} - \hat{\beta}_1 \bar{X}\) into the fitted equation (\(\hat{Y} = \hat{\beta}_0 + \hat{\beta}_1 \cdot X\)). If \(X = \bar{X}\), what must \(Y\) equal?
Question 3: OLS and the conditional mean
Ask for help with how to use ntile(), or how to add the OLS regression line to the plot.
One way to think about fitting models is that we’d like to know, for any given value of \(X\), what we should expect \(Y\) to look like. If we could just compute the average \(Y\) at each value of \(X\), we’d have the conditional mean: \(E[Y \mid X = x]\). OLS approximates this conditional mean with a straight line. How good is that approximation?
Using the small_elections data:
Create a categorical variable that bins
income_10kinto income deciles (10 equal-sized groups;ntile()can do this). Compute the mean ofper_dem_2020within each decile, along with the mean ofincome_10k(the midpoint). Plot the midpoints against the decile means as points.Overlay the OLS regression line from
lm(per_dem_2020 ~ income_10k)on the same plot. Hint:geom_smooth()can do this with the right arguments.Add a
loess()smooth to the plot withgeom_smooth(). This is a flexible, non-parametric curve that estimates \(E[Y \mid X]\) without assuming linearity.In 3–4 sentences, discuss what you see. Where does the OLS line agree with the binned means and the loess curve? Where does it diverge? What does this tell you about the linearity assumption?
Question 4: Does the effect of income vary across states?
You can fully use AI to help you with part (b) and to troubleshoot plotting aesthetics in (d). The code involves tools we haven’t covered yet (nest(), map(), unnest() from the purrr package). Let the AI generate the pipeline, but you must explain what each step does in your own words.
In lecture we estimated a single model pooling all US counties. But does the relationship between income and Democratic vote share look the same in every state? Maybe income matters more in some states than others.
Using the small_elections data:
Pick two states. For each, estimate a separate bivariate regression of
per_dem_2020onincome_10kusing only observations from that state. Compare the two slopes.Now scale this up: estimate a separate bivariate regression for every state. Use
nest()to split the data by state,map()to fit a model within each group, andtidy()to extract the coefficient onincome_10kalong with its 95% confidence interval.In your own words, explain what
nest(),map(), andunnest()are each doing in the pipeline.Create a coefficient plot: a dot-and-whisker plot showing \(\hat{\beta}_1\) for each state, sorted from smallest to largest, with 95% confidence intervals. Draw a vertical reference line at 0.
In 3–4 sentences, discuss what you see. Do all states show the same relationship? Are there states where income has no detectable effect, or where the effect is negative? What might explain the variation?
Question 5: What does “controlling” actually do?
In Q1, you controlled for one or more variables. But what does “controlling for \(Z\)” actually do inside the regression?
When we worry that \(Z\) confounds the relationship between \(X\) and \(Y\), what we really want to know is: does the relationship between \(X\) and \(Y\) still hold among observations that have the same value of \(Z\)? If income only predicts Democratic vote share because Southern and non-Southern states differ in both income and partisanship, then the relationship should weaken or disappear once we compare counties within the same region.
That’s one way of thinking about what “controlling for \(Z\)” does. It turns out that when \(Z\) is binary, the coefficient on \(X\) in lm(Y ~ X + Z) is a weighted average of the slopes estimated separately within each group defined by \(Z\). The weights reflect both group size and within-group variation in \(X\):
\[ \hat{\beta}_{\text{pooled}} = \frac{\sum_{g=1}^{G} w_g \cdot \hat{\beta}_g}{\sum_{g=1}^{G} w_g}, \quad w_g = (n_g - 1) \cdot \text{Var}_g(X) \]
where \(g\) indexes the groups defined by \(Z\) (e.g., South vs. non-South), \(\hat{\beta}_g\) is the slope on \(X\) estimated within group \(g\), \(n_g\) is the number of observations in group \(g\), and \(\text{Var}_g(X)\) is the variance of \(X\) within group \(g\).
Let’s verify this with the small_elections data.
Create a binary variable called
souththat equals 1 for the following states: AL, AR, FL, GA, LA, MS, NC, SC, TN, TX, VA. These are the 11 states of the former Confederacy.Estimate two separate bivariate regressions of
per_dem_2020onincome_10k— one for Southern counties and one for non-Southern counties. Report the slope on income for each.Compute the weight for each group using the formula above: \(w_g = (n_g - 1) \cdot \text{Var}_g(X)\), where \(X\) is
income_10k. Which group gets more weight, and why?Compute the weighted average of the two slopes using the weights from (c).
Now estimate the pooled model with the control:
per_dem_2020 ~ income_10k + south. Compare the coefficient onincome_10kto your weighted average from (d). Show that they are the same.