Chapter 4 AMCE’s

Average Marginal Component Effects of turnout level, majority size, and outcome favorability.

if(!require("broom")){install.packages("broom");  library(broom)}
if(!require(descr2)) {devtools::install_github("mikajoh/descr2"); library(descr2)}
if(!require("forcats")){install.packages("forcats");  library(forcats)}
if(!require("ggplot2")){install.packages("ggplot2");  library(ggplot2)}
if(!require("haven")){install.packages("haven");  library(haven)}
if(!require("here")){install.packages("here");  library(here)}
if(!require("kableExtra")){install.packages("kableExtra");  library(kableExtra)}
if(!require("knitr")){install.packages("knitr");  library(knitr)}
if(!require("lmtest")){install.packages("lmtest");  library(lmtest)}
if(!require("sandwich")){install.packages("sandwich");  library(sandwich)}
if(!require("tidyverse")){install.packages("tidyverse");  library(tidyverse)}

knitr::opts_chunk$set(echo = FALSE, knitr.kable.NA = "", cache = FALSE, warning = FALSE, message = FALSE, error = TRUE, echo = FALSE)

#load data
d <- read_sav("Data/EIPS2017-data/eips-wiggle-pooled.sav")

#Function for saving output
ggsave2 <- function(filename, ...) {
  for (format in c(".pdf", ".png")) ggsave(filename = paste0("output/", filename, format), ...)
}


## Functions for calculating AMCE's.
source("wiggle-utils.R")

## Theme for ggplot2.
theme_m <- function(...) {
  theme(text = element_text(size = 9,
                            colour = "black"),
        axis.text = element_text(size = 7,
                                 colour = "black"),
        axis.title = element_text(size = 8,
                                  colour = "black"),
        axis.line = element_line(),
        axis.ticks = element_line(colour = "black"),
        axis.ticks.length = unit(1, "mm"),
        plot.margin = unit(c(1, 5, 1, 1), "mm"),
        panel.spacing.x = unit(7.5, "mm"),
        panel.spacing.y = unit(2.5, "mm"),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        strip.background = element_blank(),
        strip.text = element_text(size = 7,
                                  face = "bold"),
        strip.text.y = element_text(angle = 0, face = "bold.italic"),
        legend.background = element_blank(),
        legend.key = element_blank(),
        legend.position = "bottom",
        ...)
}

add_labels <- function(amce) {
  
  labels <- data.frame(
    treatment = c(
      "ref_turnout",
      "ref_majoritysize",
      "ref_winningside",
      "ref_outcome"
    ),
    treatment_label = c(
      "Turnout",
      "Majority size",
      "Winning side",
      "Outcome favorability"
    ),
    stringsAsFactors = FALSE
  )
  
  amce <- amce[rev(order(amce$value)), ]
  amce$value <- paste0("plain('", as.character(amce$value), "')")
  amce$value <- factor(amce$value, levels = unique(amce$value))
  add_data <- labels[labels$treatment %in% amce$treatment, ]
  names(add_data)[2] <- "value"
  add_data$value <- gsub("\n", " ", as.character(add_data$value))
  add_data$value <- paste0("bold('", add_data$value, "')")
  for (var in c("value_order", "estimate", "std_error", "statistic", "p_value")) {
    add_data[[var]] <- NA
  }
  add_data$value_order <- -Inf
  if (any(!(names(amce) %in% names(add_data)))) {
    for (.c in names(amce)[!(names(amce) %in% names(add_data))]) {
      add_data[[.c]] <- amce[[.c]][1]
    }
  }
  if (any(!(names(add_data) %in% names(amce)))) {
    for (.c in names(add_data)[!(names(add_data) %in% names(amce))]) {
      add_data <- add_data[, -c(which(names(add_data) == .c))]
    }
  }
  amce <- rbind(amce, add_data)
  amce <- amce[order(amce$treatment, amce$value_order), ]
  return(amce)
}

eips <- eips_raw 

eips <- eips %>%
  mutate(
    ref_turnout = lvls_reorder(factor(ref_turnout),
                               c(5, 4:1)),
    ref_majoritysize = lvls_reorder(factor(ref_majoritysize),
                                    c(4, 3:1)),
    ref_winner = lvls_reorder(factor(ref_winner), 
                              c(2, 1, 3)),
    ref_outcome =  lvls_reorder(factor(ref_outcome),
                                c(2, 1, 3))
    
  ) 

## Write prepared data to file.
save(eips, file = "output/eips-wiggle-prepared-data.RData")

## Load the prepared data.
load("output/eips-wiggle-prepared-data.RData")

## Error in grDevices::pdf(file = filename, ..., version = version): cannot open file 'output/Wiggle-eips-main.pdf'
Table 4.1: Average Marginal Component Effects
Treatment Value Estimate Std.error t.statistic
Turnout Not shown 0.0000000 0.0000000 NA
Turnout 85% 0.0306519 0.0108327 2.8295801
Turnout 53% -0.0688915 0.0108193 -6.3674695
Turnout 47% -0.1346479 0.0108526 -12.4070260
Turnout 35% -0.1910309 0.0107699 -17.7374077
Majority size Not shown 0.0000000 0.0000000 NA
Majority size 70% 0.0088295 0.0099387 0.8883981
Majority size 55% -0.0410149 0.0099020 -4.1420701
Majority size 51% -0.0447053 0.0093469 -4.7828866
Outcome favorability Not shown 0.0000000 0.0000000 NA
Outcome favorability Favorable outcome 0.1065260 0.0087365 12.1932532
Outcome favorability Unfavorable outcome -0.0884784 0.0088335 -10.0162493