【问题标题】:xts to.quarterly giving incorrect results for monthlyxts to.quarterly 每月给出不正确的结果
【发布时间】:2016-08-22 18:44:19
【问题描述】:

当我进行以下每月 -> 季度转换时

xts.testm <- xts(rnorm(440*12, mean=0, sd=10), order.by=timeBasedSeq(155001/1989))
xts.testq<-to.quarterly(xts.testm, OHLC = FALSE)
tail(xts.testm)
tail(xts.testq)

我得到以下显示原始月度和新季度输出的输出:

               [,1]
Jul 1989  4.4441175
Aug 1989 -0.2839412
Sep 1989 -3.3491154
Oct 1989 -1.9351425
Nov 1989  7.5427961
Dec 1989 -4.5846861
> tail(xts.testq)
             [,1]
1988 Q4 -1.537608
1989 Q1 -7.190733
1989 Q2  9.430785
1989 Q3  4.444117
1989 Q4 -1.935143
1989 Q4 -4.584686

请注意上一季度的重复数据和不正确的值。 to.quarterly 应该获取最后一个值。它不是。 Sep 1989 -3.349 应该是 1989Q3 -4.44Dec 1989 -4.58 应该是 1989Q4 -4.58。不知何故,有两个 1989Q4 值。不知何故,正在抓取 10 月和 7 月的值,而不是 9 月和 12 月的值。

这到底是怎么回事?

【问题讨论】:

    标签: r time-series frequency xts zoo


    【解决方案1】:

    这是 CRAN 上 xts 版本中的一个错误,已在 development version 中修复。

    问题是您的 xts.testm 对象正在使用您的本地时区,即使索引是 yearmon 类(没有时区)。这在开发版本中通过确保所有没有时区的索引类(例如 Date、yearmon、yearqtr、chron)具有 TZ"UTC" 来修复。

    使用来自 CRAN 的 xts:

    R> set.seed(21)
    R> xts.testm <- xts(rnorm(440*12, sd=10), order.by=timeBasedSeq(155001/1989))
    R> str(xts.testm)  # "TZ: " implies local timezone
    An ‘xts’ object on Jan 1550/Dec 1989 containing:
      Data: num [1:5280, 1] 7.93 5.22 17.46 -12.71 21.97 ...
      Indexed by objects of class: [yearmon] TZ: 
      xts Attributes:  
     NULL
    R> xts.testq <- to.quarterly(xts.testm, OHLC = FALSE)
    R> tail(xts.testq)
                  [,1]
    1988 Q4 -17.874026
    1989 Q1   3.346780
    1989 Q2  18.418469
    1989 Q3   9.461461
    1989 Q4 -16.074923
    1989 Q4  -3.615878
    

    使用当前开发版本的 xts:

    R> set.seed(21)
    R> xts.testm <- xts(rnorm(440*12, sd=10), order.by=timeBasedSeq(155001/1989))
    R> str(xts.testm)  # note "TZ: UTC"
    An ‘xts’ object on Jan 1550/Dec 1989 containing:
      Data: num [1:5280, 1] 7.93 5.22 17.46 -12.71 21.97 ...
      Indexed by objects of class: [yearmon] TZ: UTC
      xts Attributes:  
     NULL
    R> xts.testq <- to.quarterly(xts.testm, OHLC = FALSE)
    R> tail(xts.testq)
                  [,1]
    1988 Q3   4.936151
    1988 Q4   5.404136
    1989 Q1  -3.331241
    1989 Q2 -23.621581
    1989 Q3   2.687675
    1989 Q4  -3.615878
    

    【讨论】:

    • 非常感谢您的回答!我还是 R 的新手,所以请原谅我的无知,但是我将如何使用您友好链接的开发版本?我需要这个我读过的“R-devel”吗?
    • @Knixd:只是xts的开发版,不是R的开发版。
    • 太棒了。我会考虑使用 xts 的开发版本。再次感谢您的帮助!
    【解决方案2】:

    它对我有用。尝试使用 set.seed() 的可重现示例。

    set.seed(10)
    xts.testm <- xts(rnorm(440*12, mean=0, sd=10), order.by=timeBasedSeq(155001/1989))
    xts.testq<-to.quarterly(xts.testm, OHLC = FALSE)
    tail(xts.testm,12)
    tail(xts.testq,8)
    

    你有这个输出吗?

    tail(xts.testm,12)
                   [,1]
    dic 1988 -16.984259
    gen 1989 -13.103928
    feb 1989  -3.515761
    mar 1989  23.712606
    apr 1989   6.635250
    mag 1989  13.826195
    giu 1989   1.771894
    lug 1989 -11.096838
    ago 1989  14.664982
    set 1989  12.516992
    ott 1989  11.572212
    nov 1989 -18.763949
    > tail(xts.testq,8)
                   [,1]
    1988 Q1   4.2217167
    1988 Q2   0.4267038
    1988 Q3  25.3994248
    1988 Q4 -16.9842591
    1989 Q1  23.7126065
    1989 Q2   1.7718940
    1989 Q3  12.5169922
    1989 Q4 -18.7639487
    

    【讨论】:

      猜你喜欢
      • 2015-10-24
      • 2012-08-27
      • 2017-12-14
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多