You Try Answers: Conditional Expectation

POSC 8410

Author

Will Horne

Published

October 26, 2026

library(tidyverse)
# In your own class folder these paths are just "Data/anes.csv" and "Data/resumes-2.csv"
anes <- read_csv("../Slides/Data/anes.csv", show_col_types = FALSE)
resumes <- read_csv("../Slides/Data/resumes-2.csv", show_col_types = FALSE) |>
  mutate(callback = call == "yes")   # TRUE/FALSE, built as in class

You Try (at home)

1. Compute \(E[\text{ft\_science} \mid \text{votechoice}]\) with group_by. Then get the same numbers from lm.

First, decide which rows we are using. We need respondents with both a vote choice and a science rating. I save that as its own data frame, sci, because questions 3 and 4 use the same rows.

sci <- anes |> filter(!is.na(votechoice), !is.na(ft_science))

sci |>
  group_by(votechoice) |>
  summarise(EY = mean(ft_science), n = n())
# A tibble: 3 × 3
  votechoice    EY     n
  <chr>      <dbl> <int>
1 Biden       88.2  3245
2 Other       78.7   148
3 Trump       69.7  2436

Those means are the CEF: the average science thermometer at each value of vote choice. Now regression:

m_sci <- lm(ft_science ~ votechoice, data = sci)
coef(m_sci)
    (Intercept) votechoiceOther votechoiceTrump 
      88.202157       -9.458914      -18.528512 

R turns votechoice into dummy variables, one for every group except the baseline. Biden comes first alphabetically, so Biden is the baseline (this can be changed by reordering the factor variable), and:

  • The intercept is the Biden mean: 88.2.
  • Each other coefficient is a difference from Biden. Other: 88.2 + (-9.5) = 78.7. Trump: 88.2 + (-18.5) = 69.7.

Intercept plus coefficient reproduces each group mean exactly. Both coefficients are negative because both other groups are cooler toward science than Biden voters. With a categorical \(X\), regression doesn’t approximate the CEF, it is the CEF, just reported as a baseline plus differences.

2. For callback in the résumé data, compute \(E[Y \mid \text{quality}]\). Why is this also a probability?

resumes |>
  group_by(quality) |>
  summarise(EY = mean(callback), n = n())
# A tibble: 2 × 3
  quality     EY     n
  <chr>    <dbl> <int>
1 high    0.0875  2446
2 low     0.0734  2424

High-quality résumés get a callback 8.7% of the time, and low-quality résumés 7.3% of the time.

Why is this a probability? callback takes only two values, 1 (TRUE) and 0 (FALSE). The mean of a 0/1 variable is the share of 1s, because the 0s add nothing to the sum:

\[E[Y \mid X = x] = 0 \cdot P(Y = 0 \mid X = x) + 1 \cdot P(Y = 1 \mid X = x) = P(Y = 1 \mid X = x)\]

So “the average callback among high-quality résumés” and “the probability that a high-quality résumé gets a callback” are the same number. This is why we can run regressions on yes/no outcomes.

3. Verify the law of iterated expectations for ft_science: do the group means, weighted by group size, recover the overall mean?

The LIE says \(E[Y] = \sum_x E[Y \mid X = x]\, P(X = x)\). Each group mean gets weighted by its share of the sample, \(n_x / n\):

g_sci <- sci |>
  group_by(votechoice) |>
  summarise(mu = mean(ft_science), n = n())

c(weighted = sum(g_sci$mu * g_sci$n / sum(g_sci$n)),   # E[E[Y | X]]
  overall  = mean(sci$ft_science))                    # E[Y]
weighted  overall 
80.21873 80.21873 

Identical: 80.22 both ways. The CEF splits the overall mean into group pieces, and weighting by group size puts them back together.

One potential trap here. The overall mean has to come from the same rows as the group means. If you compute it from the full data instead,

mean(anes$ft_science, na.rm = TRUE)
[1] 79.31383

you get a different number, because the full data also includes the 1,538 respondents with a science rating but no recorded vote choice. They belong to no vote-choice group, so they cannot be in the weighted average.

4. Would the unweighted average of the three group means give the same answer? Why not?

mean(g_sci$mu)
[1] 78.87302

No: 78.87, against the true 80.22.

Taking a simple average of the three group means treats each group as if it were the same size. That gives the 148 “Other” voters the same weight as the 3,245 Biden voters and the 2,436 Trump voters. The small Other group ends up with a third of the influence when it is only 2.5% of the people.

This is the same mistake as the county map from earlier: averaging group rates as though each group were a single person. The weighted and unweighted averages agree only in two special cases: when every group is the same size, or when every group has the same mean (in which case \(X\) tells you nothing about \(Y\)).