我正在使用 PerformanceAnalytics 包来分析一些月度返回。 charts.RollingRegression 应该根据某些基准绘制 n 个月的滚动回归。 数据只是从 2014 年 4 月 8 日到 2014 年 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
seems happen about every other function call .) 有人对这类东西有更好的包装有什么建议吗?
最佳答案
数据的子集化应该在传递给 charts.RollingRegression
函数之前完成。强大的 xts
提供了这个功能:
charts.RollingRegression(Ret["2009-04::",], Rb["2009-04::",2, drop = FALSE], Rf = 0.001, na.pad = TRUE)
您可以通过 ?subset.xts
查看 R 中的帮助页面,详细了解如何使用 xts 进行子集化。
关于r - PerformanceAnalytics charts.RollingRegression 绘制初始窗口值。我如何让它不那样做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28968597/