xts 索引滞后错误
xts indexing lag error
我有一个关于使用 xts 建立索引的问题。我知道当我使用 SPY['2002-10-17/']
时,我可以获取我的 xts 对象中从 2002 年 10 月 17 日到最后一个日期的所有数据。但是,如果我编写 ifelse 语句并执行相同的调用,则情况并非如此。下面是代码:
library(quantmod)
getSymbols('SPY',from='2002-01-01')
SPY=Cl(SPY) #pull only the closes
returns=(SPY-lag(SPY,1))/(lag(SPY,1)) #returns calculation
head(SPY['2002-10-17/']) #This works and starts at 2002-10-17
head(ifelse(returns>0,1,0)['2002-10-17/']) #this for some odd reason starts at 2002-10-18
谁能告诉我为什么会这样。我真是糊涂了。
正如 Joshua 建议的那样,这里是 sessionInfo()
的输出
> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] quantmod_0.4-3 TTR_0.22-0 xts_0.9-7 zoo_1.7-12
loaded via a namespace (and not attached):
[1] tools_3.2.0 grid_3.2.0 lattice_0.20-31
我可以在 xts_0.9-7 中确认此行为。这是 a bug that has been fixed in the development version.
错误出现在逻辑运算符 (>
) 中,删除了 xts 对象的时区属性。这会导致索引偏移您当地时区与 UTC 偏移的小时数。所以,逻辑运算符本质上是在改变对逻辑运算符创建的对象的索引的解释。
> head((returns > 0)['2002-10-17/'])
SPY.Close
2002-10-18 TRUE
2002-10-21 TRUE
2002-10-22 FALSE
2002-10-23 TRUE
2002-10-24 FALSE
2002-10-25 TRUE
> indexTZ(returns)
[1] "UTC"
> indexTZ(returns > 0)
[1] ""
我有一个关于使用 xts 建立索引的问题。我知道当我使用 SPY['2002-10-17/']
时,我可以获取我的 xts 对象中从 2002 年 10 月 17 日到最后一个日期的所有数据。但是,如果我编写 ifelse 语句并执行相同的调用,则情况并非如此。下面是代码:
library(quantmod)
getSymbols('SPY',from='2002-01-01')
SPY=Cl(SPY) #pull only the closes
returns=(SPY-lag(SPY,1))/(lag(SPY,1)) #returns calculation
head(SPY['2002-10-17/']) #This works and starts at 2002-10-17
head(ifelse(returns>0,1,0)['2002-10-17/']) #this for some odd reason starts at 2002-10-18
谁能告诉我为什么会这样。我真是糊涂了。
正如 Joshua 建议的那样,这里是 sessionInfo()
> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] quantmod_0.4-3 TTR_0.22-0 xts_0.9-7 zoo_1.7-12
loaded via a namespace (and not attached):
[1] tools_3.2.0 grid_3.2.0 lattice_0.20-31
我可以在 xts_0.9-7 中确认此行为。这是 a bug that has been fixed in the development version.
错误出现在逻辑运算符 (>
) 中,删除了 xts 对象的时区属性。这会导致索引偏移您当地时区与 UTC 偏移的小时数。所以,逻辑运算符本质上是在改变对逻辑运算符创建的对象的索引的解释。
> head((returns > 0)['2002-10-17/'])
SPY.Close
2002-10-18 TRUE
2002-10-21 TRUE
2002-10-22 FALSE
2002-10-23 TRUE
2002-10-24 FALSE
2002-10-25 TRUE
> indexTZ(returns)
[1] "UTC"
> indexTZ(returns > 0)
[1] ""