具有每周季节性的每日数据的ts()对象的日期索引错误

时间:2018-07-17 14:55:23

标签: r time-series

这是每日数据的示例数据集:

df_so <- structure(list(date = structure(c(16953, 16954, 16955, 16956, 
                                           16957, 16958, 16959, 16960, 16961, 16962, 16963, 16964, 16965, 
                                           16966, 16967, 16968, 16969, 16970, 16971, 16972, 16973, 16974, 
                                           16975, 16976, 16977, 16978, 16979, 16980), class = "Date"),
                        valor = c(42430, 34567, 28485, 10675, 10168, 40799, 38640, 41542, 37668, 36769, 
                                  8670, 10567, 51198, 42259, 44858, 41091, 41320, 10966, 7902, 
                                  35748, 39939, 37009, 34137, 32406, 16680, 11858, 36763, 33702
                                           )),
                   class = "data.frame", row.names = c(NA, -28L))

我已经正确定义了日期向量,如下面的输出所示:

> df_so$date
 [1] "2016-06-01" "2016-06-02" "2016-06-03" "2016-06-04" "2016-06-05" "2016-06-06" "2016-06-07" "2016-06-08" "2016-06-09"
[10] "2016-06-10" "2016-06-11" "2016-06-12" "2016-06-13" "2016-06-14" "2016-06-15" "2016-06-16" "2016-06-17" "2016-06-18"
[19] "2016-06-19" "2016-06-20" "2016-06-21" "2016-06-22" "2016-06-23" "2016-06-24" "2016-06-25" "2016-06-26" "2016-06-27"
[28] "2016-06-28"

但是,ts()对象不维护该时间索引。相反,该函数将其弄乱了:

> ts_so <- ts(df_so$valor, frequency = 7, start = lubridate::decimal_date(min(df_so$date)))
> time(ts_so)
Time Series:
Start = 2016.41530054645 
End = 2020.27244340359 
Frequency = 7 
 [1] 2016.415 2016.558 2016.701 2016.844 2016.987 2017.130 2017.272 2017.415 2017.558 2017.701 2017.844 2017.987 2018.130
[14] 2018.272 2018.415 2018.558 2018.701 2018.844 2018.987 2019.130 2019.272 2019.415 2019.558 2019.701 2019.844 2019.987
[27] 2020.130 2020.272

我尝试了另一种定义ts对象的方法,该方法提供了另一个时间索引,但仍然很不正确:

> ts_so <- ts(df_so$valor, frequency = 7, start = c(2016, lubridate::yday(min(df_so$date))))
> time(ts_so)
Time Series:
Start = c(2037, 6) 
End = c(2041, 5) 
Frequency = 7 
 [1] 2037.714 2037.857 2038.000 2038.143 2038.286 2038.429 2038.571 2038.714 2038.857 2039.000 2039.143 2039.286 2039.429
[14] 2039.571 2039.714 2039.857 2040.000 2040.143 2040.286 2040.429 2040.571 2040.714 2040.857 2041.000 2041.143 2041.286
[27] 2041.429 2041.571

如何确保ts()对象为频率为7的每日数据保持正确的时间索引?

0 个答案:

没有答案