XTS to.weekly返回不同的每周端点

时间:2012-07-05 15:01:29

标签: r xts zoo

我遇到一个问题,endpoints()中的xts函数(以及使用端点的to.weekly函数)有时会在星期五作为周末返回,有时会返回星期一。

我的数据集名为sp2

> head(sp2)
           [,1]
2012-01-09 1.78
2012-01-10 1.78
2012-01-11 1.77
2012-01-12 1.80
2012-01-13 1.77
2012-01-16 1.77

> tail(sp2)
           [,1]
2012-06-28 1.94
2012-06-29 1.92
2012-07-02 1.92
2012-07-03 1.90
2012-07-04 1.89
2012-07-05 1.89

> class(sp2)
[1] "xts" "zoo"

> weekdays(index(sp2))
  [1] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [11] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [21] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [31] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [41] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [51] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [61] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [71] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [81] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [91] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
[101] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
[111] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
[121] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday" 

因此,您可以看到数据系列中没有错过工作日。

然而.....

> weekdays(index(to.weekly(sp2)))
 [1] "Friday"   "Friday"   "Friday"   "Friday"   "Friday"   "Friday"   "Friday"   "Friday"   "Friday"   "Friday"   "Monday"  
[12] "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"  
[23] "Monday"   "Monday"   "Monday"   "Thursday"

当然这应该是星期五(除了最后一点,即今天,星期四)?

我在这里做错了什么,是否有解决方法?

我希望计算周五到周五此数据系列的每周更改。

..........................................可重复的例子...... .....................

i <- as.Date((as.numeric(Sys.Date())-200):as.numeric(Sys.Date()))
x <- xts(rep(1, length(i)), order.by = i)
weekdays(index(to.weekly(x)))
#  [1] "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"  
# [14] "Sunday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"  
# [27] "Monday"   "Monday"   "Monday"   "Thursday"
y <- xts(rep(1, length(i)), order.by = as.POSIXct(i))
weekdays(index(to.weekly(y)))
# [1] "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"  
# [14] "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"  
# [27] "Sunday"   "Sunday"   "Sunday"   "Thursday"

1 个答案:

答案 0 :(得分:3)

这是已在R-Forge上修复的CRAN版本中的错误。基本上,索引的时间组件没有被删除,你遇到了一些时区和夏令时的时髦业务。

Install from R-Forge使用更正的功能。

require(xts)
i <- Sys.Date()+200:0
x <- xts(rep(1, length(i)), order.by = i)
y <- xts(rep(1, length(i)), order.by = as.POSIXct(i))
all.equal(weekdays(index(to.weekly(x))), weekdays(index(to.weekly(y))))