# Quant Camp Day 4: relationships in data --------------------------------------
#
# This is the file we fill in together during the morning lecture. Save it
# inside your camp project as code/04-code.R and run it chunk by chunk.
#
# Today's moves:
#   condense data into numbers   -> summarize(), mean(), median(), sd()
#   missing values               -> NA, na.rm = TRUE, is.na()
#   compare groups               -> group_by() + summarize(), n()
#   categories                   -> count(), percents, "percent of what?"


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

library(tidyverse)   # wrangling + plotting
library(gapminder)   # country-year data on health and wealth
library(juanr)       # leader, bonica, trade, elections, coups
library(here)        # file paths that work on any computer


# 1. Summarizing: summarize() --------------------------------------------------

# summarize() condenses many rows into one. Name the number you make
gapminder |>
  filter(year == 2007) |>
  summarize(avg_life = mean(lifeExp))

# more summaries, separated by commas. Each one is a new column
gapminder |>
  filter(year == 2007) |>
  summarize(avg_life = mean(lifeExp),
            med_life = median(lifeExp),
            min_life = min(lifeExp),
            max_life = max(lifeExp),
            sd_life  = sd(lifeExp))

# summary functions work inside filter(): countries above average GDP but
# below average life expectancy (the outliers from Day 1)
gapminder |>
  filter(year == 2007) |>
  filter(gdpPercap > mean(gdpPercap),
         lifeExp < mean(lifeExp))

# and inside mutate(): how far is each country from the world average?
gap_07 = gapminder |>
  filter(year == 2007) |>
  mutate(gdp_center = gdpPercap - mean(gdpPercap)) |>
  select(country, gdpPercap, gdp_center)
gap_07


# 2. Dummy variables are proportions -------------------------------------------

# a dummy is coded 0/1 for no/yes. The mean of a dummy is the share of 1s
leader |>
  filter(year == 1990) |>
  select(country, leader, year, mil_service, combat, rebel)

leader |>
  filter(year == 1990) |>
  summarize(prop_military = mean(mil_service, na.rm = TRUE),
            prop_combat   = mean(combat, na.rm = TRUE),
            prop_rebel    = mean(rebel, na.rm = TRUE))


# 3. Missing data: NA ----------------------------------------------------------

# R writes a missing value as NA. Any math with NA gives NA
bonica |>
  summarize(average_donors = median(num_distinct_donors))

# na.rm = TRUE tells the summary function to ignore the missing rows
bonica |>
  summarize(average_donors = median(num_distinct_donors, na.rm = TRUE))

# that number is the median AMONG THE POLITICIANS WE HAVE DATA FOR. Say so.
# is.na() is TRUE when a cell is missing; adding up TRUEs counts them
bonica |>
  summarize(n_missing = sum(is.na(num_distinct_donors)),
            n_total = n())


# 🚨 Your turn: leaders --------------------------------------------------------

# leader has one row per leader-year
# 1. what share of leaders served in the military (mil_service)?
# 2. how many leader-years is that share ignoring? (is.na() tells you whether
#    a cell is missing; add up how many cells in that variable are)
# 3. ask your agent for the share with combat experience (combat) and the
#    share who were rebels (rebel). Read its code. Did it drop the missing
#    rows? Did it tell you?



# 4. Grouped comparisons: group_by() -------------------------------------------

# how much do imports vary over time, for ONE country?
trade |>
  filter(country == "Afghanistan") |>
  summarize(var_trade = sd(imports, na.rm = TRUE))

# group_by(VARIABLE) + summarize(): the same summary, once per group
trade |>
  group_by(country) |>
  summarize(var_trade = sd(imports, na.rm = TRUE))

# the result is a table, so we can sort it, slice it, and plot it
var_time_10 = trade |>
  group_by(country) |>
  summarize(var_trade = sd(imports, na.rm = TRUE)) |>
  slice_max(var_trade, n = 10)

ggplot(var_time_10, aes(y = reorder(country, var_trade), x = var_trade)) +
  geom_col(color = "white", fill = "darkblue") +
  labs(x = "Variation in imports (SD)", y = NULL)

# group by more than one variable: one row per continent-year
life_continent_year = gapminder |>
  group_by(year, continent) |>
  summarize(avg_life = mean(lifeExp))

ggplot(life_continent_year, aes(x = year, y = avg_life, color = continent)) +
  geom_line(linewidth = 2, alpha = .8) +
  labs(x = "Year", y = "Average life expectancy", color = "Continent")


# 5. Report the n behind every average ----------------------------------------

# n() counts the rows in each group. Put it in every grouped summary
gapminder |>
  filter(year == 2007) |>
  group_by(continent) |>
  summarize(avg_life = mean(lifeExp),
            n = n()) |>
  arrange(desc(avg_life))

# Oceania "wins" with two countries


# 6. group_by() works with other verbs -----------------------------------------

# the top fundraiser in each state: group_by() + slice_max()
bonica |>
  filter(cycle == 2018) |>
  group_by(state) |>
  slice_max(total_receipts, n = 1) |>
  select(state, name, party, total_receipts)

# the richest county in each state: group_by() + filter(). max() is computed
# WITHIN each state
elections |>
  group_by(state) |>
  filter(hh_income == max(hh_income)) |>
  select(state, name, hh_income)


# 🚨 Your turn: who serves? ---------------------------------------------------

# back to leader
# 1. what share of leaders served in the military, BY GENDER?
# 2. ask your agent: "What share of leaders of each gender have a graduate
#    degree (edu)? Tell me how you handled missing values."
# 3. now ask it to redo the calculation with the missing leaders KEPT in the
#    denominator. Did the numbers change? Which version answers the question
#    you asked?



# 7. Categories: count() and percents ------------------------------------------

# can't average a category. Count it instead
leader |>
  count(edu)

# counts to percents: n / sum(n)
leader |>
  count(edu) |>
  mutate(percent = n / sum(n) * 100)

# count() keeps NA as a category. drop_na(VARIABLE) removes those rows first,
# which changes the denominator
leader |>
  drop_na(edu) |>
  count(edu) |>
  mutate(percent = n / sum(n) * 100)

# two variables at once: a cross-tabulation
bonica |>
  count(incumbent, seat) |>
  mutate(percent = n / sum(n) * 100)

# incumbents are 1% of Senate candidates? No: sum(n) here is ALL politicians.
# that's the percent of EVERYONE who is a Senate incumbent


# 8. Percent of what? ----------------------------------------------------------

# "by seat type" means within House races and within Senate races.
# group_by(seat) before mutate(), and sum(n) is computed within each seat
bonica |>
  count(incumbent, seat) |>
  group_by(seat) |>
  mutate(percent = n / sum(n) * 100)

# now each seat sums to 100. Same six counts, different denominator.
# before you report a percent, say what it is a percent OF. The agent will
# pick a denominator for you; read the code and check which one


# 🚨 Your turn: coups ---------------------------------------------------------

# coups has one row per coup event, worldwide
# 1. what percent of all known coup attempts succeed (realized)?
# 2. what percent of coups end with the leader jailed (jailed)? fleeing (fled)?
#    exiled (exile)? One summarize(), three numbers
# 3. what percent of all coup attempts are led by the military (military)
#    AND succeed?
# 4. ask your agent: "Are coups more likely to succeed when the military leads
#    them?" Read its code. Is its percent out of ALL coups, or WITHIN military
#    and non-military coups? Only one of those answers the question
