Skip to content

Instantly share code, notes, and snippets.

@thoughtfulbloke
Last active October 6, 2019 22:30
Show Gist options
  • Save thoughtfulbloke/498a63dac0aab47e6e63e404f10e37ae to your computer and use it in GitHub Desktop.
Save thoughtfulbloke/498a63dac0aab47e6e63e404f10e37ae to your computer and use it in GitHub Desktop.
Change in opinion about Treaty of Waiting over 2002-2017
library(foreign) # read NZESin SPSS files
library(dplyr) # general data manipulation
library(tidyr) # restructuring
library(ggplot2)
library(ggthemes)
# NZES files in nzes subfolder
nz17 <- suppressWarnings(read.spss("nzes/NZES2017Release14-07-19.sav",
to.data.frame = TRUE, add.undeclared.levels = "sort"))
nz17meta <- data.frame(varnames = attributes(nz17)$names, eyear=2017,
descriptions = attributes(nz17)$variable.labels,
stringsAsFactors = FALSE)
nz02 <- suppressWarnings(read.spss("nzes/nzes_02_1.1.por",
to.data.frame = TRUE, add.undeclared.levels = "sort"))
nz02meta <- data.frame(varnames = attributes(nz02)$names, eyear=2002,
descriptions = attributes(nz02)$variable.labels,
stringsAsFactors = FALSE)
w17 <- nz17 %>% filter(!is.na(rsamage), !is.na(rtreaty), rtreaty != "9. Don't know") %>%
group_by(flage=floor(rsamage)) %>%
summarise(sa17 = sum((rtreaty == "1. Strongly agree") *rwt),
ag17 = sum((rtreaty == "2. Somewhat agree") *rwt),
ne17 = sum((rtreaty == "3. Neither") *rwt),
ds17 = sum((rtreaty == "4. Somewhat disagree") *rwt),
sd17 = sum((rtreaty == "5. Strongly disagree") *rwt)) %>%
ungroup() %>% mutate(age2002=flage, age2017=flage)
w02 <- nz02 %>% filter(!is.na(WNAGE), !is.na(WTREAT), !is.na(ALZWT)) %>%
group_by(flage=floor(WNAGE)) %>%
summarise(sa02 = sum((WTREAT == "Strongly Agree") *ALZWT),
ag02 = sum((WTREAT == "Agree") *ALZWT),
ne02 = sum((WTREAT == "Neutral") *ALZWT),
ds02 = sum((WTREAT == "Disagree") *ALZWT),
sd02 = sum((WTREAT == "Strongly Disagree") *ALZWT)) %>%
ungroup() %>% mutate(age2017=flage + 15)
w02 %>% inner_join(w17, by="age2017") %>%
mutate(age_in_02 = case_when(age2002 < 28 ~ "18-27",
age2002 < 38 ~ "28-37",
age2002 < 48 ~ "38-47",
age2002 < 58 ~ "48-57",
age2002 < 68 ~ "58-67",
TRUE ~ "68+")) %>%
group_by(age_in_02) %>%
summarise(old_sa = sum(sa02), old_ag = sum(ag02), old_ne=sum(ne02), old_ds = sum(ds02), old_sd = sum(sd02),
new_sa = sum(sa17), new_ag = sum(ag17), new_ne=sum(ne17), new_ds = sum(ds17), new_sd = sum(sd17),
old_sa_p = 100*old_sa/(old_sa + old_ag + old_ne + old_ds + old_sd),
old_ag_p = 100*old_ag/(old_sa + old_ag + old_ne + old_ds + old_sd),
old_ne_p = 100*old_ne/(old_sa + old_ag + old_ne + old_ds + old_sd),
old_ds_p = 100*old_ds/(old_sa + old_ag + old_ne + old_ds + old_sd),
old_sd_p = 100*old_sd/(old_sa + old_ag + old_ne + old_ds + old_sd),
new_sa_p = 100*new_sa/(new_sa + new_ag + new_ne + new_ds + new_sd),
new_ag_p = 100*new_ag/(new_sa + new_ag + new_ne + new_ds + new_sd),
new_ne_p = 100*new_ne/(new_sa + new_ag + new_ne + new_ds + new_sd),
new_ds_p = 100*new_ds/(new_sa + new_ag + new_ne + new_ds + new_sd),
new_sd_p = 100*new_sd/(new_sa + new_ag + new_ne + new_ds + new_sd)) %>%
ungroup() %>% select(age_in_02, old_sa_p:new_sd_p) %>% gather(category, percent, old_sa_p:new_sd_p) %>%
separate(category, into=c("Time","Opine","permark"), sep="_") %>%
mutate(election = ifelse(Time=="old", "2002", "2017"),
Opinion = case_when(Opine == "sa" ~ "Strongly Agree",
Opine == "ag" ~ "Agree",
Opine == "ne" ~ "Neither",
Opine == "ds" ~ "Disagree",
TRUE ~ "Strongly Disagree"),
Opinion = factor(Opinion, levels=c("Strongly Agree","Agree","Neither","Disagree","Strongly Disagree"))) %>%
ggplot(aes(x=election,y=percent,colour=age_in_02, group=age_in_02)) + geom_line() + facet_wrap(~Opinion, ncol=5) +
theme_tufte() + geom_point() + scale_colour_viridis_d() +
ggtitle("15 Years Later: NZES does respondent agree with removing the Treaty of Waitangi from the law")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment