我正在使用 PerformanceAnalytics 包来分析一些月度回报。chart.RollingRegression 应该针对某个基准绘制 n 个月的滚动回归。该数据仅为 2014 年 4 月 8 日至 12 月的 6 个返回系列,试图对 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)
这将产生以下图表:
我希望它省略前 12 个月的“无意义的”,但没有关于如何完成的文档,我能找到的该图表的任何其他描述如下所示:
查看源代码,在函数的主要内容中,我看到:
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)
}
}
我们可以看到没有 na.pad 被传递给最终的“R-Squared”函数,这导致了我希望在前两个图表中看到的图表。我想解决这个问题,但我无法编辑包代码。我尝试使用“assignInNamespace”,但它不起作用。该功能似乎可以工作,但包中的功能代码没有变化。我也想删除图表中的前导空格,但是如果你们可以让我知道如何编辑它,或者知道任何解决方法,请告诉我。(谢谢!你们是神!)
哦!还有 PS - 为什么我的软件包版本似乎是唯一有这个问题的版本???为什么我的图表默认看起来不正确?
编辑:这不是这个包中唯一值得怀疑的代码。我一直让事情中断并且无法正常工作,因为它似乎被记录在案(Error in R[, nc] - coredata(Rf) : non-numeric argument to binary operator
似乎发生在所有其他函数调用中。)有人对这种类型的东西有更好的包有什么建议吗?