library(tidyverse) # for manipulating data
library(srvyr) # for dealing with survey weights
library(sjlabelled) # for using labels
library(flextable) # for formatting output tables
library(haven) # for reading SPSS files
library(broom) # for dealing with annoying output chunks
library(scales) # for percentage labelling
library(countrycode) # for working with country names
library(ggthemes) # for plot formatting
library(ggpubr) # for plot arrangements
library(cowplot) # for plot arrangements
library(gridExtra) # for plot arrangements
library(egg) # for plot arrangements
library(glue) # because paste() can work in annoying ways

SUPLEMENTARY MATERIALS

As accompanying material to “…”, this document presents details of data transformations, statistical procedures and visualisation techniques. It also includes additional material, which has not been included in the main body of the paper for reasons of brevity, but support specific statements made in the paper. Code chunks are hidden by default but can be accessed by clicking the relevant buttons. All calculations rely on the publicly available integrated spss data file.

EMPIRICAL BASE

Data_harmonised_EP_trust <- haven::read_sav("Data_harmonised_EP_trust.sav")

country_vector <- c("BE", "DE", "EE", "ES", "FI", "FR", "GB", "HU", "IE", "NL", "PL", "PT", "SE", "SI")

trust_survey_data <-  Data_harmonised_EP_trust %>%
  select(T_SURVEY_ID, T_SURVEY_NAME, T_SURVEY_COUNTRY, T_SURVEY_YEAR, T_SURVEY_EDITION, Respondent_ID_harmonised, EP_trust_harmonised, NP_trust_harmonised, EP_trust_harmonised_item_nonresponse, NP_trust_harmonised_item_nonresponse, Weight_total_harmonised) %>%
  filter(T_SURVEY_NAME %in% c("EB", "ESS")) %>%
  filter(T_SURVEY_COUNTRY %in% country_vector) %>%
  mutate(weights = sjlabelled::remove_all_labels(Weight_total_harmonised))

Figure 0

trust_survey_data %>%
  filter(T_SURVEY_COUNTRY %in% country_vector) %>%
  group_by(T_SURVEY_COUNTRY, T_SURVEY_YEAR, T_SURVEY_NAME) %>%
  count() %>%
  mutate(T_SURVEY_COUNTRY = 
           countrycode::countrycode(sjlabelled::remove_all_labels(T_SURVEY_COUNTRY), origin = "iso2c", destination = "country.name")) %>%
  ggplot(aes(x = T_SURVEY_COUNTRY, y = T_SURVEY_YEAR, label = n, color = T_SURVEY_NAME)) + 
  geom_label(show.legend = F) + 
  scale_color_manual(values = c("#56B4E9", "#D55E00"), labels = c("Eurobarometer", "European Social Survey")) +
  coord_flip() + theme_bw() + 
  xlab("") + ylab("") + ggtitle("Survey projects - surveyed countries - survey waves - survey respondents") +
  theme(text = element_text(colour = "black"),
        legend.position = "bottom",
        legend.title = element_text(size = 10, face = "bold"),
        strip.background = element_rect(fill = "gray98"),
        strip.text = element_text(size = 10, face = "bold", margin = margin(1,0,1,0, "pt")),
        panel.spacing.x = unit(2, "pt"),
        panel.spacing.y = unit(2, "pt"),
        axis.text = element_text(color = "black", size = 9, face = "bold"),
        plot.title = element_text(size = 10, face = "bold")) + facet_wrap(~T_SURVEY_NAME, nrow = 2)

FIGURE 1

#EP_NP_TRUST_PLOT

#EP_trust 

#data_transformations

prop_EP_value_plot1 <- trust_survey_data %>%
  filter(!is.na(EP_trust_harmonised)) %>%
  filter(EP_trust_harmonised != 88) %>%
  group_by(T_SURVEY_ID, T_SURVEY_NAME, T_SURVEY_COUNTRY, T_SURVEY_YEAR) %>%
  count(EP_trust_harmonised, wt = weights) %>%
  group_by(T_SURVEY_ID) %>%
  mutate(prop_EP =  n/sum(n)) 

prop_EP_DK_plot1 <- trust_survey_data %>%
  filter(!is.na(EP_trust_harmonised)) %>%
  group_by(T_SURVEY_ID, T_SURVEY_NAME, T_SURVEY_COUNTRY, T_SURVEY_YEAR) %>%
  count(EP_trust_harmonised, wt = weights) %>%
  group_by(T_SURVEY_ID) %>%
  mutate(prop_EP =  n/sum(n)) %>%
  filter(EP_trust_harmonised == 88)

prop_EP_plot1 <- full_join(prop_EP_value_plot1, prop_EP_DK_plot1)

survey_means_ESS_EP <- trust_survey_data %>% srvyr::as_survey(weights = weights) %>%
  mutate(T_SURVEY_COUNTRY = countrycode::countrycode(sjlabelled::remove_all_labels(T_SURVEY_COUNTRY), 
                                                     origin = "iso2c", 
                                                     destination = "country.name")) %>%
  filter(EP_trust_harmonised != 88) %>%
  filter(T_SURVEY_NAME == "ESS") %>%
  group_by(T_SURVEY_ID, T_SURVEY_COUNTRY, T_SURVEY_YEAR) %>%
  summarise(mean_EP_trust = srvyr::survey_mean(EP_trust_harmonised, na.rm = T))

survey_means_EB_EP <- trust_survey_data %>% srvyr::as_survey(weights = weights) %>%
  mutate(T_SURVEY_COUNTRY = countrycode::countrycode(sjlabelled::remove_all_labels(T_SURVEY_COUNTRY), 
                                                     origin = "iso2c", 
                                                     destination = "country.name")) %>%
  filter(EP_trust_harmonised != 88) %>%
  filter(T_SURVEY_NAME == "EB") %>%
  group_by(T_SURVEY_ID, T_SURVEY_COUNTRY, T_SURVEY_YEAR) %>%
  summarise(mean_EP_trust = srvyr::survey_mean(EP_trust_harmonised, na.rm = T))

EB_DK_data_EP <- prop_EP_plot1 %>% 
  filter(T_SURVEY_NAME == "EB") %>%
  mutate(T_SURVEY_COUNTRY = countrycode::countrycode(sjlabelled::remove_all_labels(T_SURVEY_COUNTRY), 
                                                     origin = "iso2c", 
                                                     destination = "country.name")) %>%
  filter(!is.na(EP_trust_harmonised)) %>% 
  filter(EP_trust_harmonised == 88)

#EP_trust plot
  
trust_ep <- prop_EP_plot1 %>% ungroup() %>%
  mutate(T_SURVEY_COUNTRY = countrycode::countrycode(sjlabelled::remove_all_labels(T_SURVEY_COUNTRY), 
                                               origin = "iso2c", 
                                               destination = "country.name")) %>%
  filter(T_SURVEY_NAME == "ESS") %>%
  filter(!is.na(EP_trust_harmonised)) %>% 
  mutate(EP_trust_recoded = ifelse(EP_trust_harmonised == 88, -0.3, EP_trust_harmonised)) %>%
 ggplot(aes(x = as.numeric(T_SURVEY_YEAR))) + 
  geom_point(aes(y = EP_trust_recoded, size = prop_EP,
                 fill = ifelse(EP_trust_recoded == -0.3, "1", "2"), 
                 alpha = prop_EP, shape = ifelse(EP_trust_recoded == -0.3, "1", "2")), 
             color = "gray20") +
  geom_point(data = EB_DK_data_EP, aes(y = -0.15, size = prop_EP, alpha = prop_EP, fill = "1", shape = "1")) +
  geom_smooth(data = survey_means_ESS_EP, aes(y = mean_EP_trust, linetype = "1"), 
              se = F, color = "gray20", alpha = 0.8) +
  geom_smooth(data = survey_means_EB_EP, aes(y = mean_EP_trust, linetype = "2"), 
              se = F, color = "gray20", alpha = 0.8) +
  geom_hline(yintercept = -0.05, linetype = "solid", size = 0.5) +
  xlab("") +
  ylab("") +
  ggtitle("Trust in the European Parliament") +
  scale_shape_manual(values = c(21, 22), labels = c("Don't know", "Scale-point indicaiton")) +
  scale_linetype_discrete(labels = c("ESS 11-point scale average", "EB 2-point scale average")) +
  scale_fill_manual(values = c("#56B4E9", "#D55E00"), labels = c("Don't know", "Scale-point indicaiton")) +
  scale_size_continuous(breaks = seq(0.05, 0.3, by = 0.05), 
                        labels = scales::label_percent(accuracy = 5L),
                        limits = c(0, 0.34)) +
  scale_alpha_continuous(breaks = seq(0.05, 0.3, by = 0.05),
                         labels = scales::label_percent(accuracy = 5L),
                         limits = c(0, 0.34)) +
  scale_y_continuous(labels = c("ESS", "EB", seq(0.0, 1.0, by = 0.1)), 
                     breaks = c(-0.3, -0.15, seq(0, 1.0, by = 0.1))) +
  scale_x_continuous(labels = c("'04", "'06", "'08", "'10", "'12", "'14", "'16", "'18"), 
                     breaks = seq(2004, 2018, by = 2)) +
  coord_cartesian(ylim = c(-0.31, 1.01)) +
  guides(shape = FALSE,
         fill = FALSE,
         size = guide_legend("Percentage of\nDon't Knows", 
                             override.aes = list(shape = 21, fill =  "#56B4E9"), 
                             label.position = "right",
                             reverse = T),
         linetype = guide_legend("Trend-lines", reverse = T, label.position = "bottom"),
         alpha = guide_legend("Percentage of\nDon't Knows", 
                                       reverse = T)) +
  facet_wrap(~ T_SURVEY_COUNTRY, nrow = 2) + 
    theme_bw() + 
  theme(text = element_text(colour = "black"),
     legend.position = "none",
     plot.margin = margin(-4, 3, 0, 0, unit = "pt"),
        legend.title = element_text(size = 10, face = "bold"),
        strip.background = element_rect(fill = "gray98"),
        strip.text = element_text(size = 10, face = "bold", margin = margin(1,0,1,0, "pt")),
     panel.spacing.x = unit(2, "pt"),
     panel.spacing.y = unit(2, "pt"),
    axis.text = element_text(color = "black", size = 9, face = "bold"),
    plot.title = element_text(size = 10, face = "bold")) 

#NP_trust

#data_transformations

prop_NP_value <- trust_survey_data %>%
  filter(!is.na(NP_trust_harmonised)) %>%
  filter(NP_trust_harmonised != 88) %>%
  group_by(T_SURVEY_ID, T_SURVEY_NAME, T_SURVEY_COUNTRY, T_SURVEY_YEAR) %>%
  count(NP_trust_harmonised, wt = weights) %>%
  group_by(T_SURVEY_ID) %>%
  mutate(prop_NP =  n/sum(n)) 

prop_NP_DK <- trust_survey_data %>%
  filter(!is.na(NP_trust_harmonised)) %>%
  group_by(T_SURVEY_ID, T_SURVEY_NAME, T_SURVEY_COUNTRY, T_SURVEY_YEAR) %>%
  count(NP_trust_harmonised, wt = weights) %>%
  group_by(T_SURVEY_ID) %>%
  mutate(prop_NP =  n/sum(n)) %>%
  filter(NP_trust_harmonised == 88)

prop_NP_plot1 <- full_join(prop_NP_value, prop_NP_DK)


survey_means_ESS_NP <- trust_survey_data %>% srvyr::as_survey(weights = weights) %>%
  mutate(T_SURVEY_COUNTRY = countrycode::countrycode(sjlabelled::remove_all_labels(T_SURVEY_COUNTRY), 
                                                     origin = "iso2c", 
                                                     destination = "country.name")) %>%
  filter(NP_trust_harmonised != 88) %>%
  filter(T_SURVEY_NAME == "ESS") %>%
  group_by(T_SURVEY_ID, T_SURVEY_COUNTRY, T_SURVEY_YEAR) %>%
  summarise(mean_NP_trust = srvyr::survey_mean(NP_trust_harmonised, na.rm = T))

survey_means_EB_NP <- trust_survey_data %>% srvyr::as_survey(weights = weights) %>%
  mutate(T_SURVEY_COUNTRY = countrycode::countrycode(sjlabelled::remove_all_labels(T_SURVEY_COUNTRY), 
                                                     origin = "iso2c", 
                                                     destination = "country.name")) %>%
  filter(NP_trust_harmonised != 88) %>%
  filter(T_SURVEY_NAME == "EB") %>%
  group_by(T_SURVEY_ID, T_SURVEY_COUNTRY, T_SURVEY_YEAR) %>%
  summarise(mean_NP_trust = srvyr::survey_mean(NP_trust_harmonised, na.rm = T))

EB_DK_data_NP <- prop_NP_plot1 %>% 
  filter(T_SURVEY_NAME == "EB") %>%
  mutate(T_SURVEY_COUNTRY = countrycode::countrycode(sjlabelled::remove_all_labels(T_SURVEY_COUNTRY), 
                                                     origin = "iso2c", 
                                                     destination = "country.name")) %>%
  filter(!is.na(NP_trust_harmonised)) %>% 
  filter(NP_trust_harmonised == 88)

#NP_trust plot

trust_np <- prop_NP_plot1 %>% ungroup() %>%
  mutate(T_SURVEY_COUNTRY = countrycode::countrycode(sjlabelled::remove_all_labels(T_SURVEY_COUNTRY), 
                                                     origin = "iso2c", 
                                                     destination = "country.name")) %>%
  filter(T_SURVEY_NAME == "ESS") %>%
  filter(!is.na(NP_trust_harmonised)) %>% 
  mutate(NP_trust_recoded = ifelse(NP_trust_harmonised == 88, -0.3, NP_trust_harmonised)) %>%
  ggplot(aes(x = as.numeric(T_SURVEY_YEAR))) + 
  geom_point(aes(y = NP_trust_recoded, size = prop_NP,
                 fill = ifelse(NP_trust_recoded == -0.3, "1", "2"), 
                 alpha = prop_NP, shape = ifelse(NP_trust_recoded == -0.3, "1", "2")), 
             color = "gray20") +
  geom_point(data = EB_DK_data_NP, aes(y = -0.15, size = prop_NP, alpha = prop_NP, fill = "1", shape = "1")) +
  geom_smooth(data = survey_means_ESS_NP, aes(y = mean_NP_trust, linetype = "1"), 
              se = F, color = "gray20", alpha = 0.8) +
  geom_smooth(data = survey_means_EB_NP, aes(y = mean_NP_trust, linetype = "2"), 
              se = F, color = "gray20", alpha = 0.8) +
  geom_hline(yintercept = -0.05, linetype = "solid", size = 0.5) +
  xlab("") +
  ylab("") +
  ggtitle("Trust in the National Parliament") +
  scale_shape_manual(values = c(21, 22), labels = c("Don't know", "Scale-point indicaiton")) +
  scale_linetype_discrete(labels = c("ESS 11-point scale average", "EB 2-point scale average")) +
  scale_fill_manual(values = c("#56B4E9", "#D55E00"), labels = c("Don't know", "Scale-point indicaiton")) +
  scale_size_continuous(breaks = seq(0.05, 0.3, by = 0.05), 
                        labels = scales::label_percent(accuracy = 5L),
                        limits = c(0, 0.35)) +
  scale_alpha_continuous(breaks = seq(0.05, 0.3, by = 0.05),
                         labels = scales::label_percent(accuracy = 5L),
                         limits = c(0, 0.35)) +
  scale_y_continuous(labels = c("ESS", "EB", seq(0.0, 1.0, by = 0.1)), 
                     breaks = c(-0.3, -0.15, seq(0, 1.0, by = 0.1))) +
  scale_x_continuous(labels = c("'04", "'06", "'08", "'10", "'12", "'14", "'16", "'18"), 
                     breaks = seq(2004, 2018, by = 2)) +
  coord_cartesian(ylim = c(-0.31, 1.01)) +
  guides(shape = FALSE,
         fill = FALSE,
         size = guide_legend("Percentage of ESS\nvalid responses", 
                             override.aes = list(shape = 22, fill =  "#D55E00"), 
                             label.position = "right",
                             reverse = T),
         linetype = FALSE,
         alpha = guide_legend("Percentage of ESS\nvalid responses", 
                              reverse = T)) +
  facet_wrap(~ T_SURVEY_COUNTRY, nrow = 2) + 
  theme_bw() + 
  theme(text = element_text(colour = "black"),
        plot.margin = margin(3, 3, 0, 0, unit = "pt"),
        legend.position = "none",
        strip.background = element_rect(fill = "gray98"),
        legend.title = element_text(size = 10, face = "bold"),
        strip.text = element_text(size = 10, face = "bold", margin = margin(1,0,1,0, "pt")), 
        axis.text = element_text(color = "black", size = 9, face = "bold"),
        panel.spacing.x = unit(2, "pt"),
        panel.spacing.y = unit(2, "pt"),
        plot.title = element_text(size = 10, face = "bold")) 

#Merging EP_plot with NP_plot

legend_1 <- cowplot::get_legend(trust_np + 
                                  theme(legend.position = "bottom",
                                        legend.box.margin = margin(-5, 1, 1, 50, unit = "pt"),
                                        legend.margin = margin(-5, 1, 1, 50, unit = "pt"),
                                        legend.justification = "left"))
legend_2 <- cowplot::get_legend(trust_ep + 
                                  theme(legend.position = "bottom",
                                        legend.box.margin = margin(-5, 2, 1, 60, unit = "pt"),
                                        legend.margin = margin(-5, 2, 1, 60, unit = "pt"),
                                        legend.justification = "left"))

plot_dot_1 <- ggpubr::ggarrange(trust_np, trust_ep, nrow = 2)

gridExtra::grid.arrange(plot_dot_1, legend_1, legend_2,
             layout_matrix = rbind(c(1, 1),c(2, 3)),
             heights = c(5.5, 0.25),
             widths = c(10, 35, 1))

FIGURE 2

#PLOT_COR

#data transformaitons

survey_means_1 <- trust_survey_data %>% srvyr::as_survey(weights = weights) %>%
  filter(EP_trust_harmonised != 88) %>%
  filter(T_SURVEY_NAME == "EB") %>%
  group_by(T_SURVEY_ID, T_SURVEY_COUNTRY, T_SURVEY_YEAR) %>%
  summarise(EPtrust_EB = srvyr::survey_mean(EP_trust_harmonised, na.rm = T)) %>%
  select(-EPtrust_EB_se) %>%
  mutate(join_id = glue::glue(T_SURVEY_COUNTRY, T_SURVEY_YEAR))

survey_means_EP_ESS_1 <- trust_survey_data %>% srvyr::as_survey(weights = weights) %>%
  filter(EP_trust_harmonised != 88) %>%
  filter(T_SURVEY_NAME == "ESS") %>%
  group_by(T_SURVEY_ID, T_SURVEY_COUNTRY, T_SURVEY_YEAR) %>%
  summarise(EPtrust_ESS = srvyr::survey_mean(EP_trust_harmonised, na.rm = T)) %>%
  mutate(join_id = glue::glue(T_SURVEY_COUNTRY, T_SURVEY_YEAR)) %>%
  ungroup() %>%
  select(join_id, EPtrust_ESS)

survey_means_ESS_NP_1 <- trust_survey_data %>% srvyr::as_survey(weights = weights) %>%
  filter(NP_trust_harmonised != 88) %>%
  filter(T_SURVEY_NAME == "ESS") %>%
  group_by(T_SURVEY_ID, T_SURVEY_COUNTRY, T_SURVEY_YEAR) %>%
  summarise(NPtrust_ESS = srvyr::survey_mean(NP_trust_harmonised, na.rm = T)) %>%
  mutate(join_id = glue::glue(T_SURVEY_COUNTRY, T_SURVEY_YEAR)) %>%
  ungroup() %>%
  select(join_id, NPtrust_ESS)

survey_means_EB_NP_1 <- trust_survey_data %>% srvyr::as_survey(weights = weights) %>%
  filter(NP_trust_harmonised != 88) %>%
  filter(T_SURVEY_NAME == "EB") %>%
  group_by(T_SURVEY_ID, T_SURVEY_COUNTRY, T_SURVEY_YEAR) %>%
  summarise(NPtrust_EB = srvyr::survey_mean(NP_trust_harmonised, na.rm = T)) %>%
  mutate(join_id = glue::glue(T_SURVEY_COUNTRY, T_SURVEY_YEAR)) %>%
  ungroup() %>%
  select(join_id, NPtrust_EB)

#plot

plot_x1 <- inner_join(survey_means_1, survey_means_EP_ESS_1, by = c("join_id")) %>%
  inner_join(survey_means_ESS_NP_1, by = c("join_id")) %>%
  inner_join(survey_means_EB_NP_1, by = c("join_id")) %>%
  select(-join_id) %>% 
  pivot_longer(4:7, names_to = c("variable", "survey"), names_sep = "_", values_to = "trust") %>%
  mutate(year = as.numeric(T_SURVEY_YEAR), country = sjlabelled::remove_all_labels(T_SURVEY_COUNTRY)) %>%
  ungroup() %>%
  select(country, year, survey, variable, trust) %>%
  pivot_wider(names_from = survey, values_from = trust) %>%
  ggplot(aes(x = EB, y = ESS, group = variable, fill = variable)) + 
  geom_point(aes(shape = variable, alpha = ifelse(variable == "EPtrust", "1", "2"))) + 
  geom_line(aes(color = variable, linetype = variable),
            stat = "smooth",
              alpha = 0.8,
              linetype = "solid", 
              method = "lm", 
              se = FALSE) +
  ggpubr::stat_cor(aes(color = variable, label = ..r.label..), 
                   method = c("pearson"), 
                   cor.coef.name = "r",
                   label.y = c(0.25, 0.6),
                   label.x = c(0.6, 0.15),
                   r.accuracy = 0.001,
                   output.type = "text") +
  scale_alpha_manual(values = c(0.9, 0.5), breaks = c("1", "2")) +
  scale_shape_manual(values = c(22, 21), 
                     labels = c("Trust in the European Parliament", "Trust in the National parliament")) +
  scale_fill_manual(values = c("#56B4E9", "#D55E00"), 
                    labels = c("Trust in the European Parliament", "Trust in the National parliament")) +
  scale_color_manual(values = c("#56B4E9", "#D55E00"), 
                    labels = c("Trust in the European Parliament", "Trust in the National parliament")) +
  scale_x_continuous(breaks = c(0, 0.25, 0.5, 0.75, 1),
                     labels = c(0, 0.25, 0.5, 0.75, 1)) +
  scale_y_continuous(breaks = c(0, 0.25, 0.5, 0.75, 1),
                     labels = c(0, 0.25, 0.5, 0.75, 1)) +
  coord_cartesian(xlim = c(0, 1), ylim = c(0, 1)) +
  guides(shape = guide_legend("Type of trust measured", 
                              label.position = "right",
                              reverse = T),
         fill = guide_legend("Type of trust measured", 
                             label.position = "right",
                             reverse = T),
         label = FALSE,
         color = FALSE,
         linetype = FALSE,
         alpha = FALSE) +
  facet_wrap(.~year, nrow = 2) +
  xlab("Eurobarometer: country-level mean-aggregates (rescaled: [0,1])") +
  ylab("ESS: country-level mean-aggregates (rescaled: [0,1])") +
  theme_bw() + 
  theme(text = element_text(colour = "black"),
        legend.position = "bottom",
        legend.direction = "horizontal",
        legend.text = element_text(size = 9), 
        legend.title = element_text(size = 9, face = "bold"),
        axis.title.y = element_text(size = 9, face = "bold"),
        axis.title.x = element_text(size = 9, face = "bold"),
        legend.margin = margin(-1, -1, -1, -1, unit = "pt"),
        axis.text = element_text(color = "black", size = 9, face = "bold"),
        panel.spacing.x = unit(2, "pt"),
        panel.spacing.y = unit(2, "pt"),
        strip.background = element_rect(fill = "gray98"),
        strip.text = element_text(size = 9, face = "bold")) 

#facet editing

  egg::tag_facet(plot_x1,
                 x = Inf, y = Inf, 
                 open = "", close = "",
                 hjust = 1.6,
                 vjust = 2.7,
                 tag_pool = seq(2004, 2018, by = 2))

 #DATA TRANSFORMATIONS
  
  prop_EP_DK_h1a <- trust_survey_data %>%
    filter(!is.na(EP_trust_harmonised)) %>%
    group_by(T_SURVEY_ID, T_SURVEY_NAME, T_SURVEY_COUNTRY, T_SURVEY_YEAR) %>%
    count(EP_trust_harmonised, wt = weights) %>%
    group_by(T_SURVEY_ID) %>%
    mutate(prop_EP =  n/sum(n)) %>%
    filter(EP_trust_harmonised == 88) %>%
    mutate(T_SURVEY_COUNTRY = countrycode::countrycode(sjlabelled::remove_all_labels(T_SURVEY_COUNTRY), 
                                                       origin = "iso2c", 
                                                       destination = "country.name")) %>%
    select(T_SURVEY_ID, T_SURVEY_NAME, T_SURVEY_COUNTRY, T_SURVEY_YEAR, prop_EP)

  prop_NP_DK_h1a <- trust_survey_data %>%
    filter(!is.na(NP_trust_harmonised)) %>%
    group_by(T_SURVEY_ID, T_SURVEY_NAME, T_SURVEY_COUNTRY, T_SURVEY_YEAR) %>%
    count(NP_trust_harmonised, wt = weights) %>%
    group_by(T_SURVEY_ID) %>%
    mutate(prop_NP =  n/sum(n)) %>%
    filter(NP_trust_harmonised == 88) %>%
    mutate(T_SURVEY_COUNTRY = countrycode::countrycode(sjlabelled::remove_all_labels(T_SURVEY_COUNTRY), 
                                                       origin = "iso2c", 
                                                       destination = "country.name")) %>%
    select(T_SURVEY_ID, prop_NP)

pqr <-  inner_join(prop_EP_DK_h1a, prop_NP_DK_h1a, by = c("T_SURVEY_ID")) 

pqr1 <- pqr[ , 2:6] %>%
    select(T_SURVEY_COUNTRY, T_SURVEY_YEAR, T_SURVEY_NAME, prop_EP, prop_NP) %>%
    pivot_longer(cols = 4:5, names_to = "key", values_to = "value") %>%
    unite(3:4, col = "name", sep = "*") %>%
    pivot_wider(names_from = name, values_from = value) 

#t_tests

ttest1 <- t.test(pqr1$`EB*prop_EP`, pqr1$`ESS*prop_EP`, paired = T, alternative = "greater", conf.level = 0.95) %>% broom::tidy() 

ttest2 <- t.test(pqr1$`EB*prop_NP`, pqr1$`ESS*prop_NP`, paired = T, alternative = "greater", conf.level = 0.95) %>% broom::tidy()

ttest3 <- t.test(pqr1$`EB*prop_EP`, pqr1$`EB*prop_NP`, paired = T, alternative = "greater", conf.level = 0.95) %>% broom::tidy()

ttest4 <- t.test(pqr1$`ESS*prop_EP`, pqr1$`ESS*prop_NP`, paired = T, alternative = "greater", conf.level = 0.95) %>% broom::tidy()

#flexTable

column <- c("EB_EP_trust - ESS_EP_trust", "EB_NP_trust - ESS_NP_trust", "EB_EP_trust - EB_NP_trust", "ESS_EP_trust - ESS_NP_trust")

rbind(ttest1, ttest2, ttest3, ttest4) %>% cbind(column) %>% select(column, estimate, statistic, p.value, method, alternative) %>% 
  rename("Mean differences" = column) %>% as_tibble() %>%
  qflextable() %>%
  theme_booktabs() %>%
  align_nottext_col(align = "center") %>% 
  colformat_double(j = 2:3, digits = 3)

H2aH2b

#H2ah2b

rty <- inner_join(survey_means_1, survey_means_EP_ESS_1, by = c("join_id")) %>%
  inner_join(survey_means_ESS_NP_1, by = c("join_id")) %>%
  inner_join(survey_means_EB_NP_1, by = c("join_id")) %>%
  select(-join_id) %>%
  mutate(EP_abs_diff = abs(EPtrust_EB - EPtrust_ESS),
         NP_abs_diff = abs(NPtrust_EB - NPtrust_ESS))

#ttests

ttestx <- t.test(rty$EP_abs_diff, mu = 0, alternative = "greater", conf.level = 0.95) %>% broom::tidy()

ttesty <- t.test(rty$NP_abs_diff, mu = 0, alternative = "greater", conf.level = 0.95) %>% broom::tidy()

ttestz <- t.test(rty$EP_abs_diff, rty$NP_abs_diff, paired = T, alternative = "greater", conf.level = 0.95) %>% broom::tidy() 

#flexTable

column1 <- c("EP_abs_difference - 0", "NP_abs_difference - 0", "EP_abs_difference - NP_abs_difference")

rbind(ttestx, ttesty, ttestz) %>% cbind(column1) %>% select(column1, estimate, statistic, p.value, method, alternative) %>% 
  rename("Mean differences" = column1) %>% as_tibble() %>%
  qflextable() %>%
  theme_booktabs() %>%
  align_nottext_col(align = "center") %>% 
  colformat_double(j = 2:3, digits = 3)

H2c

inner_join(survey_means_1, survey_means_EP_ESS_1, by = c("join_id")) %>%
  inner_join(survey_means_ESS_NP_1, by = c("join_id")) %>%
  inner_join(survey_means_EB_NP_1, by = c("join_id")) %>%
  select(-join_id) %>% 
  rename("Survey year" = T_SURVEY_YEAR) %>%
  group_by(`Survey year`) %>%
  summarise(cor_pearson_ep = cor(EPtrust_EB, EPtrust_ESS, method = "pearson"),
            cor_spearman_ep = cor(EPtrust_EB, EPtrust_ESS, method = "spearman"),
            cor_pearson_np = cor(NPtrust_EB, NPtrust_ESS, method = "pearson"),
            cor_spearman_np = cor(NPtrust_EB, NPtrust_ESS, method = "spearman"), .groups = "drop") %>%
  as_tibble() %>%
  qflextable() %>%
  theme_booktabs() %>%
  align_nottext_col(align = "center") %>% 
  colformat_double(digits = 3)