# China leadership project: plot all baseline results from rank model # Create lineplots with estimated first differences over: # covariates, models, and time periods # Chris Adolph rm(list=ls()) library(MASS) library(car) library(tile) library(RColorBrewer) # REPLICATORS! Edit these paths to match the location of the results on your computer! # folders pasted together with each element of filenames should lead to the output file folders <- c("/projects/China/Replication" ) filenames <- c( "/pc12/efirstdiffranknewpc12m1run10000ties1.csv", "/pc13/efirstdiffranknewpc13m1run10000ties1.csv", "/pc14/efirstdiffranknewpc14m1run10000ties1.csv", "/pc15/efirstdiffranknewpc15m1run10000ties1.csv", "/pc16/efirstdiffranknewpc16m1run10000ties1.csv" ) # Insert an object into a list vector (needed to work around limits to assign) insertLV <- function(l,o,p) {l[[p]] <- o; return(l)} # Light multicolor for robustness models. Text labels at right outfile <- "longplotBASE" mr <- 1 nmodels <- length(folders) whichModels <- c(1) modelLab <- c("Original Model") colGroup <- c(1, 2, 3, 4, 4, 4, 5, 5, 5, 6) colList <- c("black", brewer.pal(5,"Set1")[c(3,4,1,2,5)]) ltyGroup <- c(1, 2, 2, 2,2,2, 2,2,2, 2) ltyList <- c(1, 1, 1, 1,1,1, 1,1,1, 2) lwdGroup <- c(1, 2, 2, 2,2,2, 2,2,2, 2) lwdList <- c(1, 0.8, 0.8, 0.8,0.8,0.8, 0.8,0.8,0.8, 0.8) layerGroup <- c(1, 2, 2, 2,2,2, 2,2,2, 2) layerList <- c(5, 7, 7, 7,7,7, 7,7,7, 7) ciGroup <- c(1, 2, 2, 2,2,2, 2,2,2, 2) ciList <- c(0, 0, 0, 0,0,0, 0,0,0, 0) ciList <- rep(1,10) # Set labels for x axis toplabels <- c("12th", "13th", "14th", "15th", "16th" ) bottomlabels <- c(1982,1987,1992,1997,2002) # Loop over variables cases <- list(a05 = "Mao Faction", a06 = "Long Marcher", a07 = "2nd Field Army Vet", a08 = "Deng Faction", a09 = "Hu Yaobang Faction", a10 = "Zhao Ziyang Faction", a11 = "Jiang Zemin Faction", a12 = "Hu Jintao Faction", a13 = "Princeling", a14 = c("Relative GDP Growth +1 sd", "AR Unexpected GDP Growth +1 sd", "ARMA Unexpected GDP Growth +1 sd", "Relative Econ Growth (f) +1 sd", "AR Unexpected Econ Growth (f) +1 sd", "ARMA Unexpected Econ Growth (f) +1 sd"), a15 = c("Relative Fiscal Growth +1 sd", "AR Unexpected Fiscal Growth +1 sd", "ARMA Unexpected Fiscal Growth +1 sd", "Relative Fiscal (f) +1 sd","AR Unexpected Fiscal (f) +1 sd", "ARMA Unexpected Fiscal (f) +1 sd"), a03 = "College vs < HS", a00 = "Minority", a01 = "Female", a04 = "Graduate School vs < HS" ) subplotTitle <- c("Mao Faction", "Long Marcher", "2nd Field Army Vet", "Deng Faction", "Hu Yaobang Fact'n", "Zhao Ziyang\nFaction", "Jiang Zemin\nFaction", "Hu Jintao Faction", "Princeling", "GDP Growth +1 sd", "Fiscal Revenue +1 sd", "College Degree", "Ethnic\nMinority", "Female", "Graduate Degree" ) plotlist <- c(1,1,1,1,1, 2,2,2,2, 3,3,3, 4,4,4) topbottom <- c(1,0,0,0,0, 1,1,1,1, 1,1,0, 1,1,0) minX <- 0 maxX <- 18 cex <- 0.7 spaceX <- list(plot1 = c(0,1,1,1,1), plot2 = c(1,1,1,2), plot3 = c(0,1,1), plot4 = c(0,1,1) ) lastX <- c(0,0,0,0) nextX <- c(0,0,0,0) nplots <- length(lastX) atX <- labelsX <- list(plot1=NULL, plot2=NULL, plot3=NULL, plot4=NULL ) titleTrace <- vector("list",length(cases)) # Loop over covariates, grouped by concept (subplots) for (j in 1:length(cases)) { # Make list vector to store traces for this covariate assign(paste("trace",j,sep=""), vector("list",nmodels)) # increment nextX nextX[plotlist[j]] <- nextX[plotlist[j]] + 1 # Loop over models (traces) for (k in whichModels) { y <- lower <- upper <- NULL x <- c(1982,1987,1992,1997,2002) # Loop over years (x-axis) for (i in 1:length(filenames)) { # extract percentiles and CI's data <- read.csv(paste(folders[k],filenames[i],sep=""),header=TRUE) cur <- rep(0,length(data$case)) for (m in 1:length(cur)) cur[m] <- any(data$case[m]==cases[[j]]) cur <- (cur==1) if (any(cur)) { y <- c(y,data$px[cur]) lower <- rbind(lower, c(data$lowerpx1[cur], data$lowerpx2[cur] ) ) upper <- rbind(upper, c(data$upperpx1[cur], data$upperpx2[cur] ) ) } else { y <- c(y,NA) lower <- rbind(lower,c(NA,NA)) upper <- rbind(upper,c(NA,NA)) } } # Delete missing rows complete <- na.omit(cbind(x,y,lower,upper)) x <- complete[,1] y <- complete[,2] lower <- complete[,3:4] upper <- complete[,5:6] # Convert to shared long plot x's if (k==1) { labelsX[[plotlist[j]]] <- c(labelsX[[plotlist[j]]], paste("'",substr(as.character(x),3,4),sep="")) midX <- lastX[plotlist[j]] + spaceX[[plotlist[j]]][nextX[plotlist[j]]] + mean(1:length(x)) x <- lastX[plotlist[j]] + spaceX[[plotlist[j]]][nextX[plotlist[j]]] + (1:length(x)) atX[[plotlist[j]]] <- c(atX[[plotlist[j]]], x) lastX[plotlist[j]] <- max(x) oldX <- x } else { x <- oldX } # Create subplot titles if (k==1) { titleTrace[[j]] <- textTile(labels = subplotTitle[j], x=midX, y=40*topbottom[j] -20*(1-topbottom[j]), alpha=1, cex=cex-.1, plot=plotlist[j]) } # Make data traces if (!ciList[ciGroup[k]]) { newtrace <- lineplot(x=x, y=y, col=colList[colGroup[k]], lty=ltyList[ltyGroup[k]], lwd=lwdList[lwdGroup[k]], alpha=1, layer=layerList[layerGroup[k]], plot=plotlist[j] ) } else { newtrace <- lineplot(x=x, y=y, lower=as.matrix(lower), upper=as.matrix(upper), ci=list(mark=c("shaded","dashed")), col=colList[colGroup[k]], lty=ltyList[ltyGroup[k]], lwd=lwdList[lwdGroup[k]], alpha=1, layer=layerList[layerGroup[k]], plot=plotlist[j] ) } assign(paste("trace",j,sep=""), insertLV(get(paste("trace",j,sep="")), newtrace, k)) } } zeroline <- vector("list",nplots) for (i in 1:nplots) { zeroline[[i]] <- linesTile(x = c(minX,maxX), y=c(0,0), lwd=0.5,lex=0.5, alpha=1, plot=i ) } baselineLabel <- textTile(labels=modelLab[whichModels[1]], x = 4, y = -70, cex= cex + 0.1, col = "black", clip="off", alpha=1, plot=nplots ) baselineLine <- linesTile(x = c(1,2), y = c(-70,-70), col=colList[colGroup[whichModels[1]]], lty=ltyList[ltyGroup[whichModels[1]]], lwd=lwdList[lwdGroup[whichModels[1]]], clip="off", alpha=1, plot=nplots ) robustLabel <- textTile(labels=modelLab[whichModels[2]], x = 14, y = -70, cex= cex + 0.1, col=colList[colGroup[whichModels[2]]], lty=ltyList[ltyGroup[whichModels[2]]], lwd=lwdList[lwdGroup[whichModels[2]]], clip="off", alpha=1, plot=nplots ) robustLine <- linesTile(x = c(9.5,10.5), y = c(-70,-70), col=colList[colGroup[whichModels[2]]], lty=ltyList[ltyGroup[whichModels[2]]], lwd=lwdList[lwdGroup[whichModels[2]]], clip="off", alpha=1, plot=nplots ) divider <- linesTile(x=c(12,12), y=c(-30,50), lwd=0.5,lex=0.5, col="gray50", plot=3:4) # Make lineplot yat <- c(-30,-20,-10,0,10,20,30,40,50) ylab <- as.character(seq(-30,50,by=10)) ylab <- c(" ","-20"," ","0"," ","20"," ","40"," ") tile(trace1[[1]], trace2[[1]], trace3[[1]], trace4[[1]], trace5[[1]], trace6[[1]], trace7[[1]], trace8[[1]], trace9[[1]], trace10[[1]], trace11[[1]], trace12[[1]], trace13[[1]], trace14[[1]], trace15[[1]], zeroline, titleTrace, divider, limits=c(minX,maxX,-30,50), RxC=c(4,1), xaxis=list(at1=atX[[1]],at2=atX[[2]],at3=atX[[3]],at4=atX[[4]], labels1=labelsX[[1]],labels2=labelsX[[2]],labels3=labelsX[[3]],labels4=labelsX[[4]], cex=cex+0.05), yaxis=list(major=FALSE,at=yat,labels=ylab,cex=cex+0.05), yaxistitle=list(cex=cex-0.10,labels="Change in \n Rank Percentile", x=1.15), rowtitle=list(labels1="Faction", labels2="Faction", labels3="Merit", labels4="Demography/Merit"), width=list(spacer=0,plot=10,yaxistitle=0.15,yaxis.labelspace=-0.50), height=list(plot=2,spacer=0.75), output = list(wide=7.5,outfile=paste(outfile,mr,sep=""),type="pdf") )