> library(ggplot2)
> library(car)
> library(RJSONIO)
>
> ftse=read.delim("201112_FTSE_Market_Caps.tsv",sep="\t")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file '201112_FTSE_Market_Caps.tsv': No such file or directory
>
> ##See www.msci.com/products/indices/tools/index.html
> ##Emerging::Brazil, Chile, China, Colombia, Czech Republic, Egypt, Hungary,
> #India, Indonesia, Korea, Malaysia, Mexico, Morocco, Peru, Philippines,
> #Poland, Russia, South Africa, Taiwan, Thailand, Turkey
> ##Europe::Austria, Belgium, Denmark, Finland, France, Germany, Greece, Ireland, Italy,
> #Netherlands, Norway, Portugal, Spain, Sweden, Switzerland, United Kingdom
> ##Asia::Japan, Australia, Hong Kong, New Zealand, Singapore
>
> recodestring=" c('USA','CAN')='North America';
+ c('BRA','CHL','CHN','COL','CZE','EGY','HUN','IND','IDN','KOR','MYS','MEX','MAR','PER','PHL','POL',
+ 'RUS','ZAF','TWN','THA','TUR')= 'Emerging';
+ c('AUT','BEL','DNK','FIN','FRA','DEU','GRC','IRL','ITA',
+ 'NLD','NOR','PRT','ESP','SWE','CHE','GBR')= 'Europe';
+ c('JPN','AUS','HKG','NZL','SGP')= 'Asia';
+ else='Other'"
>
> #How much of the world do we want to consider (to keep graphs manageable)
> CUTOFF=.9
> #Some code from internet to read world bank data. Adapted from
> #http://www.r-bloggers.com/accessing-and-plotting-world-bank-data-with-r/
> #See also http://data.worldbank.org/indicator/NY.GDP.MKTP.CD
> getWorldBankData = function(id='NY.GDP.MKTP.CD', date='2010:2011',
+ value="value", per.page=12000){
+ require(RJSONIO)
+ url = paste("http://api.worldbank.org/countries/all/indicators/", id,
+ "?date=", date, "&format=json&per_page=", per.page,
+ sep="")
+ wbData = fromJSON(url)[[2]]
+ wbData = data.frame(
+ year = as.numeric(sapply(wbData, "[[", "date")),
+ value = as.numeric(sapply(wbData, function(x)
+ ifelse(is.null(x[["value"]]),NA, x[["value"]]))),
+ country.name = sapply(wbData, function(x) x[["country"]]['value']),
+ country.id = sapply(wbData, function(x) x[["country"]]['id'])
+ )
+ names(wbData)[2] = value
+ return(wbData)
+ }
> getWorldBankCountries = function(){
+ require(RJSONIO)
+ wbCountries =
+ fromJSON("http://api.worldbank.org/countries?per_page=12000&format=json")
+ wbCountries = data.frame(t(sapply(wbCountries[[2]], unlist)))
+ levels(wbCountries$region.value) = gsub("\\(all income levels\\)",
+ "", levels(wbCountries$region.value))
+ return(wbCountries)
+ }
> ### Get the most recent year with data
> curryear = as.numeric(format(Sys.Date(), "%Y"))
> years = paste("2010:", curryear, sep="")
> ##Get GDP for the countries (GDP in Current US Dollars)
> GDP = getWorldBankData(id='NY.GDP.MKTP.CD',
+ date=years,
+ value="gdp")
> #If a year has > 60 NAs it is not ready
> while( sum(is.na(subset(GDP,year==curryear)$gdp)) > 60 )
+ {curryear=curryear-1}
> #Keep just the most current year
> GDP=subset(GDP,year==curryear);
> ## Get country mappings
> wbCountries = getWorldBankCountries()
> ## Add regional information
> GDP = merge(GDP, wbCountries[c("iso2Code", "region.value", "id")],
+ by.x="country.id", by.y="iso2Code")
> ## Filter out the aggregates, NAs, and irrelevant columns
> GDP = subset(GDP, !region.value %in% "Aggregates" & !is.na(gdp))
> GDP=GDP[,c('year','gdp','country.name','region.value','id')]
> GDP=GDP[order(-1*GDP[,2]),]
> GDP$prop=with(GDP,gdp/sum(gdp))
> GDP$cumsum=cumsum(GDP$gdp)
> GDP$cumprop=with(GDP,cumsum(gdp)/sum(gdp))
> GDP$market.region=recode(GDP$id,recodestring)
Error in `$<-.data.frame`(`*tmp*`, "market.region", value = 1L) :
replacement has 1 rows, data has 0
> resize.win <- function(Width=6, Height=6)
+ {
+ plot.new()
+ dev.off();
+ #dev.new(width=6, height=6)
+ windows(record=TRUE, width=Width, height=Height)
+ }
> slice=max(which(GDP$cumprop<=CUTOFF))
Warning message:
In max(which(GDP$cumprop <= CUTOFF)) :
no non-missing arguments to max; returning -Inf
> smGDP=GDP[1:slice,]
Error in 1:slice : result would be too long a vector
> smGDP$country.name=factor(smGDP$country.name,levels=unique(smGDP$country.name))
Error in factor(smGDP$country.name, levels = unique(smGDP$country.name)) :
object 'smGDP' not found
> smGDP$region.value=factor(smGDP$region.value,levels=unique(smGDP$region.value))
Error in factor(smGDP$region.value, levels = unique(smGDP$region.value)) :
object 'smGDP' not found
> smGDP$market.region=factor(smGDP$market.region,levels=unique(smGDP$market.region))
Error in factor(smGDP$market.region, levels = unique(smGDP$market.region)) :
object 'smGDP' not found
> g=ggplot(smGDP,aes(x=as.factor(country.name),y=prop))
Error in ggplot(smGDP, aes(x = as.factor(country.name), y = prop)) :
object 'smGDP' not found
> g=g+geom_bar(aes(fill=market.region))
> g=g+opts(axis.text.x=theme_text(angle=90, hjust=1))+xlab(NULL)+
+ ylab( paste("Proportion of global GDP\nTop ",CUTOFF*100,"% countries",sep=""))
> g=g+opts(legend.position=c(.8,.8))
> resize.win(5,5) #might have to run this twice to get it to shift from the default window
> g
Error in if (length(range) == 1 || diff(range) == 0) { :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
> ggsave("GDPbyCountry.png",g,dpi=600)
Saving 5" x 5" image
Error in if (length(range) == 1 || diff(range) == 0) { :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
> smGDPsum=ddply(GDP,.(market.region),function(x) {sum(x$gdp)})
> smGDPsum$V1=smGDPsum$V1/sum(smGDPsum$V1)
> smGDPsum=smGDPsum[order(-1*smGDPsum[,2]),]
> smGDPsum$market.region=factor(smGDPsum$market.region,levels=c("North America","Emerging","Asia","Europe","Other"))
> g=ggplot(smGDPsum,aes(x=as.factor(market.region),y=V1))
> g=g+geom_bar(aes(fill=market.region))
> g=g+opts(axis.text.x=theme_text(angle=90, hjust=1))+xlab(NULL)+ylab("GDP of Countries by Market Region\n")
> g=g+opts(legend.position="none")
> resize.win(5,5) #might have to run this twice to get it to shift from the default window
> g
Error in if (length(range) == 1 || diff(range) == 0) { :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
> ggsave("GDPbyRegion.png",g,dpi=600)
Saving 5" x 5" image
Error in if (length(range) == 1 || diff(range) == 0) { :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
> ##Get Market Cap data from World Bank CM.MKT.LCAP.CD
> ### Get the most recent year with data
> curryear = as.numeric(format(Sys.Date(), "%Y"))
> years = paste("2010:", curryear, sep="")
> ##Get MarketCap for the countries (MCP in Current US Dollars)
> MCP = getWorldBankData(id='CM.MKT.LCAP.CD',
+ date=years,
+ value="mcp")
> #If a year has > 60 NAs it is not ready
> while( sum(is.na(subset(MCP,year==curryear)$mcp)) > 150 )
+ {curryear=curryear-1}
> #Keep just the most current year
> MCP=subset(MCP,year==curryear);
> ## Add regional information
> MCP=merge(MCP, wbCountries[c("iso2Code", "region.value", "id")],
+ by.x="country.id", by.y="iso2Code")
> ## Filter out the aggregates, NAs, and irrelevant columns
> MCP = subset(MCP, !region.value %in% "Aggregates" & !is.na(mcp))
> MCP=MCP[,c('year','mcp','country.name','region.value','id')]
> MCP=MCP[order(-1*MCP[,2]),]
> row.names(MCP)=NULL
> MCP$prop=with(MCP,mcp/sum(mcp))
> MCP$cumsum=cumsum(MCP$mcp)
> MCP$cumprop=with(MCP,cumsum(mcp)/sum(mcp))
> MCP$market.region=recode(MCP$id, recodestring)
Error in `$<-.data.frame`(`*tmp*`, "market.region", value = 1L) :
replacement has 1 rows, data has 0
> slice=max(which(MCP$cumprop<=CUTOFF))
Warning message:
In max(which(MCP$cumprop <= CUTOFF)) :
no non-missing arguments to max; returning -Inf
> smMCP=MCP[1:slice,]
Error in 1:slice : result would be too long a vector
> smMCP$country.name=factor(smMCP$country.name,levels=unique(smMCP$country.name))
Error in factor(smMCP$country.name, levels = unique(smMCP$country.name)) :
object 'smMCP' not found
> smMCP$region.value=factor(smMCP$region.value,levels=unique(smMCP$region.value))
Error in factor(smMCP$region.value, levels = unique(smMCP$region.value)) :
object 'smMCP' not found
> smMCP$market.region=factor(smMCP$market.region,levels=unique(smMCP$market.region))
Error in factor(smMCP$market.region, levels = unique(smMCP$market.region)) :
object 'smMCP' not found
> g=ggplot(smMCP,aes(x=as.factor(country.name),y=prop))
Error in ggplot(smMCP, aes(x = as.factor(country.name), y = prop)) :
object 'smMCP' not found
> g=g+geom_bar(aes(fill=market.region))
> g=g+opts(axis.text.x=theme_text(angle=90, hjust=1))+xlab(NULL)+
+ ylab( paste("Proportion of global Market Cap\nTop ",CUTOFF*100,"% countries",sep=""))
> g=g+opts(legend.position=c(.8,.8))
> resize.win(5,5) #might have to run this twice to get it to shift from the default window
> g
Error in if (length(range) == 1 || diff(range) == 0) { :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
> ggsave("MCPbyCountry.png",g,dpi=600)
Saving 5" x 5" image
Error in if (length(range) == 1 || diff(range) == 0) { :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
> smMCPsum=ddply(MCP,.(market.region),function(x) {sum(x$mcp)})
> smMCPsum$V1=smMCPsum$V1/sum(smMCPsum$V1)
> smMCPsum=smMCPsum[order(-1*smMCPsum[,2]),]
> smMCPsum$market.region=factor(smMCPsum$market.region,levels=c("North America","Emerging","Asia","Europe","Other"))
> g=ggplot(smMCPsum,aes(x=as.factor(market.region),y=V1))
> g=g+geom_bar(aes(fill=market.region))
> g=g+opts(axis.text.x=theme_text(angle=90, hjust=1))+xlab(NULL)+ylab("Market Cap by Market Region\n")
> g=g+opts(legend.position="none")
> resize.win(5,5) #might have to run this twice to get it to shift from the default window
> g
Error in if (length(range) == 1 || diff(range) == 0) { :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
> ggsave("MCPbyRegion.png",g,dpi=600)
Saving 5" x 5" image
Error in if (length(range) == 1 || diff(range) == 0) { :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
>