Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. library(foreign) # read NZESin SPSS files
  2. library(dplyr) # general data manipulation
  3. library(tidyr) # restructuring
  4. library(ggplot2)
  5. library(ggthemes)
  6.  
  7. # NZES files in nzes subfolder
  8. nz17 <- suppressWarnings(read.spss("nzes/NZES2017Release14-07-19.sav",
  9. to.data.frame = TRUE, add.undeclared.levels = "sort"))
  10. nz17meta <- data.frame(varnames = attributes(nz17)$names, eyear=2017,
  11. descriptions = attributes(nz17)$variable.labels,
  12. stringsAsFactors = FALSE)
  13. nz02 <- suppressWarnings(read.spss("nzes/nzes_02_1.1.por",
  14. to.data.frame = TRUE, add.undeclared.levels = "sort"))
  15. nz02meta <- data.frame(varnames = attributes(nz02)$names, eyear=2002,
  16. descriptions = attributes(nz02)$variable.labels,
  17. stringsAsFactors = FALSE)
  18.  
  19. w17 <- nz17 %>% filter(!is.na(rsamage), !is.na(rtreaty), rtreaty != "9. Don't know") %>%
  20. group_by(flage=floor(rsamage)) %>%
  21. summarise(sa17 = sum((rtreaty == "1. Strongly agree") *rwt),
  22. ag17 = sum((rtreaty == "2. Somewhat agree") *rwt),
  23. ne17 = sum((rtreaty == "3. Neither") *rwt),
  24. ds17 = sum((rtreaty == "4. Somewhat disagree") *rwt),
  25. sd17 = sum((rtreaty == "5. Strongly disagree") *rwt)) %>%
  26. ungroup() %>% mutate(age2002=flage, age2017=flage)
  27.  
  28. w02 <- nz02 %>% filter(!is.na(WNAGE), !is.na(WTREAT), !is.na(ALZWT)) %>%
  29. group_by(flage=floor(WNAGE)) %>%
  30. summarise(sa02 = sum((WTREAT == "Strongly Agree") *ALZWT),
  31. ag02 = sum((WTREAT == "Agree") *ALZWT),
  32. ne02 = sum((WTREAT == "Neutral") *ALZWT),
  33. ds02 = sum((WTREAT == "Disagree") *ALZWT),
  34. sd02 = sum((WTREAT == "Strongly Disagree") *ALZWT)) %>%
  35. ungroup() %>% mutate(age2017=flage + 15)
  36.  
  37. w02 %>% inner_join(w17, by="age2017") %>%
  38. mutate(age_in_02 = case_when(age2002 < 28 ~ "18-27",
  39. age2002 < 38 ~ "28-37",
  40. age2002 < 48 ~ "38-47",
  41. age2002 < 58 ~ "48-57",
  42. age2002 < 68 ~ "58-67",
  43. TRUE ~ "68+")) %>%
  44. group_by(age_in_02) %>%
  45. summarise(old_sa = sum(sa02), old_ag = sum(ag02), old_ne=sum(ne02), old_ds = sum(ds02), old_sd = sum(sd02),
  46. new_sa = sum(sa17), new_ag = sum(ag17), new_ne=sum(ne17), new_ds = sum(ds17), new_sd = sum(sd17),
  47. old_sa_p = 100*old_sa/(old_sa + old_ag + old_ne + old_ds + old_sd),
  48. old_ag_p = 100*old_ag/(old_sa + old_ag + old_ne + old_ds + old_sd),
  49. old_ne_p = 100*old_ne/(old_sa + old_ag + old_ne + old_ds + old_sd),
  50. old_ds_p = 100*old_ds/(old_sa + old_ag + old_ne + old_ds + old_sd),
  51. old_sd_p = 100*old_sd/(old_sa + old_ag + old_ne + old_ds + old_sd),
  52. new_sa_p = 100*new_sa/(new_sa + new_ag + new_ne + new_ds + new_sd),
  53. new_ag_p = 100*new_ag/(new_sa + new_ag + new_ne + new_ds + new_sd),
  54. new_ne_p = 100*new_ne/(new_sa + new_ag + new_ne + new_ds + new_sd),
  55. new_ds_p = 100*new_ds/(new_sa + new_ag + new_ne + new_ds + new_sd),
  56. new_sd_p = 100*new_sd/(new_sa + new_ag + new_ne + new_ds + new_sd)) %>%
  57. ungroup() %>% select(age_in_02, old_sa_p:new_sd_p) %>% gather(category, percent, old_sa_p:new_sd_p) %>%
  58. separate(category, into=c("Time","Opine","permark"), sep="_") %>%
  59. mutate(election = ifelse(Time=="old", "2002", "2017"),
  60. Opinion = case_when(Opine == "sa" ~ "Strongly Agree",
  61. Opine == "ag" ~ "Agree",
  62. Opine == "ne" ~ "Neither",
  63. Opine == "ds" ~ "Disagree",
  64. TRUE ~ "Strongly Disagree"),
  65. Opinion = factor(Opinion, levels=c("Strongly Agree","Agree","Neither","Disagree","Strongly Disagree"))) %>%
  66. ggplot(aes(x=election,y=percent,colour=age_in_02, group=age_in_02)) + geom_line() + facet_wrap(~Opinion, ncol=5) +
  67. theme_tufte() + geom_point() + scale_colour_viridis_d() +
  68. ggtitle("15 Years Later: NZES does respondent agree with removing the Treaty of Waitangi from the law")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement