---
title: "Replication Code for CCES Survey Data Analysis"
subtitle: "Ideological Asymmetries and the Determinants of Politically-Motivated Reasoning (American Journal of Political Science, 2020)"
author: "Brian Guay & Christopher Johnston"
date: "6/18/2020"
output:
  html_document:
    number_sections: yes
    toc: yes

---

```{r knitr setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Introduction

This markdown file contains the code necessary to run the replication of Study 1 with the data collected from the 2018 Cooperative Congressional Election Study (CCES) survey. The two data sets below contain the unmatched (cces_unmatched_data.rds) and matched (cces_matched_data.rds) versions of the 2018 CCES data, containing items from both the common content and Duke University module components of the CCES. We use common content items to measure issue positions and respondent demographics. Documentation for the common content is available on the [2018 CCES dataverse](https://doi.org/10.7910/DVN/ZSBZ7K/WZWCZ1) and specific common content items are described in detail below. We use items from the Duke University module of the CCES for the experiment. 

When compiling in markdown, verify that this markdown file is saved in the same folder as the other replication. R Markdown will set the working directory to this folder by default. When running the code line-by-line, set the working directory to the same location as these materials are located.

# Import Data & Setup

```{r setup, warning = F, echo = T, message = F, include = T}

# Create output log
sink(file = "cces_code_log.txt") 

# Clear environment
rm(list = ls())

# Install required packages
install.packages(c("foreign", "survey", "knitr", "psych"))

# Load packages
library(foreign) # for loading .sav files
library(survey) # for running weighted analysis of survey data
library(knitr)  # for compiling markdown
library(psych)  # for calculating measures of internal consistency

# Load unmatched data
data <- readRDS(file = "cces_unmatched_data.rds")

# Create folder to store figures
dir.create("cces_figures")

# Load post functions (see README file for description)
source("post.R")
source("postSim.R")

# Function to make colors transparent
makeTransparent<-function(someColor, alpha=100) 
{
  newColor<-col2rgb(someColor)
  apply(newColor, 2, function(curcoldata){rgb(red=curcoldata[1], green=curcoldata[2],
                                              blue=curcoldata[3],alpha=alpha, 
                                              maxColorValue=255)})
}

# Set number of simulations for post estimation
nsims <-  10000

# Set seed
set.seed(3109)

```

# Data Cleaning

## Issue Positions

### Gun Control

"On the issue of gun regulation, are you for or against each of the following proposals?"

1. Background checks for all sales, including at gun shows and over the Internet [CC18_320a]
2. Ban assault rifles [CC18_320c]
3. Make it easier for people to obtain a concealed-carry gun permit [CC18_320d]

```{r}

# note: In all cases 1 = support, 2 = opposed, 8 = skipped, and 9 = not asked.

gun_vars <- c("CC18_320a", "CC18_320c", "CC18_320d") 

# recode each item such that 0 = liberal position and 1 = conservative position

# 1
table(data$CC18_320a, useNA = "always")
data$gun_1 <- with(data, ifelse(CC18_320a == "For", 0, 1))
table(data$CC18_320a, data$gun_1, useNA = "always")

# 2
table(data$CC18_320c, useNA = "always")
data$gun_2 <- with(data, ifelse(CC18_320c == "For", 0, 1))
table(data$CC18_320c, data$gun_2, useNA = "always")

# 3
table(data$CC18_320d, useNA = "always")
data$gun_3 <- with(data, ifelse(CC18_320d == "For", 1, 0))
table(data$CC18_320d, data$gun_3, useNA = "always")

new_gun_vars <- paste("gun", 1:3, sep = "_")

psych::alpha(data[,new_gun_vars])

data$gun_mean <- rowMeans(data[,new_gun_vars], na.rm=T)

```

### Abortion

"Do you support or oppose each of the following proposals?"

1. Always allow a woman to obtain an abortion as a matter of choice [CC18_321a]
2. Permit abortion ONLY in case of rape, incest or when the woman's life is in danger [CC18_321b]
3. Ban abortions after the 20th week of pregnancy [CC18_321c]
4. Allow employers to decline coverage of abortions in insurance plans [CC18_321d]
5. Prohibit the expenditure of funds authorized or appropriated by federal law for any abortion. [CC18_321e]
6. Make abortions illegal in all circumstances [CC18_321f]

```{r}

# Recode each item such that 0 = liberal position and 1 = conservative position

abort_vars <- c("CC18_321a", "CC18_321b", "CC18_321c", "CC18_321d", 
                "CC18_321e", "CC18_321f")

# 1 
table(data$CC18_321a, useNA = "always")
data$abort_1 <- ifelse(data$CC18_321a == "Support", 0, 1)
table(data$CC18_321a,data$abort_1, useNA = "always")

# 2
table(data$CC18_321b, useNA = "always")
data$abort_2 <- ifelse(data$CC18_321b == "Support", 1, 0)
table(data$CC18_321b, data$abort_2, useNA = "always")

# 3
table(data$CC18_321c, useNA = "always")
data$abort_3 <- ifelse(data$CC18_321c == "Support", 1, 0)
table(data$CC18_321c, data$abort_3, useNA = "always")

# 4
table(data$CC18_321d, useNA = "always")
data$abort_4 <- ifelse(data$CC18_321d == "Support", 1, 0)
table(data$CC18_321d, data$abort_4, useNA = "always")

# 5
table(data$CC18_321e, useNA = "always")
data$abort_5 <- ifelse(data$CC18_321e == "Support", 1, 0)
table(data$CC18_321e, data$abort_5, useNA = "always")

# 6
table(data$CC18_321f, useNA = "always")
data$abort_6 <- ifelse(data$CC18_321f == "Support", 1, 0)
table(data$CC18_321f,data$abort_6, useNA = "always")

new_abort_vars <- paste("abort", 1:6, sep = "_")

psych::alpha(data[,new_abort_vars])

data$abort_mean <- rowMeans(data[,new_abort_vars], na.rm=T)

```

### Immigration

CCES common content has 8 immigration issue position questions. We exclude questions that ask about supporting a compromise, because it is not clear how these compromises map onto a pro-anti immigration spectrum (e.g., "Grant legal status to DACA children, spend $25 billion to build the border  wall, and reduce legal immigration by eliminating the visa lottery and ending family-based migration"). We also excluded items for which only half the sample was assigned to respond.

"What do you think the U.S. government should do about immigration?  Do you support or oppose each of the following?"

- Increase spending on border security by $25 billion, including building a wall between the U.S. and Mexico. (CC18_322a)
- Provide legal status to children of immigrants who are already in the United  States and were brought to the United States by their parents. Provide these children the option of citizenship in 10 years if they meet citizenship requirements and commit no crimes. (DACA). (CC18_322b)
- Reduce legal immigration by eliminating the visa lottery and ending family-based migration. (CC18_322c_new)
- Withhold federal funds from any local police department that does not report to the federal government anyone they identify as an illegal immigrant. (CC18_322c)
- Send to prison any person who has been deported from the United States and reenters the United States. (CC18_322f)

```{r}
# recode each item such that 0 = liberal position and 1 = conservative position

#1
table(data$CC18_322a, useNA = "always")
data$imm_1 <- ifelse(data$CC18_322a == "Support", 1, 0)
table(data$CC18_322a, data$imm_1, useNA = "always")

#2
table(data$CC18_322b, useNA = "always")
data$imm_2 <- ifelse(data$CC18_322b == "Support", 0, 1)
table(data$CC18_322b, data$imm_2, useNA = "always")

#3
table(data$CC18_322c_new, useNA = "always")
data$imm_3 <- ifelse(data$CC18_322c_new == "Support", 1, 0)
table(data$CC18_322c_new, data$imm_3, useNA = "always")

#4
table(data$CC18_322c, useNA = "always")
data$imm_4 <- ifelse(data$CC18_322c == "Support", 1, 0)
table(data$CC18_322c, data$imm_4, useNA = "always")

#5
table(data$CC18_322f, useNA = "always")
data$imm_5 <- ifelse(data$CC18_322f == "Support", 1, 0)
table(data$CC18_322f, data$imm_5, useNA = "always")

new_imm_vars <- paste("imm", 1:5, sep = "_")

psych::alpha(data[,new_imm_vars])

data$imm_mean <- rowMeans(data[,new_imm_vars], na.rm=T)

```

## Experimental Variables

Participants participated in a 2 (congenial finding, uncongenial finding) X 3 (gun control, immigration, abortion) experiment. Respondents were assigned a value for the *cond_direction* variable, which indicated the direction of the finding they were presented with. Respondents were also assigned a value for the *cond_issue* variable, which indicated the issue they were provided evidence about.

### Create Congeniality Variable

To determine whether we evidence provided to respondents was congenial with their prior beliefs, two pieces of information are taken into account: whether the evidence supports a liberal or conservative position and the respondent's prior beliefs. 

Because one of the only differences between conditions in Experiment 2 is the words "increase" and "decrease", which determine the direction of the evidence provided to respondents, the variable used to assign experimental condition (*cond_direction* variable, below) has two levels: increase and decrease. 

For abortion, 'increase' indicates evidence in the liberal direction (restricting abortion increases abortion-related maternal deaths). For gun control and immigration, 'increase' indicates evidence in the conservative direction (restricting open carry increases the crime rate, sanctuary cities increase crime). 

Below we create a variable ('finding_lr'), which indicates whether the finding presented to respondents supported the liberal or conservative position. 

```{r}

# original variable indicating increase/decrease condition assignment
data$cond_direction <- data$DKU439_treat # increase, decrease
table(data$cond_direction, useNA = "always")

# original variable indicating issue condition assignment
data$cond_issue <- data$DKU440_treat # guns, imm, abort
table(data$cond_issue, useNA = "always")

# sample size in each condition
with(data, table(cond_issue, cond_direction))
with(data, prop.table(table(cond_issue, cond_direction)))

# create finding direction variable
data$finding_direction <- 
  ifelse(data$cond_issue == "guns" & data$cond_direction == "increase", "right", 
  ifelse(data$cond_issue == "guns" & data$cond_direction == "decrease", "left", 
  ifelse(data$cond_issue == "imm" & data$cond_direction == "increase", "right", 
  ifelse(data$cond_issue == "imm" & data$cond_direction == "decrease", "left",  
  ifelse(data$cond_issue == "abort" & data$cond_direction == "increase", "left",
  ifelse(data$cond_issue == "abort" & data$cond_direction == "decrease", "right", 
         "foo")))))) 

# note: NAs didn't take post-election survey
#data[is.na(data$finding_direction), c("cond_issue", "cond_direction", "tookpost")]

# create variable indicating whether finding is liberal (vs. conservative).
data$finding_lr <- ifelse(data$finding_direction == "left", 1, 
                          ifelse(data$finding_direction == "right", 0, NA))

```

To determine whether the finding reported was congenial or uncongenial, we must take each respondents' prior beliefs into account. We operationalize prior beliefs in three separate ways: self-reported ideology, party ID, and positions on the issue at hand. 

```{r}

# Note: ideology, party ID, and issue positions are all coded so that high values indicate
# right (vs. left) views. 

# Congeniality Variable (Operationalization = Issue Position) ----------------------------

# create variable for mean response on issue position items for assigned issue
data$issue_mean <- ifelse(data$cond_issue == "guns", data$gun_mean, 
                          ifelse(data$cond_issue == "abort", data$abort_mean, 
                                 ifelse(data$cond_issue == "imm", data$imm_mean, NA)))

# create binary version (1 = conservative)
data$issue_mean_binaryCon <- ifelse(data$issue_mean > .50, 1, 
                                 ifelse(data$issue_mean < .50, 0, NA))

# verify
with(data, table(issue_mean, issue_mean_binaryCon, useNA = "always"))

# create congeniality variable
data$congenial_issue <- ifelse(data$finding_direction == "right", data$issue_mean, 
                               ifelse(data$finding_direction == "left", 
                                      abs(1-data$issue_mean), NA))

# Congeniality Variable (Operationalization = Party ID) ----------------------------

# recode party ID variable
data$pid7_original <- data$pid7

data$pid7[data$pid7 == "Not sure"] <- NA

data$pid7 <- as.numeric(data$pid7)

# verify
table(data$pid7, data$pid7_original)

# indicator for republican affiliation (vs. democrat affiliation)
data$rep_dem <- ifelse(data$pid7 > 4, 1, 
                       ifelse(data$pid7 < 4, 0, NA))

# verify
with(data, table(pid7, rep_dem, useNA = "always"))

# create congeniality variable
data$congenial_pid <- ifelse(data$finding_direction == "right", data$pid7, 
                             ifelse(data$finding_direction == "left", abs(8-data$pid7), 
                                    1111))

# Congeniality Variable (Operationalization = Ideology) ----------------------------------

# recode ideology variable
data$ideo7 <- as.numeric(data$CC18_334A.1)
table(data$CC18_334A, data$ideo7)
data$ideo7[data$ideo7 == 8] <- NA 

# verify
table(data$CC18_334A.1, data$ideo7)

# create binary variable indicating conservative ideology (vs. liberal)
data$con_lib <- ifelse(data$ideo7 > 4, 1, 
                       ifelse(data$ideo7 < 4, 0, NA))

