【问题标题】:merge.xts produce false results when merging weekly series合并每周系列时,merge.xts 会产生错误的结果
【发布时间】:2013-04-23 22:09:51
【问题描述】:

我正在尝试合并两个 xts 对象。一个是使用quantmod 生成的,另一个是在data.frame 上使用xts() 手动生成的。

> class(rets.weekly)
[1] "xts" "zoo"
> class(result.weekly.xts)
[1] "xts" "zoo"
> tail(rets.weekly)
                    ret
2013-03-22  0.002231087
2013-03-26 -0.007846839
2013-04-06 -0.007501789
2013-04-12  0.001569891
2013-04-20 -0.023628035
2013-04-21  0.005055358
> tail(result.weekly.xts)
           prediction.date  standard.Deviation
2013-03-22 "2013-03-22"     "0.01681222"      
2013-03-26 "2013-03-26"     "0.01578790"      
2013-04-06 "2013-04-06"     "0.01578170"      
2013-04-12 "2013-04-12"     "0.01556793"      
2013-04-20 "2013-04-20"     "0.01504504"      
2013-04-21 "2013-04-21"     "0.01696417"      
> tail(merge.xts(result.weekly.xts , rets.weekly))
           prediction.date standard.Deviation          ret
2013-04-07              NA                 NA -0.007501789
2013-04-12              NA         0.01556793           NA
2013-04-13              NA                 NA  0.001569891
2013-04-20              NA         0.01504504           NA
2013-04-21              NA         0.01696417 -0.023628035
2013-04-22              NA                 NA  0.005055358
Warning message:
In merge.xts(result.weekly.xts, rets.weekly) : NAs introduced by coercion
> tail(merge.zoo(result.weekly.xts , rets.weekly))
<< R QUIT>>

如您所见,日期是相同的。两个集合都是xts 对象。对merge.xts 的调用产生了不正确的输出。我不知道这些日期是从哪里来的。尝试使用 merge.zoo 合并时,R 会退出(蓝屏样式)。

> sessionInfo()
R version 2.15.3 (2013-03-01)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=Hebrew_Israel.1255  LC_CTYPE=Hebrew_Israel.1255    LC_MONETARY=Hebrew_Israel.1255 LC_NUMERIC=C                   LC_TIME=Hebrew_Israel.1255    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] quantmod_0.4-0 TTR_0.22-0     xts_0.9-3      zoo_1.7-9      Defaults_1.1-1

loaded via a namespace (and not attached):
[1] grid_2.15.3     lattice_0.20-13 tools_2.15.3 

【问题讨论】:

  • 请提供reproducible examplesessionInfo() 的输出。
  • 您在之前的步骤中是否使用了to.weekly
  • sessionInfo() 的那部分不是我们关心的部分。请张贴其余部分。
  • @GSee - 是的。 s &lt;- to.weekly(get(getSymbols("xx"))["2012::"]); rets.weekly &lt;- ROC(Cl(s),na.pad=F).

标签: r xts quantmod


【解决方案1】:

这是一个长期存在的时区错误。时区没有传递给应该在的as.Date 调用。大约一个月前,我通过电子邮件为它提交了一个补丁。在 xts/R/index.R 的第 29:31 行,当前是:

if(value[[1]] == "Date")
  #return( as.Date(.index(x)/86400) )
  return( structure(.index(x) %/% 86400, class="Date")) 

应该改为

if(value[[1]] == "Date")
  as.Date(as.POSIXct.numeric(attr(x, "index"), origin=as.Date('1970-01-01')), 
          tz=indexTZ(x))

另外,xts:::as.POSIXct.numeric 应该更新以匹配基础 R。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多