#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)

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

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

#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. Declare the survey and collapse the data

anes_svy <- anes %>%
  as_survey_design(weights = weight)

# now your dplyr pipeline works, but with survey-weighted means
figure1_cumul <- anes_svy %>%
  filter(year > 1950) %>%
  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),
    presyear = round(survey_mean(presyear, vartype = NULL), 1) #this round command fixes an annoying filter error later!
  )


figure2_cumul <- anes_svy %>%
  filter(year > 1950) %>%
  group_by(year, turnout) %>%
  summarise(
    identifiers = survey_mean(identifiers, na.rm = TRUE, vartype = NULL),
    presyear = round(survey_mean(presyear, vartype = NULL), 1) #this round command fixes an annoying filter error later!
  )
