Dygraphs Focus on Retail

Recent price performance by the Retail Industry group seems mighty strong. We already set up the framework for using dygraphs to explore the Kenneth French industry dataset in this post. In honor of Black Friday, let's use it now to focus on US Retail with some help from the R package PerformanceAnalytics.

Notice how the two dygraphs' zoom and pan are synchronized by using the technique shown in this gallery example.

click/drag to zoom; shift+click/drag to pan; double-click to unzoom

Nothing odd really stands out after this inspection. However, if you look at the Dow Jones US Retail Index, a much different picture appears. I'll save that analysis until after Thanksgiving.

Happy Black Friday!

Get the Data Just Like Before

#get very helpful Ken French data
#for this project we will look at Industry Portfolios

#my.url will be the location of the zip file with the data
#this will be the temp file set up for the zip file
#my.usefile is the name of the txt file with the data
download.file(my.url, my.tempfile, method="auto", 
              quiet = FALSE, mode = "wb",cacheOK = 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=23027)

#get dates ready for xts index
datestoformat <- rownames(french_industry)
datestoformat <- paste(substr(datestoformat,1,4),

#get xts for analysis
french_industry_xts <- as.xts(french_industry[,1:NCOL(french_industry)],

#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 <- log(cumprod(french_industry_xts+1))

Transform and Supply Data as an Array

Dygraphs allows multiple data format options. We used a CSV url last time. Let's try the Array method.

retail.df <- data.frame(
  paste0("#!new Date(\'",index(french_industry_price),"\')!#"),
colnames(retail.df) <- c("date","price")
sharpe.df <- data.frame(
  paste0("#!new Date(\'",index(french_industry_price),"\')!#"),
    width = 36,
    by = 1
colnames(sharpe.df) <- c("date","sharpe")
  var price = ',
  toObj(rjson::toJSON( retail.df )),
  var sharpe = ',
  toObj(rjson::toJSON( sharpe.df )),
  var blockRedraw = false;
  var gs = [];