我试图在一些xts对象上滚动计算一些函数。在我加载PerformanceAnalytics包之后,在很多情况下使用xts这样做似乎有问题...请参阅下面的示例。我究竟做错了什么?这对我来说非常重要,因为它扼杀了我正在执行的许多计算(很好,它仍然是周末)。我几小时前重建了所有主要的包裹。请参阅下面的sessionInfo。 编辑: 看看这个PerformanceAnalytics的事情,我想现在有更多的事情被打破了。例如:
table.DownsideRisk(RET)
Error: could not find function "sd.xts"
这是我原来的例子:
require(quantmod)
# Get SP500 Index
getSymbols("^GSPC")
# This does not work
tail(rollapply(Cl(GSPC), 20, FUN=sd, fill=NA), 5)
# Hm, something with xts maybe?
z <- zoo(1:1500, as.Date(1:1500))
tail(rollapply(z, 20, mean, fill=NA), 5)
tail(rollapply(z, 20, sd, fill=NA), 5)
z <- as.xts(z)
tail(rollapply(z, 20, mean, fill=NA), 5)
tail(rollapply(z, 20, sd, fill=NA), 5)
require(PerformanceAnalytics)
# This does not work anymore
tail(rollapply(z, 20, sd, fill=NA), 5)
# This does not work anymore
tail(rollapply(Cl(GSPC), 20, FUN=sd, fill=NA), 5)
# Well, it looks like xts or PerformanceAnalytics problem... When I load PerformanceAnalytics, problems...
> require(quantmod)
Loading required package: quantmod
Loading required package: Defaults
Loading required package: xts
Loading required package: zoo
Attaching package: ‘zoo’
The following object(s) are masked from ‘package:base’:
as.Date, as.Date.numeric
Loading required package: TTR
>
> # Get SP500 Index
> getSymbols("^GSPC")
[1] "GSPC"
>
> # This does not work
> tail(rollapply(Cl(GSPC), 20, FUN=sd, fill=NA), 5)
GSPC.Close
2012-10-25 NA
2012-10-26 NA
2012-10-31 NA
2012-11-01 NA
2012-11-02 NA
>
> # Hm, something with xts maybe?
> z <- zoo(1:1500, as.Date(1:1500))
> tail(rollapply(z, 20, mean, fill=NA), 5)
1974-02-05 1974-02-06 1974-02-07 1974-02-08 1974-02-09
NA NA NA NA NA
> tail(rollapply(z, 20, sd, fill=NA), 5)
1974-02-05 1974-02-06 1974-02-07 1974-02-08 1974-02-09
NA NA NA NA NA
>
> z <- as.xts(z)
> tail(rollapply(z, 20, mean, fill=NA), 5)
[,1]
1974-02-05 NA
1974-02-06 NA
1974-02-07 NA
1974-02-08 NA
1974-02-09 NA
> tail(rollapply(z, 20, sd, fill=NA), 5)
1974-02-05 NA
1974-02-06 NA
1974-02-07 NA
1974-02-08 NA
1974-02-09 NA
>
> require(PerformanceAnalytics)
Loading required package: PerformanceAnalytics
Package PerformanceAnalytics (1.0.5.1) loaded.
Econometric tools for performance and risk analysis.
(c) 2004-2012 Peter Carl, Brian G. Peterson. License: GPL
http://r-forge.r-project.org/projects/returnanalytics/
Attaching package: ‘PerformanceAnalytics’
The following object(s) are masked from ‘package:graphics’:
legend
> # This does not work anymore
> tail(rollapply(z, 20, sd, fill=NA), 5)
Error in FUN(.subset_xts(data, (i - width + 1):i), ...) :
unused argument(s) (fill = NA)
> # This does not work anymore
> tail(rollapply(Cl(GSPC), 20, FUN=sd, fill=NA), 5)
Error in FUN(.subset_xts(data, (i - width + 1):i), ...) :
unused argument(s) (fill = NA)
> # Well, it looks like xts or PerformanceAnalytics problem... When I load PerformanceAnalytics, problems...
> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=en_US.UTF-8
[9] LC_ADDRESS=en_US.UTF-8 LC_TELEPHONE=en_US.UTF-8
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] PerformanceAnalytics_1.0.5.1 quantmod_0.3-21
[3] TTR_0.21-1 xts_0.8-8
[5] zoo_1.7-9 Defaults_1.1-1
[7] rj_1.1.0-4
loaded via a namespace (and not attached):
[1] grid_2.15.2 lattice_0.20-0 tools_2.15.2
答案 0 :(得分:1)
PerformanceAnalytics 会注册显然存在问题的rollapply.xts
方法。
您可以在对象上使用as.zoo
代替使用rollapply.zoo
。
尝试tail(rollapply(as.zoo(z), 20, sd, fill=NA, align='right'), 5)
我不知道你会遇到关于sd.xts
(这不是一种方法)和mean.xts
(现在是 xts )的其他问题使用PerformanceAnalytics - 充其量,取决于您使用的是哪个版本。
这可能会使您的计算机爆炸,但在运行“大量代码”之前尝试获取此代码
rollapply.xts <- function(data, width, FUN, ...) {
xdata <- xcoredata(data)
out <- zoo:::rollapply.zoo(data=data, width=width, FUN=FUN, ...)
xcoredata(out) <- xdata
out
}
R> suppressPackageStartupMessages(library(PerformanceAnalytics))
R> tail(rollapply(Cl(GSPC), 20, FUN=sd, fill=NA), 20)
GSPC.Close
2012-10-04 10.80165
2012-10-05 10.80888
2012-10-08 10.82280
2012-10-09 12.94614
2012-10-10 14.96816
2012-10-11 16.27298
2012-10-12 17.48353
2012-10-15 18.42794
2012-10-16 18.44652
2012-10-17 18.76495
2012-10-18 NA
2012-10-19 NA
2012-10-22 NA
2012-10-23 NA
2012-10-24 NA
2012-10-25 NA
2012-10-26 NA
2012-10-31 NA
2012-11-01 NA
2012-11-02 NA