Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code for post on the Good, the Bad, and the Ugly among regimes
CIRI <- read.csv("../Data/CIRI Human Rights Data.csv")
codes <- read.csv("../Data/extended codes.csv")
names(CIRI)[2] <- "year"
CIRI$ccode <- CIRI$COW
CIRI <- merge(CIRI,codes,by=c("year","ccode"))
names(polity)[2] <- "politycode"
names(CIRI)[6] <- "politycode"
CIRIplusPolity <- merge(CIRI,polity,by=c("politycode","year"))
CIRIplusPolity$polityCut <- cut(CIRIplusPolity$polity,breaks=c(-100,-11,-6,6,10),labels=c("Interruption","Autocracy","Anocracy","Democracy"))
dd <- read.csv("../Data/ddrevisited-data-v1.csv")
CIRIplusDD <- merge(CIRI,dd, by=c("politycode","year"))
cpolity <- subset(CIRIplusPolity, select=c("CIRI","year","polity2","polity","exconst","CAPLONG","CAPLAT"))
cddpolity <- merge(CIRIplusDD,cpolity)
rm(cpolity, CIRIplusDD, CIRI, CIRIplusPolity)
cddpolity <- subset(cddpolity, !is.na(PHYSINT) & exconst > 0)
cddpolity$democracy2 <- factor(cddpolity$democracy,labels=c("Non-democracy","Democracy"))
cddpolity$polityCut <- cut(cddpolity$polity2, breaks=c(-11,-6,6,10),labels=c("Autocracy","Anocracy","Democracy"))
cddpolity$regime2 <- factor(cddpolity$regime,labels=c("Parl. Dem.","Mixed Dem.","Pres. Dem.","Civilian Dict.","Military Dict.","Monarchy"))
cddpolity <- ddply(cddpolity, .(CIRI), transform, yearsMeasured = length(PHYSINT))
cddpolity <- ddply(cddpolity, .(CIRI, regime2, stra), transform, yearsMeasuredReg = length(PHYSINT))
cddpolity <- ddply(cddpolity, .(CIRI, democracy2, stra), transform, yearsMeasuredDem = length(PHYSINT))
cddpolity <- ddply(cddpolity, .(CIRI, regime2, stra), transform, meanPhysintReg = mean(PHYSINT))
cddpolity <- ddply(cddpolity, .(CIRI, democracy2, stra), transform, meanPhysintDem = mean(PHYSINT))
cddpolity <- ddply(cddpolity, .(CIRI, regime2, stra), transform, meanExconstReg = mean(exconst))
cddpolity <- ddply(cddpolity, .(CIRI, democracy2, stra), transform, meanExconstDem = mean(exconst))
cddpolity <- ddply(cddpolity, .(CIRI, democracy2, stra), transform, rangeYearsDem = paste(min(year),"-",max(year),sep=""))
cddpolity <- ddply(cddpolity, .(CIRI, regime2, stra), transform, rangeYearsReg = paste(min(year),"-",max(year),sep=""))
cddpolity$casesDem <- paste(cddpolity$dpicode,cddpolity$rangeYearsDem)
cddpolity$casesReg <- paste(cddpolity$dpicode,cddpolity$rangeYearsReg)
cddpolity$casesDemLong <- paste(cddpolity$CTRY,cddpolity$rangeYearsDem)
cddpolity$casesRegLong <- paste(cddpolity$CTRY,cddpolity$rangeYearsReg)
cddpolity$benevolence <- (cddpolity$PHYSINT*6/8) - (cddpolity$exconst - 1)
cddpolity <- ddply(cddpolity, .(casesDem), transform, meanBenevolenceDem = mean(benevolence))
cddpolity <- ddply(cddpolity, .(casesReg), transform, meanBenevolenceReg = mean(benevolence))
cddpolity$constraint <- ((cddpolity$PHYSINT*6/8) + (cddpolity$exconst - 1))/2
cddpolity <- ddply(cddpolity, .(casesReg), transform, meanConstraintReg = mean(constraint))
pwt <- read.csv("../Data/pwt70_w_country_names.csv")
library(countrycode)
pwt$isocode <-gsub("ROM","ROU",pwt$isocode)
pwt$isocode <-gsub("ZAR","COD",pwt$isocode)
pwt$isocode <-gsub("GER","DEU",pwt$isocode)
pwt$cowcode <- countrycode(pwt$isocode,"iso3c","cown")
pwt <- ddply(pwt,.(isocode), transform, cgdpgrowthch = 100*diff(rgdpch)/rgdpch)
pwt <- ddply(pwt,.(isocode), transform, cgdpgrowthl = 100*diff(rgdpl)/rgdpl)
cddpwt <- merge(cddpolity,pwt,by=c("cowcode","year"))
cddpwt <- ddply(cddpwt, .(casesReg), transform, meanRgdplReg = mean(rgdpl,na.rm=TRUE))
cddpwt <- ddply(cddpwt, .(casesDem), transform, meanRgdplDem = mean(rgdpl,na.rm=TRUE))
p <- qplot(data=cddpwt,x=meanExconstReg,y=meanPhysintReg,position="jitter",stat="unique",size=meanRgdplReg,log="size",colour=meanBenevolenceReg,shape=regime2)
p <- p + scale_colour_gradient2(low="red",high="blue")
p <- p + scale_size(range=c(2,10))
benevolentRegimes <- subset(cddpwt, meanBenevolenceReg > quantile(meanBenevolenceReg,probs=c(0,0.95))[2])
malevolentRegimes <- subset(cddpwt, meanBenevolenceReg < quantile(meanBenevolenceReg,probs=c(0.05,0))[1])
unconstrainedRegimes <- subset(cddpwt, meanConstraintReg < quantile(meanConstraintReg,probs=c(0.05,0))[1])
p <- p + labs(x="Avg. degree of executive constraint",y="Avg. Physical Integrity Index",shape="Regime type",colour="Avg \ndegree of \nbenevolence", size="Avg. GDP \nper \ncapita", title="Benevolent and malevolent regimes, 1981-2010")
p <- p + geom_text(data=benevolentRegimes,aes(x=meanExconstReg,y=meanPhysintReg,position="jitter",label=casesReg,hjust=-0.25,angle=45),size=2,color="black")
p <- p + geom_text(data=malevolentRegimes,aes(x=meanExconstReg,y=meanPhysintReg,position="jitter",label=casesReg,hjust=1.25,angle=45),size=2,color="black")
p <- p + geom_text(data=unconstrainedRegimes,aes(x=meanExconstReg,y=meanPhysintReg,position="jitter",label=casesReg,hjust=-0.25,angle=45),size=2,color="black")
p <- p + geom_hline(yintercept=4,color="red") + geom_vline(xintercept=4,color="red")
p <- p + annotate("text",x=c(2.5,5.5,2.5,5.5),y=c(6,6,2,2),label=c("The Good: benevolent despots","The Boring: constrained and ok","The Ugly: Unconstrained and bad","The Bad: malevolent regimes"))
ggsave(plot=p,file="Benevolent and Malevolent Regimes.png",width=12, height=7)
rankings <- qplot(data=cddpolity, x=reorder(casesRegLong,meanBenevolenceReg), y= benevolence, geom="boxplot", color=regime2) + geom_hline(yintercept=0,color="red")+coord_flip()
rankings <- rankings + scale_color_brewer(type="div",palette="RdYlBu")
rankings <- rankings + labs(y=paste("Benevolence (",expression(PHYSINT*6/8 - exconst + 1),")",sep=""),x="Regimes",color="Regime type")
cases <- unique(cddpolity[ order(cddpolity$meanBenevolenceReg), "casesRegLong" ])
rankings <- rankings + geom_vline(xintercept= match("United States of America 1981-2008",cases),linetype="dashed")
rankings <- rankings + geom_vline(xintercept= match("New Zealand 1981-2008",cases),linetype="dashed")
rankings <- rankings + geom_vline(xintercept= match("Venezuela 1981-2008",cases),linetype="dashed")
rankings
ggsave(file="Rankings of benvolence.png",width=8,height=30)
cddpolity <- ddply(cddpolity, .(CTRY), transform, meanBenevolence = mean(benevolence))
histories <- qplot(data=cddpolity, x=year, y=reorder(CTRY,meanBenevolence), colour=PHYSINT, size=polity2, geom="line") + scale_colour_gradient2(low="red",high="blue",midpoint=4)
cases <- unique(cddpolity[ order(cddpolity$meanBenevolence), "CTRY" ])
histories <- histories + geom_hline(yintercept= match("United States of America",cases),linetype="dashed")
histories <- histories + geom_hline(yintercept= match("New Zealand",cases),linetype="dashed")
histories <- histories + geom_hline(yintercept= match("Venezuela",cases),linetype="dashed")
histories <- histories + labs(y=paste("Countries listed in order of benevolence parameter (",expression(PHYSINT*6/8 - exconst + 1),")",sep=""),x="Year",color="Index of \nPhysical \nIntegrity", size="Polity2 \ndemocracy \nscore")
histories <- histories + geom_vline(xintercept=1990)
histories
ggsave("Histories.png",width=8,height=30)
cddpolity <- ddply(cddpolity, .(CTRY), transform, meanPhysintCtry = mean(PHYSINT))
histories.2 <- qplot(data=cddpolity, x=year, y=reorder(CTRY,meanPhysintCtry), colour=PHYSINT, size=polity2, geom="line") + scale_colour_gradient2(low="red",high="blue",midpoint=4)
cases <- unique(cddpolity[ order(cddpolity$meanPhysintCtry), "CTRY" ])
histories.2 <- histories.2 + geom_hline(yintercept= match("United States of America",cases),linetype="dashed")
histories.2 <- histories.2 + geom_hline(yintercept= match("New Zealand",cases),linetype="dashed")
histories.2 <- histories.2 + geom_hline(yintercept= match("Venezuela",cases),linetype="dashed")
histories.2 <- histories.2 + labs(y="Countries listed by average Physical Integrity Index during the period 1981-2008",x="Year",color="Index of \nPhysical \nIntegrity", size="Polity2 \ndemocracy \nscore")
histories.2 <- histories.2 + geom_vline(xintercept=1990)
histories.2
ggsave("Histories2.png",width=8,height=30)
cddpwt <- ddply(cddpwt, .(CTRY), transform, meanRgdplCtry = mean(rgdpl,na.rm=TRUE))
histories.3 <- qplot(data=cddpwt, x=year, y=reorder(CTRY,meanRgdplCtry), colour=PHYSINT, size=polity2, geom="line") + scale_colour_gradient2(low="red",high="blue",midpoint=4)
cases <- unique(cddpwt[ order(cddpwt$meanRgdplCtry), "CTRY" ])
histories.3 <- histories.3 + geom_hline(yintercept= match("United States of America",cases),linetype="dashed")
histories.3 <- histories.3 + geom_hline(yintercept= match("New Zealand",cases),linetype="dashed")
histories.3 <- histories.3 + geom_hline(yintercept= match("Venezuela",cases),linetype="dashed")
histories.3 <- histories.3 + labs(y="Countries listed by average GDP per capita during the period 1981-2008",x="Year",color="Index of \nPhysical \nIntegrity", size="Polity2 \ndemocracy \nscore")
histories.3 <- histories.3 + geom_vline(xintercept=1990)
histories.3
ggsave("Histories3.png",width=8,height=30)
cddpolity <- ddply(cddpolity, .(ehead), transform, meanPhysintEhead = mean(PHYSINT))
cddpolity <- ddply(cddpolity, .(ehead), transform, meanBenevolenceEhead = mean(benevolence))
cddpolity <- ddply(cddpolity, .(ehead), transform, meanConstraintEhead = mean(constraint))
cddpolity <- ddply(cddpolity, .(ehead), transform, casesEhead = paste(ehead,min(year),"-",max(year),dpicode))
benevolentLeaders <- subset(cddpolity, meanBenevolenceEhead > quantile(meanBenevolenceEhead,probs=c(0,0.99))[2])
malevolentLeaders <- subset(cddpolity, meanBenevolenceEhead < quantile(meanBenevolenceEhead,probs=c(0.01,0))[1])
unconstrainedLeaders <- subset(cddpolity, meanConstraintEhead < quantile(meanConstraintEhead,probs=c(0.01,0))[1])
unique(benevolentLeaders$casesEhead)
unique(malevolentLeaders$casesEhead)
unique(unconstrainedLeaders$casesEhead)
unconstrainedRegimes <- subset(cddpolity, meanConstraintReg < quantile(meanConstraintReg,probs=c(0.05,0))[1])
benevolentRegimes <- subset(cddpolity, meanBenevolenceReg > quantile(meanBenevolenceReg,probs=c(0.95,0))[1])
malevolentRegimes <- subset(cddpolity, meanBenevolenceReg < quantile(meanBenevolenceReg,probs=c(0.05,0))[1])
unique(unconstrainedRegimes[, c("CTRY","casesReg","ehead")])
unique(benevolentRegimes[, c("CTRY","casesReg","ehead")])
unique(malevolentRegimes[, c("CTRY","casesReg","ehead")])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.