1 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 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.

2 Import Data & Setup

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)

3 Data Cleaning

3.1 Issue Positions

3.1.1 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]
# 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")
## 
##     For Against    <NA> 
##    1485     171       8
data$gun_1 <- with(data, ifelse(CC18_320a == "For", 0, 1))
table(data$CC18_320a, data$gun_1, useNA = "always")
##          
##              0    1 <NA>
##   For     1485    0    0
##   Against    0  171    0
##   <NA>       0    0    8
# 2
table(data$CC18_320c, useNA = "always")
## 
##     For Against    <NA> 
##    1098     553      13
data$gun_2 <- with(data, ifelse(CC18_320c == "For", 0, 1))
table(data$CC18_320c, data$gun_2, useNA = "always")
##          
##              0    1 <NA>
##   For     1098    0    0
##   Against    0  553    0
##   <NA>       0    0   13
# 3
table(data$CC18_320d, useNA = "always")
## 
##     For Against    <NA> 
##     565    1084      15
data$gun_3 <- with(data, ifelse(CC18_320d == "For", 1, 0))
table(data$CC18_320d, data$gun_3, useNA = "always")
##          
##              0    1 <NA>
##   For        0  565    0
##   Against 1084    0    0
##   <NA>       0    0   15
new_gun_vars <- paste("gun", 1:3, sep = "_")

psych::alpha(data[,new_gun_vars])
## 
## Reliability analysis   
## Call: psych::alpha(x = data[, new_gun_vars])
## 
##   raw_alpha std.alpha G6(smc) average_r S/N   ase mean   sd median_r
##       0.66      0.67    0.59       0.4   2 0.013 0.26 0.33     0.39
## 
##  lower alpha upper     95% confidence boundaries
## 0.64 0.66 0.69 
## 
##  Reliability if an item is dropped:
##       raw_alpha std.alpha G6(smc) average_r  S/N alpha se var.r med.r
## gun_1      0.67      0.67    0.51      0.51 2.05    0.016    NA  0.51
## gun_2      0.45      0.48    0.32      0.32 0.94    0.024    NA  0.32
## gun_3      0.52      0.56    0.39      0.39 1.27    0.021    NA  0.39
## 
##  Item statistics 
##          n raw.r std.r r.cor r.drop mean   sd
## gun_1 1656  0.65  0.73  0.49   0.41 0.10 0.30
## gun_2 1651  0.84  0.81  0.68   0.56 0.33 0.47
## gun_3 1649  0.82  0.78  0.62   0.51 0.34 0.47
## 
## Non missing response frequency for each item
##          0    1 miss
## gun_1 0.90 0.10 0.00
## gun_2 0.67 0.33 0.01
## gun_3 0.66 0.34 0.01
data$gun_mean <- rowMeans(data[,new_gun_vars], na.rm=T)

3.1.2 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]
# 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")
## 
## Support  Oppose    <NA> 
##    1011     651       2
data$abort_1 <- ifelse(data$CC18_321a == "Support", 0, 1)
table(data$CC18_321a,data$abort_1, useNA = "always")
##          
##              0    1 <NA>
##   Support 1011    0    0
##   Oppose     0  651    0
##   <NA>       0    0    2
# 2
table(data$CC18_321b, useNA = "always")
## 
## Support  Oppose    <NA> 
##     686     978       0
data$abort_2 <- ifelse(data$CC18_321b == "Support", 1, 0)
table(data$CC18_321b, data$abort_2, useNA = "always")
##          
##             0   1 <NA>
##   Support   0 686    0
##   Oppose  978   0    0
##   <NA>      0   0    0
# 3
table(data$CC18_321c, useNA = "always")
## 
## Support  Oppose    <NA> 
##    1048     616       0
data$abort_3 <- ifelse(data$CC18_321c == "Support", 1, 0)
table(data$CC18_321c, data$abort_3, useNA = "always")
##          
##              0    1 <NA>
##   Support    0 1048    0
##   Oppose   616    0    0
##   <NA>       0    0    0
# 4
table(data$CC18_321d, useNA = "always")
## 
## Support  Oppose    <NA> 
##     707     957       0
data$abort_4 <- ifelse(data$CC18_321d == "Support", 1, 0)
table(data$CC18_321d, data$abort_4, useNA = "always")
##          
##             0   1 <NA>
##   Support   0 707    0
##   Oppose  957   0    0
##   <NA>      0   0    0
# 5
table(data$CC18_321e, useNA = "always")
## 
## Support  Oppose    <NA> 
##     751     913       0
data$abort_5 <- ifelse(data$CC18_321e == "Support", 1, 0)
table(data$CC18_321e, data$abort_5, useNA = "always")
##          
##             0   1 <NA>
##   Support   0 751    0
##   Oppose  913   0    0
##   <NA>      0   0    0
# 6
table(data$CC18_321f, useNA = "always")
## 
## Support  Oppose    <NA> 
##     258    1405       1
data$abort_6 <- ifelse(data$CC18_321f == "Support", 1, 0)
table(data$CC18_321f,data$abort_6, useNA = "always")
##          
##              0    1 <NA>
##   Support    0  258    0
##   Oppose  1405    0    0
##   <NA>       0    0    1
new_abort_vars <- paste("abort", 1:6, sep = "_")

