library(tidyverse)
library(broom)
library(modelsummary)
library(marginaleffects)
library(MASS, exclude = "select") # polr — load before tidyverse functions
library(brant) # Brant test for proportional odds
library(nnet) # multinom — multinomial logit
library(mlogit) # hmftest — Hausman-McFadden IIA test
library(estimatr) # lm_robust — cluster-robust OLS for AMCEs
library(survival) # clogit — conditional logit
library(cregg) # HHY immigrant conjoint data
library(juanr)Problem Set 7: Ordered & Multinomial Logit
Due: Tuesday, May 19 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: Territorial control during the Vietnam War
The juanr::vietnam dataset records monthly assessments of which side controlled each South Vietnamese hamlet during the Vietnam War. The outcome control is a five-level categorical variable indicating who had the upper hand. Other variables in the dataset measure US military activity in the hamlet (bombs, selective_terror), demographic composition (buddhist, vietnamese, urban), and geography (pop, border, dev_index).
You’ll fit an ordered logit predicting control from explanatory variable(s) of your choice, present predicted probabilities, and run a diagnostic on the model’s central assumption.
Set up the outcome. Before fitting anything, check the levels of
controland confirm the ordering. The five levels are"Full rebel","Partial rebel","Contested","Partial state","Full state"— the variable comes as a factor, but not necessarily ordered in the direction you want. Rebuild the variable as an ordered factor so that higher values correspond to more state control (i.e."Full rebel"is the lowest level,"Full state"is the highest). Make a barplot of the distribution of control across hamlets.Pick a predictor (or two) and motivate. Choose at least one explanatory variable from the dataset to focus on as your “focal” predictor. You may add a small number of controls if you’d like. Briefly motivate your choice — what’s the substantive question you’re asking?
Fit the ordered logit. Fit your model. Report a clean coefficient table (e.g. via
modelsummary()ortidy()). Read the sign on your focal predictor — does it go the direction you expected?Predicted probabilities. Compute \(\Pr(Y = j \mid X)\) across the range of your focal predictor, holding other covariates at sensible values. Plot the probability curves (or points if \(X\) is binary) — one per category — on the same panel, with shaded confidence bands. Discuss.
Brant test of proportional odds. Ordered logit imposes the proportional odds assumption: the effect of your predictor on the cumulative odds is the same at every cut. Conduct a Brant test:
- What does the omnibus row tell you?
- What does the row for your focal predictor tell you?
- If the test rejects proportional odds for your focal predictor, what would you do next? (You don’t have to actually do it — just describe the move. The slides list a few options.)
Question 2: Where do the cutpoints come from?
When you fit an ordered logit, polr prints a coefficient table and a separate set cutpoints (or “intercepts,” labeled \(\tau_1, \tau_2, \ldots\) in the slides and Intercepts: / $zeta in R). These can be a bit mysterious. A useful insight for what they mean is that, in a model without covariates, they’re nothing more than the logit of the cumulative empirical share of each category (e.g., \(\tau_j = \text{logit}\big(\Pr(Y \le \text{Partial rebel})\big)\)).
Let’s see that this is true.
Reuse the control outcome you set up in Q1(a) — the ordered factor running from “Full rebel” (low) to “Full state” (high).
Compute the cumulative empirical shares of
control. That is, for each cut \(j = 1, 2, 3, 4\), compute \(\widehat{\Pr}(Y \le j)\) — the proportion of hamlets whose control level is at or below category \(j\). (prop.table(table(...))andcumsum()are one approach — note thatcumsum()will give you five numbers, the last of which is 1; drop it, since \(\widehat{\Pr}(Y \le 5) = 1\) doesn’t correspond to a cut.) You should end up with four numbers, all strictly between 0 and 1.Convert to the logit scale by hand. For each of the four cumulative shares, compute \(\tau_j = \text{logit}\big(\widehat{\Pr}(Y \le j)\big) = \log\!\big(\widehat{\Pr}(Y \le j) \;/\; (1 - \widehat{\Pr}(Y \le j))\big)\). Report the four numbers.
Compare to
polr’s cutpoints. Fit an intercept-only ordered logit:polr(control ~ 1, data = ...). Pull the cutpoints out of the fitted object withmod$zeta. Put your four hand-computed \(\tau_j\)’s side by side with the four$zetavalues — they should match to several decimals.
Question 3: Racial identity and physical appearance in LA
The juanr::race has survey data from the Latin American Public Opinion Project on race in a region where racial identity is famously “fluid”.
You’ll look at how a person’s skin-tone (colorr) shapes the likelihood that they identify with one of the six most common ethnic categories (etid). You’ll control for the respondent’s age and sex.
Set up the outcome. Subset to only the top six ethnic identities (
etid) by frequency.Fit the multinomial logit. Fit your model. Report a clean coefficient table.
Predicted probabilities. Compute \(\Pr(Y = k \mid X)\) across the range of your focal predictor, holding any other covariates at sensible values. Plot the probability curves — one per ethnic category — on the same panel, with shaded confidence bands. Discuss.
Question 4: Prove it to yourself — multinomial with K = 2 is just binary logit
The slides claim that multinomial logit is “binary logit, generalized” — that when the outcome has only two categories (\(K = 2\)), the softmax collapses to the logistic CDF and you recover the exact same model. This question asks you to verify that claim numerically.
Take your
racedataset from Q3, subset to two ethnic identities or collapse levels into broader categories.Fit two models. Using whatever predictors you want:
- Fit a binary logit
- Fit a multinomial logit. Note: make sure your reference level is the same as for the binary logit, otherwise you won’t get the same answer.
Compare coefficients. Put the two coefficient vectors side by side (a small
tibbleworks, ormodelsummary()with both models). They should match to several decimal places.
This question is optional. It is intended for students building a methods portfolio for the quantitative methods subfield.
Derive the softmax from scratch. The slides claim that the multinomial logit’s predicted probabilities,
\[\Pr(Y_i = k \mid X_i) \;=\; \frac{\exp(X_i\beta_k)}{\sum_{\ell=1}^K \exp(X_i\beta_\ell)},\]
result from the modeling assumption that, for each non-baseline category \(k\),
\[\log\!\frac{\Pr(Y_i = k \mid X_i)}{\Pr(Y_i = \text{baseline} \mid X_i)} \;=\; X_i\beta_k,\]
with the convention \(\beta_{\text{baseline}} = 0\). Walk through the derivation explicitly, with the three-category party-ID setup from the slides (Dem, Ind, Rep, with Independent as the baseline). The whole thing is just algebra.
Step 1 — Solve for the non-baseline categories. Write down the two modeling assumptions you’re starting from:
\[\log\!\frac{\Pr(\text{Dem})}{\Pr(\text{Ind})} = X\beta_{\text{Dem}}, \qquad \log\!\frac{\Pr(\text{Rep})}{\Pr(\text{Ind})} = X\beta_{\text{Rep}}\]
Solve for \(\Pr(\text{Dem})\) and \(\Pr(\text{Rep})\) so that they are expressed in terms of \(\Pr(\text{Ind})\) and the linear predictors.
Step 2 — plug into the sum-to-one constraint and solve for \(\Pr(\text{Ind})\). The three probabilities must sum to 1:
\[\Pr(\text{Dem}) + \Pr(\text{Ind}) + \Pr(\text{Rep}) = 1\]
Substitute your two expressions from Step 1 in for \(\Pr(\text{Dem})\) and \(\Pr(\text{Rep})\). Isolate \(\Pr(\text{Ind})\). You should end up with
\[\Pr(\text{Ind}) = \frac{1}{1 + \exp(X\beta_{\text{Dem}}) + \exp(X\beta_{\text{Rep}})}.\]
Step 3 — recover the other two probabilities. Substitute the Step-2 expression for \(\Pr(\text{Ind})\) back into the two equations from Step 1. Put all three probabilities side by side and confirm that they share the same denominator — that shared denominator is the “glue” that forces them to sum to 1. Verify by adding the three numerators: they should equal the denominator.
Step 4 — sanity check (\(K = 2\)). Now drop Republicans and pretend you only have Dem and Ind, with Ind as baseline. The Rep terms disappear from every expression in Step 3. Show that the formula for \(\Pr(\text{Dem})\) collapses to
\[\Pr(\text{Dem}) = \frac{\exp(X\beta_{\text{Dem}})}{1 + \exp(X\beta_{\text{Dem}})} = \sigma(X\beta_{\text{Dem}}),\]
which is the binary logistic CDF from Week 3. In one sentence, connect this back to what you saw numerically in Question 4 — why the K = 2 multinomial had to give the same answer as a binary logit, not just a close one.
Format. You can write the derivation by hand and embed a photo/scan, type it in LaTeX inside the Quarto document, or mix the two. Either way, the substance matters more than the polish.