# verify
with(data, table(con_lib, ideo7, useNA = "always"))

# create congeniality variable
data$congenial_ideo <- ifelse(data$finding_direction == "right", data$ideo7, 
                             ifelse(data$finding_direction == "left", abs(8-data$ideo7), 
                                    1111))

```

### Experimental Outcomes

Each respondent answered 2 questions:
  - Is sample size sufficient? (7 = sample is too small)
  - Can we make causal claim?  (7 = cannot make causal claim)
  
In each case, high values reflect a critical/skeptical interpretation of the evidence.
  
```{r}

# guns sample DV
data$guns_sample <- as.numeric(data$DKU441)
table(data$guns_sample, data$DKU441)

# guns causal DV
data$guns_causal <- as.numeric(data$DKU442)
table(data$guns_causal, data$DKU442)

# abort sample DV
data$abort_sample <- as.numeric(data$DKU443)
table(data$abort_sample, data$DKU443)

# abort causal DV
data$abort_causal <- as.numeric(data$DKU444)
table(data$abort_causal, data$DKU444)

# imm sample DV
data$imm_sample <- as.numeric(data$DKU447)
table(data$imm_sample, data$DKU447)

# imm causal DV
data$imm_causal <- as.numeric(data$DKU448)
table(data$imm_causal, data$DKU448)

causal_vars <- c(paste(c("guns", "abort", "imm"), "causal", sep = "_"))
sample_vars <- c(paste(c("guns", "abort", "imm"), "sample", sep = "_"))

head(data[,causal_vars])
head(data[,sample_vars])

# combine outcomes from each experimental condition
data$goodCausal.0 <- ifelse(data$cond_issue == "guns", data$guns_causal, 
                     ifelse(data$cond_issue == "abort", data$abort_causal, 
                     ifelse(data$cond_issue == "imm", data$imm_causal, NA)))

data$goodSample.0 <- ifelse(data$cond_issue == "guns", data$guns_sample, 
                     ifelse(data$cond_issue == "abort", data$abort_sample, 
                     ifelse(data$cond_issue == "imm", data$imm_sample, NA)))

# Reverse code, such that high values = positive judgments of sample size/causal claim
data$goodCausal <- 8-data$goodCausal.0
table(data$goodCausal, data$goodCausal.0)
data$goodCausal_uns <- data$goodCausal

data$goodSample <- 8-data$goodSample.0
table(data$goodSample, data$goodSample.0)
data$goodSample_uns <- data$goodSample

# Scale DVs
data$goodSample <- as.numeric(scale(data$goodSample))
data$goodCausal <- as.numeric(scale(data$goodCausal))

cor(data$goodSample, data$goodCausal, use = "complete.obs") # .37

```


# Unmatched Analysis (Binary Left/Right Measures)

```{r}

# Run Models -----------------------------------------------------------------------------

# Ideology
ss_ideo_lib_ub <- lm(goodSample ~ con_lib + cond_issue, data[data$finding_lr == 1,])
ss_ideo_con_ub <- lm(goodSample ~ con_lib + cond_issue, data[data$finding_lr == 0,])

cc_ideo_lib_ub <- lm(goodCausal ~ con_lib + cond_issue, data[data$finding_lr == 1,])
cc_ideo_con_ub <- lm(goodCausal ~ con_lib + cond_issue, data[data$finding_lr == 0,])

# Party ID
ss_pid_lib_ub <- lm(goodSample ~ rep_dem + cond_issue, data[data$finding_lr == 1,])
ss_pid_con_ub <- lm(goodSample ~ rep_dem + cond_issue, data[data$finding_lr == 0,])

cc_pid_lib_ub <- lm(goodCausal ~ rep_dem + cond_issue, data[data$finding_lr == 1,])
cc_pid_con_ub <- lm(goodCausal ~ rep_dem + cond_issue, data[data$finding_lr == 0,])

# Issue Mean
ss_iss_lib_ub <- lm(goodSample ~ issue_mean_binaryCon + cond_issue,
                    data[data$finding_lr == 1,])
ss_iss_con_ub <- lm(goodSample ~ issue_mean_binaryCon + cond_issue, 
                    data[data$finding_lr == 0,])

cc_iss_lib_ub <- lm(goodCausal ~ issue_mean_binaryCon + cond_issue, 
                    data[data$finding_lr == 1,])
cc_iss_con_ub <- lm(goodCausal ~ issue_mean_binaryCon + cond_issue, 
                    data[data$finding_lr == 0,])

# Post-Estimation ------------------------------------------------------------------------
p_ss_ideo_lib_ub <- post(model = ss_ideo_lib_ub, x1name = "con_lib", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5)
p_ss_ideo_con_ub <- post(model = ss_ideo_con_ub, x1name = "con_lib", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5)

p_cc_ideo_lib_ub <- post(model = cc_ideo_lib_ub, x1name = "con_lib", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5)
p_cc_ideo_con_ub <- post(model = cc_ideo_con_ub, x1name = "con_lib", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5)

p_ss_pid_lib_ub <-  post(model = ss_pid_lib_ub, x1name = "rep_dem", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5)
p_ss_pid_con_ub <-  post(model = ss_pid_con_ub, x1name = "rep_dem", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5)

p_cc_pid_lib_ub <-  post(model = cc_pid_lib_ub, x1name = "rep_dem", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5)
p_cc_pid_con_ub <-  post(model = cc_pid_con_ub, x1name = "rep_dem", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5)

p_ss_iss_lib_ub <-  post(model = ss_iss_lib_ub, x1name = "issue_mean_binaryCon", 
                         x1vals = c(0, 1), n.sims = nsims, digits = 5)
p_ss_iss_con_ub <-  post(model = ss_iss_con_ub, x1name = "issue_mean_binaryCon", 
                         x1vals = c(0, 1), n.sims = nsims, digits = 5)

p_cc_iss_lib_ub <-  post(model = cc_iss_lib_ub, x1name = "issue_mean_binaryCon", 
                         x1vals = c(0, 1), n.sims = nsims, digits = 5)
p_cc_iss_con_ub <-  post(model = cc_iss_con_ub, x1name = "issue_mean_binaryCon", 
                         x1vals = c(0, 1), n.sims = nsims, digits = 5)

# Create Figure --------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------

# Sample Size Outcome ----------------------------------

pdf("cces_figures/cces_ub_1.pdf", height = 6, width = 8)
par(mfrow = c(1,3), oma = c(.1,5,1,0), mar = c(4.1,1,1.1,1))

# Issue Positions
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.4, .6),
     xlab = "Issue Position",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 2)

points(0:1, p_ss_iss_lib_ub@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 4)
points(0:1, p_ss_iss_con_ub@est[1:2,1], pch = 15, col = "firebrick2", cex = 4)

points(0:1, p_ss_iss_lib_ub@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 4, lty = 2)
points(0:1, p_ss_iss_con_ub@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 4, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_iss_lib_ub@est[1:2,2], 
         y1 =  p_ss_iss_lib_ub@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 7)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_iss_con_ub@est[1:2,2], 
         y1 =  p_ss_iss_con_ub@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 7)


legend("topleft", legend = c("Left", "Right"),
    #   lty = , lwd = 4, 
       col = c("deepskyblue3", "firebrick2"),
       pch = c(16,15), 
       bty = "n", cex = 2, title = "Evidence:")


# Ideology
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.4, .6),
     xlab = "Ideology",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 2)

points(0:1, p_ss_ideo_lib_ub@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 4)
points(0:1, p_ss_ideo_con_ub@est[1:2,1], pch = 15, col = "firebrick2", cex = 4)

points(0:1, p_ss_ideo_lib_ub@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 4, lty = 2)
points(0:1, p_ss_ideo_con_ub@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 4, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_ideo_lib_ub@est[1:2,2], 
         y1 =  p_ss_ideo_lib_ub@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 7)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_ideo_con_ub@est[1:2,2], 
         y1 =  p_ss_ideo_con_ub@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 7)

mtext("Sample Size is Sufficient (in SDs)", side = 2, outer = T, padj = -2, cex = 1.7)

# Party ID
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.4, .6),
     xlab = "Party ID",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 2)

points(0:1, p_ss_pid_lib_ub@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 4)
points(0:1, p_ss_pid_con_ub@est[1:2,1], pch = 15, col = "firebrick2", cex = 4)

points(0:1, p_ss_pid_lib_ub@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 4, lty = 2)
points(0:1, p_ss_pid_con_ub@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 4, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_pid_lib_ub@est[1:2,2], 
         y1 =  p_ss_pid_lib_ub@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 7)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_pid_con_ub@est[1:2,2], 
         y1 =  p_ss_pid_con_ub@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 7)

dev.off()

# Causal Claim Outcome -------------------------------------------------------------------

pdf("cces_figures/cces_ub_2.pdf", height = 6, width = 8)
par(mfrow = c(1,3), oma = c(.1,5,1,0), mar = c(4.1,1,1.1,1))

# Issue Positions
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.7, .7),
     xlab = "Issue Position",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 1.5)

points(0:1, p_cc_iss_lib_ub@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 4)
points(0:1, p_cc_iss_con_ub@est[1:2,1], pch = 15, col = "firebrick2", cex = 4)

points(0:1, p_cc_iss_lib_ub@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 4, lty = 2)
points(0:1, p_cc_iss_con_ub@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 4, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_iss_lib_ub@est[1:2,2], 
         y1 =  p_cc_iss_lib_ub@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 7)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_iss_con_ub@est[1:2,2], 
         y1 =  p_cc_iss_con_ub@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 7)

# Ideology
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.7, .7),
     xlab = "Ideology",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 1.5)

points(0:1, p_cc_ideo_lib_ub@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 4)
points(0:1, p_cc_ideo_con_ub@est[1:2,1], pch = 15, col = "firebrick2", cex = 4)

points(0:1, p_cc_ideo_lib_ub@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 4, lty = 2)
points(0:1, p_cc_ideo_con_ub@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 4, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_ideo_lib_ub@est[1:2,2], 
         y1 =  p_cc_ideo_lib_ub@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 7)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_ideo_con_ub@est[1:2,2], 
         y1 =  p_cc_ideo_con_ub@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 7)

mtext("Can Make Causal Claim (in SDs)", side = 2, outer = T, padj = -2, cex = 1.7)

# Party ID
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.7, .7),
     xlab = "Party ID",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 1.5)

points(0:1, p_cc_pid_lib_ub@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 4)
points(0:1, p_cc_pid_con_ub@est[1:2,1], pch = 15, col = "firebrick2", cex = 4)

points(0:1, p_cc_pid_lib_ub@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 4, lty = 2)
points(0:1, p_cc_pid_con_ub@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 4, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_pid_lib_ub@est[1:2,2], 
         y1 =  p_cc_pid_lib_ub@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 7)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_pid_con_ub@est[1:2,2], 
         y1 =  p_cc_pid_con_ub@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 7)

dev.off()

# Difference in Differences --------------------------------------------------------------

# Sample Size Outcome
d_ss_iss_ub <- (p_ss_iss_con_ub@sims[,ncol(p_ss_iss_con_ub@sims)] -
             p_ss_iss_lib_ub@sims[,ncol(p_ss_iss_lib_ub@sims)]) -
            (p_ss_iss_lib_ub@sims[,1] -
             p_ss_iss_con_ub@sims[,1])

d_ss_ideo_ub <- (p_ss_ideo_con_ub@sims[,ncol(p_ss_ideo_con_ub@sims)] -
              p_ss_ideo_lib_ub@sims[,ncol(p_ss_ideo_lib_ub@sims)]) -
             (p_ss_ideo_lib_ub@sims[,1] -
              p_ss_ideo_con_ub@sims[,1])

d_ss_pid_ub <- (p_ss_pid_con_ub@sims[,ncol(p_ss_pid_con_ub@sims)] -
             p_ss_pid_lib_ub@sims[,ncol(p_ss_pid_lib_ub@sims)]) -
            (p_ss_pid_lib_ub@sims[,1] -
             p_ss_pid_con_ub@sims[,1])

# Causal Claim Outcome
d_cc_iss_ub <- (p_cc_iss_con_ub@sims[,ncol(p_cc_iss_con_ub@sims)] -
             p_cc_iss_lib_ub@sims[,ncol(p_cc_iss_lib_ub@sims)]) -
            (p_cc_iss_lib_ub@sims[,1] -
             p_cc_iss_con_ub@sims[,1])

d_cc_ideo_ub <- (p_cc_ideo_con_ub@sims[,ncol(p_cc_ideo_con_ub@sims)] -
              p_cc_ideo_lib_ub@sims[,ncol(p_cc_ideo_lib_ub@sims)]) -
             (p_cc_ideo_lib_ub@sims[,1] -
              p_cc_ideo_con_ub@sims[,1])

d_cc_pid_ub <- (p_cc_pid_con_ub@sims[,ncol(p_cc_pid_con_ub@sims)] -
             p_cc_pid_lib_ub@sims[,ncol(p_cc_pid_lib_ub@sims)]) -
            (p_cc_pid_lib_ub@sims[,1] -
             p_cc_pid_con_ub@sims[,1])
 
