Created
May 17, 2012 09:30
-
-
Save xmarquez/2717743 to your computer and use it in GitHub Desktop.
Code for post on the economic performance of democracies and dictatorships through history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uds <- read.csv("../Data/uds_summary.csv") | |
pwt <- read.csv("../Data/pwt70_w_country_names.csv") | |
codes <- read.csv("../Data/codes.csv") | |
require(countrycode) | |
require(plyr) | |
require(ggplot2) | |
codes$isocode <- countrycode(codes$cowcode,"cown","iso3c") | |
names(pwt)[1] <- "countryname.pwt" | |
pwt <- ddply(pwt,.(isocode), transform, cgdpgrowthch = 100*diff(rgdpch)/rgdpch) | |
pwt <- ddply(pwt,.(isocode), transform, cgdpgrowthl = 100*diff(rgdpl)/rgdpl) | |
uds$regime <- cut(uds$mean,breaks=3,labels=c("Dictatorship","Hybrid","Democracy")) | |
uds$regime.2 <- cut(uds$mean,breaks=2, | |
labels=c("More or less non-democratic", | |
"More or less democratic")) | |
uds <- ddply(uds,.(year,regime),transform,number.of.regimes=length(regime)) | |
uds <- ddply(uds,.(year,regime.2),transform,number.of.regimes.2=length(regime.2)) | |
uds <- merge(uds,codes,all.x=TRUE) | |
uds.and.pwt <- merge(uds,pwt) | |
uds.and.pwt <- ddply(uds.and.pwt,.(year, regime), transform, avgpcgdp = mean(rgdpl,na.rm=TRUE)) | |
uds.and.pwt <- ddply(uds.and.pwt,.(year, regime), transform, medianpcgdp = median(rgdpl,na.rm=TRUE)) | |
# for a nice pointrange graph of scores for 2008 | |
uds.2008.table <- subset(uds, year == 2008) | |
uds.2008 <- ggplot(uds.2008.table, | |
aes(x=country, | |
y=mean, | |
ymin=pct025, | |
ymax=pct975, | |
color=regime)) + geom_pointrange() + coord_flip() | |
# Distribution of regimes per year | |
distribution.per.year <- qplot(x=year, | |
fill=regime, | |
data=uds.and.pwt, | |
ylab="Number of countries", | |
binwidth=1) | |
#per capita GDP growth and regime type per year | |
growth.per.regime <- qplot(x=year,y=cgdpgrowthch, | |
color=regime, | |
data=uds.and.pwt, | |
stat="summary", | |
fun.y="median", | |
size=number.of.regimes) + geom_smooth() + labs(colour="Regime type",y="Per capita gdp growth",size="Number of countries") | |
growth.per.regime.2 <- qplot(x=year,y=cgdpgrowthch, | |
color=regime.2, | |
data=uds.and.pwt, | |
stat="summary", | |
fun.y="median", | |
size=number.of.regimes.2) + geom_smooth() + labs(colour="Regime type",y="Per capita gdp growth",size="Number of countries") | |
growth.per.regime.3 <- qplot(x=year,y=cgdpgrowthch, | |
color=regime, | |
data=uds.and.pwt, | |
stat="summary", | |
fun.y="median", | |
size=avgpcgdp) + geom_smooth() + labs(colour="Regime type",y="Per capita gdp growth",size="Avg per capita gdp") | |
growth.per.regime.4 <- qplot(x=year,y=cgdpgrowthch, | |
color=regime, | |
data=uds.and.pwt, | |
stat="summary", | |
fun.y="median", | |
size=medianpcgdp) + geom_smooth() + labs(colour="Regime type",y="Per capita gdp growth",size="Median per capita gdp") | |
#plot of basic relation between democracy and growth | |
income.and.growth <- qplot(x=mean,y=cgdpgrowthch,data=uds.and.pwt,geom="smooth") | |
income.and.growth + labs(x="Unified Democracy Score",y="Per capita growth") | |
#plot per year of the relation between democracy and income | |
income.per.regime <- qplot(x=year,y=rgdpch, | |
color=regime, | |
data=uds.and.pwt, | |
stat="summary", | |
fun.y="median", | |
size=number.of.regimes) + geom_smooth() + labs(colour="Regime type",y="Per capita gdp",size="Number of countries") | |
income.per.regime.2 <- qplot(x=year,y=rgdpch, | |
color=regime.2, | |
data=uds.and.pwt, | |
stat="summary", | |
fun.y="median", | |
size=number.of.regimes.2) + geom_smooth() + labs(colour="Regime type",y="Per capita gdp",size="Number of countries") | |
linmod <- function(df) { | |
lm(cgdpgrowthch ~ df$mean + log(rgdpch), data = df) | |
} | |
models <- dlply(uds.and.pwt, .(year), linmod) | |
coefs <- ldply(models,coef) | |
names(coefs) <- c("year","intercept","uds_mean","log_rgdpch") | |
democracy.advantage <- qplot(x=year,y=uds_mean, | |
data=coefs, | |
geom=c("smooth","point")) + labs(y="Democracy advantage") | |
democracy.advantage + geom_hline(y=0,color="red") | |
linmod.2 <- function(df) { | |
lm(cgdpgrowthch ~ df$mean, data = df) | |
} | |
models.2 <- dlply(uds.and.pwt, .(year), linmod.2) | |
coefs.2 <- ldply(models.2,coef) | |
names(coefs.2) <- c("year","intercept","uds_mean") | |
democracy.advantage.2 <- qplot(x=year,y=uds_mean, | |
data=coefs.2, | |
geom=c("smooth","point")) + labs(y="Democracy advantage") | |
linmod.3 <- function(df) { | |
lm(cgdpgrowthch ~ df$mean + log(rgdpch), data = df) | |
} | |
models.3 <- dlply(uds.and.pwt, .(un_region_name), linmod.3) | |
coefs.3 <- ldply(models.3,coef) | |
names(coefs.3) <- c("Region","intercept","uds_mean", "log_gdp") | |
democracy.advantage.3 <- qplot(x=Region,y=uds_mean, | |
data=coefs.3, | |
geom=c("point")) + labs(y="Democracy advantage") + coord_flip() | |
democracy.advantage.3 + geom_hline(y=0,colour="red") | |
linmod.4 <- function(df) { | |
lm(cgdpgrowthch ~ df$mean + log(rgdpch), data = df) | |
} | |
models.4 <- dlply(uds.and.pwt, .(un_continent_name), linmod.4) | |
coefs.4 <- ldply(models.4,coef) | |
names(coefs.4) <- c("Region","intercept","uds_mean", "log_gdp") | |
democracy.advantage.4 <- qplot(x=Region,y=uds_mean, | |
data=coefs.4, | |
geom=c("point")) + labs(y="Democracy advantage") + coord_flip() | |
democracy.advantage.4 + geom_hline(y=0,colour="red") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment