xts 子集在几个月内给出了不正确的结果 [英] xts subsetting gives incorrect results for months
问题描述
我正在为 Mac OS X 使用 R 3.2.1,并且似乎在 xts 子集设置中遇到了不正确的行为.简而言之,对月度数据进行子集化得出的结果比应有的结果滞后 1 个月.这是一个简单的例子,类似于我正在做的古温度分析:
I am using R 3.2.1 for Mac OS X and seem to have run into incorrect behavior in xts subsetting. In brief, subsetting monthly data give a result that is 1 month lagged from what it should be. Here is a simple example that is similar to an analysis of paleotemperature I am doing:
首先我会为这个例子做一些测试数据:
First I will make some test data for the example:
xts.test <- xts(rnorm(440*12, mean=0, sd=10),order.by=timeBasedSeq(155001/1989))
这会生成正确的 xts
文件 AFAICT.这是 12 个月的第一年.
This produces a correct xts
file AFAICT. Here is the first year of 12 months.
head(xts.test, 12L)
[,1]
Jan 1550 -6.9301845
Feb 1550 12.1581413
Mar 1550 3.9688139
Apr 1550 3.9540268
May 1550 9.8200923
Jun 1550 -4.2090998
Jul 1550 7.5950340
Aug 1550 -6.5967389
Sep 1550 -0.6736532
Oct 1550 6.4939221
Nov 1550 4.3916465
Dec 1550 19.8800872
但是,当我尝试通过选择一年来对其进行子集化时,我得到以下信息:
However, when I try to subset this by selecting for a single year, I get the following:
xts.test["1550"]
[,1]
Feb 1550 12.1581413
Mar 1550 3.9688139
Apr 1550 3.9540268
May 1550 9.8200923
Jun 1550 -4.2090998
Jul 1550 7.5950340
Aug 1550 -6.5967389
Sep 1550 -0.6736532
Oct 1550 6.4939221
Nov 1550 4.3916465
Dec 1550 19.8800872
Jan 1551 -2.9549224
也就是说,我得到的不是正确的 1550 年 1 月到 12 月,而是 1550 年 2 月到 1551 年 1 月
That is, instead of the correct Jan-Dec 1550, I get Feb 1550 through Jan 1551
当我尝试通过选择一年中的月份来进行子集化时,我得到了类似的延迟.c(2, 3,4,5,6,7)
让我从四月到九月而不是三月到八月.
I get a similar lag when I try to subset by selecting months of a year. c(2, 3,4,5,6,7)
get me April through September instead of March through August.
这里有什么想法吗?
推荐答案
这是一个已经在最近的 GitHub上的开发版本.
This is a bug that has been fixed in the most recent development version on GitHub.
R> require(xts)
R> xts.test <- xts(rnorm(440*12, mean=0, sd=10),order.by=timeBasedSeq(155001/1989))
R> packageVersion("xts")
[1] ‘0.9.7’
R> str(xts.test) # notice TZ is not set
An ‘xts’ object on Jan 1550/Dec 1989 containing:
Data: num [1:5280, 1] -8.11 -7.65 2.07 -3.29 17.03 ...
Indexed by objects of class: [yearmon] TZ:
xts Attributes:
NULL
注意上面没有设置TZ
,但是在下面的新版本中设置了.
Notice that TZ
is not set above, but it's set in the new version below.
R> packageVersion("xts")
[1] ‘0.9.8’
R> str(xts.test) # notice TZ is set
An ‘xts’ object on Jan 1550/Dec 1989 containing:
Data: num [1:5280, 1] 0.357 12.318 24.291 22.181 6.123 ...
Indexed by objects of class: [yearmon] TZ: UTC
xts Attributes:
NULL
这篇关于xts 子集在几个月内给出了不正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!