Skip to content

Instantly share code, notes, and snippets.

@resulumit
Created February 14, 2021 16:08
Show Gist options
  • Save resulumit/81c23d5cf5d6ce6d6818392df32bcdfc to your computer and use it in GitHub Desktop.
Save resulumit/81c23d5cf5d6ce6d6818392df32bcdfc to your computer and use it in GitHub Desktop.
Evaluation of party positions in the CHES dataset
# load the data
library(tidyverse)
# write functions to calculate confidence intervals
lower_ci <- function(mean, se, n, conf_level = 0.95){
lower_ci <- mean - qt(1 - ((1 - conf_level) / 2), n - 1) * se
}
upper_ci <- function(mean, se, n, conf_level = 0.95){
upper_ci <- mean + qt(1 - ((1 - conf_level) / 2), n - 1) * se
}
# get the 1999-2019 chapel hill expert survey (CHES) trend file
ches <- read.csv("https://www.chesdata.eu/s/1999-2019_CHES_dataset_meansv1.csv")
# name the party families
par_fam <- ches %>%
select(year, party_id, family) %>%
mutate(family = recode_factor(family, `1` = "Radical Right", `2` = "Conservatives", `3` = "Liberal",
`4` = "Christian-Democratic", `5` = "Socialist", `6` = "Radical Left",
`7` = "Green", `8` = "Regionalist", `10` = "Confessional",
`11` = "Agrarian/Center", `9` = "No Family"))
# select the position variables to be used
par_pos <- ches %>%
select(year, party_id, vote, lrgen, galtan, immigrate_policy)
# start preparing the data
ches %>%
# keep only the large families
filter(family < 7) %>%
# create party combinations
select(year, party_id, family, lrgen, galtan, immigrate_policy) %>%
mutate(family = recode_factor(
family, `1` = "Radical Right", `2` = "Conservatives", `3` = "Liberal",
`4` = "Christian-Democratic", `5` = "Socialist",
`6` = "Radical Left", `7` = "Green", `8` = "Regionalist",
`10` = "Confessional", `11` = "Agrarian/Center",
`9` = "No Family")) %>%
# switch from wide to long data
pivot_longer(c("lrgen", "galtan", "immigrate_policy"),
names_to = "area", values_to = "values") %>%
# calculate statistics by family and position
group_by(year, family, area) %>%
summarise(mean_v = mean(values, na.rm = TRUE),
sd_v = sd(values, na.rm = TRUE),
count = n()) %>%
mutate(se = sd_v / sqrt(count),
lower_ci = lower_ci(mean_v, se, count),
upper_ci = upper_ci(mean_v, se, count)) %>%
# plot the data
ggplot(aes(x = year, y = mean_v, color = area)) +
geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci),
width = .1, size = 0.8) +
geom_line(size = 1.2) +
geom_point(size = 1.3) +
facet_wrap(. ~ family) +
theme_bw() +
theme(legend.position = "bottom",
legend.title = element_blank()) +
scale_x_continuous(breaks = c(1999, 2002, 2006, 2010, 2014, 2019)) +
scale_color_discrete(breaks = c("lrgen", "galtan", "immigrate_policy"),
labels = c("Left-Right", "GAL-TAN", "Immigration")) +
ylab("Mean position\n") + xlab("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment