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"