Please see http://timelyportfolio.blogspot.com/search/label/horizonplot for all horizon plot posts.
Once more thanks to Ken French for his data, we can accomplish something I think is fairly amazing. In 640x800, we can see 250 day rollling returns for 48 U.S. industries since 1963.
![]() |
From TimelyPortfolio |
R code in GIST (do raw for copy/paste):
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
#get very helpful Ken French data | |
#for this project we will look at Industry Portfolios | |
#http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/48_Industry_Portfolios_daily.zip | |
require(latticeExtra) | |
require(PerformanceAnalytics) | |
require(quantmod) | |
#my.url will be the location of the zip file with the data | |
my.url="http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/48_Industry_Portfolios_daily.zip" | |
#this will be the temp file set up for the zip file | |
my.tempfile<-paste(tempdir(),"\\frenchindustry.zip",sep="") | |
#my.usefile is the name of the txt file with the data | |
my.usefile<-paste(tempdir(),"\\48_Industry_Portfolios_daily.txt",sep="") | |
download.file(my.url, my.tempfile, method="auto", | |
quiet = FALSE, mode = "wb",cacheOK = TRUE) | |
unzip(my.tempfile,exdir=tempdir(),junkpath=TRUE) | |
#read space delimited text file extracted from zip | |
french_industry <- read.table(file=my.usefile, | |
header = TRUE, sep = "", | |
as.is = TRUE, | |
skip = 9, nrows=12211) | |
#get dates ready for xts index | |
datestoformat <- rownames(french_industry) | |
datestoformat <- paste(substr(datestoformat,1,4), | |
substr(datestoformat,5,6),substr(datestoformat,7,8),sep="-") | |
#get xts for analysis | |
french_industry_xts <- as.xts(french_industry[,1:NCOL(french_industry)], | |
order.by=as.Date(datestoformat)) | |
#divide by 100 to get percent | |
french_industry_xts <- french_industry_xts/100 | |
#delete missing data which is denoted by -0.9999 | |
french_industry_xts[which(french_industry_xts < -0.99,arr.ind=TRUE)[,1], | |
unique(which(french_industry_xts < -0.99,arr.ind=TRUE)[,2])] <- 0 | |
#get price series or cumulative growth of 1 | |
french_industry_price <- cumprod(french_industry_xts+1) | |
#get 250 day rate of change or feel free to change to something other than 250 | |
roc <- french_industry_price | |
#split into groups so do not run out of memory | |
for (i in seq(12,48,by=12)) { | |
roc[,((i-11):(i))] <- ROC(french_industry_price[,((i-11):(i))],n=250,type="discrete") | |
} | |
roc[1:250,] <- 0 | |
#do a horizon plot of all 48 industries with horizonscale of 0.25 | |
horizonplot(roc, | |
layout=c(1,48), | |
horizonscale=0.25, #feel free to change to whatever you would like | |
scales = list(tck = c(1,0), y = list(draw = FALSE,relation = "same")), | |
origin = 0, | |
colorkey = FALSE, | |
#since so many industries, we will comment out grid | |
# panel = function(x, ...) { | |
# panel.horizonplot(x, ...) | |
# panel.grid(h=3, v=0,col = "white", lwd=1,lty = 3) | |
# }, | |
ylab = list(rev(colnames(roc)), rot = 0, cex = 0.7, pos = 3), | |
xlab = NULL, | |
par.settings=theEconomist.theme(box = "gray70"), | |
#use ylab above for labelling so we can specify FALSE for strip and strip.left | |
strip = FALSE, | |
strip.left = FALSE, | |
main = "French Daily 48 Industry 1963-2011\n source: http://mba.tuck.dartmouth.edu/pages/faculty/ken.french") |
I have noticed a small issue with your script, perhaps you are developing on Windows but in Linux and other *NIX systems, that path does not work with those slashes but needs to be /
ReplyDeleteSo, instead of:
#this will be the temp file set up for the zip file
my.tempfile<-paste(tempdir(),"\\frenchindustry.zip",sep="")
#my.usefile is the name of the txt file with the data
my.usefile<-paste(tempdir(),"\\48_Industry_Portfolios_daily.txt",sep="")
It should be this for support on other platforms like Linux and OS X etc:
#this will be the temp file set up for the zip file
my.tempfile<-paste(tempdir(),"/frenchindustry.zip",sep="")
#my.usefile is the name of the txt file with the data
my.usefile<-paste(tempdir(),"/48_Industry_Portfolios_daily.txt",sep="")
Thanks for the useful guide
Cheers,
Mohomed
thanks so much for pointing this out in your helpful comment. thanks for reading.
ReplyDeleteI think it would be interesting to see the industries ordered by some sort of similarity of returns.
ReplyDeleteI agree so I did in this post http://timelyportfolio.blogspot.com/2012/08/48-industries-dendrogram-ordered-over.html
DeleteWhile running this line:
ReplyDeletefrench_industry_xts <- as.xts(french_industry[,1:NCOL(french_industry)], order.by=as.Date(datestoformat,format="%Y-%m-%d"))
I get this error:
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format
I changed it to
Deletefrench_industry_xts <- as.xts(french_industry[,1:NCOL(french_industry)],
as.POSIXct(datestoformat,format="%Y-%m-%d"))
and it's all OK.