#Title: DWV Assign 7 - Bartels (draft).R
#Author: Jack Reilly
#Purpose: Replicate Figures 1 and 2 from Bartels
#Requires: Cumulative ANES; 2024 ANES
#Output: Figures as PDFs (ggplot only)

library(tidyverse)
library(haven)

setwd("~/desktop/working/dwv/bartels2026")

#OK, so let's think big picture here.  What are we doing?
#We need to get from two files (one quite large, one still somewhat large) with an individaul UoA
#To a single, small file with "year" as a UoA.
#There'll also be some data recoding/managing, graphing, etc.  
#And we need to have graphs with and without midterms
#We also know this is a survey, so we have to keep an eye out for weights
#So we know we have: 

#1. Variable and Observation Identification/filtering/selecting (x2)
#2. Variable manipulation (x2)
#3. Dataset summarisation/collapse (x2)
#4. Data merge
#5. Possibly: weight?
#6. Graph

#Load data

anes_full<-read_dta("anes_timeseries_cdf_stata_20220916.dta")
anes<-anes_full

anes_24<-read_dta("anes_timeseries_2024_stata_20250808.dta")

names(anes)<-tolower(names(anes))
names(anes_24)<-tolower(names(anes_24))

#0. Find Variables, note missing

unique(anes$vcf0301) #orgpid
unique(anes$vcf0004) #year
unique(anes$vcf0702) #voted        
unique(anes$vcf0703) #randvote
unique(anes$vcf0704) #presvote
unique(anes$vcf0704a)#presvotemajparty
unique(anes$vcf0009z)#survey weight       

#optional: rename them intuitively

anes$orgpid <- anes$vcf0301
anes$year <- anes$vcf0004
anes$voted <- anes$vcf0702
anes$randvote <- anes$vcf0703
anes$presvote <- anes$vcf0704
anes$presvotemajparty <- anes$vcf0704a
anes$weight <- anes$vcf0009z

#1. Filter and select
#anes <- anes |> select(vcf0301, vcf0004, vcf0702, vcf0703, vcf0704, vcf0704a, vcf0009z)
anes <- anes |> select(orgpid, year, voted, randvote, presvote, presvotemajparty, weight)

#Later, we'll need to manipulate things around midterm and not midterm years.  
#So I'm just going to make a variable to make that easy now

anes <- anes %>%
  mutate(presyear = if_else(
    year %in% c(1948, 1952, 1956, 1960, 1964, 1968,
                1972, 1976, 1980, 1984, 1988, 1992,
                1996, 2000, 2004, 2008, 2012, 2016, 
                2020, 2024),
    1, 0
  ))  

#2. Manipulate

#Drop the DKs, etc
anes <- anes %>%
  mutate(pid = if_else(orgpid == 0, NA_real_, orgpid))

#Now, this requires a little bit of thought.  
#We need proportions, so we need 1 and 0 for strong, weak, leaners, and indeps
#For when we summarize the data later

anes <- anes %>%
  mutate(
    strong  = case_when(
      pid %in% c(1, 7) ~ 1,
      pid %in% 2:6     ~ 0,
    ),
    weak = case_when(
      pid %in% c(2, 6) ~ 1,
      pid %in% c(1, 3, 4, 5, 7) ~ 0,
    ),
    leaners = case_when(
      pid %in% c(3, 5) ~ 1,
      pid %in% c(1, 2, 4, 6, 7) ~ 0,
    ),
    indeps = case_when(
      pid == 4         ~ 1,
      pid %in% c(1, 2, 3, 5, 6, 7) ~ 0,
    )
  )

#For figure 2, we need to combine some of them

anes <- anes %>%
  mutate(
    # Generate a dummy variable for all party identifiers, strong or weak
    identifiers = strong + weak,
    
    # Variable equals "identifiers" for voters (voted==2), missing otherwise
    identvote = if_else(voted == 2, identifiers, NA),
    
    # Variable equals "identifiers" for nonvoters (voted==1), missing otherwise
    identnovote = if_else(voted == 1, identifiers, NA),
    
    # Simple turnout variable: recode voted (1=0 nonvoter, 2=1 voter, else missing)
    turnout = case_when(
      voted == 1 ~ 0,
      voted == 2 ~ 1,
      )
  )

#3. Collapse the data