# Create table of differences and means

# as dataframe
d_names_ub <- as.data.frame(cbind(d_ss_iss_ub, d_ss_ideo_ub, d_ss_pid_ub,
                                  d_cc_iss_ub, d_cc_ideo_ub, d_cc_pid_ub))

dtbl_ub <- data.frame(name = colnames(d_names_ub),
                   mean = apply(d_names_ub, 2, mean),
                   ci.lo = apply(d_names_ub, 2, quantile, probs = .025),
                   ci.hi = apply(d_names_ub, 2, quantile, probs = .975))

pdf("cces_figures/cces_ub_3.pdf", height = 6, width = 8)

# Sample Size Outcome
par(mfrow = c(1,1), oma = c(.1,6,1,0), mar = c(4.1,1,1.1,1), xpd = NA)

x_vals <- c(1,3,5)

# Experiment 2 (SS)
plot(1,1, col = "white", bty = "n",
     xlim = c(0,7), ylim = c(-1,1),
     xlab = "Sample Size",
     ylab = "", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

points(x_vals, dtbl_ub$mean[1:3], pch = c(15, 16, 17),
       cex = 4)
segments(x0 = x_vals, x1 = x_vals,
         y0 = dtbl_ub$ci.lo[1:3], y1 = dtbl_ub$ci.hi[1:3],
         lwd = 5, col = makeTransparent("black", 150))

# cross-plot axis
axis(1, at = 0:6, lwd.tick=0, labels=FALSE)

segments(x0 = 0, x1 = 0,
         y0 = -1.08, y1 = -1.12)
segments(x0 = 6, x1 = 6,
         y0 = -1.08, y1 = -1.12)

# horizontal line at y = 0
segments(x0 = -17.5, x1 = 6,
         y0 = 0, y1 = 0, lty = 2)

dev.off()

# Causal Claim Outcome
pdf("cces_figures/cces_ub_4.pdf", height = 6, width = 8)

par(mfrow = c(1,1), oma = c(.1,6,1,0), mar = c(4.1,1,1.1,1), xpd = NA)
# Experiment 2 (CC)
plot(1,1, col = "white", bty = "n",
     xlim = c(0,7), ylim = c(-1,1),
     xlab = "Causal Claim",
     ylab = "", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

points(x_vals, dtbl_ub$mean[4:6], pch = c(15, 16, 17),
       cex = 4)
segments(x0 = x_vals, x1 = x_vals,
         y0 = dtbl_ub$ci.lo[4:6], y1 = dtbl_ub$ci.hi[4:6],
         lwd = 5, col = makeTransparent("black", 150) )

legend("bottomleft", legend = c("Issue Position", "Ideology", "Party ID"),
       pch = c(15, 16, 17),
       bty = "n", cex = 2)

# cross-plot axis
axis(1, at = 0:6, lwd.tick=0, labels=FALSE)

segments(x0 = 0, x1 = 0,
         y0 = -1.08, y1 = -1.12)
segments(x0 = 6, x1 = 6,
         y0 = -1.08, y1 = -1.12)

# horizontal line at y = 0
segments(x0 = 0, x1 = 6,
         y0 = 0, y1 = 0, lty = 2)

mtext("(Conservative - Liberal)", side = 2, outer = T, padj = -1.8, cex = 1.7)

dev.off()
                   
```

# Unmatched Analysis (Continuous Left/Right Measures)

```{r}

# Run Models -----------------------------------------------------------------------------

# Ideology
ss_ideo_lib_uc <- lm(goodSample ~ ideo7 + cond_issue, data[data$finding_lr == 1,])
ss_ideo_con_uc <- lm(goodSample ~ ideo7 + cond_issue, data[data$finding_lr == 0,])

cc_ideo_lib_uc <- lm(goodCausal ~ ideo7 + cond_issue, data[data$finding_lr == 1,])
cc_ideo_con_uc <- lm(goodCausal ~ ideo7 + cond_issue, data[data$finding_lr == 0,])

# Party ID
ss_pid_lib_uc <- lm(goodSample ~ pid7 + cond_issue, data[data$finding_lr == 1,])
ss_pid_con_uc <- lm(goodSample ~ pid7 + cond_issue, data[data$finding_lr == 0,])

cc_pid_lib_uc <- lm(goodCausal ~ pid7 + cond_issue, data[data$finding_lr == 1,])
cc_pid_con_uc <- lm(goodCausal ~ pid7 + cond_issue, data[data$finding_lr == 0,])

# Issue Mean
ss_iss_lib_uc <- lm(goodSample ~ issue_mean + cond_issue, data[data$finding_lr == 1,])
ss_iss_con_uc <- lm(goodSample ~ issue_mean + cond_issue, data[data$finding_lr == 0,])

cc_iss_lib_uc <- lm(goodCausal ~ issue_mean + cond_issue, data[data$finding_lr == 1,])
cc_iss_con_uc <- lm(goodCausal ~ issue_mean + cond_issue, data[data$finding_lr == 0,])

# Post-Estimation ------------------------------------------------------------------------
p_ss_ideo_lib_uc <- post(model = ss_ideo_lib_uc, x1name = "ideo7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5)
p_ss_ideo_con_uc <- post(model = ss_ideo_con_uc, x1name = "ideo7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5)

p_cc_ideo_lib_uc <- post(model = cc_ideo_lib_uc, x1name = "ideo7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5)
p_cc_ideo_con_uc <- post(model = cc_ideo_con_uc, x1name = "ideo7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5)

p_ss_pid_lib_uc <-  post(model = ss_pid_lib_uc, x1name = "pid7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5)
p_ss_pid_con_uc <-  post(model = ss_pid_con_uc, x1name = "pid7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5)

p_cc_pid_lib_uc <-  post(model = cc_pid_lib_uc, x1name = "pid7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5)
p_cc_pid_con_uc <-  post(model = cc_pid_con_uc, x1name = "pid7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5)

p_ss_iss_lib_uc <-  post(model = ss_iss_lib_uc, x1name = "issue_mean", seq(0,1,.1), 
                         n.sims = nsims, digits = 5)
p_ss_iss_con_uc <-  post(model = ss_iss_con_uc, x1name = "issue_mean", seq(0,1,.1), 
                         n.sims = nsims, digits = 5)

p_cc_iss_lib_uc <-  post(model = cc_iss_lib_uc, x1name = "issue_mean", seq(0,1,.1), 
                         n.sims = nsims, digits = 5)
p_cc_iss_con_uc <-  post(model = cc_iss_con_uc, x1name = "issue_mean", seq(0,1,.1), 
                         n.sims = nsims, digits = 5)


# Create Figure --------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------

# Sample Size Plot -----------------------------------------------------------------------
pdf("cces_figures/cces_uc_1.pdf", height = 6, width = 8)
par(mfrow = c(1,3), oma = c(.1,5,1,0), mar = c(4.1,1,1,1))

# Issue Position

plot(1,1, col = "white", bty = "n",
     xlim = c(0,1), ylim = c(-1, 1),
     xlab = "Issue Position",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7)

points(seq(0,1,.1), p_ss_iss_lib_uc@est[1:11,1], pch = 16, 
       col = makeTransparent("deepskyblue3"), lty = 2,  type = "l", lwd = 4)

points(seq(0,1,.1), p_ss_iss_con_uc@est[1:11,1], pch = 15, 
       col = "firebrick2", type = "l", lwd = 4)

polygon(c(seq(0,1,.1), rev(seq(0,1,.1))),
        c(p_ss_iss_lib_uc@est[1:11,2], rev(p_ss_iss_lib_uc@est[1:11,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(0,1,.1), rev(seq(0,1,.1))),
        c(p_ss_iss_con_uc@est[1:11,2], rev(p_ss_iss_con_uc@est[1:11,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

legend(0, 1, legend = c("Left", "Right"),
       lty = c(2,1), lwd = 4, col = c(makeTransparent("deepskyblue3"), "firebrick2"),
       bty = "n", cex = 2, title = "Evidence:")

# Ideology
plot(1,1, col = "white", bty = "n",
     xlim = c(1,7), ylim = c(-1, 1),
     xlab = "Ideology",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7)

points(1:7, p_ss_ideo_lib_uc@est[1:7,1], pch = 16, 
       col = makeTransparent("deepskyblue3"), lty = 2,  
       type = "l", lwd = 4)

points(1:7, p_ss_ideo_con_uc@est[1:7,1], pch = 15, 
       col = "firebrick2", type = "l", lwd = 4)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_ss_ideo_lib_uc@est[1:7,2], rev(p_ss_ideo_lib_uc@est[1:7,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_ss_ideo_con_uc@est[1:7,2], rev(p_ss_ideo_con_uc@est[1:7,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

mtext("Sample Size is Sufficient (in SDs)", side = 2, outer = T, padj = -2, cex = 1.7)


# Party ID
plot(1,1, col = "white", bty = "n",
     xlim = c(1,7), ylim = c(-1, 1),
     xlab = "Party ID",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7)

points(1:7, p_ss_pid_lib_uc@est[1:7,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       lty = 2,  type = "l", lwd = 4)

points(1:7, p_ss_pid_con_uc@est[1:7,1], pch = 15, col = "firebrick2", type = "l", lwd = 4)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_ss_pid_lib_uc@est[1:7,2], rev(p_ss_pid_lib_uc@est[1:7,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_ss_pid_con_uc@est[1:7,2], rev(p_ss_pid_con_uc@est[1:7,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

dev.off()

# Causal Claim Plot ----------------------------------------------------------------------

pdf("cces_figures/cces_uc_2.pdf", height = 6, width = 8)

par(mfrow = c(1,3), oma = c(.1,5,1,0), mar = c(4.1,1,1,1))

# Issue Mean
seq(0,1,.1)
plot(1,1, col = "white", bty = "n",
     xlim = c(0,1), ylim = c(-1, 1),
     xlab = "Issue Position",
     ylab = "Can Make Causal Claim (in SDs)", cex.lab = 2, cex.axis = 1.7)
length(seq(0,1,.1))
points(seq(0,1,.1), p_cc_iss_lib_uc@est[1:11,1], pch = 16, 
       col = makeTransparent("deepskyblue3"), lty = 2,  type = "l", lwd = 4)

points(seq(0,1,.1), p_cc_iss_con_uc@est[1:11,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 4)

polygon(c(seq(0,1,.1), rev(seq(0,1,.1))),
        c(p_cc_iss_lib_uc@est[1:11,2], rev(p_cc_iss_lib_uc@est[1:11,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(0,1,.1), rev(seq(0,1,.1))),
        c(p_cc_iss_con_uc@est[1:11,2], rev(p_cc_iss_con_uc@est[1:11,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

# Ideology
plot(1,1, col = "white", bty = "n",
     xlim = c(1,7), ylim = c(-1, 1),
     xlab = "Ideology",
     ylab = "Can Make Causal Claim (in SDs)", cex.lab = 2, cex.axis = 1.7)

points(1:7, p_cc_ideo_lib_uc@est[1:7,1], pch = 16, 
       col = makeTransparent("deepskyblue3"), 
       lty = 2,  type = "l", lwd = 4)

points(1:7, p_cc_ideo_con_uc@est[1:7,1], pch = 15, 
       col = "firebrick2", type = "l", lwd = 4)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_cc_ideo_lib_uc@est[1:7,2], rev(p_cc_ideo_lib_uc@est[1:7,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_cc_ideo_con_uc@est[1:7,2], rev(p_cc_ideo_con_uc@est[1:7,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

mtext("Can Make Causal Claim (in SDs)", side = 2, outer = T, padj = -2, cex = 1.7)

# Party ID
plot(1,1, col = "white", bty = "n",
     xlim = c(1,7), ylim = c(-1, 1),
     xlab = "Party ID",
     ylab = "Can Make Causal Claim (in SDs)", cex.lab = 2, cex.axis = 1.7)

points(1:7, p_cc_pid_lib_uc@est[1:7,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       lty = 2,  type = "l", lwd = 4)

points(1:7, p_cc_pid_con_uc@est[1:7,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 4)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_cc_pid_lib_uc@est[1:7,2], rev(p_cc_pid_lib_uc@est[1:7,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_cc_pid_con_uc@est[1:7,2], rev(p_cc_pid_con_uc@est[1:7,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

dev.off()

# Difference in Differences --------------------------------------------------------------

# Sample Size
d_ss_iss_uc <- (p_ss_iss_con_uc@sims[,ncol(p_ss_iss_con_uc@sims)] - 
                p_ss_iss_lib_uc@sims[,ncol(p_ss_iss_lib_uc@sims)]) - 
               (p_ss_iss_lib_uc@sims[,1] -
                p_ss_iss_con_uc@sims[,1])

d_ss_ideo_uc <- (p_ss_ideo_con_uc@sims[,ncol(p_ss_ideo_con_uc@sims)] - 
                 p_ss_ideo_lib_uc@sims[,ncol(p_ss_ideo_lib_uc@sims)]) - 
                (p_ss_ideo_lib_uc@sims[,1] - 
                 p_ss_ideo_con_uc@sims[,1])

d_ss_pid_uc <- (p_ss_pid_con_uc@sims[,ncol(p_ss_pid_con_uc@sims)] -  
                p_ss_pid_lib_uc@sims[,ncol(p_ss_pid_lib_uc@sims)]) - 
               (p_ss_pid_lib_uc@sims[,1] -
                p_ss_pid_con_uc@sims[,1])

# Causal Claim
d_cc_iss_uc <- (p_cc_iss_con_uc@sims[,ncol(p_cc_iss_con_uc@sims)] -  
                p_cc_iss_lib_uc@sims[,ncol(p_cc_iss_lib_uc@sims)]) - 
               (p_cc_iss_lib_uc@sims[,1] - 
                p_cc_iss_con_uc@sims[,1])

d_cc_ideo_uc  <- (p_cc_ideo_con_uc@sims[,ncol(p_cc_ideo_con_uc@sims)] -  
                  p_cc_ideo_lib_uc@sims[,ncol(p_cc_ideo_lib_uc@sims)]) - 
                 (p_cc_ideo_lib_uc@sims[,1] - 
                  p_cc_ideo_con_uc@sims[,1])

d_cc_pid_uc  <- (p_cc_pid_con_uc@sims[,ncol(p_cc_pid_con_uc@sims)] -  
                 p_cc_pid_lib_uc@sims[,ncol(p_cc_pid_lib_uc@sims)]) - 
                (p_cc_pid_lib_uc@sims[,1] -
                 p_cc_pid_con_uc@sims[,1])

# as dataframe
d_names_uc <- as.data.frame(cbind(d_ss_iss_uc, d_ss_ideo_uc, d_ss_pid_uc, 
                                  d_cc_iss_uc, d_cc_ideo_uc, d_cc_pid_uc))

dtbl_uc <- data.frame(name = colnames(d_names_uc), 
                   mean = apply(d_names_uc, 2, mean), 
                   ci.lo = apply(d_names_uc, 2, quantile, probs = .025),
                   ci.hi = apply(d_names_uc, 2, quantile, probs = .975))

# Difference in Differences Plot ---------------------------------------------------------

pdf("cces_figures/cces_uc_3.pdf", height = 6, width = 8)
par(mfrow = c(1,3), oma = c(.1,6,1,0), mar = c(4.1,1,1,1), xpd = NA)

x_vals <- c(1,3,5)

# Experiment 1
plot(1,1, col = "white", bty = "n",
     xlim = c(0,7), ylim = c(-1,1),
     xlab = "",
     ylab = "", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

points(x_vals, dtbl_uc$mean[1:3], pch = c(15, 16, 17), 
       cex = 4, col = "white")
segments(x0 = x_vals, x1 = x_vals, 
         y0 = dtbl_uc$ci.lo[1:3], y1 = dtbl_uc$ci.hi[1:3], 
         lwd = 5, col = makeTransparent("white", 150))

legend("topleft", legend = c("Issue Position", "Ideology", "Party ID"),
       pch = c(15, 16, 17),
       bty = "n", cex = 3)

mtext("(Conservative - Liberal)", side = 2, outer = T, padj = -1.8, cex = 1.7)

# Experiment 2 (SS)
plot(1,1, col = "white", bty = "n",
     xlim = c(0,7), ylim = c(-1,1),
     xlab = "Sample Size",
     ylab = "", cex.lab = 2, cex.axis = 1.7, yaxt = "n", xaxt = "n")

points(x_vals, dtbl_uc$mean[1:3], pch = c(15, 16, 17), 
       cex = 4)
segments(x0 = x_vals, x1 = x_vals, 
         y0 = dtbl_uc$ci.lo[1:3], y1 = dtbl_uc$ci.hi[1:3], 
         lwd = 5, col = makeTransparent("black", 150))

# Experiment 2 (CC)
plot(1,1, col = "white", bty = "n",
     xlim = c(0,7), ylim = c(-1,1),
     xlab = "Causal Claim",
     ylab = "", cex.lab = 2, cex.axis = 1.7, yaxt = "n", xaxt = "n")

points(x_vals, dtbl_uc$mean[4:6], pch = c(15, 16, 17), 
       cex = 4)
segments(x0 = x_vals, x1 = x_vals, 
         y0 = dtbl_uc$ci.lo[4:6], y1 = dtbl_uc$ci.hi[4:6], 
         lwd = 5, col = makeTransparent("black", 150) )

# cross-plot axis
axis(1, at = -17:6, lwd.tick=0, labels=FALSE)

segments(x0 = -17, x1 = -17, 
         y0 = -1.08, y1 = -1.12)
segments(x0 = 6, x1 = 6, 
         y0 = -1.08, y1 = -1.12)

segments(x0 = -17.5, x1 = 6, 
         y0 = 0, y1 = 0, lty = 2)

dev.off()
```


# Create Matched Dataset

```{r setup II, warning = F, echo = T, message = F, include = T}

# Load raw matched data
#data_matched_original <- read.spss("CCES18_DKU_OUTPUT_II.sav", to.data.frame=TRUE)
#data_matched_original <- read.csv("cces_matched_data.csv")
data_matched_original <- readRDS(file = "cces_matched_data.rds")

class(data_matched_original)
dim(data_matched_original)

# Create vector of caseids in the matched data
matched_ids <- data_matched_original$caseid
length(matched_ids)

# Subset cleaned dataset to include only respondents in matched sample
data_matched <- data[data$caseid %in% matched_ids,]

dim(data_matched)

```

# Matched Analysis (Binary Left/Right Measures)

```{r}

# Run Models -----------------------------------------------------------------------------

# Ideology
ss_ideo_lib_mb <- lm(goodSample ~ con_lib + cond_issue, 
                     data_matched[data_matched$finding_lr == 1,])
ss_ideo_con_mb <- lm(goodSample ~ con_lib + cond_issue, 
                     data_matched[data_matched$finding_lr == 0,])

cc_ideo_lib_mb <- lm(goodCausal ~ con_lib + cond_issue, 
                     data_matched[data_matched$finding_lr == 1,])
cc_ideo_con_mb <- lm(goodCausal ~ con_lib + cond_issue, 
                     data_matched[data_matched$finding_lr == 0,])

# Party ID
ss_pid_lib_mb <- lm(goodSample ~ rep_dem + cond_issue, 
                    data_matched[data_matched$finding_lr == 1,])
ss_pid_con_mb <- lm(goodSample ~ rep_dem + cond_issue, 
                    data_matched[data_matched$finding_lr == 0,])

cc_pid_lib_mb <- lm(goodCausal ~ rep_dem + cond_issue, 
                    data_matched[data_matched$finding_lr == 1,])
cc_pid_con_mb <- lm(goodCausal ~ rep_dem + cond_issue, 
                    data_matched[data_matched$finding_lr == 0,])

# Issue Mean
ss_iss_lib_mb <- lm(goodSample ~ issue_mean_binaryCon + cond_issue, 
                    data_matched[data_matched$finding_lr == 1,])
ss_iss_con_mb <- lm(goodSample ~ issue_mean_binaryCon + cond_issue, 
                    data_matched[data_matched$finding_lr == 0,])

cc_iss_lib_mb <- lm(goodCausal ~ issue_mean_binaryCon + cond_issue, 
                    data_matched[data_matched$finding_lr == 1,])
cc_iss_con_mb <- lm(goodCausal ~ issue_mean_binaryCon + cond_issue, 
                    data_matched[data_matched$finding_lr == 0,])

# Post-Estimation ------------------------------------------------------------------------
p_ss_ideo_lib_mb <- post(model = ss_ideo_lib_mb, x1name = "con_lib", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5)
p_ss_ideo_con_mb <- post(model = ss_ideo_con_mb, x1name = "con_lib", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5)

p_cc_ideo_lib_mb <- post(model = cc_ideo_lib_mb, x1name = "con_lib", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5)
p_cc_ideo_con_mb <- post(model = cc_ideo_con_mb, x1name = "con_lib", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5)

p_ss_pid_lib_mb <-  post(model = ss_pid_lib_mb, x1name = "rep_dem", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5)
p_ss_pid_con_mb <-  post(model = ss_pid_con_mb, x1name = "rep_dem", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5)

p_cc_pid_lib_mb <-  post(model = cc_pid_lib_mb, x1name = "rep_dem", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5)
p_cc_pid_con_mb <-  post(model = cc_pid_con_mb, x1name = "rep_dem", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5)

p_ss_iss_lib_mb <-  post(model = ss_iss_lib_mb, x1name = "issue_mean_binaryCon", 
                         x1vals = c(0, 1), n.sims = nsims, digits = 5)
p_ss_iss_con_mb <-  post(model = ss_iss_con_mb, x1name = "issue_mean_binaryCon", 
                         x1vals = c(0, 1), n.sims = nsims, digits = 5)

p_cc_iss_lib_mb <-  post(model = cc_iss_lib_mb, x1name = "issue_mean_binaryCon", 
                         x1vals = c(0, 1), n.sims = nsims, digits = 5)
p_cc_iss_con_mb <-  post(model = cc_iss_con_mb, x1name = "issue_mean_binaryCon", 
                         x1vals = c(0, 1), n.sims = nsims, digits = 5)

# Create Figure --------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------

# Sample Size Outcome ----------------------------------

pdf("cces_figures/cces_mb_1.pdf", height = 6, width = 8)
par(mfrow = c(1,3), oma = c(.1,5,1,0), mar = c(4.1,1,1.1,1))

# Issue Positions
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.5, .5),
     xlab = "Issue Position",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 2)

points(0:1, p_ss_iss_lib_mb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 3)
points(0:1, p_ss_iss_con_mb@est[1:2,1], pch = 15, col = "firebrick2", cex = 3)

points(0:1, p_ss_iss_lib_mb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 2, lty = 2)
points(0:1, p_ss_iss_con_mb@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 2, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_iss_lib_mb@est[1:2,2], 
         y1 =  p_ss_iss_lib_mb@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 5)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_iss_con_mb@est[1:2,2], 
         y1 =  p_ss_iss_con_mb@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 5)

legend(.5, 1, legend = c("Left", "Right"),
       lty = 1, lwd = 4, col = c(makeTransparent("deepskyblue3"), "firebrick2"),
       pch = 16, 
       bty = "n", cex = 2, title = "Evidence:")

# Ideology
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.5, .5),
     xlab = "Ideology",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 2)

points(0:1, p_ss_ideo_lib_mb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 3)
points(0:1, p_ss_ideo_con_mb@est[1:2,1], pch = 15, col = "firebrick2", cex = 3)

points(0:1, p_ss_ideo_lib_mb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 2, lty = 2)
points(0:1, p_ss_ideo_con_mb@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 2, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_ideo_lib_mb@est[1:2,2], 
         y1 =  p_ss_ideo_lib_mb@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 5)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_ideo_con_mb@est[1:2,2], 
         y1 =  p_ss_ideo_con_mb@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 5)

mtext("Sample Size is Sufficient (in SDs)", side = 2, outer = T, padj = -2, cex = 1.7)

# Party ID
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.5, .5),
     xlab = "Party ID",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 2)

points(0:1, p_ss_pid_lib_mb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 3)
points(0:1, p_ss_pid_con_mb@est[1:2,1], pch = 15, col = "firebrick2", cex = 3)

points(0:1, p_ss_pid_lib_mb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 2, lty = 2)
points(0:1, p_ss_pid_con_mb@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 2, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_pid_lib_mb@est[1:2,2], 
         y1 =  p_ss_pid_lib_mb@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 5)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_pid_con_mb@est[1:2,2], 
         y1 =  p_ss_pid_con_mb@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 5)

dev.off()

# Causal Claim Outcome -------------------------------------------------------------------

pdf("cces_figures/cces_mb_2.pdf", height = 6, width = 8)
par(mfrow = c(1,3), oma = c(.1,5,1,0), mar = c(4.1,1,1.1,1))

# Issue Positions
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.7, .7),
     xlab = "Issue Position",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 1.5)

points(0:1, p_cc_iss_lib_mb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 3)
points(0:1, p_cc_iss_con_mb@est[1:2,1], pch = 15, col = "firebrick2", cex = 3)

points(0:1, p_cc_iss_lib_mb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 2, lty = 2)
points(0:1, p_cc_iss_con_mb@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 2, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_iss_lib_mb@est[1:2,2], 
         y1 =  p_cc_iss_lib_mb@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 5)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_iss_con_mb@est[1:2,2], 
         y1 =  p_cc_iss_con_mb@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 5)

# Ideology
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.7, .7),
     xlab = "Ideology",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 1.5)

points(0:1, p_cc_ideo_lib_mb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 3)
points(0:1, p_cc_ideo_con_mb@est[1:2,1], pch = 15, col = "firebrick2", cex = 3)

points(0:1, p_cc_ideo_lib_mb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 2, lty = 2)
points(0:1, p_cc_ideo_con_mb@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 2, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_ideo_lib_mb@est[1:2,2], 
         y1 =  p_cc_ideo_lib_mb@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 5)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_ideo_con_mb@est[1:2,2], 
         y1 =  p_cc_ideo_con_mb@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 5)

mtext("Can Make Causal Claim (in SDs)", side = 2, outer = T, padj = -2, cex = 1.7)

# Party ID
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.7, .7),
     xlab = "Party ID",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 1.5)

points(0:1, p_cc_pid_lib_mb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 3)
points(0:1, p_cc_pid_con_mb@est[1:2,1], pch = 15, col = "firebrick2", cex = 3)

points(0:1, p_cc_pid_lib_mb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 2, lty = 2)
points(0:1, p_cc_pid_con_mb@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 2, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_pid_lib_mb@est[1:2,2], 
         y1 =  p_cc_pid_lib_mb@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 5)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_pid_con_mb@est[1:2,2], 
         y1 =  p_cc_pid_con_mb@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 5)

dev.off()

# Difference in Differences --------------------------------------------------------------

# Sample Size Outcome
d_ss_iss_mb <- (p_ss_iss_con_mb@sims[,ncol(p_ss_iss_con_mb@sims)] -
             p_ss_iss_lib_mb@sims[,ncol(p_ss_iss_lib_mb@sims)]) -
            (p_ss_iss_lib_mb@sims[,1] -
             p_ss_iss_con_mb@sims[,1])

d_ss_ideo_mb <- (p_ss_ideo_con_mb@sims[,ncol(p_ss_ideo_con_mb@sims)] -
              p_ss_ideo_lib_mb@sims[,ncol(p_ss_ideo_lib_mb@sims)]) -
             (p_ss_ideo_lib_mb@sims[,1] -
              p_ss_ideo_con_mb@sims[,1])

d_ss_pid_mb <- (p_ss_pid_con_mb@sims[,ncol(p_ss_pid_con_mb@sims)] -
             p_ss_pid_lib_mb@sims[,ncol(p_ss_pid_lib_mb@sims)]) -
            (p_ss_pid_lib_mb@sims[,1] -
             p_ss_pid_con_mb@sims[,1])

# Causal Claim Outcome
d_cc_iss_mb <- (p_cc_iss_con_mb@sims[,ncol(p_cc_iss_con_mb@sims)] -
             p_cc_iss_lib_mb@sims[,ncol(p_cc_iss_lib_mb@sims)]) -
            (p_cc_iss_lib_mb@sims[,1] -
             p_cc_iss_con_mb@sims[,1])

d_cc_ideo_mb <- (p_cc_ideo_con_mb@sims[,ncol(p_cc_ideo_con_mb@sims)] -
              p_cc_ideo_lib_mb@sims[,ncol(p_cc_ideo_lib_mb@sims)]) -
             (p_cc_ideo_lib_mb@sims[,1] -
              p_cc_ideo_con_mb@sims[,1])

d_cc_pid_mb <- (p_cc_pid_con_mb@sims[,ncol(p_cc_pid_con_mb@sims)] -
             p_cc_pid_lib_mb@sims[,ncol(p_cc_pid_lib_mb@sims)]) -
            (p_cc_pid_lib_mb@sims[,1] -
             p_cc_pid_con_mb@sims[,1])
 
# Create table of differences and means

# as dataframe
d_names_mb <- as.data.frame(cbind(d_ss_iss_mb, d_ss_ideo_mb, d_ss_pid_mb,
                                  d_cc_iss_mb, d_cc_ideo_mb, d_cc_pid_mb))

dtbl_mb <- data.frame(name = colnames(d_names_mb),
                   mean = apply(d_names_mb, 2, mean),
                   ci.lo = apply(d_names_mb, 2, quantile, probs = .025),
                   ci.hi = apply(d_names_mb, 2, quantile, probs = .975))

pdf("cces_figures/cces_mb_3.pdf", height = 6, width = 8)

# Sample Size Outcome
par(mfrow = c(1,1), oma = c(.1,6,1,0), mar = c(4.1,1,1.1,1), xpd = NA)

x_vals <- c(1,3,5)

# Experiment 2 (SS)
plot(1,1, col = "white", bty = "n",
     xlim = c(0,7), ylim = c(-1,1),
     xlab = "Sample Size",
     ylab = "", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

points(x_vals, dtbl_mb$mean[1:3], pch = c(15, 16, 17),
       cex = 4)
segments(x0 = x_vals, x1 = x_vals,
         y0 = dtbl_mb$ci.lo[1:3], y1 = dtbl_mb$ci.hi[1:3],
         lwd = 5, col = makeTransparent("black", 150))

# cross-plot axis
axis(1, at = 0:6, lwd.tick=0, labels=FALSE)

segments(x0 = 0, x1 = 0,
         y0 = -1.08, y1 = -1.12)
segments(x0 = 6, x1 = 6,
         y0 = -1.08, y1 = -1.12)

# horizontal line at y = 0
segments(x0 = -17.5, x1 = 6,
         y0 = 0, y1 = 0, lty = 2)

dev.off()

# Causal Claim Outcome
pdf("cces_figures/cces_mb_4.pdf", height = 6, width = 8)

par(mfrow = c(1,1), oma = c(.1,6,1,0), mar = c(4.1,1,1.1,1), xpd = NA)
# Experiment 2 (CC)
plot(1,1, col = "white", bty = "n",
     xlim = c(0,7), ylim = c(-1,1),
     xlab = "Causal Claim",
     ylab = "", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

points(x_vals, dtbl_mb$mean[4:6], pch = c(15, 16, 17),
       cex = 4)
segments(x0 = x_vals, x1 = x_vals,
         y0 = dtbl_mb$ci.lo[4:6], y1 = dtbl_mb$ci.hi[4:6],
         lwd = 5, col = makeTransparent("black", 150) )

legend("bottomleft", legend = c("Issue Position", "Ideology", "Party ID"),
       pch = c(15, 16, 17),
       bty = "n", cex = 2)

# cross-plot axis
axis(1, at = 0:6, lwd.tick=0, labels=FALSE)

segments(x0 = 0, x1 = 0,
         y0 = -1.08, y1 = -1.12)
segments(x0 = 6, x1 = 6,
         y0 = -1.08, y1 = -1.12)

# horizontal line at y = 0
segments(x0 = 0, x1 = 6,
         y0 = 0, y1 = 0, lty = 2)

mtext("(Conservative - Liberal)", side = 2, outer = T, padj = -1.8, cex = 1.7)

dev.off()
                   
```

# Matched Analysis (Continuous Left/Right Measures)

```{r}

# Run Models -----------------------------------------------------------------------------

# Ideology
ss_ideo_lib_mc <- lm(goodSample ~ ideo7 + cond_issue, 
                     data_matched[data_matched$finding_lr == 1,])
ss_ideo_con_mc <- lm(goodSample ~ ideo7 + cond_issue, 
                     data_matched[data_matched$finding_lr == 0,])

cc_ideo_lib_mc <- lm(goodCausal ~ ideo7 + cond_issue, 
                     data_matched[data_matched$finding_lr == 1,])
cc_ideo_con_mc <- lm(goodCausal ~ ideo7 + cond_issue, 
                     data_matched[data_matched$finding_lr == 0,])

# Party ID
ss_pid_lib_mc <- lm(goodSample ~ pid7 + cond_issue, 
                    data_matched[data_matched$finding_lr == 1,])
ss_pid_con_mc <- lm(goodSample ~ pid7 + cond_issue, 
                    data_matched[data_matched$finding_lr == 0,])

cc_pid_lib_mc <- lm(goodCausal ~ pid7 + cond_issue, 
                    data_matched[data_matched$finding_lr == 1,])
cc_pid_con_mc <- lm(goodCausal ~ pid7 + cond_issue, 
                    data_matched[data_matched$finding_lr == 0,])

# Issue Mean
ss_iss_lib_mc <- lm(goodSample ~ issue_mean + cond_issue, 
                    data_matched[data_matched$finding_lr == 1,])
ss_iss_con_mc <- lm(goodSample ~ issue_mean + cond_issue, 
                    data_matched[data_matched$finding_lr == 0,])

cc_iss_lib_mc <- lm(goodCausal ~ issue_mean + cond_issue, 
                    data_matched[data_matched$finding_lr == 1,])
cc_iss_con_mc <- lm(goodCausal ~ issue_mean + cond_issue, 
                    data_matched[data_matched$finding_lr == 0,])

# Post-Estimation ------------------------------------------------------------------------
p_ss_ideo_lib_mc <- post(model = ss_ideo_lib_mc, x1name = "ideo7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5)
p_ss_ideo_con_mc <- post(model = ss_ideo_con_mc, x1name = "ideo7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5)

p_cc_ideo_lib_mc <- post(model = cc_ideo_lib_mc, x1name = "ideo7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5)
p_cc_ideo_con_mc <- post(model = cc_ideo_con_mc, x1name = "ideo7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5)

p_ss_pid_lib_mc <-  post(model = ss_pid_lib_mc, x1name = "pid7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5)
p_ss_pid_con_mc <-  post(model = ss_pid_con_mc, x1name = "pid7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5)

p_cc_pid_lib_mc <-  post(model = cc_pid_lib_mc, x1name = "pid7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5)
p_cc_pid_con_mc <-  post(model = cc_pid_con_mc, x1name = "pid7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5)

p_ss_iss_lib_mc <-  post(model = ss_iss_lib_mc, x1name = "issue_mean", seq(0,1,.1), 
                         n.sims = nsims, digits = 5)
p_ss_iss_con_mc <-  post(model = ss_iss_con_mc, x1name = "issue_mean", seq(0,1,.1), 
                         n.sims = nsims, digits = 5)

p_cc_iss_lib_mc <-  post(model = cc_iss_lib_mc, x1name = "issue_mean", seq(0,1,.1), 
                         n.sims = nsims, digits = 5)
p_cc_iss_con_mc <-  post(model = cc_iss_con_mc, x1name = "issue_mean", seq(0,1,.1), 
                         n.sims = nsims, digits = 5)


# Create Figure --------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------

# Sample Size Plot -----------------------------------------------------------------------
pdf("cces_figures/cces_mc_1.pdf", height = 6, width = 8)
par(mfrow = c(1,3), oma = c(.1,5,1,0), mar = c(4.1,1,1,1))

# Ideology
plot(1,1, col = "white", bty = "n",
     xlim = c(1,7), ylim = c(-1, 1),
     xlab = "Ideology",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7)

points(1:7, p_ss_ideo_lib_mc@est[1:7,1], pch = 16, 
       col = makeTransparent("deepskyblue3"), lty = 2,  
       type = "l", lwd = 4)

points(1:7, p_ss_ideo_con_mc@est[1:7,1], pch = 15, 
       col = "firebrick2", type = "l", lwd = 4)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_ss_ideo_lib_mc@est[1:7,2], rev(p_ss_ideo_lib_mc@est[1:7,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_ss_ideo_con_mc@est[1:7,2], rev(p_ss_ideo_con_mc@est[1:7,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

mtext("Sample Size is Sufficient (in SDs)", side = 2, outer = T, padj = -2, cex = 1.7)

legend(1.5, 1, legend = c("L", "R"),
       lty = c(2,1), lwd = 4, col = c(makeTransparent("deepskyblue3"), "firebrick2"),
       bty = "n", cex = 2, title = "Evidence:")

# Party ID
plot(1,1, col = "white", bty = "n",
     xlim = c(1,7), ylim = c(-1, 1),
     xlab = "Party ID",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7)

points(1:7, p_ss_pid_lib_mc@est[1:7,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       lty = 2,  type = "l", lwd = 4)

points(1:7, p_ss_pid_con_mc@est[1:7,1], pch = 15, col = "firebrick2", type = "l", lwd = 4)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_ss_pid_lib_mc@est[1:7,2], rev(p_ss_pid_lib_mc@est[1:7,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_ss_pid_con_mc@est[1:7,2], rev(p_ss_pid_con_mc@est[1:7,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

# Issue Position

plot(1,1, col = "white", bty = "n",
     xlim = c(0,1), ylim = c(-1, 1),
     xlab = "Issue Position",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7)

points(seq(0,1,.1), p_ss_iss_lib_mc@est[1:11,1], pch = 16, 
       col = makeTransparent("deepskyblue3"), lty = 2,  type = "l", lwd = 4)

points(seq(0,1,.1), p_ss_iss_con_mc@est[1:11,1], pch = 15, 
       col = "firebrick2", type = "l", lwd = 4)

polygon(c(seq(0,1,.1), rev(seq(0,1,.1))),
        c(p_ss_iss_lib_mc@est[1:11,2], rev(p_ss_iss_lib_mc@est[1:11,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(0,1,.1), rev(seq(0,1,.1))),
        c(p_ss_iss_con_mc@est[1:11,2], rev(p_ss_iss_con_mc@est[1:11,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

dev.off()

# Causal Claim Plot ----------------------------------------------------------------------

pdf("cces_figures/cces_mc_2.pdf", height = 6, width = 8)

par(mfrow = c(1,3), oma = c(.1,5,1,0), mar = c(4.1,1,1,1))

# Ideology
plot(1,1, col = "white", bty = "n",
     xlim = c(1,7), ylim = c(-1, 1),
     xlab = "Ideology",
     ylab = "Can Make Causal Claim (in SDs)", cex.lab = 2, cex.axis = 1.7)

points(1:7, p_cc_ideo_lib_mc@est[1:7,1], pch = 16, 
       col = makeTransparent("deepskyblue3"), 
       lty = 2,  type = "l", lwd = 4)

points(1:7, p_cc_ideo_con_mc@est[1:7,1], pch = 15, 
       col = "firebrick2", type = "l", lwd = 4)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_cc_ideo_lib_mc@est[1:7,2], rev(p_cc_ideo_lib_mc@est[1:7,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_cc_ideo_con_mc@est[1:7,2], rev(p_cc_ideo_con_mc@est[1:7,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

mtext("Can Make Causal Claim (in SDs)", side = 2, outer = T, padj = -2, cex = 1.7)

# Party ID
plot(1,1, col = "white", bty = "n",
     xlim = c(1,7), ylim = c(-1, 1),
     xlab = "Party ID",
     ylab = "Can Make Causal Claim (in SDs)", cex.lab = 2, cex.axis = 1.7)

points(1:7, p_cc_pid_lib_mc@est[1:7,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       lty = 2,  type = "l", lwd = 4)

points(1:7, p_cc_pid_con_mc@est[1:7,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 4)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_cc_pid_lib_mc@est[1:7,2], rev(p_cc_pid_lib_mc@est[1:7,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_cc_pid_con_mc@est[1:7,2], rev(p_cc_pid_con_mc@est[1:7,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

# Issue Mean
seq(0,1,.1)
plot(1,1, col = "white", bty = "n",
     xlim = c(0,1), ylim = c(-1, 1),
     xlab = "Issue Position",
     ylab = "Can Make Causal Claim (in SDs)", cex.lab = 2, cex.axis = 1.7)
length(seq(0,1,.1))
points(seq(0,1,.1), p_cc_iss_lib_mc@est[1:11,1], pch = 16, 
       col = makeTransparent("deepskyblue3"), lty = 2,  type = "l", lwd = 4)

points(seq(0,1,.1), p_cc_iss_con_mc@est[1:11,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 4)

polygon(c(seq(0,1,.1), rev(seq(0,1,.1))),
        c(p_cc_iss_lib_mc@est[1:11,2], rev(p_cc_iss_lib_mc@est[1:11,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(0,1,.1), rev(seq(0,1,.1))),
        c(p_cc_iss_con_mc@est[1:11,2], rev(p_cc_iss_con_mc@est[1:11,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

dev.off()

# Difference in Differences --------------------------------------------------------------

# Sample Size
d_ss_iss_mc <- (p_ss_iss_con_mc@sims[,ncol(p_ss_iss_con_mc@sims)] - 
                p_ss_iss_lib_mc@sims[,ncol(p_ss_iss_lib_mc@sims)]) - 
               (p_ss_iss_lib_mc@sims[,1] -
                p_ss_iss_con_mc@sims[,1])

d_ss_ideo_mc <- (p_ss_ideo_con_mc@sims[,ncol(p_ss_ideo_con_mc@sims)] - 
                 p_ss_ideo_lib_mc@sims[,ncol(p_ss_ideo_lib_mc@sims)]) - 
                (p_ss_ideo_lib_mc@sims[,1] - 
                 p_ss_ideo_con_mc@sims[,1])

d_ss_pid_mc <- (p_ss_pid_con_mc@sims[,ncol(p_ss_pid_con_mc@sims)] -  
                p_ss_pid_lib_mc@sims[,ncol(p_ss_pid_lib_mc@sims)]) - 
               (p_ss_pid_lib_mc@sims[,1] -
                p_ss_pid_con_mc@sims[,1])

# Causal Claim
d_cc_iss_mc <- (p_cc_iss_con_mc@sims[,ncol(p_cc_iss_con_mc@sims)] -  
                p_cc_iss_lib_mc@sims[,ncol(p_cc_iss_lib_mc@sims)]) - 
               (p_cc_iss_lib_mc@sims[,1] - 
                p_cc_iss_con_mc@sims[,1])

d_cc_ideo_mc  <- (p_cc_ideo_con_mc@sims[,ncol(p_cc_ideo_con_mc@sims)] -  
                  p_cc_ideo_lib_mc@sims[,ncol(p_cc_ideo_lib_mc@sims)]) - 
                 (p_cc_ideo_lib_mc@sims[,1] - 
                  p_cc_ideo_con_mc@sims[,1])

d_cc_pid_mc  <- (p_cc_pid_con_mc@sims[,ncol(p_cc_pid_con_mc@sims)] -  
                 p_cc_pid_lib_mc@sims[,ncol(p_cc_pid_lib_mc@sims)]) - 
                (p_cc_pid_lib_mc@sims[,1] -
                 p_cc_pid_con_mc@sims[,1])

# as dataframe
d_names_mc <- as.data.frame(cbind(d_ss_iss_mc, d_ss_ideo_mc, d_ss_pid_mc, 
                                  d_cc_iss_mc, d_cc_ideo_mc, d_cc_pid_mc))

dtbl_mc <- data.frame(name = colnames(d_names_mc), 
                   mean = apply(d_names_mc, 2, mean), 
                   ci.lo = apply(d_names_mc, 2, quantile, probs = .025),
                   ci.hi = apply(d_names_mc, 2, quantile, probs = .975))

# Difference in Differences Plot ---------------------------------------------------------

pdf("cces_figures/cces_mc_3.pdf", height = 6, width = 8)
par(mfrow = c(1,3), oma = c(.1,6,1,0), mar = c(4.1,1,1,1), xpd = NA)

x_vals <- c(1,3,5)

# Experiment 1
plot(1,1, col = "white", bty = "n",
     xlim = c(0,7), ylim = c(-1,1),
     xlab = "",
     ylab = "", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

points(x_vals, dtbl_mc$mean[1:3], pch = c(15, 16, 17), 
       cex = 4, col = "white")
segments(x0 = x_vals, x1 = x_vals, 
         y0 = dtbl_mc$ci.lo[1:3], y1 = dtbl_mc$ci.hi[1:3], 
         lwd = 5, col = makeTransparent("white", 150))

legend("topleft", legend = c("Issue Position", "Ideology", "Party ID"),
       pch = c(15, 16, 17),
       bty = "n", cex = 3)

mtext("(Conservative - Liberal)", side = 2, outer = T, padj = -1.8, cex = 1.7)

# Experiment 2 (SS)
plot(1,1, col = "white", bty = "n",
     xlim = c(0,7), ylim = c(-1,1),
     xlab = "Sample Size",
     ylab = "", cex.lab = 2, cex.axis = 1.7, yaxt = "n", xaxt = "n")

points(x_vals, dtbl_mc$mean[1:3], pch = c(15, 16, 17), 
       cex = 4)
segments(x0 = x_vals, x1 = x_vals, 
         y0 = dtbl_mc$ci.lo[1:3], y1 = dtbl_mc$ci.hi[1:3], 
         lwd = 5, col = makeTransparent("black", 150))

# Experiment 2 (CC)
plot(1,1, col = "white", bty = "n",
     xlim = c(0,7), ylim = c(-1,1),
     xlab = "Causal Claim",
     ylab = "", cex.lab = 2, cex.axis = 1.7, yaxt = "n", xaxt = "n")

points(x_vals, dtbl_mc$mean[4:6], pch = c(15, 16, 17), 
       cex = 4)
segments(x0 = x_vals, x1 = x_vals, 
         y0 = dtbl_mc$ci.lo[4:6], y1 = dtbl_mc$ci.hi[4:6], 
         lwd = 5, col = makeTransparent("black", 150) )

# cross-plot axis
axis(1, at = -17:6, lwd.tick=0, labels=FALSE)

segments(x0 = -17, x1 = -17, 
         y0 = -1.08, y1 = -1.12)
segments(x0 = 6, x1 = 6, 
         y0 = -1.08, y1 = -1.12)

segments(x0 = -17.5, x1 = 6, 
         y0 = 0, y1 = 0, lty = 2)

dev.off()

```


# Weighted Analysis (Binary Left/Right Measures)

```{r}

# Create Survey Object -------------------------------------------------------------------
data_weighted <- svydesign(~1, weights = ~teamweight, data = data_matched)

# Run Models -----------------------------------------------------------------------------

# Ideology
ss_ideo_lib_wb <- svyglm(goodSample ~ con_lib + cond_issue, 
                         subset(data_weighted, finding_lr == 1))
ss_ideo_con_wb <- svyglm(goodSample ~ con_lib + cond_issue, 
                         subset(data_weighted, finding_lr == 0))

cc_ideo_lib_wb <- svyglm(goodCausal ~ con_lib + cond_issue, 
                         subset(data_weighted, finding_lr == 1))
cc_ideo_con_wb <- svyglm(goodCausal ~ con_lib + cond_issue, 
                         subset(data_weighted, finding_lr == 0))

# Party ID
ss_pid_lib_wb <- svyglm(goodSample ~ rep_dem + cond_issue, 
                        subset(data_weighted, finding_lr == 1))
ss_pid_con_wb <- svyglm(goodSample ~ rep_dem + cond_issue, 
                        subset(data_weighted, finding_lr == 0))

cc_pid_lib_wb <- svyglm(goodCausal ~ rep_dem + cond_issue, 
                        subset(data_weighted, finding_lr == 1))
cc_pid_con_wb <- svyglm(goodCausal ~ rep_dem + cond_issue, 
                        subset(data_weighted, finding_lr == 0))

# Issue Mean
ss_iss_lib_wb <- svyglm(goodSample ~ issue_mean_binaryCon + cond_issue, 
                        subset(data_weighted, finding_lr == 1))
ss_iss_con_wb <- svyglm(goodSample ~ issue_mean_binaryCon + cond_issue, 
                        subset(data_weighted, finding_lr == 0))

cc_iss_lib_wb <- svyglm(goodCausal ~ issue_mean_binaryCon + cond_issue, 
                        subset(data_weighted, finding_lr == 1))
cc_iss_con_wb <- svyglm(goodCausal ~ issue_mean_binaryCon + cond_issue, 
                        subset(data_weighted, finding_lr == 0))

# Post-Estimation ------------------------------------------------------------------------
p_ss_ideo_lib_wb <- post(model = ss_ideo_lib_wb, x1name = "con_lib", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5, weights = ss_ideo_lib_wb$priorweights)
p_ss_ideo_con_wb <- post(model = ss_ideo_con_wb, x1name = "con_lib", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5, weights = ss_ideo_con_wb$priorweights)

p_cc_ideo_lib_wb <- post(model = cc_ideo_lib_wb, x1name = "con_lib", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5, weights = cc_ideo_lib_wb$priorweights)
p_cc_ideo_con_wb <- post(model = cc_ideo_con_wb, x1name = "con_lib", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5, weights = cc_ideo_con_wb$priorweights)

p_ss_pid_lib_wb <-  post(model = ss_pid_lib_wb, x1name = "rep_dem", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5, weights = ss_pid_lib_wb$priorweights)
p_ss_pid_con_wb <-  post(model = ss_pid_con_wb, x1name = "rep_dem", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5, weights = ss_pid_con_wb$priorweights)

p_cc_pid_lib_wb <-  post(model = cc_pid_lib_wb, x1name = "rep_dem", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5, weights = cc_pid_lib_wb$priorweights)
p_cc_pid_con_wb <-  post(model = cc_pid_con_wb, x1name = "rep_dem", x1vals = c(0, 1), 
                         n.sims = nsims, digits = 5, weights = cc_pid_con_wb$priorweights)

p_ss_iss_lib_wb <-  post(model = ss_iss_lib_wb, x1name = "issue_mean_binaryCon", 
                         x1vals = c(0, 1), n.sims = nsims, digits = 5, 
                         weights = ss_iss_lib_wb$priorweights)
p_ss_iss_con_wb <-  post(model = ss_iss_con_wb, x1name = "issue_mean_binaryCon", 
                         x1vals = c(0, 1), n.sims = nsims, digits = 5, 
                         weights = ss_iss_con_wb$priorweights)

p_cc_iss_lib_wb <-  post(model = cc_iss_lib_wb, x1name = "issue_mean_binaryCon", 
                         x1vals = c(0, 1), n.sims = nsims, digits = 5, 
                         weights = cc_iss_lib_wb$priorweights)
p_cc_iss_con_wb <-  post(model = cc_iss_con_wb, x1name = "issue_mean_binaryCon", 
                         x1vals = c(0, 1), n.sims = nsims, digits = 5, 
                         weights = cc_iss_con_wb$priorweights)

# Create Figure --------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------

# Sample Size Outcome ----------------------------------

pdf("cces_figures/cces_wb_1.pdf", height = 6, width = 8)
par(mfrow = c(1,3), oma = c(.1,5,1,0), mar = c(4.1,1,1.1,1))

# Issue Positions
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.5, .5),
     xlab = "Issue Position",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 2)

points(0:1, p_ss_iss_lib_wb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 3)
points(0:1, p_ss_iss_con_wb@est[1:2,1], pch = 15, col = "firebrick2", cex = 3)

points(0:1, p_ss_iss_lib_wb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 2, lty = 2)
points(0:1, p_ss_iss_con_wb@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 2, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_iss_lib_wb@est[1:2,2], 
         y1 =  p_ss_iss_lib_wb@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 5)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_iss_con_wb@est[1:2,2], 
         y1 =  p_ss_iss_con_wb@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 5)

legend(.5, 1, legend = c("Left", "Right"),
       lty = 1, lwd = 4, col = c(makeTransparent("deepskyblue3"), "firebrick2"),
       pch = 16, 
       bty = "n", cex = 2, title = "Evidence:")

# Ideology
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.5, .5),
     xlab = "Ideology",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 2)

points(0:1, p_ss_ideo_lib_wb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 3)
points(0:1, p_ss_ideo_con_wb@est[1:2,1], pch = 15, col = "firebrick2", cex = 3)

points(0:1, p_ss_ideo_lib_wb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 2, lty = 2)
points(0:1, p_ss_ideo_con_wb@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 2, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_ideo_lib_wb@est[1:2,2], 
         y1 =  p_ss_ideo_lib_wb@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 5)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_ideo_con_wb@est[1:2,2], 
         y1 =  p_ss_ideo_con_wb@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 5)

mtext("Sample Size is Sufficient (in SDs)", side = 2, outer = T, padj = -2, cex = 1.7)

# Party ID
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.5, .5),
     xlab = "Party ID",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 2)

points(0:1, p_ss_pid_lib_wb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 3)
points(0:1, p_ss_pid_con_wb@est[1:2,1], pch = 15, col = "firebrick2", cex = 3)

points(0:1, p_ss_pid_lib_wb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 2, lty = 2)
points(0:1, p_ss_pid_con_wb@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 2, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_pid_lib_wb@est[1:2,2], 
         y1 =  p_ss_pid_lib_wb@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 5)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_ss_pid_con_wb@est[1:2,2], 
         y1 =  p_ss_pid_con_wb@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 5)

dev.off()

# Causal Claim Outcome -------------------------------------------------------------------

pdf("cces_figures/cces_wb_2.pdf", height = 6, width = 8)
par(mfrow = c(1,3), oma = c(.1,5,1,0), mar = c(4.1,1,1.1,1))

# Issue Positions
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.7, .7),
     xlab = "Issue Position",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 1.5)

points(0:1, p_cc_iss_lib_wb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 3)
points(0:1, p_cc_iss_con_wb@est[1:2,1], pch = 15, col = "firebrick2", cex = 3)

points(0:1, p_cc_iss_lib_wb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 2, lty = 2)
points(0:1, p_cc_iss_con_wb@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 2, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_iss_lib_wb@est[1:2,2], 
         y1 =  p_cc_iss_lib_wb@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 5)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_iss_con_wb@est[1:2,2], 
         y1 =  p_cc_iss_con_wb@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 5)

# Ideology
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.7, .7),
     xlab = "Ideology",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 1.5)

points(0:1, p_cc_ideo_lib_wb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"),
      cex = 3)
points(0:1, p_cc_ideo_con_wb@est[1:2,1], pch = 15, col = "firebrick2", cex = 3)

points(0:1, p_cc_ideo_lib_wb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 2, lty = 2)
points(0:1, p_cc_ideo_con_wb@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 2, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_ideo_lib_wb@est[1:2,2], 
         y1 =  p_cc_ideo_lib_wb@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 5)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_ideo_con_wb@est[1:2,2], 
         y1 =  p_cc_ideo_con_wb@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 5)

mtext("Can Make Causal Claim (in SDs)", side = 2, outer = T, padj = -2, cex = 1.7)

# Party ID
plot(1,1, col = "white", bty = "n",
     xlim = c(-.20, 1.20), ylim = c(-.7, .7),
     xlab = "Party ID",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

axis(1, at = c(0, 1), labels = c("L", "R"), cex.axis = 1.5)

points(0:1, p_cc_pid_lib_wb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       cex = 3)
points(0:1, p_cc_pid_con_wb@est[1:2,1], pch = 15, col = "firebrick2", cex = 3)

points(0:1, p_cc_pid_lib_wb@est[1:2,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       type = "l", lwd = 2, lty = 2)
points(0:1, p_cc_pid_con_wb@est[1:2,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 2, lty = 2)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_pid_lib_wb@est[1:2,2], 
         y1 =  p_cc_pid_lib_wb@est[1:2,3],
         col = makeTransparent("deepskyblue3"), lwd = 5)
segments(x0 = c(0, 1), x1 = c(0, 1), 
         y0 =  p_cc_pid_con_wb@est[1:2,2], 
         y1 =  p_cc_pid_con_wb@est[1:2,3],
         col = makeTransparent("firebrick2"), lwd = 5)

dev.off()

# Difference in Differences --------------------------------------------------------------

# Sample Size Outcome
d_ss_iss_wb <- (p_ss_iss_con_wb@sims[,ncol(p_ss_iss_con_wb@sims)] -
             p_ss_iss_lib_wb@sims[,ncol(p_ss_iss_lib_wb@sims)]) -
            (p_ss_iss_lib_wb@sims[,1] -
             p_ss_iss_con_wb@sims[,1])

d_ss_ideo_wb <- (p_ss_ideo_con_wb@sims[,ncol(p_ss_ideo_con_wb@sims)] -
              p_ss_ideo_lib_wb@sims[,ncol(p_ss_ideo_lib_wb@sims)]) -
             (p_ss_ideo_lib_wb@sims[,1] -
              p_ss_ideo_con_wb@sims[,1])

d_ss_pid_wb <- (p_ss_pid_con_wb@sims[,ncol(p_ss_pid_con_wb@sims)] -
             p_ss_pid_lib_wb@sims[,ncol(p_ss_pid_lib_wb@sims)]) -
            (p_ss_pid_lib_wb@sims[,1] -
             p_ss_pid_con_wb@sims[,1])

# Causal Claim Outcome
d_cc_iss_wb <- (p_cc_iss_con_wb@sims[,ncol(p_cc_iss_con_wb@sims)] -
             p_cc_iss_lib_wb@sims[,ncol(p_cc_iss_lib_wb@sims)]) -
            (p_cc_iss_lib_wb@sims[,1] -
             p_cc_iss_con_wb@sims[,1])

d_cc_ideo_wb <- (p_cc_ideo_con_wb@sims[,ncol(p_cc_ideo_con_wb@sims)] -
              p_cc_ideo_lib_wb@sims[,ncol(p_cc_ideo_lib_wb@sims)]) -
             (p_cc_ideo_lib_wb@sims[,1] -
              p_cc_ideo_con_wb@sims[,1])

d_cc_pid_wb <- (p_cc_pid_con_wb@sims[,ncol(p_cc_pid_con_wb@sims)] -
             p_cc_pid_lib_wb@sims[,ncol(p_cc_pid_lib_wb@sims)]) -
            (p_cc_pid_lib_wb@sims[,1] -
             p_cc_pid_con_wb@sims[,1])
 
# Create table of differences and means

# as dataframe
d_names_wb <- as.data.frame(cbind(d_ss_iss_wb, d_ss_ideo_wb, d_ss_pid_wb,
                                  d_cc_iss_wb, d_cc_ideo_wb, d_cc_pid_wb))

dtbl_wb <- data.frame(name = colnames(d_names_wb),
                   mean = apply(d_names_wb, 2, mean),
                   ci.lo = apply(d_names_wb, 2, quantile, probs = .025),
                   ci.hi = apply(d_names_wb, 2, quantile, probs = .975))

pdf("cces_figures/cces_wb_3.pdf", height = 6, width = 8)

# Sample Size Outcome
par(mfrow = c(1,1), oma = c(.1,6,1,0), mar = c(4.1,1,1.1,1), xpd = NA)

x_vals <- c(1,3,5)

# Experiment 2 (SS)
plot(1,1, col = "white", bty = "n",
     xlim = c(0,7), ylim = c(-1,1),
     xlab = "Sample Size",
     ylab = "", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

points(x_vals, dtbl_wb$mean[1:3], pch = c(15, 16, 17),
       cex = 4)
segments(x0 = x_vals, x1 = x_vals,
         y0 = dtbl_wb$ci.lo[1:3], y1 = dtbl_wb$ci.hi[1:3],
         lwd = 5, col = makeTransparent("black", 150))

# cross-plot axis
axis(1, at = 0:6, lwd.tick=0, labels=FALSE)

segments(x0 = 0, x1 = 0,
         y0 = -1.08, y1 = -1.12)
segments(x0 = 6, x1 = 6,
         y0 = -1.08, y1 = -1.12)

# horizontal line at y = 0
segments(x0 = 0, x1 = 6,
         y0 = 0, y1 = 0, lty = 2)

dev.off()

# Causal Claim Outcome
pdf("cces_figures/cces_wb_4.pdf", height = 6, width = 8)

par(mfrow = c(1,1), oma = c(.1,6,1,0), mar = c(4.1,1,1.1,1), xpd = NA)
# Experiment 2 (CC)
plot(1,1, col = "white", bty = "n",
     xlim = c(0,7), ylim = c(-1,1),
     xlab = "Causal Claim",
     ylab = "", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

points(x_vals, dtbl_wb$mean[4:6], pch = c(15, 16, 17),
       cex = 4)
segments(x0 = x_vals, x1 = x_vals,
         y0 = dtbl_wb$ci.lo[4:6], y1 = dtbl_wb$ci.hi[4:6],
         lwd = 5, col = makeTransparent("black", 150) )

legend("bottomleft", legend = c("Issue Position", "Ideology", "Party ID"),
       pch = c(15, 16, 17),
       bty = "n", cex = 2)

# cross-plot axis
axis(1, at = 0:6, lwd.tick=0, labels=FALSE)

segments(x0 = 0, x1 = 0,
         y0 = -1.08, y1 = -1.12)
segments(x0 = 6, x1 = 6,
         y0 = -1.08, y1 = -1.12)

# horizontal line at y = 0
segments(x0 = 0, x1 = 6,
         y0 = 0, y1 = 0, lty = 2)

mtext("(Conservative - Liberal)", side = 2, outer = T, padj = -1.8, cex = 1.7)

dev.off()
                   
```

# Weighted Analysis (Continous Left/Right Measures)

```{r}

# Run Models -----------------------------------------------------------------------------

# Ideology
ss_ideo_lib_wc <- svyglm(goodSample ~ ideo7 + cond_issue, 
                         subset(data_weighted, finding_lr == 1))
ss_ideo_con_wc <- svyglm(goodSample ~ ideo7 + cond_issue, 
                         subset(data_weighted, finding_lr == 0))

cc_ideo_lib_wc <- svyglm(goodCausal ~ ideo7 + cond_issue, 
                         subset(data_weighted, finding_lr == 1))
cc_ideo_con_wc <- svyglm(goodCausal ~ ideo7 + cond_issue, 
                         subset(data_weighted, finding_lr == 0))

# Party ID
ss_pid_lib_wc <- svyglm(goodSample ~ pid7 + cond_issue, 
                        subset(data_weighted, finding_lr == 1))
ss_pid_con_wc <- svyglm(goodSample ~ pid7 + cond_issue, 
                        subset(data_weighted, finding_lr == 0))

cc_pid_lib_wc <- svyglm(goodCausal ~ pid7 + cond_issue, 
                        subset(data_weighted, finding_lr == 1))
cc_pid_con_wc <- svyglm(goodCausal ~ pid7 + cond_issue, 
                        subset(data_weighted, finding_lr == 0))

# Issue Mean
ss_iss_lib_wc <- svyglm(goodSample ~ issue_mean + cond_issue, 
                        subset(data_weighted, finding_lr == 1))
ss_iss_con_wc <- svyglm(goodSample ~ issue_mean + cond_issue, 
                        subset(data_weighted, finding_lr == 0))

cc_iss_lib_wc <- svyglm(goodCausal ~ issue_mean + cond_issue, 
                        subset(data_weighted, finding_lr == 1))
cc_iss_con_wc <- svyglm(goodCausal ~ issue_mean + cond_issue, 
                        subset(data_weighted, finding_lr == 0))

# Post-Estimation ------------------------------------------------------------------------
p_ss_ideo_lib_wc <- post(model = ss_ideo_lib_wc, x1name = "ideo7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5, weights = ss_ideo_lib_wc$priorweights)
p_ss_ideo_con_wc <- post(model = ss_ideo_con_wc, x1name = "ideo7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5, weights = ss_ideo_con_wc$priorweights)

p_cc_ideo_lib_wc <- post(model = cc_ideo_lib_wc, x1name = "ideo7", x1vals = 1:7,
                         n.sims = nsims, digits = 5, weights = cc_ideo_lib_wc$priorweights)
p_cc_ideo_con_wc <- post(model = cc_ideo_con_wc, x1name = "ideo7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5, weights = cc_ideo_con_wc$priorweights)

p_ss_pid_lib_wc <-  post(model = ss_pid_lib_wc, x1name = "pid7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5, weights = ss_pid_lib_wc$priorweights)
p_ss_pid_con_wc <-  post(model = ss_pid_con_wc, x1name = "pid7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5, weights = ss_pid_con_wc$priorweights)

p_cc_pid_lib_wc <-  post(model = cc_pid_lib_wc, x1name = "pid7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5, weights = cc_pid_lib_wc$priorweights)
p_cc_pid_con_wc <-  post(model = cc_pid_con_wc, x1name = "pid7", x1vals = 1:7, 
                         n.sims = nsims, digits = 5, weights = cc_pid_con_wc$priorweights)

p_ss_iss_lib_wc <-  post(model = ss_iss_lib_wc, x1name = "issue_mean", seq(0,1,.1), 
                         n.sims = nsims, digits = 5, weights = ss_iss_lib_wc$priorweights)
p_ss_iss_con_wc <-  post(model = ss_iss_con_wc, x1name = "issue_mean", seq(0,1,.1), 
                         n.sims = nsims, digits = 5, weights = ss_iss_con_wc$priorweights)

p_cc_iss_lib_wc <-  post(model = cc_iss_lib_wc, x1name = "issue_mean", seq(0,1,.1), 
                         n.sims = nsims, digits = 5, weights = cc_iss_lib_wc$priorweights)
p_cc_iss_con_wc <-  post(model = cc_iss_con_wc, x1name = "issue_mean", seq(0,1,.1), 
                         n.sims = nsims, digits = 5, weights = cc_iss_con_wc$priorweights)


# Create Figure --------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------

# Sample Size Plot -----------------------------------------------------------------------
pdf("cces_figures/cces_wc_1.pdf", height = 6, width = 8)
par(mfrow = c(1,3), oma = c(.1,5,1,0), mar = c(4.1,1,1,1))

# Issue Position
plot(1,1, col = "white", bty = "n",
     xlim = c(0,1), ylim = c(-1, 1),
     xlab = "Issue Position",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7)

points(seq(0,1,.1), p_ss_iss_lib_wc@est[1:11,1], pch = 16, 
       col = makeTransparent("deepskyblue3"), lty = 2,  type = "l", lwd = 4)

points(seq(0,1,.1), p_ss_iss_con_wc@est[1:11,1], pch = 15, 
       col = "firebrick2", type = "l", lwd = 4)

polygon(c(seq(0,1,.1), rev(seq(0,1,.1))),
        c(p_ss_iss_lib_wc@est[1:11,2], rev(p_ss_iss_lib_wc@est[1:11,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(0,1,.1), rev(seq(0,1,.1))),
        c(p_ss_iss_con_wc@est[1:11,2], rev(p_ss_iss_con_wc@est[1:11,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

legend(0, 1, legend = c("Left", "Right"),
       lty = c(2,1), lwd = 4, col = c(makeTransparent("deepskyblue3"), "firebrick2"),
       bty = "n", cex = 2, title = "Evidence:")

# Ideology
plot(1,1, col = "white", bty = "n",
     xlim = c(1,7), ylim = c(-1, 1),
     xlab = "Ideology",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7)

points(1:7, p_ss_ideo_lib_wc@est[1:7,1], pch = 16, 
       col = makeTransparent("deepskyblue3"), lty = 2,  
       type = "l", lwd = 4)

points(1:7, p_ss_ideo_con_wc@est[1:7,1], pch = 15, 
       col = "firebrick2", type = "l", lwd = 4)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_ss_ideo_lib_wc@est[1:7,2], rev(p_ss_ideo_lib_wc@est[1:7,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_ss_ideo_con_wc@est[1:7,2], rev(p_ss_ideo_con_wc@est[1:7,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

mtext("Sample Size is Sufficient (in SDs)", side = 2, outer = T, padj = -2, cex = 1.7)


# Party ID
plot(1,1, col = "white", bty = "n",
     xlim = c(1,7), ylim = c(-1, 1),
     xlab = "Party ID",
     ylab = "Sample Size is Sufficient (in SDs)", cex.lab = 2, cex.axis = 1.7)

points(1:7, p_ss_pid_lib_wc@est[1:7,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       lty = 2,  type = "l", lwd = 4)

points(1:7, p_ss_pid_con_wc@est[1:7,1], pch = 15, col = "firebrick2", type = "l", lwd = 4)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_ss_pid_lib_wc@est[1:7,2], rev(p_ss_pid_lib_wc@est[1:7,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_ss_pid_con_wc@est[1:7,2], rev(p_ss_pid_con_wc@est[1:7,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

dev.off()

# Causal Claim Plot ----------------------------------------------------------------------

pdf("cces_figures/cces_wc_2.pdf", height = 6, width = 8)

par(mfrow = c(1,3), oma = c(.1,5,1,0), mar = c(4.1,1,1,1))

# Ideology
plot(1,1, col = "white", bty = "n",
     xlim = c(1,7), ylim = c(-1, 1),
     xlab = "Ideology",
     ylab = "Can Make Causal Claim (in SDs)", cex.lab = 2, cex.axis = 1.7)

points(1:7, p_cc_ideo_lib_wc@est[1:7,1], pch = 16, 
       col = makeTransparent("deepskyblue3"), 
       lty = 2,  type = "l", lwd = 4)

points(1:7, p_cc_ideo_con_wc@est[1:7,1], pch = 15, 
       col = "firebrick2", type = "l", lwd = 4)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_cc_ideo_lib_wc@est[1:7,2], rev(p_cc_ideo_lib_wc@est[1:7,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_cc_ideo_con_wc@est[1:7,2], rev(p_cc_ideo_con_wc@est[1:7,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

mtext("Can Make Causal Claim (in SDs)", side = 2, outer = T, padj = -2, cex = 1.7)

# Party ID
plot(1,1, col = "white", bty = "n",
     xlim = c(1,7), ylim = c(-1, 1),
     xlab = "Party ID",
     ylab = "Can Make Causal Claim (in SDs)", cex.lab = 2, cex.axis = 1.7)

points(1:7, p_cc_pid_lib_wc@est[1:7,1], pch = 16, col = makeTransparent("deepskyblue3"), 
       lty = 2,  type = "l", lwd = 4)

points(1:7, p_cc_pid_con_wc@est[1:7,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 4)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_cc_pid_lib_wc@est[1:7,2], rev(p_cc_pid_lib_wc@est[1:7,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(1,7,1), rev(seq(1,7,1))),
        c(p_cc_pid_con_wc@est[1:7,2], rev(p_cc_pid_con_wc@est[1:7,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

# Issue Mean
seq(0,1,.1)
plot(1,1, col = "white", bty = "n",
     xlim = c(0,1), ylim = c(-1, 1),
     xlab = "Issue Position",
     ylab = "Can Make Causal Claim (in SDs)", cex.lab = 2, cex.axis = 1.7)
length(seq(0,1,.1))
points(seq(0,1,.1), p_cc_iss_lib_wc@est[1:11,1], pch = 16, 
       col = makeTransparent("deepskyblue3"), lty = 2,  type = "l", lwd = 4)

points(seq(0,1,.1), p_cc_iss_con_wc@est[1:11,1], pch = 15, col = "firebrick2", 
       type = "l", lwd = 4)

polygon(c(seq(0,1,.1), rev(seq(0,1,.1))),
        c(p_cc_iss_lib_wc@est[1:11,2], rev(p_cc_iss_lib_wc@est[1:11,3])),
        col= adjustcolor("deepskyblue3", .1), border=NA)

polygon(c(seq(0,1,.1), rev(seq(0,1,.1))),
        c(p_cc_iss_con_wc@est[1:11,2], rev(p_cc_iss_con_wc@est[1:11,3])),
        col= adjustcolor("firebrick2", .1), border=NA)

dev.off()

# Difference in Differences --------------------------------------------------------------

# Sample Size
d_ss_iss_wc <- (p_ss_iss_con_wc@sims[,ncol(p_ss_iss_con_wc@sims)] - 
                p_ss_iss_lib_wc@sims[,ncol(p_ss_iss_lib_wc@sims)]) - 
               (p_ss_iss_lib_wc@sims[,1] -
                p_ss_iss_con_wc@sims[,1])

d_ss_ideo_wc <- (p_ss_ideo_con_wc@sims[,ncol(p_ss_ideo_con_wc@sims)] - 
                 p_ss_ideo_lib_wc@sims[,ncol(p_ss_ideo_lib_wc@sims)]) - 
                (p_ss_ideo_lib_wc@sims[,1] - 
                 p_ss_ideo_con_wc@sims[,1])

d_ss_pid_wc <- (p_ss_pid_con_wc@sims[,ncol(p_ss_pid_con_wc@sims)] -  
                p_ss_pid_lib_wc@sims[,ncol(p_ss_pid_lib_wc@sims)]) - 
               (p_ss_pid_lib_wc@sims[,1] -
                p_ss_pid_con_wc@sims[,1])

# Causal Claim
d_cc_iss_wc <- (p_cc_iss_con_wc@sims[,ncol(p_cc_iss_con_wc@sims)] -  
                p_cc_iss_lib_wc@sims[,ncol(p_cc_iss_lib_wc@sims)]) - 
               (p_cc_iss_lib_wc@sims[,1] - 
                p_cc_iss_con_wc@sims[,1])

d_cc_ideo_wc  <- (p_cc_ideo_con_wc@sims[,ncol(p_cc_ideo_con_wc@sims)] -  
                  p_cc_ideo_lib_wc@sims[,ncol(p_cc_ideo_lib_wc@sims)]) - 
                 (p_cc_ideo_lib_wc@sims[,1] - 
                  p_cc_ideo_con_wc@sims[,1])

d_cc_pid_wc  <- (p_cc_pid_con_wc@sims[,ncol(p_cc_pid_con_wc@sims)] -  
                 p_cc_pid_lib_wc@sims[,ncol(p_cc_pid_lib_wc@sims)]) - 
                (p_cc_pid_lib_wc@sims[,1] -
                 p_cc_pid_con_wc@sims[,1])

# as dataframe
d_names_wc <- as.data.frame(cbind(d_ss_iss_wc, d_ss_ideo_wc, d_ss_pid_wc, 
                                  d_cc_iss_wc, d_cc_ideo_wc, d_cc_pid_wc))

dtbl_wc <- data.frame(name = colnames(d_names_wc), 
                   mean = apply(d_names_wc, 2, mean), 
                   ci.lo = apply(d_names_wc, 2, quantile, probs = .025),
                   ci.hi = apply(d_names_wc, 2, quantile, probs = .975))

# Difference in Differences Plot ---------------------------------------------------------

pdf("cces_figures/cces_wc_3.pdf", height = 6, width = 8)
par(mfrow = c(1,3), oma = c(.1,6,1,0), mar = c(4.1,1,1,1), xpd = NA)

x_vals <- c(1,3,5)

# Experiment 1
plot(1,1, col = "white", bty = "n",
     xlim = c(0,7), ylim = c(-1,1),
     xlab = "",
     ylab = "", cex.lab = 2, cex.axis = 1.7, xaxt = "n")

points(x_vals, dtbl_wc$mean[1:3], pch = c(15, 16, 17), 
       cex = 4, col = "white")
segments(x0 = x_vals, x1 = x_vals, 
         y0 = dtbl_wc$ci.lo[1:3], y1 = dtbl_wc$ci.hi[1:3], 
         lwd = 5, col = makeTransparent("white", 150))

legend("topleft", legend = c("Issue Position", "Ideology", "Party ID"),
       pch = c(15, 16, 17),
       bty = "n", cex = 3)

mtext("(Conservative - Liberal)", side = 2, outer = T, padj = -1.8, cex = 1.7)

# Experiment 2 (SS)
plot(1,1, col = "white", bty = "n",
     xlim = c(0,7), ylim = c(-1,1),
     xlab = "Sample Size",
     ylab = "", cex.lab = 2, cex.axis = 1.7, yaxt = "n", xaxt = "n")

points(x_vals, dtbl_wc$mean[1:3], pch = c(15, 16, 17), 
       cex = 4)
segments(x0 = x_vals, x1 = x_vals, 
         y0 = dtbl_wc$ci.lo[1:3], y1 = dtbl_wc$ci.hi[1:3], 
         lwd = 5, col = makeTransparent("black", 150))

# Experiment 2 (CC)
plot(1,1, col = "white", bty = "n",
     xlim = c(0,7), ylim = c(-1,1),
     xlab = "Causal Claim",
     ylab = "", cex.lab = 2, cex.axis = 1.7, yaxt = "n", xaxt = "n")

points(x_vals, dtbl_wc$mean[4:6], pch = c(15, 16, 17), 
       cex = 4)
segments(x0 = x_vals, x1 = x_vals, 
         y0 = dtbl_wc$ci.lo[4:6], y1 = dtbl_wc$ci.hi[4:6], 
         lwd = 5, col = makeTransparent("black", 150) )

# cross-plot axis
axis(1, at = -17:6, lwd.tick=0, labels=FALSE)

segments(x0 = -17, x1 = -17, 
         y0 = -1.08, y1 = -1.12)
segments(x0 = 6, x1 = 6, 
         y0 = -1.08, y1 = -1.12)

segments(x0 = -17.5, x1 = 6, 
         y0 = 0, y1 = 0, lty = 2)

dev.off()


sink()
```