psych::alpha(data[,new_abort_vars])
## 
## Reliability analysis   
## Call: psych::alpha(x = data[, new_abort_vars])
## 
##   raw_alpha std.alpha G6(smc) average_r S/N    ase mean   sd median_r
##       0.79      0.78    0.78      0.37 3.6 0.0077 0.41 0.33     0.38
## 
##  lower alpha upper     95% confidence boundaries
## 0.77 0.79 0.8 
## 
##  Reliability if an item is dropped:
##         raw_alpha std.alpha G6(smc) average_r S/N alpha se var.r med.r
## abort_1      0.72      0.71    0.70      0.33 2.5   0.0103 0.029  0.34
## abort_2      0.78      0.77    0.75      0.40 3.4   0.0082 0.022  0.40
## abort_3      0.77      0.76    0.76      0.39 3.2   0.0084 0.032  0.35
## abort_4      0.73      0.72    0.71      0.34 2.6   0.0100 0.027  0.37
## abort_5      0.72      0.71    0.70      0.33 2.5   0.0103 0.025  0.34
## abort_6      0.80      0.80    0.79      0.45 4.1   0.0076 0.011  0.41
## 
##  Item statistics 
##            n raw.r std.r r.cor r.drop mean   sd
## abort_1 1662  0.80  0.80  0.77   0.68 0.39 0.49
## abort_2 1664  0.64  0.63  0.53   0.46 0.41 0.49
## abort_3 1664  0.66  0.65  0.53   0.48 0.63 0.48
## abort_4 1664  0.78  0.78  0.74   0.65 0.42 0.49
## abort_5 1664  0.80  0.80  0.77   0.68 0.45 0.50
## abort_6 1663  0.46  0.51  0.35   0.30 0.16 0.36
## 
## Non missing response frequency for each item
##            0    1 miss
## abort_1 0.61 0.39    0
## abort_2 0.59 0.41    0
## abort_3 0.37 0.63    0
## abort_4 0.58 0.42    0
## abort_5 0.55 0.45    0
## abort_6 0.84 0.16    0
data$abort_mean <- rowMeans(data[,new_abort_vars], na.rm=T)

3.1.3 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)
# recode each item such that 0 = liberal position and 1 = conservative position