figure1_data <- anes %>%
  # filter first
  filter(presyear == 1, year > 1948) %>%
  # then group by year
  group_by(year) %>%
  # summarize means of selected vars
  summarise(
    strong = mean(strong, na.rm=T),
    weak = mean(weak, na.rm=T),
    leaners = mean(leaners, na.rm=T),
    indeps = mean(indeps, na.rm=T)
  )

#Check the data, to make sure it looks like what it should

#4. Let's Graph!

plot(x=0, type='n',
     xlim=c(1952, 2024), xaxt = "n",
     ylim=c(0,.6), yaxt = "n",
     xlab=" ", 
     ylab=" ", 
     main="Bartels 1")


lines(figure1_data$year, figure1_data$strong, type = "o", col = "black", lty=1)
lines(figure1_data$year, figure1_data$weak, type = "o", col = "black", lty=2)

legend("topright", legend = c("Strong", "Weak"),
       col = c("black", "black"), lty = c(1,2) , pch = 1)

#OK, so where are we?
#We have the basics!

#But
#1. some things don't quite look quite right yet.
#2. #And we're still missing 2024

#For #1 
#Let's try a weight

library(srvyr)

# define a survey design
anes_svy <- anes %>%
  as_survey_design(weights = weight)

# now your dplyr pipeline works, but with survey-weighted means
figure1_data <- anes_svy %>%
  filter(presyear == 1, year > 1948) %>%
  group_by(year) %>%
  summarise(
    strong  = survey_mean(strong, na.rm = TRUE, vartype = NULL),
    weak    = survey_mean(weak,   na.rm = TRUE, vartype = NULL),
    leaners = survey_mean(leaners,na.rm = TRUE, vartype = NULL),
    indeps  = survey_mean(indeps, na.rm = TRUE, vartype = NULL)
  )

plot(x=0, type='n',
     xlim=c(1952, 2024), xaxt = "n",
     ylim=c(0,.6), yaxt = "n",
     xlab=" ", 
     ylab=" ", 
     main="Bartels 1")

lines(figure1_data$year, figure1_data$strong, type = "o", col = "black", lty=1)
lines(figure1_data$year, figure1_data$weak, type = "o", col = "black", lty=2)

legend("topright", legend = c("Strong", "Weak"),
       col = c("black", "black"), lty = c(1,2) , pch = 1)

#Now, we're cooking with gas
#Let's double check figure 1b
plot(x=0, type='n',
     xlim=c(1952, 2024), xaxt = "n",
     ylim=c(0,.6), yaxt = "n",
     xlab=" ", 
     ylab=" ", 
     main="Bartels 1")

lines(figure1_data$year, figure1_data$indeps, type = "o", col = "black", lty=1)
lines(figure1_data$year, figure1_data$leaners, type = "o", col = "black", lty=2)

legend("topright", legend = c("Strong", "Weak"),
       col = c("black", "black"), lty = c(1,2) , pch = 1)


#Double check figure 2
#This is a different kind of summarise
#I'm doing it by two groups: year and turnout
#So first, let's do the summarisation

figure2_data <- anes_svy %>%
  filter(presyear == 1, year > 1948) %>%
  group_by(year, turnout) %>%
  summarise(
    identifiers = survey_mean(identifiers, na.rm = TRUE),
  )

#Make a picture
plot(x=0, type='n',
     xlim=c(1952, 2024), xaxt = "n",
     ylim=c(0,1), yaxt = "n",
     xlab=" ", 
     ylab=" ", 
     main="Bartels 2")

lines(figure2_data$year[figure2_data$turnout==1], figure2_data$identifiers[figure2_data$turnout==1], type = "o", col = "black", lty=1)
lines(figure2_data$year[figure2_data$turnout==0], figure2_data$identifiers[figure2_data$turnout==0], type = "o", col = "black", lty=2)

nonvoters <- subset(figure2_data, turnout == 0)
voters    <- subset(figure2_data, turnout == 1)

plot(x=0, type='n',
     xlim=c(1952, 2024), xaxt = "n",
     ylim=c(0,1), yaxt = "n",
     xlab=" ", 
     ylab=" ", 
     main="Bartels 2")

lines(voters$year, voters$identifiers, type = "o", col = "black", lty=1)
lines(nonvoters$year, nonvoters$identifiers, type = "o", col = "black", lty=2)


# Legend
legend("topleft",
       legend = c("Nonvoters", "Voters"),
       lty = c(2, 1), pch = c(1, 16),
       bty = "n")