Search code examples
rquantitative-financeperformanceanalytics

PerformanceAnalytics charts.RollingRegression plots initial window values. How do I make it not do that?


I am using the PerformanceAnalytics package to analyze some monthly returns. The charts.RollingRegression should plot the n-month rolling regression against some benchmark. The data is just 6 returns series from April 08 to December 2014, trying to regress against the SPY.

indexReturns <- read.table("quantIndices.csv", stringsAsFactors = FALSE, sep = ",", fill = TRUE, row.names = 1, header=TRUE)
hfIndexReturns <- read.table("quantHFIndices.csv", stringsAsFactors = FALSE, sep = ",", fill = TRUE, row.names = 1, header=TRUE)
peerReturns <- read.table("quantPeers.csv", stringsAsFactors = FALSE, sep = ",", fill = TRUE, row.names = 1, header=TRUE)

splits <- as.data.frame(strsplit(rownames(indexReturns), "/"))
rownames(indexReturns) <- unname(sapply(splits, function(x) paste0(x[3], "-", x[1], "-", x[2])))

splits <- as.data.frame(strsplit(rownames(peerReturns), "/"))
rownames(peerReturns) <- unname(sapply(splits, function(x) paste0(x[3], "-", x[1], "-", x[2])))

Ret <- xts(peerReturns, order.by = as.Date(row.names(peerReturns)))
Rb <- xts(indexReturns, order.by = as.Date(row.names(indexReturns)))

charts.RollingRegression(Ret, Rb[,2, drop = FALSE], Rf = 0.001, na.pad = TRUE)

This produces the following chart: enter image description here

I would like it to omit the "meaningless) first 12 months, but there is no documentation on how this is done, and any other depiction of this chart I can find looks like this: enter image description here

Looking at the source, in the main meat of the function, I see:

for (column.a in 1:columns.a) {
    for (column.b in 1:columns.b) {
        merged.assets = merge(Ra.excess[, column.a, drop = FALSE], 
            Rb.excess[, column.b, drop = FALSE])
        if (attribute == "Alpha") 
            column.result = rollapply(na.omit(merged.assets), 
              width = width, FUN = function(x) lm(x[, 1, 
                drop = FALSE] ~ x[, 2, drop = FALSE])$coefficients[1], 
              by = 1, by.column = FALSE, fill = na.pad, align = "right")
        if (attribute == "Beta") 
            column.result = rollapply(na.omit(merged.assets), 
              width = width, FUN = function(x) lm(x[, 1, 
                drop = FALSE] ~ x[, 2, drop = FALSE])$coefficients[2], 
              by = 1, by.column = FALSE, fill = na.pad, align = "right")
        if (attribute == "R-Squared") 
            column.result = rollapply(na.omit(merged.assets), 
              width = width, FUN = function(x) summary(lm(x[, 
                1, drop = FALSE] ~ x[, 2, drop = FALSE]))$r.squared, 
              by = 1, by.column = FALSE, align = "right")
        column.result.tmp = xts(column.result)
        colnames(column.result.tmp) = paste(columnnames.a[column.a], 
            columnnames.b[column.b], sep = " to ")
        column.result = xts(column.result.tmp, order.by = time(column.result))
        if (column.a == 1 & column.b == 1) 
            Result.calc = column.result
        else Result.calc = merge(Result.calc, column.result)
    }
}

And we can see there is no na.pad being passed to the final "R-Squared" function, which results in the graph I would expect to see for both the first two charts. I would like to fix this, but I cannot edit the package code. I tried using "assignInNamespace", but it doesn't work. The function seems to work, but the function code does not change in the package. I would also like to remove the leading blank space in the graphs as well, but if you guys could let me know how to edit this, or know any workarounds please let me know. (And thanks! You guys are gods!)

OH! And PS - Why the heck is my version of the package seemingly the only one that has this problem??? Why don't my graphs look right by default?

EDIT: This is not the only piece of code from this package which is suspect. I keep having things break and not work as it seems to be documented (Error in R[, nc] - coredata(Rf) : non-numeric argument to binary operator seems to happen about every other function call.) Anyone have any suggestions for better packages for this type stuff?


Solution

  • The subsetting of the data should be done prior to passing to the charts.RollingRegression function. The mighty xts provides this functionality:

    charts.RollingRegression(Ret["2009-04::",], Rb["2009-04::",2, drop = FALSE], Rf = 0.001, na.pad = TRUE)

    You can read more about how to subset with xts by looking at the help page in R via ?subset.xts.