Created
October 13, 2012 18:42
-
-
Save chrishanretty/3885712 to your computer and use it in GitHub Desktop.
Replicates Chris Giles' analysis of IMF WEO
This file contains 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
library(gdata) | |
library(countrycode) | |
infile <- "IMFmultipliers.xls" | |
imf_rep <- read.xls(infile, sheet = 2, | |
skip = 3, | |
header=F) | |
names(imf_rep) <- c("Country", | |
"gdp_4cast_2011","gdp_4cast_2012", | |
"struct_bal_2010","struct_bal_2011","struct_bal_2012", | |
"cyc_adj_prim_bal_2010","cyc_adj_prim_bal_2012", | |
NA,"gdp_4cast_2011_new","gdp_4cast_2012_new", | |
NA,NA,"cumgrowth_2010","cumgrowth_2012","Country.again", | |
"delta_growth","delta_struct_bal","delta_cyc_adj_prim_bal","ca_def") | |
imf_rep <- imf_rep[which(imf_rep$Country!=""),] | |
imf_rep$Country.again<-NULL | |
imf_rep$iso3c <- countrycode(imf_rep$Country,"country.name","iso3c") | |
imf_rep$iso3c [ which(imf_rep$Country == "Kosovo") ] <- "KOS" | |
rownames(imf_rep) <- imf_rep$iso3c | |
## Exclude missing countries | |
imf_rep <- imf_rep[,!is.na(names(imf_rep))] | |
imf_rep <- imf_rep[!is.na(imf_rep$delta_struct_bal),] | |
par (mar=c(3,3,2,1), mgp=c(2,.7,0), tck=-.01) | |
with(imf_rep, | |
plot(delta_struct_bal,delta_growth,type="n", | |
xlab = "Fiscal consolidation",ylab="Growth forecast error")) | |
with(imf_rep, | |
text(delta_struct_bal,delta_growth,iso3c,cex=.9)) | |
summary(imf.mod <- lm(delta_growth~delta_struct_bal,data=imf_rep)) | |
abline(imf.mod) | |
with(imf_rep, | |
text(min(delta_struct_bal,na.rm=T),max(delta_growth,na.rm=T), | |
paste("y=", | |
round(coef(imf.mod)["(Intercept)"],2), | |
"+ x * ", | |
round(coef(imf.mod)["delta_struct_bal"],2), | |
", p-value = ", | |
round(summary(imf.mod)$coef["delta_struct_bal","Pr(>|t|)"],2), | |
sep=""), | |
pos=4)) | |
png(file="imf_outliers.png",width=600,height=500) | |
par(mfrow=c(2,2),mar=c(3,3,2,1), mgp=c(2,.7,0), tck=-.01) | |
plot(imf.mod) | |
dev.off() | |
outliers <- names(which(cooks.distance(imf.mod)>1)) | |
imf.mod2 <- update(imf.mod, | |
data = imf_rep[!is.element(imf_rep$iso3c,outliers),]) | |
summary(imf.mod2) | |
png(file="imf_outliers2.png",width=600,height=500) | |
par(mfrow=c(2,2),mar=c(3,3,2,1), mgp=c(2,.7,0), tck=-.01) | |
plot(imf.mod2) | |
dev.off() | |
imf.mod3 <- update(imf.mod, | |
data = imf_rep[!is.element(imf_rep$iso3c,c("DEU","GRC")),]) | |
summary(imf.mod3) | |
## Do mash-up of all | |
png(file="imf_final.png",width=600,height=500) | |
par (mar=c(3,3,2,1), mgp=c(2,.7,0), tck=-.01) | |
with(imf_rep, | |
plot(delta_struct_bal,delta_growth,type="n", | |
xlab = "Fiscal consolidation",ylab="Growth forecast error")) | |
with(imf_rep, | |
text(delta_struct_bal,delta_growth,iso3c,cex=.9)) | |
abline(imf.mod) | |
abline(imf.mod2,lty=2,col="red") | |
abline(imf.mod3,lty=3,col="blue") | |
with(imf_rep, | |
text(min(delta_struct_bal,na.rm=T),max(delta_growth,na.rm=T), | |
paste("y=", | |
round(coef(imf.mod)["(Intercept)"],2), | |
"+ x * ", | |
round(coef(imf.mod)["delta_struct_bal"],2), | |
", p-value = ", | |
round(summary(imf.mod)$coef["delta_struct_bal","Pr(>|t|)"],2), | |
", All", | |
sep=""), | |
pos=4)) | |
with(imf_rep, | |
text(min(delta_struct_bal,na.rm=T),max(delta_growth,na.rm=T)-1, | |
paste("y=", | |
round(coef(imf.mod2)["(Intercept)"],2), | |
"+ x * ", | |
round(coef(imf.mod2)["delta_struct_bal"],2), | |
", p-value = ", | |
round(summary(imf.mod2)$coef["delta_struct_bal","Pr(>|t|)"],2), | |
", w/o ",paste(outliers,collapse=", "), | |
sep=""), | |
pos=4,col="red")) | |
with(imf_rep, | |
text(min(delta_struct_bal,na.rm=T),max(delta_growth,na.rm=T)-2, | |
paste("y=", | |
round(coef(imf.mod3)["(Intercept)"],2), | |
"+ x * ", | |
round(coef(imf.mod3)["delta_struct_bal"],2), | |
", p-value = ", | |
round(summary(imf.mod3)$coef["delta_struct_bal","Pr(>|t|)"],2), | |
", w/o GRC, DEU", | |
sep=""), | |
pos=4,col="blue")) | |
dev.off() | |
## Choose to exclude countries taken two at a time | |
combinations <- combn(unique(imf_rep$iso3c),2,simplify=F) | |
my.pvals <- rep(NA,length(combinations)) | |
for (i in 1:length(combinations)) { | |
my.subset <- imf_rep[!is.element(imf_rep$iso3c,combinations[[i]]),] | |
the.mod <- update(imf.mod,data=my.subset) | |
my.pvals[i] <- summary(the.mod)$coef[2,4] | |
} | |
length(my.pvals) | |
length(which(my.pvals>0.05)) | |
length(which(my.pvals>0.1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is pretty interesting, and I just forked it to remix the code for a class. However, I am confused about the naming of the columns. It seems that you import sheet #2 of this spreadsheet, but use the names from sheet #1, which are at T+1. As a result, your CAPB 2012 column, for instance, is actually CAPB 2011, based on the IMF WEO from the previous year. I hope to be mistaken.