# POL 211 Week 2: Bias, simulation, and measurement -------------------------
#
# This script repeats the analysis from class, in the order of the slides.
# Save it in your POL 211 project's notes/ folder and run it one chunk at a
# time.
#
# Before you start:
#   - download WaffleDivorce.csv from week 2 on the course schedule, and save
#     it in your project's data/ folder
#   - the civil war data (fearon) come with the juanr package
#
# The "🚨 Your turn" sections set up the in-class activities. The answers
# aren't here: working them out is the point.


# Packages -------------------------------------------------------------------

library(tidyverse)   # wrangling + plotting
library(juanr)       # fearon: civil war data
library(here)        # file paths that work on any computer


# 1. Does democracy make civil war less likely? ------------------------------

# fearon has one row per country-year, 1945-1999 (Fearon and Laitin 2003).
# We keep rows with a Polity score and GDP per capita, and make three columns
wars = fearon |>
  filter(!is.na(polity2), !is.na(gdp_cap)) |>      # drop rows missing either one
  mutate(democracy = if_else(polity2 > 5, "Democracy", "Not a democracy"),
         civil_war = if_else(onset > 0, 1, 0),      # 1 = a civil war began that year
         income = ntile(gdp_cap, 3))                # 3 equal-sized groups: 1 = poorest

# take a look
wars |>
  select(country, year, polity2, democracy, gdp_cap, civil_war)

# the share of country-years with a new civil war, by regime
war_rates = wars |>
  group_by(democracy) |>
  summarize(war_rate = mean(civil_war))

war_rates

# the estimate: the difference in means, democracies minus the rest
# (rows are in alphabetical order, so "Democracy" is row 1)
war_rates$war_rate[1] - war_rates$war_rate[2]

# plot it
ggplot(war_rates, aes(x = democracy, y = war_rate)) +
  geom_col(width = 0.5) +
  scale_y_continuous(labels = scales::percent) +
  labs(x = NULL, y = "Share of country-years with a new civil war")


# 2. When a coin picks the democracies, they're no richer than the rest ------

set.seed(211)   # makes the random draws the same every time we run this

# flip a coin for each country-year: heads, it's a "democracy".
# sample() picks from x, n() times, putting each pick back (replace = TRUE)
coin_flip = wars |>
  mutate(coin = sample(x = c("Democracy", "Not a democracy"), size = n(), replace = TRUE))

coin_flip |>
  select(country, year, civil_war, democracy, coin)

# average GDP per capita ($1,000s): real democracies vs. the rest
real_gdp = coin_flip |>
  group_by(democracy) |>
  summarize(gdp = mean(gdp_cap))

real_gdp

# coin-flip "democracies" vs. the rest
coin_flip |>
  group_by(coin) |>
  summarize(gdp = mean(gdp_cap))

# rerun the coin flip 1,000 times, keeping the GDP gap from each run.
# map_dbl() runs the code inside it once for each number in 1:1000. Week 4
# covers how it works; for now, run it and look at the picture
set.seed(2026)
coin_gaps = tibble(
  gap = map_dbl(1:1000, \(i) {
    coin = sample(c(1, 0), size = nrow(wars), replace = TRUE)   # 1 = heads
    mean(wars$gdp_cap[coin == 1]) - mean(wars$gdp_cap[coin == 0])
  })
)

# the real gap: democracies minus the rest (rows are in alphabetical order)
real_gap = real_gdp$gdp[1] - real_gdp$gdp[2]

# the coin-flip gaps pile up around zero, far from the real gap (red line)
ggplot(coin_gaps, aes(x = gap)) +
  geom_histogram(binwidth = 0.05, color = "white") +
  geom_vline(xintercept = real_gap, color = "red") +
  labs(x = "Coin-flip democracies minus the rest: GDP per capita ($1,000s)",
       y = "Coin flips")


# 3. Waffle Houses and divorce ----------------------------------------------

# one row per US state (from McElreath's Statistical Rethinking).
# Divorce is divorces per 1,000 adults. The file uses ; between columns
waffles = read_delim(here("data", "WaffleDivorce.csv"), delim = ";") |>
  mutate(any_waffle = if_else(WaffleHouses > 0, "Has a Waffle House", "No Waffle House"),
         region = if_else(South == 1, "South", "Not South"))

# average divorce rate, by whether a state has any Waffle Houses
waffle_rates = waffles |>
  group_by(any_waffle) |>
  summarize(divorce = mean(Divorce), states = n())

waffle_rates

ggplot(waffle_rates, aes(x = any_waffle, y = divorce)) +
  geom_col(width = 0.5) +
  labs(x = NULL, y = "Divorces per 1,000 adults")

