xts to.quarterly每月给出不正确的结果

xts to.quarterly每月给出不正确的结果,r,time-series,frequency,xts,zoo,R,Time Series,Frequency,Xts,Zoo,当我进行以下每月->季度转换时 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) 请注意重复的上一个季度和不正确的值至。季度应获取最后一个值。事实并非如此1989年9月-3.349应该是1989Q3-4.44

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

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)
请注意重复的上一个季度和不正确的值<代码>至。季度应获取最后一个值。事实并非如此<代码>1989年9月-3.349应该是
1989Q3-4.44
1989年12月-4.58
应该是
1989Q4-4.58
。有两个
1989Q4
值。不知何故,10月和7月的价值观被抢走,而不是9月和12月的价值观


到底发生了什么事?

这对我很有效。使用
set.seed()
尝试一个可复制的示例


它对我有用。使用
set.seed()
尝试一个可复制的示例


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

问题是,您的
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
R>set.seed(21)
R> xts.testm str(xts.testm)#“TZ:”表示本地时区
1989年1月1550日/12月的“xts”对象包含:
数据:num[1:5280,1]7.935.2217.46-12.7121.97。。。
由类的对象索引:[yearmon]TZ:
xts属性:
无效的
R> xts.testq尾部(xts.testq)
[,1]
1988年第4季度-17.874026
1989年第一季度3.346780
1989年第2季度18.418469
1989年第3季度9.461461
1989年第4季度-16.074923
1989年第4季度-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>set.seed(21)
R> xts.testm str(xts.testm)#注“TZ:UTC”
1989年1月1550日/12月的“xts”对象包含:
数据:num[1:5280,1]7.935.2217.46-12.7121.97。。。
由类的对象索引:[yearmon]TZ:UTC
xts属性:
无效的
R> xts.testq尾部(xts.testq)
[,1]
1988年第3季度4.936151
1988年第4季度5.404136
1989年第一季度-3.331241
1989年第2季度-23.621581
1989年第3季度2.687675
1989年第4季度-3.615878

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

问题是,您的
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
R>set.seed(21)
R> xts.testm str(xts.testm)#“TZ:”表示本地时区
1989年1月1550日/12月的“xts”对象包含:
数据:num[1:5280,1]7.935.2217.46-12.7121.97。。。
由类的对象索引:[yearmon]TZ:
xts属性:
无效的
R> xts.testq尾部(xts.testq)
[,1]
1988年第4季度-17.874026
1989年第一季度3.346780
1989年第2季度18.418469
1989年第3季度9.461461
1989年第4季度-16.074923
1989年第4季度-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>set.seed(21)
R> xts.testm str(xts.testm)#注“TZ:UTC”
1989年1月1550日/12月的“xts”对象包含:
数据:num[1:5280,1]7.935.2217.46-12.7121.97。。。
由类的对象索引:[yearmon]TZ:UTC
xts属性:
无效的
R> xts.testq尾部(xts.testq)
[,1]
1988年第3季度4.936151
1988年第4季度5.404136
1989年第一季度-3.331241
1989年第2季度-23.621581
1989年第3季度2.687675
1989年第4季度-3.615878

非常感谢您的回答!我对R还是新手,所以请原谅我的无知,但是我该如何使用您所链接的开发版本呢?我需要我读过的这个“R-devel”吗?@Knixd:它只是xts的开发版本,而不是R的开发版本。我将研究使用xts的开发版本。再次感谢您的帮助!非常感谢您的回答!我对R还是新手,所以请原谅我的无知,但是我该如何使用您所链接的开发版本呢?我需要我读过的这个“R-devel”吗?@Knixd:它只是xts的开发版本,而不是R的开发版本。我将研究使用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