Skip to content

Instantly share code, notes, and snippets.

@favstats
Last active April 6, 2022 15:08
Show Gist options
  • Save favstats/1106c7232666231464d15f4dbb4e3524 to your computer and use it in GitHub Desktop.
Save favstats/1106c7232666231464d15f4dbb4e3524 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(modelbased)
library(magrittr)
overview <- readRDS("data/overview.rds")
## helper function
get_plabs <- function (pval) {
dplyr::case_when(is.na(pval) ~ "", pval < 0.001 ~ "***",
pval < 0.01 ~ "**", pval < 0.05 ~ "*",
pval < 0.1 ~ "", TRUE ~ "")
}
## estimate model
reach_mod <- lm(reach ~ targeting * party + engagement, data = overview)
## estimate contrasts
contrasts_nobreak <- estimate_contrasts(reach_mod,
contrast = c("targeting", "party"),
at = c("targeting", "party")) %>%
as.data.frame() %>%
mutate(Contrast = paste(Level1, "-", Level2)) %>%
#### in the following I filter down the data to only the comparisons I am interested in ####
## only keep within partyy comparisons (i.e. if party appears twice)
filter(
str_count(condition_comparison, "VVD") == 2 |
str_count(condition_comparison, "GroenLinks") == 2 |
str_count(condition_comparison, "PvdA") == 2
) %>%
## only keep comparison between targeting pairs (i.e. targeting appears twice)
filter(
str_count(condition_comparison, "Environment") == 2 |
str_count(condition_comparison, "Economy") == 2 |
str_count(condition_comparison, "Politics") == 2 |
str_count(condition_comparison, "Education") == 2
) %>%
# in case the comparison is in wrong direction, change around
mutate_at(vars(Difference, CI_low, CI_high), ~ifelse(str_detect(Level1, "excluded"), .x*-1, .x)) %>%
mutate_at(vars(Difference, CI_low, CI_high), ~ifelse(str_detect(Level1, "Low"), .x*-1, .x)) %>%
## get labels for p-values
mutate(plabel = get_plabs(p)) %>%
mutate(diff_label = paste0(round(Difference), plabel)) %>%
## some more labelling for plot
mutate(party = str_extract(condition_comparison, "VVD|GroenLinks|PvdA")) %>%
mutate(condition_comparison = str_remove_all(condition_comparison, "VVD|GroenLinks|PvdA")) %>%
mutate(condition_comparison = case_when(
str_detect(condition_comparison, "Economy") ~ "Economy (vs. not)",
str_detect(condition_comparison, "Education") ~ "Higher Educated (vs. lower)",
str_detect(condition_comparison, "Politics") ~ "Politics (vs. not)",
str_detect(condition_comparison, "Environment") ~ "Environment (vs. not)"
)) %>%
mutate(condition_comparison = fct_reorder(condition_comparison, Difference))
## create visualization
contrasts_nobreak %>%
ggplot(aes(condition_comparison, Difference, color = party)) +
geom_point(position = position_dodge(width = 0.9)) +
geom_errorbar(aes(ymin = CI_low, ymax = CI_high), width = 0, position = position_dodge(width = 0.9)) +
coord_flip() +
geom_text(aes(label = diff_label, x = as.factor(condition_comparison) %>% as.numeric() %>% magrittr::add(0.15)), show.legend = F, position = position_dodge(width = 0.9)) +
geom_hline(yintercept = 0, linetype = "dashed") +
labs(y = "Estimated reach differences",
x = "Targeting Comparisons") +
theme_minimal() +
theme(legend.position = "bottom",
plot.title = element_text(size = 19, face = "bold"),
strip.text.y = element_blank(),
axis.text.y = element_text(face = "bold"),
strip.background = element_rect(fill = "lightgrey")) +
ggtitle("Reach differences per party")
@favstats
Copy link
Author

favstats commented Apr 6, 2022

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment