# Create Interaction Term Plot for Presentation


####
create_interaction_term_plot <- function(ols_summary, treatment_labels, ord, unit, down, up) {
  alpha <- 0.1
  z_value <- qnorm(1 - alpha / 2)
  
  plot_data <- summary(ols_summary)
  plot_data <- as.data.frame(plot_data$coefficients)
  plot_data$ME <- z_value * plot_data$`Std. Error`
  plot_data <- rownames_to_column(plot_data, "Coefficient")
  
  plot_data <- plot_data %>% filter(str_detect(Coefficient, "Treatment"))
  
  plot_data$Coefficient <- treatment_labels
  
  plot <- ggplot(data = plot_data) +
    geom_bar(aes(x = factor(Coefficient, levels=c(ord)), y = Estimate, fill = Coefficient), stat = "identity", position = 'dodge', width = 0.5, alpha = 0.7) +
    geom_errorbar(aes(x = Coefficient, ymin = Estimate - ME, ymax = Estimate + ME), width = 0.3, position = position_dodge(0.8)) +
    scale_x_discrete(guide = guide_axis(angle = 0)) +
    guides(fill = "none") +
    coord_cartesian(ylim=c(down, up)) +
    xlab("Treatment Group") +
    ylab(paste0(unit))
  
  return(plot)
}


case_A_labels <- c("Treated", "Voluntary Treated")
case_C_labels <- c("No Info 2", "Text 1", "Text 2", "Video 1", "Video 2")
case_C_labels_re <- c("Text 1", "Text 2", "Video 1", "Video 2", "No Info 2")

plot_interview_A <- create_interaction_term_plot(ols_time_spent_control_A, case_A_labels, case_A_labels,
                                                 "Interview Time in Seconds", -250, 380)
plot_interview_C <- create_interaction_term_plot(ols_time_spent_control_C, case_C_labels, case_C_labels_re,
                                                 "Interview Time in Seconds", -250, 380)

plot_cc_A <- create_interaction_term_plot(ols_time_cc_control_A, case_A_labels, case_A_labels,
                                          "Mean Choice Card Time in Seconds", -5, 5)
plot_cc_C <- create_interaction_term_plot(ols_time_cc_control_C, case_C_labels, case_C_labels_re,
                                          "Mean Choice Card Time in Seconds", -5, 5)

plot_mani_A <- create_interaction_term_plot(ols_percentage_correct_control_A, case_A_labels, case_A_labels,
                                            "Percentage of Correct Quiz Statements", -5, 15)
plot_mani_C <- create_interaction_term_plot(ols_percentage_correct_control_C, case_C_labels, case_C_labels_re,
                                            "Percentage of Correct Quiz Statements", -5, 15)

plot_cons_A <- create_interaction_term_plot(conseq_model_control_A, case_A_labels, case_A_labels,
                                            "Consequentiality Score", -0.5, 0.8)
plot_cons_C <- create_interaction_term_plot(conseq_model_control_C, case_C_labels, case_C_labels_re,
                                            "Consequentiality Score", -0.5, 0.8)

plot_opt_A <- create_interaction_term_plot(ols_opt_out_control_A, case_A_labels, case_A_labels,
                                           "Number of Opt-out Choices", -1.5, 1)
plot_opt_C <- create_interaction_term_plot(ols_opt_out_control_C, case_C_labels, case_C_labels_re,
                                           "Number of Opt-out Choices", -1.5, 1)