预测:: bizdays可能的错误

时间:2015-07-13 08:41:08

标签: r datetime forecasting

library(forecast) 

library(magrittr)

dat <- ts(rep(1, 30), start = c(2015, 7), frequency = 12)
dat
#     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#2015                           1   1   1   1   1   1
#2016   1   1   1   1   1   1   1   1   1   1   1   1
#2017   1   1   1   1   1   1   1   1   1   1   1   1

dat %>% bizdays("NewYork")
#     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#2015                          21  23  23   1  21  22
#2016  22  21  23  22  21  21  21  22  23  22  22  21
#2017  22  20  23  21  20  22  21  22  23  23  22  22
#2018  21 

似乎forecast::bizdays在工作日的时间序列(2015年10月)中添加了一个奇怪的1,并将真实数据在未来一个月内转移。我做错了吗?

1 个答案:

答案 0 :(得分:0)

问题似乎与第19行有关:

bizdays <- format(biz, format = "%Y %b")

和第32行:

num.days <- table(bizdays)

与您的数据相符:

bizdays
#  [1] "2015 Jun" "2015 Jul" "2015 Jul" "2015 Jul" "2015 Jul" "2015 Jul"
#  [7] "2015 Jul" "2015 Jul" "2015 Jul" "2015 Jul" "2015 Jul" "2015 Jul"
#...
#[643] "2017 Dec" "2017 Dec" "2017 Dec" "2017 Dec" "2017 Dec" "2017 Dec"
#[649] "2017 Dec" "2017 Dec" "2017 Dec" "2017 Dec" "2017 Dec" "2017 Dec"

(请注意,由于时区,第一个日期将转移到6月。)和

num.days
#bizdays
#2015 Aug 2015 Dec 2015 Jul 2015 Jun 2015 Nov 2015 Oct 2015 Sep 2016 Apr 
#      21       23       23        1       21       22       22       21 
#2016 Aug 2016 Dec 2016 Feb 2016 Jan 2016 Jul 2016 Jun 2016 Mar 2016 May 
#      23       22       21       21       21       22       23       22 
#2016 Nov 2016 Oct 2016 Sep 2017 Apr 2017 Aug 2017 Dec 2017 Feb 2017 Jan 
#      22       21       22       20       23       21       20       22 
#2017 Jul 2017 Jun 2017 Mar 2017 May 2017 Nov 2017 Oct 2017 Sep 
#      21       22       23       23       22       22       21 

哪个不对。

一种解决方法是编辑bizdays函数并用

替换第19行
bizdays <- format(biz, format = "%Y-%m")

给出了:

#  [1] "2015-06" "2015-07" "2015-07" "2015-07" "2015-07" "2015-07" "2015-07"
#  [8] "2015-07" "2015-07" "2015-07" "2015-07" "2015-07" "2015-07" "2015-07"
#...
#[645] "2017-12" "2017-12" "2017-12" "2017-12" "2017-12" "2017-12" "2017-12"
#[652] "2017-12" "2017-12" "2017-12"

并且

numdays
#bizdays
#2015-06 2015-07 2015-08 2015-09 2015-10 2015-11 2015-12 2016-01 2016-02 2016-03 
#      1      23      21      22      22      21      23      21      21      23 
#2016-04 2016-05 2016-06 2016-07 2016-08 2016-09 2016-10 2016-11 2016-12 2017-01 
#     21      22      22      21      23      22      21      22      22      22 
#2017-02 2017-03 2017-04 2017-05 2017-06 2017-07 2017-08 2017-09 2017-10 2017-11 
#     20      23      20      23      22      21      23      21      22      22 
#2017-12 
#     21 

但第33行还存在一个问题,它没有考虑时移:

out <- ts(num.days, start = tsp(x)[1L], frequency = freq)
out
#     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#2015                           1  23  21  22  22  21
#2016  23  21  21  23  21  22  22  21  23  22  21  22
#2017  22  22  20  23  20  23  22  21  23  21  22  22
#2018  21