# ------------------------------------------------------------------------------ # LMF_test.R # Grabs Lane and Milesi-Ferretti database on net foreign assets, described in # "The External Wealth of Nations," JIE, 2007 # See paper at: http://www.imf.org/external/pubs/cat/longres.aspx?sk=18942.0 # Data is large flat file, countries stacked up, 1970-2007 or subset # Written by Espen Henriksen, September 2010 # Adapted by Dave Backus # ------------------------------------------------------------------------------ # 0. Preliminaries # clear workspace ("remove" everything in list generated by ls command) rm(list=ls()) # set working directory for output # NB: check your own directory, and make sure to use / not \ dir <- "c:/Users/dbackus/My Documents/Papers/BCH/data/LMF" setwd(dir) # load libraries of apps (must be installed -- and the second needs perl, too) library(gtools) library(gdata) # 1. Download data # download data and save in "fname" (dated to avoid overwriting old data) # url = internet source of data, destfile = filename for local version fname = paste("LMF99_",Sys.Date(),".zip",sep="") # creates string with date download.file(url="http://www.imf.org/external/pubs/ft/wp/2006/data/update/wp0669.zip",destfile=fname,method="auto") # input data sheet (first one reads data, second one reads headers for variable names) lmf.data <- read.xls(unzip(zipfile=fname,files="EWN II update for web (August 2009).xls",list=FALSE),header=TRUE,skip=1,sheet=4) series.names <- read.xls(unzip(zipfile=fname,files="EWN II update for web (August 2009).xls",list=FALSE),header=FALSE,skip=0,sheet=4,nrows=1) series.names.old <- series.names print(t(series.names.old)) # why? seems redundant series.names[1] <- "Country" series.names[2] <- "IFS.code" series.names[3] <- "Year" series.names[4] <- "Portfolio equity assets" series.names[5] <- "Portfolio equity liabilities" series.names[6] <- "FDI assets" series.names[7] <- "FDI liabilities" series.names[8] <- "Debt assets (portfolio debt plus other investment)" series.names[9] <- "Debt liabilities (portfolio debt plus other investment)" series.names[10] <- "Financial derivatives assets" series.names[11] <- "Financial derivatives liabilities" series.names[12] <- "FX Reserves minus gold" series.names[13] <- "Total assets" series.names[14] <- "Total liabilities" series.names[15] <- "NFA" series.names[16] <- "Net IIP as officially reported" series.names[17] <- "NFA (alternative FDI valuation)" series.names[18] <- "GDP" series.names[19] <- "Portfolio debt assets" series.names[20] <- "Portfolio debt liabilities" series.names[21] <- "Other investment assets" series.names[22] <- "Other investment liabilities" series.names[23] <- "FDI assets (other valuation)" series.names[24] <- "FDI liabilities (other valuation)" series.names[25] <- "Exchange rate vis-a-vis US dollar (period avg)" series.names[26] <- "Exchange rate vis-a-vis US dollar (end of period)" # assign var names to data, [[2]] means names apply to columns (see also colnames) dimnames(lmf.data)[[2]] <- series.names # save for faster access (ie, start with load) save(lmf.data,file="LMF.Rdata") rm(list=ls()) load("LMF.Rdata") # 2. Extract subset # extract a subset of columns (variables) # note use of curly brackets {} to run across lines lmf.subset <- {data.frame(Country = lmf.data$Country, IFS = lmf.data$IFS.code, Year = lmf.data$Year, NFA = as.numeric(gsub(",","",lmf.data$NFA)), GDP = as.numeric(gsub(",","",lmf.data$GDP)), NFAGDP = as.numeric(gsub(",","",lmf.data$NFA)))} lmf.subset$NFAGDP <- lmf.subset$NFA/lmf.subset$GDP # extract a subsect of countries (rows) countries <- c("Australia","Canada","China,P.R.: Mainland", "Germany", "Japan", "United States") #c("Australia","Canada","China,P.R.: Mainland", #"Germany","France","Italy","Japan","Norway","United Kingdom","United States") lmf.oecd<- NULL for(i in 1:length(countries)){ lmf.oecd <- cbind(lmf.oecd,lmf.subset[lmf.subset$Country == countries[i],]$NFAGDP) } countries <- c("AUS", "CAN", "CHN", "DEU", "JPN", "USA") dimnames(lmf.oecd)[[2]] <- countries lmf.oecd <- ts(lmf.oecd,start=1970,frequency=1) # 2. Plot NFA/GDP for selected countries # color sequence {colseq <- c("green", "red", "purple", "black", "gold", "blue")}# line width sequence lwdthin <- 2 lwdthick <- 6 lwdseq <- 0*1:length(countries) + lwdthin {ts.plot(lmf.oecd, gpars=list(main="",xlab="",ylab="NFA/GDP", mgp=c(2.5,1,0), lwd=lwdseq, lty=1, col=colseq[1:dim(lmf.oecd)[2]], axes=TRUE) )} abline(h=0,lty=1,col="grey") # axis at y=0 legend("topleft",legend=countries, cex=0.90, lwd=2, col=colseq[1:dim(lmf.oecd)[2]], y.intersp=0.95, x.intersp=0.2, ncol=3, bty="n") mtext("Source: Lane and Milesi-Ferretti", side=1,line=2.5,cex=.6,adj=0) dev.copy2eps(device=postscript, file="LMF_nfagdp.eps",width=8,height=6) # extras for slides # US version lwdseq = 0*lwdseq + lwdthin; lwdseq[6] = lwdthick; {ts.plot(lmf.oecd, gpars=list(main="",xlab="",ylab="NFA/GDP", mgp=c(2.5,1,0), lwd=lwdseq, lty=1, col=colseq[1:dim(lmf.oecd)[2]], axes=TRUE) )} abline(h=0,lty=1,col="grey") legend("topleft",legend=countries, cex=0.90, lwd=2, col=colseq[1:dim(lmf.oecd)[2]], y.intersp=0.95, x.intersp=0.2, ncol=3, bty="n") text(1972,-0.6,cex=1.25,"USA") mtext("Source: Lane and Milesi-Ferretti", side=1,line=2.5,cex=.6,adj=0) dev.copy2eps(device=postscript, file="LMF_nfagdp_USA.eps",width=8,height=6) # CAN version lwdseq = 0*lwdseq + lwdthin; lwdseq[2] = lwdthick; {ts.plot(lmf.oecd, gpars=list(main="",xlab="",ylab="NFA/GDP", mgp=c(2.5,1,0), lwd=lwdseq, lty=1, col=colseq[1:dim(lmf.oecd)[2]], axes=TRUE) )} abline(h=0,lty=1,col="grey") legend("topleft",legend=countries, cex=0.90, lwd=2, col=colseq[1:dim(lmf.oecd)[2]], y.intersp=0.95, x.intersp=0.2, ncol=3, bty="n") text(1972,-0.6,cex=1.25,"CAN") mtext("Source: Lane and Milesi-Ferretti", side=1,line=2.5,cex=.6,adj=0) dev.copy2eps(device=postscript,file="LMF_nfagdp_CAN.eps",width=8,height=6) # CH version lwdseq = 0*lwdseq + lwdthin; lwdseq[3] = lwdthick; {ts.plot(lmf.oecd, gpars=list(main="",xlab="",ylab="NFA/GDP", mgp=c(2.5,1,0), lwd=lwdseq, lty=1, col=colseq[1:dim(lmf.oecd)[2]], axes=TRUE) )} abline(h=0,lty=1,col="grey") legend("topleft",legend=countries, cex=0.90, lwd=2, col=colseq[1:dim(lmf.oecd)[2]], y.intersp=0.95, x.intersp=0.2, ncol=3, bty="n") text(1972,-0.6,cex=1.25,"CHN") mtext("Source: Lane and Milesi-Ferretti", side=1,line=2.5,cex=.6,adj=0) dev.copy2eps(device=postscript, file="LMF_nfagdp_CHN.eps",width=8,height=6) # DEU version lwdseq = 0*lwdseq + lwdthin; lwdseq[3] = lwdthick; {ts.plot(lmf.oecd, gpars=list(main="",xlab="",ylab="NFA/GDP", mgp=c(2.5,1,0), lwd=lwdseq, lty=1, col=colseq[1:dim(lmf.oecd)[2]], axes=TRUE) )} abline(h=0,lty=1,col="grey") legend("topleft",legend=countries, cex=0.90, lwd=2, col=colseq[1:dim(lmf.oecd)[2]], y.intersp=0.95, x.intersp=0.2, ncol=3, bty="n") text(1972,-0.6,cex=1.25,"DEU") mtext("Source: Lane and Milesi-Ferretti", side=1,line=2.5,cex=.6,adj=0) dev.copy2eps(device=postscript,file="LMF_nfagdp_DEU.eps",width=8,height=6) # JP version lwdseq = 0*lwdseq + lwdthin; lwdseq[5] = lwdthick; {ts.plot(lmf.oecd, gpars=list(main="",xlab="",ylab="NFA/GDP", mgp=c(2.5,1,0), lwd=lwdseq, lty=1, col=colseq[1:dim(lmf.oecd)[2]], axes=TRUE) )} abline(h=0,lty=1,col="grey") legend("topleft",legend=countries, cex=0.90, lwd=2, col=colseq[1:dim(lmf.oecd)[2]], y.intersp=0.95, x.intersp=0.2, ncol=3, bty="n") text(1972,-0.6,cex=1.25,"JPN") mtext("Source: Lane and Milesi-Ferretti", side=1,line=2.5,cex=.6,adj=0) dev.copy2eps(device=postscript,file="LMF_nfagdp_JPN.eps",width=8,height=6) # AU version lwdseq = 0*lwdseq + lwdthin; lwdseq[1] = lwdthick; {ts.plot(lmf.oecd, gpars=list(main="",xlab="",ylab="NFA/GDP", mgp=c(2.5,1,0), lwd=lwdseq, lty=1, col=colseq[1:dim(lmf.oecd)[2]], axes=TRUE) )} abline(h=0,lty=1,col="grey") legend("topleft",legend=countries, cex=0.90, lwd=2, col=colseq[1:dim(lmf.oecd)[2]], y.intersp=0.95, x.intersp=0.2, ncol=3, bty="n") text(1972,-0.6,cex=1.25,"AUS") mtext("Source: Lane and Milesi-Ferretti", side=1,line=2.5,cex=.6,adj=0) dev.copy2eps(device=postscript, file="LMF_nfagdp_AUS.eps",width=8,height=6)