我已经将《寓言》的预测与 ETS 模型结合使用,如下所示:
stl.fc <- train |>
model(stlf = decomposition_model(
STL(value), # decomposition to use (STL)
ETS(season_adjust),
SNAIVE(season_year) # it's the default but we make it explicit
)) |>
forecast(h = TIME_HORIZON)
我的数据由庇护申请数量组成,并且必须为正数或零。 当我尝试使用自动绘图绘制预测结果时,还会显示预测区间(这很好),但其值也可能为负(这不好)。
stl.fc |>
autoplot(data.tsbl)
我试图摆脱这些负值,但这似乎很棘手,因为包含这些值的对象属于
"distribution" "vctrs_vctr" "list"
类,而且我不知道如何做到这一点。
这是它的样子的屏幕截图(我指的专栏称为
value
)。
value
列中的每个元素看起来都像是两个条目的列表,mu
和sigma
,我可以使用(例如)unlist(stl.fc$value[1])
看到它们
mu sigma
8087.440 3308.573
从这里开始,我对如何删除这些负数有点困惑。
我正在检查“预测:原则与实践”一书中的“确保预测保持在限制范围内”页面,但我似乎无法理解如何实现我想要的。
或者,我想如果我能去掉图中的负 Y 值(即隐藏这些负值),我也会活下去,但这感觉更像是作弊......
我最终想出了一个解决方案,不是让工具自动计算预测间隔,而是生成 1000 条模拟路径(受 this 启发),并在按日期聚合这些路径之前,将负值更改为零。
剧本和新剧情下方:
# create the model
stl.mod <- train |>
model(stlf = decomposition_model(
STL(value), # decomposition to use (STL)
ETS(season_adjust),
SNAIVE(season_year) # it's the default but we make it explicit
))
# generate simulated paths and change negative values to zero before aggregating
# https://otexts.com/fpp3/combinations.html
stl.fc <- stl.mod |>
# Generate 1000 future sample paths
generate(h = TIME_HORIZON, times = 1000) |>
mutate(.sim = if_else(.sim < 0, 0, .sim)) |>
# Compute forecast distributions from future sample paths
as_tibble() |>
group_by(date, .model) |>
summarise(
dist = distributional::dist_sample(list(.sim))
) |>
ungroup() |>
# Create fable object
as_fable(index = date, key = .model,
distribution = dist, response = "value")
stl.fc |>
autoplot(data.tsbl)