# ======================================================================
# Klüver/Spoon (BJPS): Who responds? Voters, parties and issue attention
# Replication R Script
# ======================================================================


library(foreign)
library(nlme)
library(lattice)
library(Matrix)
library(lme4)
library(car)
library(arm)
library(memisc)
library(mice)
library(Zelig)
library(Amelia)
library(plotrix)   
library(graphics)     
options(scipen=3)       
                        



# ====================
# FIGURE 1
# ====================



rm(list=ls(all=TRUE))
setwd("C:/Users/ba9ze3-1/Dropbox/Dokumente/Papers/Party context paper/Paper #3/Replication/")

dat <- read.dta("figure1.dta")
attach(dat)
str(dat)

win.metafile(file=("C:/Users/ba9ze3-1/Dropbox/Dokumente/Papers/Party context paper/Figures #3/figure1.emf"), height=8, width=11)
par(mfrow=c(1,1),mar=c(5,5,1.5,3))
plot(1, type="n",  xlim=c(1,30), ylim=c(0,0.1), xlab= "Party size"~t[-1],
      ylab = ("Marginal effect of voter issue attention"~t[-1]~"(incl. 90% CI)"), cex=1.7, cex.axis=1.5, cex.lab=1.5)
      lines(1:30,conbx, col="black", lwd=2)
      lines(1:30,upper, col="black", lty=2, lwd=2)
      lines(1:30,lower, col="black", lty=2, lwd=2)
       abline(0, 0, lty=3)
dev.off()






# ====================
# FIGURE 2
# ====================

# Values have been extracted from running the code in STATA

type <- c(1:2)
means <- c(.0568605 , .0194026)
ub <- c(.0764014, .0521596)
lb <- c(.0373196,  -.0133543)


# Plotting point estimates with confidence intervals
win.metafile(file=("C:/Users/ba9ze3-1/Dropbox/Dokumente/Papers/Party context paper/Figures #3/figure2.emf"), height=8, width=11)
par(mfrow=c(1,1),mar=c(5,5,1.5,3))
plotCI(type, means, ui=ub, li=lb, xlim = c(0.5,2.5), ylim = c(-0.05,0.1), xlab = "", ylab = ("Marginal effect of voter issue attention"~t[-1]~"(incl. 90% CI)"),
  pch=19, xaxt="n", cex=1.5, cex.lab=1.5, cex.axis=1.5)
    abline(0, 0, lty=3)
  axis(1, at=1:2, lab=c("Opposition parties","Government parties"), cex.axis=1.5)
dev.off()






# ====================
# FIGURE 3
# ====================


rm(list=ls(all=TRUE))
setwd("C:/Users/ba9ze3-1/Dropbox/Dokumente/Papers/Party context paper/Paper #3/Replication/")

fixeff_raw <- read.csv("figure3_fixeff.csv")
fixeff_untransposed <- fixeff_raw[1,]
fixeff2 <- t(fixeff_untransposed)
fixeff <- fixeff2[1:15]
varcov_raw <- read.csv("figure3_vvector.csv")
(varcov <- varcov_raw[1:15,1:15])



# Step 1: get b and V
# ====================
(b <- as.vector(fixeff))		        
(V <- as.matrix(varcov))		      
(sqrt(diag(V)))                         
                      


# Step 2: Set up MVN distribution
# ===============================
nsim <- 1000
mvn <- mvrnorm(nsim, mu=b, Sigma=V)         


# Step 3: Get coefficients
# ========================

dat <- read.dta("figure3.dta")
attach(dat)
str(dat)


# Predicted values
# ================


