Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

horizon plot of 48 industries with dendrogram order

View horizon plot of 48 industries with dendrogram order.r
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
require(fAssets)
require(latticeExtra)
require(quantmod)
require(PerformanceAnalytics)
 
#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
 
# try to do http://stackoverflow.com/questions/9747426/how-can-i-produce-plots-like-this
# was much easier to use latticeExtra
 
# get dendrogram data from hclust
# backward and repetitive but it works
t <- assetsDendrogramPlot(as.timeSeries(french_industry_xts))
# thanks to the latticeExtra example
dd.row <- as.dendrogram(t$hclust)
row.ord <- order.dendrogram(dd.row)
 
xyplot(roc[,row.ord],
layout=c(1,48), ylim=c(0,0.25),
scales = list(tck = c(1,0), y = list(draw = FALSE,relation = "same")),
horizonscale=0.25,
origin = 0,
colorkey = TRUE,
#since so many industries, we will comment out grid
panel = function(x,y,...) {
panel.horizonplot(x,y,...) #feel free to change to whatever you would like)
# panel.grid(h=3, v=0,col = "white", lwd=1,lty = 3)
},
ylab = list(rev(colnames(roc[,row.ord])), 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 (Dendrogram Ordered) 1963-2011\n source: http://mba.tuck.dartmouth.edu/pages/faculty/ken.french",
 
legend =
list(right =
list(fun = dendrogramGrob,
args =
list(x = dd.row, ord = row.ord,
side = "right",
size = 10))))
 
 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.