# every Southern state has a Waffle House
waffles |>
  count(region, any_waffle)

# and the South has more divorce
waffles |>
  group_by(region) |>
  summarize(divorce = mean(Divorce))


# 4. Simulation: a world where waffles do nothing ---------------------------

# We make up 50 states in three steps, adding one arrow at a time.
# Same seed each time, so it's the same 50 states, just moved around

# Step 1: nothing affects anything
set.seed(1181)

fake_states = tibble(
  south = sample(c(0, 1), size = 50, replace = TRUE, prob = c(0.7, 0.3)),
  waffle = rnorm(50, mean = 20, sd = 4),     # Waffle Houses per million residents
  divorce = rnorm(50, mean = 9, sd = 1.5)    # divorces per 1,000 adults
)

ggplot(fake_states, aes(x = waffle, y = divorce)) +
  geom_point(aes(color = factor(south))) +
  geom_smooth(method = "lm", se = FALSE)

# Step 2: the South adds Waffle Houses
set.seed(1181)

fake_states = tibble(
  south = sample(c(0, 1), size = 50, replace = TRUE, prob = c(0.7, 0.3)),
  waffle = rnorm(50, mean = 20, sd = 4) + 10 * south,   # the South adds 10
  divorce = rnorm(50, mean = 9, sd = 1.5)
)

ggplot(fake_states, aes(x = waffle, y = divorce)) +
  geom_point(aes(color = factor(south))) +
  geom_smooth(method = "lm", se = FALSE)

# Step 3: the South adds divorce, too. waffle never shows up in the divorce
# line, so the true effect of waffles is zero
set.seed(1181)

fake_states = tibble(
  south = sample(c(0, 1), size = 50, replace = TRUE, prob = c(0.7, 0.3)),
  waffle = rnorm(50, mean = 20, sd = 4) + 10 * south,
  divorce = rnorm(50, mean = 9, sd = 1.5) + 2 * south   # the South adds 2
)

ggplot(fake_states, aes(x = waffle, y = divorce)) +
  geom_point(aes(color = factor(south))) +
  geom_smooth(method = "lm", se = FALSE)

# the slope of that line: the estimate. lm() finds the same line
# geom_smooth() draws (week 3 is all about it). The number under `waffle` is
# the slope: extra divorces per 1,000 adults for each extra Waffle House
lm(divorce ~ waffle, data = fake_states)


# 5. Rerun the world 1,000 times: the bias stays, the noise shrinks ----------

# one run of the made-up world with n states; returns the slope of the line
one_run = function(n) {
  states = tibble(
    south = sample(c(0, 1), size = n, replace = TRUE, prob = c(0.7, 0.3)),
    waffle = rnorm(n, mean = 20, sd = 4) + 10 * south,
    divorce = rnorm(n, mean = 9, sd = 1.5) + 2 * south
  )
  coef(lm(divorce ~ waffle, data = states))["waffle"]
}

# 1,000 runs with 50 states, and 1,000 runs with 5,000 states
set.seed(2026)
reruns = bind_rows(
  tibble(size = "50 states", slope = map_dbl(1:1000, \(i) one_run(50))),
  tibble(size = "5,000 states", slope = map_dbl(1:1000, \(i) one_run(5000)))
) |>
  mutate(size = fct_inorder(size))   # keep the panels in the order we made them

# the average slope in each: not zero, whatever the number of states
reruns |>
  group_by(size) |>
  summarize(avg_slope = mean(slope))

# the red line is the true effect, zero
ggplot(reruns, aes(x = slope)) +
  geom_histogram(binwidth = 0.01, color = "white") +
  geom_vline(xintercept = 0, color = "red") +
  facet_wrap(~size, scales = "free_y") +
  labs(x = "Slope of the line through divorce and waffles (estimate)", y = "Runs")


# 6. Compare within the South and outside it --------------------------------

# putting color inside ggplot(aes()) splits the data by region, so
# geom_smooth() draws one line per region
fake_states |>
  mutate(region = if_else(south == 1, "South", "Not South")) |>
  ggplot(aes(x = waffle, y = divorce, color = region)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  labs(x = "Waffle Houses per million residents", y = "Divorces per 1,000", color = NULL)


# 🚨 Your turn: change the bias ----------------------------------------------

set.seed(1181)
fake_states = tibble(
  south = sample(c(0, 1), size = 50, replace = TRUE, prob = c(0.7, 0.3)),
  waffle = rnorm(50, mean = 20, sd = 4) + 10 * south,
  divorce = rnorm(50, mean = 9, sd = 1.5) + 2 * south
)

ggplot(fake_states, aes(x = waffle, y = divorce)) +
  geom_point(aes(color = factor(south))) +
  geom_smooth(method = "lm", se = FALSE)

# 1. Run it. Then move color = factor(south) into ggplot(aes()). Does the
#    tilt go away?
# 2. What if the South LOWERED divorce? Change the effect and look again
# 3. Give waffles a real effect: add + 0.1 * waffle to the end of the divorce
#    line. Is the line through all the states steeper than the lines within
#    each region?


# 🚨 Your turn: does the gap survive within income groups? -------------------

# uses `wars` from section 1
wars |>
  group_by(democracy) |>
  summarize(war_rate = mean(civil_war))

# 1. Run it. Do you get the same gap?
# 2. Are democracies richer? Check with summarize()
# 3. Add income to group_by(). Does the gap survive within each income group?


# 🚨 Your turn: make up your own confounded world ----------------------------

# Start from the waffle code in section 4 (step 3). With a neighbor:
#
# 1. Pick a confounder, a treatment, and an outcome from your own field or
#    everyday life. Write the code so the treatment does NOTHING
# 2. Before you run it, sign the bias: will the line through all the data tilt
#    up or down?
# 3. Run it and plot it. Compare that line with the lines within each level of
#    the confounder (section 6). Were you right?
# 4. Bonus: give the treatment a real effect, and make the bias big enough to
#    flip the sign of the estimate


# 7. Measurement: another cutoff gives another answer -----------------------

# we called a country-year a democracy if its Polity score is above 5.
# Change the cutoff to 0, then to 9, and rerun. What happens to the gap?
cutoff = 5

wars |>
  mutate(democracy = if_else(polity2 > cutoff, "Democracy", "Not a democracy")) |>
  group_by(democracy) |>
  summarize(war_rate = mean(civil_war), country_years = n())


# 8. Face validity and content validity -------------------------------------

# face validity: do Polity scores look sensible for countries we know?
fearon |>
  filter(year == 1998,
         country %in% c("USA", "UK", "INDIA", "SOUTH AFRICA", "BRAZIL",
                        "MEXICO", "RUSSIA", "IRAN", "INDONESIA", "CHINA")) |>
  select(country, polity2) |>
  arrange(desc(polity2))

# content validity: Polity scores in years when many adults couldn't vote.
# Swiss women couldn't vote in federal elections until 1971
fearon |>
  filter(country == "SWITZERLAND", year >= 1950, year <= 1970) |>
  count(polity2)

# most Black Southerners couldn't vote until the Voting Rights Act of 1965
fearon |>
  filter(country == "USA", year >= 1945, year <= 1964) |>
  count(polity2)


# 9. The middle of the Polity scale looks most dangerous --------------------

# civil war rate in three ranges of the Polity scale
range_rates = wars |>
  mutate(polity_range = case_when(polity2 <= -6 ~ "Autocracy (-10 to -6)",
                                  polity2 <= 5 ~ "The middle (-5 to 5)",
                                  polity2 > 5 ~ "Democracy (6 to 10)")) |>
  group_by(polity_range) |>
  summarize(war_rate = mean(civil_war), country_years = n())

range_rates

# fct_relevel() puts the bars in Polity order instead of alphabetical order
range_rates |>
  mutate(polity_range = fct_relevel(polity_range, "Autocracy (-10 to -6)",
                                    "The middle (-5 to 5)")) |>
  ggplot(aes(x = polity_range, y = war_rate)) +
  geom_col(width = 0.5) +
  scale_y_continuous(labels = scales::percent) +
  labs(x = NULL, y = "Share of country-years with a new civil war")


# 10. Noise in the outcome blurs the line; noise in the treatment flattens it ----

# a made-up world where X raises Y by exactly 1
set.seed(211)
truth = tibble(x = rnorm(300),
               y = x + rnorm(300, sd = 0.5))

# measure it three ways: well, with random error in Y, with random error in X
measured = bind_rows(
  truth |> mutate(panel = "Measured well"),
  truth |> mutate(y = y + rnorm(300, sd = 1), panel = "Noise in the outcome (Y)"),
  truth |> mutate(x = x + rnorm(300, sd = 1), panel = "Noise in the treatment (X)")
)

# the dashed line is the true effect (a slope of 1); the blue line is the
# line through what we measured
ggplot(measured, aes(x = x, y = y)) +
  geom_point(alpha = 0.4) +
  geom_abline(slope = 1, intercept = 0, linetype = "dashed") +
  geom_smooth(method = "lm", se = FALSE) +
  facet_wrap(~panel) +
  labs(x = "Treatment (X), as measured", y = "Outcome (Y), as measured")

# the slope in each panel
measured |>
  group_by(panel) |>
  summarize(slope = coef(lm(y ~ x))["x"])