#1
table(data$CC18_322a, useNA = "always")
## 
## Support  Oppose    <NA> 
##     692     961      11
data$imm_1 <- ifelse(data$CC18_322a == "Support", 1, 0)
table(data$CC18_322a, data$imm_1, useNA = "always")
##          
##             0   1 <NA>
##   Support   0 692    0
##   Oppose  961   0    0
##   <NA>      0   0   11
#2
table(data$CC18_322b, useNA = "always")
## 
## Support  Oppose    <NA> 
##    1258     400       6
data$imm_2 <- ifelse(data$CC18_322b == "Support", 0, 1)
table(data$CC18_322b, data$imm_2, useNA = "always")
##          
##              0    1 <NA>
##   Support 1258    0    0
##   Oppose     0  400    0
##   <NA>       0    0    6
#3
table(data$CC18_322c_new, useNA = "always")
## 
## Support  Oppose    <NA> 
##     694     937      33
data$imm_3 <- ifelse(data$CC18_322c_new == "Support", 1, 0)
table(data$CC18_322c_new, data$imm_3, useNA = "always")
##          
##             0   1 <NA>
##   Support   0 694    0
##   Oppose  937   0    0
##   <NA>      0   0   33
#4
table(data$CC18_322c, useNA = "always")
## 
## Support  Oppose    <NA> 
##     776     887       1
data$imm_4 <- ifelse(data$CC18_322c == "Support", 1, 0)
table(data$CC18_322c, data$imm_4, useNA = "always")
##          
##             0   1 <NA>
##   Support   0 776    0
##   Oppose  887   0    0
##   <NA>      0   0    1
#5
table(data$CC18_322f, useNA = "always")
## 
## Support  Oppose    <NA> 
##     877     785       2
data$imm_5 <- ifelse(data$CC18_322f == "Support", 1, 0)
table(data$CC18_322f, data$imm_5, useNA = "always")
##          
##             0   1 <NA>
##   Support   0 877    0
##   Oppose  785   0    0
##   <NA>      0   0    2
new_imm_vars <- paste("imm", 1:5, sep = "_")

psych::alpha(data[,new_imm_vars])
## 
## Reliability analysis   
## Call: psych::alpha(x = data[, new_imm_vars])
## 
##   raw_alpha std.alpha G6(smc) average_r S/N    ase mean   sd median_r
##       0.82      0.82    0.79      0.47 4.5 0.0068 0.42 0.37     0.49
## 
##  lower alpha upper     95% confidence boundaries
## 0.81 0.82 0.83 
## 
##  Reliability if an item is dropped:
##       raw_alpha std.alpha G6(smc) average_r S/N alpha se  var.r med.r
## imm_1      0.75      0.75    0.70      0.43 3.0   0.0098 0.0138  0.42
## imm_2      0.83      0.83    0.79      0.55 5.0   0.0067 0.0044  0.55
## imm_3      0.78      0.78    0.74      0.46 3.5   0.0086 0.0183  0.47
## imm_4      0.75      0.75    0.71      0.43 3.0   0.0097 0.0129  0.43
## imm_5      0.80      0.79    0.76      0.49 3.8   0.0080 0.0170  0.49
## 
##  Item statistics 
##          n raw.r std.r r.cor r.drop mean   sd
## imm_1 1653  0.84  0.83  0.80   0.72 0.42 0.49
## imm_2 1658  0.61  0.63  0.47   0.43 0.24 0.43
## imm_3 1631  0.78  0.77  0.69   0.63 0.43 0.49
## imm_4 1663  0.83  0.83  0.79   0.71 0.47 0.50
## imm_5 1662  0.74  0.73  0.63   0.57 0.53 0.50
## 
## Non missing response frequency for each item
##          0    1 miss
## imm_1 0.58 0.42 0.01
## imm_2 0.76 0.24 0.00
## imm_3 0.57 0.43 0.02
## imm_4 0.53 0.47 0.00
## imm_5 0.47 0.53 0.00
data$imm_mean <- rowMeans(data[,new_imm_vars], na.rm=T)

3.2 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.

3.2.1 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.

# original variable indicating increase/decrease condition assignment
data$cond_direction <- data$DKU439_treat # increase, decrease
table(data$cond_direction, useNA = "always")
## 
## increase decrease     <NA> 
##      550      578      536
# original variable indicating issue condition assignment
data$cond_issue <- data$DKU440_treat # guns, imm, abort
table(data$cond_issue, useNA = "always")
## 
##  guns abort   imm  <NA> 
##   360   385   383   536
# sample size in each condition
with(data, table(cond_issue, cond_direction))
##           cond_direction
## cond_issue increase decrease
##      guns       167      193
##      abort      194      191
##      imm        189      194
with(data, prop.table(table(cond_issue, cond_direction)))
##           cond_direction
## cond_issue  increase  decrease
##      guns  0.1480496 0.1710993
##      abort 0.1719858 0.1693262
##      imm   0.1675532 0.1719858
# 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.

