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

anes24<-read_dta("anes_timeseries_2024_stata_20250808.dta")

names(anes24)<-tolower(names(anes24))

#0. Find Variables, note missing

unique(anes24$v241227x) #orgpid
unique(anes24$v242095x) #voted        
unique(anes24$v240103b)# post survey weight       

#optional: rename them intuitively

anes24$orgpid <- anes24$v241227x
anes24$voted <- anes24$v242095x
anes24$weight_post <- anes24$v240103a

#1. Filter and select
anes24 <- anes24 |> select(orgpid, voted, weight_post)

#Check
anes24

#2. Manipulate

#Drop the DKs, etc
anes24 <- anes24 %>%
  mutate(pid = if_else(orgpid %in% -9:-4, NA_real_, orgpid))

#Check
table(anes24$orgpid)
table(anes24$pid)

#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

anes24 <- anes24 %>%
  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

anes24 <- anes24 %>%
  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,
    )
  )

#Figure 2
anes24 <- anes24 %>%
  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)
    #Coding is different than cumulative: we still recode so NAs implicitly become NAs
    turnout = case_when(
      voted == 0 ~ 0,
      voted == 1 ~ 1,
    )
  )

#Now we need year variables
anes24$year<-2024
anes24$presyear<-1

#3. Declare the survey and collapse the data

#anes24_svy <- anes24 %>% as_survey_design(weights = weight_post)

#Huh. That's weird.  A lot of the observations don't have a weight (yet?)
#Two options: eliminate non-weighted observations, and don't weight

#Option 1: Eliminate non-weighted observations
anes24_clean <- anes24 %>% filter(!is.na(weight_post))

anes24_svy <- anes24_clean %>% as_survey_design(weights = weight_post)

# now your dplyr pipeline works, but with survey-weighted means
figure1_data24_w <- anes24_svy %>%
  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)
  )

figure1_data24_w

#Option 2: Don't use the weight for 2024

#How many obserations don't have a weight?
sum(is.na(anes24$weight_post))
#Erm . . . that's a lot of observations

figure1_24 <- anes24 %>%
  # summarize means of selected vars
  #note ordering - done this way to match cumulative
  summarise(
    year = mean(year),
    strong = mean(strong, na.rm=T),
    weak = mean(weak, na.rm=T),
    leaners = mean(leaners, na.rm=T),
    indeps = mean(indeps, na.rm=T),
    presyear = mean(presyear)
  )
figure1_24

figure2_24 <- anes24 %>%
  group_by(turnout) %>%
  summarise(
    year = mean(year),
    identifiers = mean(identifiers, na.rm = TRUE),
    presyear = mean(presyear)
    )
figure2_24
