library(ggplot2)
library(dplyr)
library(tsibble)
library(feasts)
structure(list(Date = structure(c(18809, 18810, 18811, 18812,
18813, 18814, 18815, 18816, 18817, 18818, 18819, 18820, 18821,
18822, 18823, 18824, 18825, 18826, 18827, 18828, 18829, 18830,
18831, 18832, 18833, 18834, 18835, 18836, 18837, 18838, 18839,
18840, 18841, 18842, 18843, 18844, 18845, 18846, 18847, 18848,
18849, 18850, 18851, 18852, 18853, 18854, 18855, 18856, 18857,
18858, 18859, 18860, 18861, 18862, 18863, 18864, 18865, 18866,
18867, 18868, 18869, 18870, 18871, 18872, 18873, 18874, 18875,
18876, 18877, 18878, 18879, 18880, 18881, 18882, 18883, 18884,
18885), class = "Date"), Cost.per.Complete.Payment = c(165.255,
121.418, 124.69, 151.7425, 155.268, 162.961666666667, 143.01,
172.854, 292.742, 104.88, 87.98, 132.7025, 73.865, 117.258, 187.297777777778,
186.31625, 194.785, 124.1825, 180.443333333333, 193.347777777778,
200.788888888889, 172.950909090909, 175.171666666667, 244.03125,
208.395, 135.334444444444, 188.028, 185.668571428571, 339.939411764706,
207.62, 259.25, 197.46, 175.341666666667, 183.813333333333, 194.3525,
93.2125, 125.871428571429, 121.939166666667, 123.983, 267.26,
134.28375, NA, 324.1675, 188.22, 132.623333333333, 76.8233333333333,
110.8, 104.606666666667, 102.306666666667, 187.265, 349.498333333333,
215.255, 188.11, 246.963333333333, 129.074, 88.59, 113.315, 115.1,
103.546, 108.793333333333, 153.595, 180.83, 144.372, 120.108,
148.9175, 342.283333333333, 318.5575, 150.116, 197.5525, 100.046666666667,
85.4525, 127.635, 177.522, 135.4375, 77.0316666666667, 123.998,
270.258)), row.names = c(NA, -77L), key = structure(list(.rows = structure(list(
1:77), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr",
"list"))), row.names = c(NA, -1L), class = c("tbl_df", "tbl",
"data.frame")), index = structure("Date", ordered = TRUE), index2 = "Date", interval = structure(list(
year = 0, quarter = 0, month = 0, week = 0, day = 1, hour = 0,
minute = 0, second = 0, millisecond = 0, microsecond = 0,
nanosecond = 0, unit = 0), .regular = TRUE, class = c("interval",
"vctrs_rcrd", "vctrs_vctr")), class = c("tbl_ts", "tbl_df", "tbl",
"data.frame"))
我转换为 tribble 对象:
data_frame <- as_tsibble(data_frame)
然后我尝试运行这段代码:
data_frame %>%
model(
STL(Cost.per.Complete.Payment ~ trend(window = 7) +
season(window = 'periodic'),
robust = TRUE)) %>%
components() %>%
autoplot()
但我得到一个错误。
1 error encountered for STL(Cost.per.Complete.Payment ~ trend(window = 7) + season(window = "periodic"),
robust = TRUE)
[1] STL decomposition does not support series with missing values.
Error: Problem with `mutate()` column `cmp`.
ℹ `cmp = map(.fit, components)`.
x no applicable method for 'components' applied to an object of class "null_mdl"
我不确定这意味着什么/我非常困惑为什么它说时间序列中存在缺失值。当我检查唯一日期值的数量时,我得到 77,当我从最小日期中减去最大日期时,我得到比 77 少 1,即 76。所以我觉得检查出来了(另外我手动检查了日期列以查看如果有缺失值并且没有)。
length(unique(data_frame$Date)) # this is 77
max(data_frame$Date) - min(data_frame$Date) # this is 76