# 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"))
##                    issue_mean_binaryCon
## issue_mean            0   1 <NA>
##   0                 404   0    0
##   0.166666666666667  70   0    0
##   0.2                58   0    0
##   0.25                3   0    0
##   0.333333333333333 103   0    0
##   0.4                38   0    0
##   0.5                 0   0   45
##   0.6                 0  35    0
##   0.666666666666667   0 104    0
##   0.75                0   2    0
##   0.8                 0  86    0
##   0.833333333333333   0  80    0
##   1                   0  99    0
##   <NA>                0   0  536
##   NaN                 0   0    1
# 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)
##    
##     Strong Democrat Not very strong Democrat Lean Democrat Independent
##   1             391                        0             0           0
##   2               0                      225             0           0
##   3               0                        0           169           0
##   4               0                        0             0         212
##   5               0                        0             0           0
##   6               0                        0             0           0
##   7               0                        0             0           0
##    
##     Lean Republican Not very strong Republican Strong Republican Not sure
##   1               0                          0                 0        0
##   2               0                          0                 0        0
##   3               0                          0                 0        0
##   4               0                          0                 0        0
##   5             163                          0                 0        0
##   6               0                        165                 0        0
##   7               0                          0               280        0
##    
##     Don't know
##   1          0
##   2          0
##   3          0
##   4          0
##   5          0
##   6          0
##   7          0
# 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"))
##       rep_dem
## pid7     0   1 <NA>
##   1    391   0    0
##   2    225   0    0
##   3    169   0    0
##   4      0   0  212
##   5      0 163    0
##   6      0 165    0
##   7      0 280    0
##   <NA>   0   0   59
# 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)
##                        
##                           1   2   3   4   5   6   7   8
##   Very Liberal          170   0   0   0   0   0   0   0
##   Liberal                 0 229   0   0   0   0   0   0
##   Somewhat Liberal        0   0 171   0   0   0   0   0
##   Middle of the Road      0   0   0 359   0   0   0   0
##   Somewhat Conservative   0   0   0   0 182   0   0   0
##   Conservative            0   0   0   0   0 271   0   0
##   Very Conservative       0   0   0   0   0   0 191   0
##   Not sure                0   0   0   0   0   0   0  90
data$ideo7[data$ideo7 == 8] <- NA 

# verify
table(data$CC18_334A.1, data$ideo7)
##                        
##                           1   2   3   4   5   6   7
##   Very Liberal          170   0   0   0   0   0   0
##   Liberal                 0 229   0   0   0   0   0
##   Somewhat Liberal        0   0 171   0   0   0   0
##   Middle of the Road      0   0   0 359   0   0   0
##   Somewhat Conservative   0   0   0   0 182   0   0
##   Conservative            0   0   0   0   0 271   0
##   Very Conservative       0   0   0   0   0   0 191
##   Not sure                0   0   0   0   0   0   0
# 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"))
##        ideo7
## con_lib   1   2   3   4   5   6   7 <NA>
##    0    170 229 171   0   0   0   0    0
##    1      0   0   0   0 182 271 191    0
##    <NA>   0   0   0 359   0   0   0   91
# create congeniality variable
data$congenial_ideo <- ifelse(data$finding_direction == "right", data$ideo7, 
                             ifelse(data$finding_direction == "left", abs(8-data$ideo7), 
                                    1111))

3.2.2 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.

