# ------------------------------------------------------------------------------ # LMF_nfa.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 \ setwd("c:/Documents and Settings/dbackus/My Documents/Userdata/Papers/BCH/data/LMF") # 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 # 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) # why not use IFS code?? countries <- 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) } dimnames(lmf.oecd)[[2]] <- countries lmf.oecd <- ts(lmf.oecd,start=1970,frequency=1) # Line plot of NFA/GDP for subset of countries # color sequence {colseq <- c("black", "green", "blue", "orange", "yellow", "red", "aquamarine", "azure","brown", "burlywood")} {ts.plot(lmf.oecd, gpars=list(main=NULL,xlab=NULL,ylab="NFA/GDP", mgp=c(2.5,1,0), lwd=c(1,2,3), lty=1, col=colseq[1:dim(lmf.oecd)[2]], axes=TRUE) )} abline(h=0,lty=1,col="black") dev.print(device=postscript, "linesnfagdp.eps") #, onefile=FALSE, horizontal=FALSE) #dev.print(device=pdf, "linesnfagdp.pdf") # Bar plot NFA/GDP for that subset of countries tab <- rbind(lmf.oecd[11,],lmf.oecd[16,],lmf.oecd[21,],lmf.oecd[26,],lmf.oecd[31,],lmf.oecd[36,]) dimnames(tab)[[2]] <- c("AU","CA","CH","DE","FR","IT","JP","NO","UK","US") barplot(tab, beside = TRUE,col=c("grey","blue","orange","darkblue","red","darkgreen"), ylab="NFA/GDP",ylim=c(-.6,.6)) legend("topleft", legend = c("1980","1985","1990","1995","2000","2005"), fill=c("grey","blue","orange","darkblue","red","darkgreen"),bty="n") dev.print(device=postscript, "barsnfagdp.eps", onefile=FALSE, horizontal=FALSE) dev.print(device=pdf, "barsnfagdp.pdf")