#Title: DWV Week 5 Workshop.R
#Author: Jack Reilly
#Purpose: Illustrate Survey Functions
#Requires: dwv_ps5_solutions.R already run
#Output: Plots to plot window; nothing to disk

#Identify path to dwv_ps5_solutions.R here
source("dwv_ps5_solutions.R")

library(srvyr)
ces2024_weighted<- ces2024 |> as_survey_design(weights=weight)

#First, let's just look at raw trump vote
ces2024_weighted |>
  group_by(pres24_trump) |>
  summarise(freq = survey_total())

ces2024_weighted |>
  group_by(pres24_trump) |>
  summarise(
    freq = survey_total(),
    pct  = survey_mean()
  )

#Now, by ideology
ces2024_weighted |>
  group_by(pres24_trump, ideo_r) |>
  summarise(
    freq = survey_total(),
    pct  = survey_mean()
  )

#dropping NAs
ces2024_weighted |>
  drop_na(pres24_trump, ideo_r) |>
  group_by(pres24_trump, ideo_r) |>
  summarise(
    freq = survey_total(),
    pct  = survey_mean()
  )

#Home ownership
ces2024_weighted |>
  drop_na(pres24_trump, home_r) |>
  group_by(pres24_trump, home_r) |>
  summarise(
    freq = survey_total(),
    pct  = survey_mean()
  )

#Education
ces2024_weighted |>
  drop_na(pres24_trump, educ_r) |>
  group_by(pres24_trump, educ_r) |>
  summarise(
    freq = survey_total(),
    pct  = survey_mean()
  ) 

#What if we wanted to make some pretty tables?
# gt package ("Grammar of Tables")
library(gt)

# Weighted crosstab: pres24_trump × home_r
tab <- ces2024_weighted %>%
  drop_na(pres24_trump, home_r) %>%
  mutate(
    pres24_trump = factor(pres24_trump,
                          levels = c(0, 1),
                          labels = c("Harris", "Trump")),
    home_r = factor(home_r,
                    levels = c(0, 1),
                    labels = c("Rent", "Own"))
  ) %>%
  group_by(pres24_trump, home_r) %>%
  summarise(n = survey_total(vartype = NULL), .groups = "drop") %>%
  group_by(home_r) %>%
  mutate(col_pct = n / sum(n)) %>%
  ungroup()

# Reshape into a crosstab layout
xtab_col <- tab %>%
  select(pres24_trump, home_r, col_pct) %>%
  pivot_wider(names_from = home_r, values_from = col_pct)

# Make it pretty
gt(xtab_col) %>%
  fmt_percent(columns = -pres24_trump, decimals = 1) %>%
  tab_header(title = "2024 Vote × Home Tenure (Weighted Column %)")

gt(xtab_col) %>%
  fmt_percent(columns = -pres24_trump, decimals = 1) %>%
  cols_label(pres24_trump = "2024 Presidential Vote") %>%
  tab_header(title = "2024 Vote × Home Tenure (Weighted Column %)")

gt(xtab_col, rowname_col = "pres24_trump") %>%
  fmt_percent(columns = everything(), decimals = 1) %>%
  tab_stubhead(label = "2024 Vote") %>%
  tab_spanner(label = "Home Tenure", columns = everything()) %>%
  tab_header(title = "2024 Vote × Home Tenure",
             subtitle = "Weighted column percentages")



#Education and Vote
ces2024_weighted |>
  drop_na(pres24_trump, educ_r) |>
  group_by(pres24_trump, educ_r) |>
  summarise(
    freq = survey_total(),
    pct  = survey_mean()
  )  |> pivot_wider(
    names_from = educ_r,
    values_from = pct
  )

# Weighted crosstab: pres24_trump × educ_r
xtab_educ <- ces2024_weighted |>
  drop_na(pres24_trump, educ_r) |>
  mutate(
    pres24_trump = factor(pres24_trump, levels = c(0, 1),
                          labels = c("Harris", "Trump"))
  ) |>
  group_by(educ_r, pres24_trump) |>
  summarise(col_pct = survey_mean(vartype = NULL)) |>
  ungroup() |>
  pivot_wider(names_from = educ_r, values_from = col_pct)

# Make it pretty
gt(xtab_educ, rowname_col = "pres24_trump") |>
  fmt_percent(columns = everything(), decimals = 1) |>
  tab_stubhead(label = "2024 Vote") |>
  tab_spanner(label = "Education", columns = everything()) |>
  tab_header(title = "2024 Vote × Education",
             subtitle = "Weighted column percentages")
