【发布时间】:2018-07-27 00:18:14
【问题描述】:
我继承了一些分析模拟结果的 R 代码。有一次,该代码使用indexAt = 'yearmon' 调用xts 包的to.monthly 函数来总结动物园中的一些值。
该代码通常可以正常运行。然而,最近,在对更早的数据进行模拟分析时,对to.monthly 的调用产生了一些令人不安的警告消息,如下所示:
Warning in zoo(xx, order.by = index(x), ...) :
some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique
我将我的数据筛选到仍然显示此警告的最小大小。从这个 R 代码开始:
library(xts)
z = structure(c(-1062503.35419463, -1080996.55425821, -1099783.92018741,
-1122831.06978888, -1138804.79976585, -1158620.33101501, -1163717.44859603,
-1183250.17288897, -1212428.97863421, -1234981.23171341, -1253605.89670471,
-1269885.84780747, -1272023.98376509, -1284471.17954946, -1313114.61914572,
-1334861.551294, -1349971.87378146, -1360596.77251109, -1363047.71977556,
-1383840.30131117, -1407963.97518998, -1427010.7195352, -1451908.36211767,
-1464563.94519573, -1470017.67402451, -1503642.02732151, -1529231.67395429,
-1560593.79655716, -1582052.24505653, -1595391.99583389), index = structure(c(1111985820,
1112072340, 1112158740, 1112245140, 1112331540, 1112392740, 1112587140,
1112673540, 1112759880, 1112846340, 1112932200, 1112993940, 1113191940,
1113278340, 1113364560, 1113451080, 1113537540, 1113598740, 1113796560,
1113883140, 1113969540, 1114055940, 1114142220, 1114203540, 1114401480,
1114487940, 1114574280, 1114660740, 1114747080, 1114808340), class = c("POSIXct",
"POSIXt")), class = "zoo")
class(z)
head(z)
tail(z)
然后对to.monthly执行这个调用:
to.monthly(z, indexAt = 'yearmon', name = "Monthly")
在生成此输出的机器上:
Warning in zoo(xx, order.by = index(x), ...) :
some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique
Warning in zoo(xx, order.by = index(x), ...) :
some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique
Monthly.Open Monthly.High Monthly.Low Monthly.Close
Apr 2005 -1062503 -1062503 -1138805 -1138805
Apr 2005 -1158620 -1158620 -1595392 -1595392
注意警告消息,然后是to.monthly 的结果,这是一个与"Apr 2005" 重复位置的动物园。
我花了一些时间逐行执行to.monthly中的代码,并确定该错误实际上发生在to.monthly对to.period的调用中。
特别是我发现to.period里面的xx局部变量最初是正确计算的,但是在行之后
indexClass(xx) <- indexAt
在xx 的位置变为非唯一时执行。
在我看来,这种行为确实像是 xts 包的 to.period 函数中的错误。
我很想听听知道to.monthly/to.period/yearmon 实际工作原理的人的意见,要么确认这是一个错误,要么向我解释为什么不是,并给我一个解决方法。
我在 xts github 页面上找到了this possibly related report(我不太明白)。
关于我的机器:
> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)
...
other attached packages:
...
xts_0.10-0
zoo_1.8-0
当我启动 Rgui 时,我看到这个关于 xts 的警告消息:
Warning: package ‘xts’ was built under R version 3.4.2
【问题讨论】:
-
我没有收到任何警告。事实上,我得到的正是我所期望的,2005 年 3 月和 4 月的开盘/高/低/收盘。
R 3.5.0, xts_0.10-2, zoo_1.8-1 -
@AkselA:你使用的是什么 R 和 xts 版本?
-
@AkselA:很抱歉,如果我在您的原始回复中错过了您的版本信息。今晚在我的家用机器上,我完全卸载了我以前的 R (3.4.3) 并安装了最新的 R (3.5.1)。然后我安装了 zoo 和 xts 包。 (注意:必须使用 0-Cloud [https] CRAN 镜像,因为我第一次尝试的其他 2 个镜像缺少它们......)。仍然不高兴:我收到相同的警告消息,随后出现两次“2005 年 4 月”的动物园。我的家用机器运行的是 Windows 7。你运行的是 linux 还是 Mac?
-
我使用的是 Mac (10.11.6)。你以前在 R 中做过很多调试吗? Hadley Wickham 有一篇很好的实用技术文章,以备不时之需。