library(dplyr)
library(ggplot2)
library(knitr)
pkg_master <- system.file("extdata", "qes_master.csv", package = "qesR")
master_paths <- c("qes_master.csv", "../qes_master.csv", pkg_master)
master_paths <- master_paths[nzchar(master_paths)]
master <- read.csv(master_paths[file.exists(master_paths)][1], stringsAsFactors = FALSE)
master <- master %>%
mutate(
qes_year_num = as.integer(substr(as.character(qes_year), 1, 4)),
vote_choice_raw = trimws(as.character(vote_choice)),
vote_choice_party = dplyr::case_when(
vote_choice_raw %in% c("CAQ", "ADQ") ~ "CAQ/ADQ",
vote_choice_raw == "PLQ" ~ "PLQ",
vote_choice_raw == "PQ" ~ "PQ",
vote_choice_raw == "QS" ~ "QS",
vote_choice_raw == "PCQ" ~ "PCQ",
TRUE ~ NA_character_
)
)
major_parties <- c("CAQ/ADQ", "PLQ", "PQ", "QS", "PCQ")
years <- sort(unique(master$qes_year_num[!is.na(master$qes_year_num)]))
vote_base <- master %>%
filter(!is.na(qes_year_num), !is.na(vote_choice_party), nzchar(vote_choice_party))
vote_den <- vote_base %>%
group_by(qes_year_num) %>%
summarise(n_with_vote_choice = n(), .groups = "drop")
vote_party <- vote_base %>%
group_by(qes_year_num, vote_choice_party) %>%
summarise(n_party = n(), .groups = "drop")
grid <- expand.grid(
qes_year_num = years,
vote_choice_party = major_parties,
stringsAsFactors = FALSE
)
vote_party <- merge(grid, vote_party, by = c("qes_year_num", "vote_choice_party"), all.x = TRUE, sort = TRUE)
vote_party$n_party[is.na(vote_party$n_party)] <- 0
vote_party <- merge(vote_party, vote_den, by = "qes_year_num", all.x = TRUE, sort = TRUE)
vote_party <- vote_party %>%
mutate(
share = ifelse(n_with_vote_choice > 0, n_party / n_with_vote_choice, NA_real_),
se = ifelse(n_with_vote_choice > 0, sqrt(pmax(share * (1 - share), 0) / n_with_vote_choice), NA_real_),
ci_low = ifelse(!is.na(se), pmax(0, share - 1.96 * se), NA_real_),
ci_high = ifelse(!is.na(se), pmin(1, share + 1.96 * se), NA_real_),
year_index = match(qes_year_num, years)
)
vote_table <- vote_party %>%
group_by(qes_year_num) %>%
summarise(
n_with_vote_choice = first(n_with_vote_choice),
caq_adq = 100 * share[vote_choice_party == "CAQ/ADQ"],
plq = 100 * share[vote_choice_party == "PLQ"],
pq = 100 * share[vote_choice_party == "PQ"],
qs = 100 * share[vote_choice_party == "QS"],
pcq = 100 * share[vote_choice_party == "PCQ"],
.groups = "drop"
)
knitr::kable(vote_table)