代码之家  ›  专栏  ›  技术社区  ›  HaroldFinch

r xts包的to.period函数中有错误吗?

xts r
  •  3
  • HaroldFinch  · 技术社区  · 7 年前

    我继承了一些分析模拟结果的R代码。在某一点上,该代码调用XTS包的 to.monthly 功能 indexAt = 'yearmon' 总结动物园的一些价值观。

    该代码通常无问题地运行。然而,最近,在分析较旧数据的模拟时,需要 至.每月 生成了一些令人不安的警告消息,如:

    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(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
    

    注意警告信息,后面是 至.每月 ,这是一个动物园,它的位置与 "Apr 2005" .

    我花了一些时间在 每月 一行接一行,并确定错误实际上发生在 至.每月 的呼叫 to.period .

    特别是,我发现 xx 内部局部变量 结束时间段 最初计算正确,但在该行之后

    indexClass(xx) <- indexAt
    

    执行时,即 XX 变得不唯一。

    这个行为看起来确实像XTS包中的一个bug 结束时间段 对我有用。

    我很想听到有人告诉我 to.monthly/to.period/yearmon 真的有效,要么确认这是一个bug,要么向我解释为什么不是,然后给我一个解决方法。

    我发现 this possibly related report 在XTS Github页面上(我不完全理解)。

    关于我的机器:

    > 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
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Joshua Ulrich    7 年前

    z POSIXct "UTC"

    "yearmon"

    Sys.setenv(TZ = "America/Chicago")
    debugonce(xts:::`indexClass<-.xts`)
    to.monthly(z, indexAt="yearmon", name="monthly")
    # <snip>
    # Browse[2]> 
    # debug: attr(attr(x, "index"), "tzone") <- "UTC"
    # Browse[2]> print(x)  # When timezone is "America/Chicago"
    #                     monthly.Open monthly.High monthly.Low monthly.Close
    # 2005-03-31 22:59:00     -1062503     -1062503    -1138805      -1138805
    # 2005-04-29 15:59:00     -1158620     -1158620    -1595392      -1595392
    # Browse[2]> 
    # debug: attr(attr(x, "index"), "tclass") <- value
    # Browse[2]> print(x)  # When timezone is "UTC"
    #                     monthly.Open monthly.High monthly.Low monthly.Close
    # 2005-04-01 04:59:00     -1062503     -1062503    -1138805      -1138805
    # 2005-04-29 20:59:00     -1158620     -1158620    -1595392      -1595392
    # Warning message:
    # timezone of object (UTC) is different than current timezone ().
    

    attr(attr(x, "index"), "tzone") <- "UTC" print(x)

    indexClass<-