# guns sample DV
data$guns_sample <- as.numeric(data$DKU441)
table(data$guns_sample, data$DKU441)
##    
##     Much larger than necessary Somewhat larger than necessary
##   1                          9                              0
##   2                          0                             19
##   3                          0                              0
##   4                          0                              0
##   5                          0                              0
##   6                          0                              0
##   7                          0                              0
##    
##     A bit larger than necessary About the right size
##   1                           0                    0
##   2                           0                    0
##   3                          24                    0
##   4                           0                  153
##   5                           0                    0
##   6                           0                    0
##   7                           0                    0
##    
##     A bit smaller than necessary Somewhat smaller than necessary
##   1                            0                               0
##   2                            0                               0
##   3                            0                               0
##   4                            0                               0
##   5                           50                               0
##   6                            0                              43
##   7                            0                               0
##    
##     Much smaller than necessary
##   1                           0
##   2                           0
##   3                           0
##   4                           0
##   5                           0
##   6                           0
##   7                          60
# guns causal DV
data$guns_causal <- as.numeric(data$DKU442)
table(data$guns_causal, data$DKU442)
##    
##     Agree strongly Agree somewhat Agree a little Neither agree nor disagree
##   1             78              0              0                          0
##   2              0             74              0                          0
##   3              0              0             48                          0
##   4              0              0              0                         70
##   5              0              0              0                          0
##   6              0              0              0                          0
##   7              0              0              0                          0
##    
##     Disagree a little Disagree somewhat Disagree strongly
##   1                 0                 0                 0
##   2                 0                 0                 0
##   3                 0                 0                 0
##   4                 0                 0                 0
##   5                17                 0                 0
##   6                 0                31                 0
##   7                 0                 0                41
# abort sample DV
data$abort_sample <- as.numeric(data$DKU443)
table(data$abort_sample, data$DKU443)
##    
##     Much larger than necessary Somewhat larger than necessary
##   1                         14                              0
##   2                          0                             11
##   3                          0                              0
##   4                          0                              0
##   5                          0                              0
##   6                          0                              0
##   7                          0                              0
##    
##     A bit larger than necessary About the right size
##   1                           0                    0
##   2                           0                    0
##   3                          20                    0
##   4                           0                  187
##   5                           0                    0
##   6                           0                    0
##   7                           0                    0
##    
##     A bit smaller than necessary Somewhat smaller than necessary
##   1                            0                               0
##   2                            0                               0
##   3                            0                               0
##   4                            0                               0
##   5                           63                               0
##   6                            0                              41
##   7                            0                               0
##    
##     Much smaller than necessary
##   1                           0
##   2                           0
##   3                           0
##   4                           0
##   5                           0
##   6                           0
##   7                          46
# abort causal DV
data$abort_causal <- as.numeric(data$DKU444)
table(data$abort_causal, data$DKU444)
##    
##     Agree strongly Agree somewhat Agree a little Neither agree nor disagree
##   1             64              0              0                          0
##   2              0             62              0                          0
##   3              0              0             51                          0
##   4              0              0              0                        112
##   5              0              0              0                          0
##   6              0              0              0                          0
##   7              0              0              0                          0
##    
##     Disagree a little Disagree somewhat Disagree strongly
##   1                 0                 0                 0
##   2                 0                 0                 0
##   3                 0                 0                 0
##   4                 0                 0                 0
##   5                27                 0                 0
##   6                 0                17                 0
##   7                 0                 0                50
# imm sample DV
data$imm_sample <- as.numeric(data$DKU447)
table(data$imm_sample, data$DKU447)
##    
##     Much larger than necessary Somewhat larger than necessary
##   1                         26                              0
##   2                          0                             16
##   3                          0                              0
##   4                          0                              0
##   5                          0                              0
##   6                          0                              0
##   7                          0                              0
##    
##     A bit larger than necessary About the right size
##   1                           0                    0
##   2                           0                    0
##   3                          22                    0
##   4                           0                  176
##   5                           0                    0
##   6                           0                    0
##   7                           0                    0
##    
##     A bit smaller than necessary Somewhat smaller than necessary
##   1                            0                               0
##   2                            0                               0
##   3                            0                               0
##   4                            0                               0
##   5                           46                               0
##   6                            0                              45
##   7                            0                               0
##    
##     Much smaller than necessary
##   1                           0
##   2                           0
##   3                           0
##   4                           0
##   5                           0
##   6                           0
##   7                          50
# imm causal DV
data$imm_causal <- as.numeric(data$DKU448)
table(data$imm_causal, data$DKU448)
##    
##     Agree strongly Agree somewhat Agree a little Neither agree nor disagree
##   1             67              0              0                          0
##   2              0             56              0                          0
##   3              0              0             59                          0
##   4              0              0              0                         96
##   5              0              0              0                          0
##   6              0              0              0                          0
##   7              0              0              0                          0
##    
##     Disagree a little Disagree somewhat Disagree strongly
##   1                 0                 0                 0
##   2                 0                 0                 0
##   3                 0                 0                 0
##   4                 0                 0                 0
##   5                27                 0                 0
##   6                 0                26                 0
##   7                 0                 0                50
causal_vars <- c(paste(c("guns", "abort", "imm"), "causal", sep = "_"))
sample_vars <- c(paste(c("guns", "abort", "imm"), "sample", sep = "_"))

