xts to.quarterly 每月给出不正确的结果
xts to.quarterly giving incorrect results for monthly
当我进行以下每月->每季度的转换时
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.44
而 Dec 1989 -4.58
应该是 1989Q4 -4.58
。不知何故有两个 1989Q4
值。以某种方式获取了 10 月和 7 月的值,而不是 9 月和 12 月的值。
这到底是怎么回事?
对我有用。尝试使用 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
这是 CRAN 上 xts 版本中的错误,已在 development version 中修复。
问题是您的 xts.testm
对象正在使用您的本地时区,即使索引是 yearmon
class(没有时区)。通过确保所有没有时区的索引 classes(例如 Date、yearmon、yearqtr、chron)具有 "UTC"
的 TZ
,这在开发版本中已得到修复。
使用来自 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
当我进行以下每月->每季度的转换时
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.44
而 Dec 1989 -4.58
应该是 1989Q4 -4.58
。不知何故有两个 1989Q4
值。以某种方式获取了 10 月和 7 月的值,而不是 9 月和 12 月的值。
这到底是怎么回事?
对我有用。尝试使用 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
这是 CRAN 上 xts 版本中的错误,已在 development version 中修复。
问题是您的 xts.testm
对象正在使用您的本地时区,即使索引是 yearmon
class(没有时区)。通过确保所有没有时区的索引 classes(例如 Date、yearmon、yearqtr、chron)具有 "UTC"
的 TZ
,这在开发版本中已得到修复。
使用来自 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