# Minimum size
green_attpred <- list(rep(NA,21))
v <- seq(0, 20, by=1)
for (i in seq_along(v)){
green_attpred[[i]] <-  (
            + mvn[,1] * mean(dat$lag_attent)
            + mvn[,2] * i
            + mvn[,3] * mean(dat$lag_pervote)
            + mvn[,4] * median(dat$gov) 
            + mvn[,5] * mean(dat$rile)
            + mvn[,6] * mean(dat$avg_sal_lag)
            + mvn[,7] * mean(dat$avgdm)
            + mvn[,8] * mean(dat$days)
            + mvn[,9] * 1
            + mvn[,10] * 1
            + mvn[,11] * 1 * 1
            + mvn[,12] * 1 * i
            + mvn[,13] * 1 * i
            + mvn[,14] * 1 * 1 * i
            + mvn[,15] * 1
            )
}

other_attpred <- list(rep(NA,21))
for (i in seq_along(v)){
other_attpred[[i]] <-  (
            + mvn[,1] * mean(dat$lag_attent)
            + mvn[,2] * i
            + mvn[,3] * mean(dat$lag_pervote)
            + mvn[,4] * median(dat$gov) 
            + mvn[,5] * mean(dat$rile)
            + mvn[,6] * mean(dat$avg_sal_lag)
            + mvn[,7] * mean(dat$avgdm)
            + mvn[,8] * mean(dat$days)
            + mvn[,9] * 0
            + mvn[,10] * 1
            + mvn[,11] * 0 * 1
            + mvn[,12] * 1 * i
            + mvn[,13] * 0 * i
            + mvn[,14] * 0 * 1 * i
            + mvn[,15] * 1
            )
}





# Transform lists into matrixes
# =============================

green_attpred_matrix <- do.call(cbind, green_attpred)     
other_attpred_matrix <- do.call(cbind, other_attpred)     



# Taking the means and quantiles of simulated values
# ==================================================

green_attpred_mean <- list(rep(NA,21))
green_attpred_lb <- list(rep(NA,21))
green_attpred_ub <- list(rep(NA,21))

for (i in seq_along(v)){
 green_attpred_mean[[i]] <- mean(green_attpred_matrix[,i])
 green_attpred_lb[[i]] <- quantile(green_attpred_matrix[,i], c(.050))
 green_attpred_ub[[i]] <- quantile(green_attpred_matrix[,i], c(.950))
}

other_attpred_mean <- list(rep(NA,21))
other_attpred_lb <- list(rep(NA,21))
other_attpred_ub <- list(rep(NA,21))

for (i in seq_along(v)){
 other_attpred_mean[[i]] <- mean(other_attpred_matrix[,i])
 other_attpred_lb[[i]] <- quantile(other_attpred_matrix[,i], c(.050))
 other_attpred_ub[[i]] <- quantile(other_attpred_matrix[,i], c(.950))
}


dataset <- data.frame(cbind(green_attpred_ub, green_attpred_mean, green_attpred_lb, other_attpred_ub, other_attpred_mean, other_attpred_lb))


# Plotting predicted values
# =========================

win.metafile(file=("C:/Users/ba9ze3-1/Dropbox/Dokumente/Papers/Party context paper/Figures #3/figure3.emf"), height=8, width=11)
par(mfrow=c(1,1),mar=c(5,5,1.5,3)) 
plot(1, type="n",  xlim=c(0,20), ylim=c(4,16), xlab= expression("Voter issue attention"~t[-1]),
      ylab=expression("Party issue attention"~t[0]~"(incl. 90% CI)"), cex=1.7, cex.axis=1.5, cex.lab=1.5)
      lines(0:20,green_attpred_mean, col="black", lwd=2)
      lines(0:20,green_attpred_ub, col="black", lty=2, lwd=2)
      lines(0:20,green_attpred_lb, col="black", lty=2, lwd=2)
      lines(0:20,other_attpred_mean, col="darkgray", lwd=2)
      lines(0:20,other_attpred_ub, col="darkgray", lty=2, lwd=2)
      lines(0:20,other_attpred_lb, col="darkgray", lty=2, lwd=2)
      legend(0.5, 16, bty = "n", "Green parties", lwd=2, col="black", cex=1.3)  
      legend(0.5, 15.4, bty = "n", "Other parties", lwd=2, col="darkgray", cex=1.3)
      rug(lag_mip_par)
dev.off()




