library(tidyverse)
library(broom)
library(modelsummary)
library(marginaleffects)
library(gapminder)
library(juanr)Problem Set 2: Common Transformations
Due: Tuesday, April 14 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:
Question 1: The cost of childcare
Using the childcare dataset from {juanr}, we’ll estimate the relationship between a county’s poverty rate and the weekly cost of infant daycare (infant_price), controlling for year and state.
Use OLS to estimate the effect of
poverty_rateoninfant_price, controlling foryear(as a factor) andstate. Present the results in a regression table and interpret the coefficient on poverty rate.Using the fitted model, generate predicted values (\(\hat{Y}\)) over a range of poverty rate values while holding the other variables constant. Plot \(\hat{Y}\) against poverty rate with confidence bounds. Discuss the estimated relationship.
Now add a quadratic term for poverty rate to your model. Present the updated regression table and remake the predicted-values plot from (b). Discuss why the effect of poverty on childcare costs might be nonlinear.
Write down the marginal effect of poverty rate on infant price. That is, take the partial derivative of the fitted equation with respect to poverty rate:
\[\frac{\partial \hat{Y}}{\partial \text{poverty\_rate}} = \text{?}\]
Using the coefficients from (c) and the formula from (d), compute the marginal effect of poverty rate at a sequence of values ranging from its minimum to its maximum. Hint: extract \(\hat{\beta}_1\) and \(\hat{\beta}_2\) with
coef()and plug them into your derivative.Plot the marginal effect of poverty rate (y-axis) against poverty rate (x-axis). Draw a horizontal reference line at zero. Discuss what you find.
Question 2: Partisanship, education, and attitudes toward immigrants
Using the therm dataset from {juanr}, we’ll explore whether partisanship shapes attitudes toward immigrants and whether that relationship depends on education.
The outcome variable is ft_immig, a feeling thermometer toward immigrants (0–100, higher = warmer). You will need to create a binary republican variable (1 = Republican, 0 = otherwise) and a numeric version of educ for use in regression.
Use OLS to estimate the effect of being Republican on feelings toward immigrants, controlling for education. Present the regression table and interpret the coefficient on
republican.Now add an interaction between
republicanandeduc_num. Present the updated regression table.Write down the marginal effect of being Republican on
ft_immig. That is, take the partial derivative of the fitted equation with respect torepublican:
\[\frac{\partial \hat{Y}}{\partial \text{republican}} = \text{?}\]
Using the coefficients from (b) and the formula from (c), compute the marginal effect of being Republican at each value of
educ_num(from 1 to 8).Plot the marginal effect of being Republican (y-axis) against education level (x-axis), this time using the
slopes()from{marginaleffects}, which will give you the same result as in (d) but with standard errors. Include confidence bounds in your plot.
Question 3: Campaign fundraising
Using the bonica dataset from {juanr}, we’ll estimate the effect of the number of unique donors on total fundraising receipts for US congressional candidates. The key variables are num_distinct_donors and total_receipts, with cycle (election year) and party as controls. Filter out rows where donors or receipts are missing or zero.
Why might it make sense to log the number of donors rather than use it on its raw scale? Give a theoretical reason.
Estimate the effect of logged number of distinct donors on receipts, controlling for election cycle and candidate party. Present the regression table and interpret the coefficient on the logged donor variable.
Generate predicted values (\(\hat{Y}\)) over a range of number of donor values while holding the other variables constant. Plot \(\hat{Y}\) against number of distinct donors (HINT: on the original scale, not logged) with confidence bounds. Discuss the shape of the relationship.
Question 4: Does it replicate?
In lecture we saw that higher-degree polynomials can chase noise: their predictions can change wildly across subsamples.
Choose any dataset and any bivariate relationship you find interesting (it can be from {juanr}, {gapminder}, or any other source).
State your outcome variable (\(Y\)), your focal predictor (\(X\)), and any controls you plan to include.
Set a seed (
set.seed()) and draw two random samples of \(n = 50\) each from your data. Hint:slice_sample()can help.In each subsample, fit four models: (1) linear in \(X\), (2–4) three polynomial specifications of your choice. All four models should include the same controls.
For each model and each subsample, generate predicted values of \(Y\) over a range of \(X\) while holding controls at their means. Plot the predictions, faceting by subsample, with one line per model.
Which specifications tell the same story in both subsamples? Which ones don’t?
Question 5: The power cost of interactions
This question involves writing a simulation, which we haven’t covered in class. You are free to use AI to help you write the loop and the plotting code. But you must explain what each step does in your own words.
In lecture we saw that the standard error of an interaction is roughly twice that of a main effect, and the true interaction is usually smaller. Together, these mean that studies are often severely underpowered for detecting interactions. Let’s see this in action.
Consider the following data-generating process (DGP):
\[Y_i = 10 + 3 \cdot X_i + 2 \cdot Z_i + 1.5 \cdot X_i \cdot Z_i + \varepsilon_i\]
where \(X \sim N(0, 1)\), \(Z \sim \text{Bernoulli}(0.5)\), and \(\varepsilon \sim N(0, 5)\). The true main effect of \(X\) is 3 and the true interaction is 1.5.
Write a simulation that repeats the following 500 times: (1) generate a dataset of \(N = 150\) observations from the DGP above, (2) estimate
lm(y ~ x * z), and (3) store the estimated coefficients and p-values for bothx(the main effect) andx:z(the interaction). Useset.seed(213)for reproducibility.Make the following plot: for each of the 500 runs (x-axis), plot the estimated coefficient (y-axis), with one panel for the main effect and one for the interaction. Draw a horizontal line at the true value in each panel. Color the points by whether the estimate is statistically significant at \(p < 0.05\).
What fraction of the 500 runs detect a significant main effect? What fraction detect a significant interaction? Discuss what you see.
Question 6: Standard errors for marginal effects (optional)
This question is optional. It is intended for students building a methods portfolio for the quantitative methods subfield.
In Questions 1 and 2, you computed marginal effects by hand — but without standard errors. In Q2(e), slopes() gave you both the point estimate and a confidence interval. How does it get those standard errors? The answer is something called the delta method, which we’ll work through step by step.
The key idea: when you estimate a model, you don’t just get coefficients — you also get a measure of how uncertain those coefficients are jointly. This is stored in the variance-covariance matrix, which you can extract with vcov(). The delta method uses this matrix to figure out how uncertain any function of the coefficients is, including marginal effects.
Return to the quadratic model from Q1. Run
vcov()on your model and look at the output. Each entry tells you how much two coefficients vary together. Which entries correspond to the poverty rate and poverty rate-squared coefficients? Extract just those rows and columns (a \(2 \times 2\) piece of the full matrix). Call them \(V_{11}\), \(V_{12}\), and \(V_{22}\).Recall that the marginal effect of poverty rate at a particular value \(x\) is:
\[ME(x) = \hat{\beta}_1 + 2\hat{\beta}_2 \cdot x\]
This is a function of two uncertain quantities (\(\hat{\beta}_1\) and \(\hat{\beta}_2\)). The delta method says the variance of this function is:
\[\text{Var}[ME(x)] = V_{11} + 4x^2 \cdot V_{22} + 4x \cdot V_{12}\]
where \(V_{11} = \text{Var}(\hat{\beta}_1)\), \(V_{22} = \text{Var}(\hat{\beta}_2)\), and \(V_{12} = \text{Cov}(\hat{\beta}_1, \hat{\beta}_2)\).
Pick three values of poverty rate (e.g., the 25th, 50th, and 75th percentiles). For each, plug into the formula above to get the variance, then take the square root to get the standard error.
Compare your hand-computed standard errors to those from
slopes(mod, variables = "poverty_rate", newdata = ...)at the same poverty rate values. Do they match?In 2–3 sentences, explain intuitively why the standard error of the marginal effect changes as you move across values of poverty rate.