head(data[,causal_vars])
##   guns_causal abort_causal imm_causal
## 1          NA           NA          4
## 2          NA           NA         NA
## 3          NA            3         NA
## 4          NA            4         NA
## 5           4           NA         NA
## 6          NA           NA         NA
head(data[,sample_vars])
##   guns_sample abort_sample imm_sample
## 1          NA           NA          4
## 2          NA           NA         NA
## 3          NA            4         NA
## 4          NA            4         NA
## 5           7           NA         NA
## 6          NA           NA         NA
# 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)
##    
##       1   2   3   4   5   6   7
##   1   0   0   0   0   0   0 141
##   2   0   0   0   0   0  74   0
##   3   0   0   0   0  71   0   0
##   4   0   0   0 278   0   0   0
##   5   0   0 158   0   0   0   0
##   6   0 192   0   0   0   0   0
##   7 209   0   0   0   0   0   0
data$goodCausal_uns <- data$goodCausal

data$goodSample <- 8-data$goodSample.0
table(data$goodSample, data$goodSample.0)
##    
##       1   2   3   4   5   6   7
##   1   0   0   0   0   0   0 156
##   2   0   0   0   0   0 129   0
##   3   0   0   0   0 159   0   0
##   4   0   0   0 516   0   0   0
##   5   0   0  66   0   0   0   0
##   6   0  46   0   0   0   0   0
##   7  49   0   0   0   0   0   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
## [1] 0.3735617

4 Unmatched Analysis (Binary Left/Right Measures)

# 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()
## quartz_off_screen 
##                 2
# 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()
## quartz_off_screen 
##                 2
# 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()
## quartz_off_screen 
##                 2
# 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()
## quartz_off_screen 
##                 2

5 Unmatched Analysis (Continuous Left/Right Measures)

# 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()
## quartz_off_screen 
##                 2
# 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)
##  [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
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))
## [1] 11
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()
## quartz_off_screen 
##                 2
# 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()
## quartz_off_screen 
##                 2

6 Create Matched Dataset

# 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)
## [1] "data.frame"
dim(data_matched_original)
## [1] 1000   27
# Create vector of caseids in the matched data
matched_ids <- data_matched_original$caseid
length(matched_ids)
## [1] 1000
# Subset cleaned dataset to include only respondents in matched sample
data_matched <- data[data$caseid %in% matched_ids,]

dim(data_matched)
## [1] 1000   69

7 Matched Analysis (Binary Left/Right Measures)

# 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()
## quartz_off_screen 
##                 2
# 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()
## quartz_off_screen 
##                 2
# 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()
## quartz_off_screen 
##                 2
# 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()
## quartz_off_screen 
##                 2

8 Matched Analysis (Continuous Left/Right Measures)

# 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()
## quartz_off_screen 
##                 2
# 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)
##  [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
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))
## [1] 11
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()
## quartz_off_screen 
##                 2
# 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()
## quartz_off_screen 
##                 2

9 Weighted Analysis (Binary Left/Right Measures)

# 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()
## quartz_off_screen 
##                 2
# 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()
## quartz_off_screen 
##                 2
# 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()
## quartz_off_screen 
##                 2
# 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()
## quartz_off_screen 
##                 2

10 Weighted Analysis (Continous Left/Right Measures)

# 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()
## quartz_off_screen 
##                 2
# 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)
##  [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
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))
## [1] 11
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()
## quartz_off_screen 
##                 2
# 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()
## quartz_off_screen 
##                 2
sink()