R组合预测使用tsibble,代码胁迫到tibble而不是mable,可以换回
我正在关注 Rob Hyndman的时间序列我在R中的预测第13章讨论了实用的预测问题(并且在处理实际的预测问题部分时,我遇到了问题!)
具体来说,我正在尝试创建一个组合预测,但是创建估计模型的线性函数的代码强制胁迫我的tsibble到一个tibble上,而没有那条代码,我最终会带有一个梅布尔,而且我似乎无法将tibble恢复到梅布尔中。
这是Hyndman博士的代码:
auscafe <- aus_retail %>%
filter(stringr::str_detect(Industry, "Takeaway")) %>%
summarise(Turnover = sum(Turnover))
train <- auscafe %>%
filter(year(Month) <= 2013)
STLF <- decomposition_model(
STL(log(Turnover) ~ season(window = Inf)),
ETS(season_adjust ~ season("N"))
)
cafe_models <- train %>%
model(
ets = ETS(Turnover),
stlf = STLF,
arima = ARIMA(log(Turnover))
) %>%
mutate(combination = (ets + stlf + arima) / 3)
cafe_fc <- cafe_models %>%
forecast(h = "5 years")
但是当我在自己的控制台中尝试一下时,我没有运气。
Hyndman的DataFrame存在于寓言或fabletools或Feasts或Tsibble或Tsibbledata软件包中。 (我不记得是哪一个。)
这是我的数据框架:
structure(list(Date = structure(c(19114, 19115, 19116, 19117,
19118, 19119, 19120, 19121, 19122, 19123, 19124, 19125, 19126,
19127, 19128, 19129, 19130, 19131, 19132, 19133, 19134, 19135,
19136, 19137, 19138, 19139, 19140, 19141, 19142, 19143, 19144,
19145, 19146, 19147, 19148, 19149, 19150, 19151, 19152, 19153,
19154, 19155, 19156, 19157, 19158, 19159, 19160, 19161, 19162,
19163, 19164, 19165), class = "Date"), Sales = c(2147350, 1953453,
1930514, 1951737, 2496552, 2091370, 1921364, 2342280, 2224779,
2124766, 2229922, 2501654, 2056751, 1908814, 2109249, 1946929,
2057711, 2001398, 2535514, 2060774, 1793765, 1954603, 2019082,
2077929, 2152838, 2802181, 2314866, 2268680, 2380746, 1887751,
2201204, 2004422, 2783170, 2238542, 2000024, 1777258, 1844045,
2138638, 2387784, 2783170, 1988945, 1749007, 2128774, 2101340,
2122877, 2085712, 2532569, 1995143, 1713529, 2045398, 1781901,
2164901)), class = c("tbl_ts", "tbl_df", "tbl", "data.frame"), row.names = c(NA,
-52L), key = structure(list(.rows = structure(list(1:52), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), 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")))
这是我的代码:
library(tsibble)
library(tsibbledata)
library(fable)
library(fabletools)
library(feasts)
my_dcmp_spec <- decomposition_model(
STL(Sales),
ETS(season_adjust ~ season("N"))
)
fit <- time_series_sample %>%
model(
stl_ets = my_dcmp_spec,
`Seasonal naïve` = SNAIVE(Sales),
holt_winters = ETS(Sales ~ error("A") + trend("N") + season("N"))
) %>%
mutate(combination = (stl_ets + `Seasonal naïve` + holt_winters) / 3)
我无法重新创建的最后一点。
fc <- fit %>% forecast(h = 21)
这是我收到的错误消息。
Error in UseMethod("forecast") :
no applicable method for 'forecast' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')"
因此,我再次尝试使用此功能将tibble胁到Mable:
fit %>% as_mable()
我收到另一个错误消息:
Error in `build_mable()`:
! A mable must contain at least one model.
Backtrace:
1. fit %>% as_mable()
3. fabletools:::as_mable.data.frame(.)
4. fabletools:::build_mable(x, key = !!enquo(key), model = !!enquo(model))
此错误消息对我没有意义,因为我的tibble确实包含模型, 如您在屏幕截图中所见。
我还尝试在有没有as_mable()
调用的情况下添加Pivot_longer,但这也不起作用。
fit <- time_series_sample %>%
model(
stl_ets = my_dcmp_spec,
`Seasonal naïve` = SNAIVE(Sales),
holt_winters = ETS(Sales ~ error("A") + trend("N") + season("N"))
) %>%
mutate(combination = (stl_ets + `Seasonal naïve` + holt_winters) / 3) %>%
pivot_longer(everything(), names_to = "Model name",
values_to = "Sales")
# as_mable()
只要我包括创建组合模型的步骤,即stutate(组合(...
行,我最终都会得到一个tibble,而不是梅布尔。Hyndman
博士的代码不做t似乎将他的模型逼入一个笨拙的,但我的代码似乎迫使
我可以
fit <- time_series_sample %>%
model(
stl_ets = my_dcmp_spec,
seasonal_naive = SNAIVE(Sales),
holt_winters = ETS(Sales ~ error("A") + trend("N") + season("N"))
)
fc <- fit %>% forecast(h = 21)
fc %>% autoplot(time_series_sample)
解决这个问题。但是我肯定缺少组合预测,这是我真正希望添加
的 东西。因为我在单元格中看到了一个s3:lst_model ...但是,mable被胁迫到tibble,我无法在其上使用预测
I'm following Dr. Rob Hyndman's textbook on time series forecasting in R. I'm in Chapter 13 which talks about Practical Forecasting Issues (and I'm having issues while working through the practical forecasting issue section!)
Specifically, I'm trying to create a combination forecast, but the code that creates a linear function of the estimated models coerces my tsibble to a tibble, whereas without that line of code I would end up with a mable, and I can't seem to work the tibble back into a mable.
Here is Dr. Hyndman's code:
auscafe <- aus_retail %>%
filter(stringr::str_detect(Industry, "Takeaway")) %>%
summarise(Turnover = sum(Turnover))
train <- auscafe %>%
filter(year(Month) <= 2013)
STLF <- decomposition_model(
STL(log(Turnover) ~ season(window = Inf)),
ETS(season_adjust ~ season("N"))
)
cafe_models <- train %>%
model(
ets = ETS(Turnover),
stlf = STLF,
arima = ARIMA(log(Turnover))
) %>%
mutate(combination = (ets + stlf + arima) / 3)
cafe_fc <- cafe_models %>%
forecast(h = "5 years")
But when I try it out in my own console I'm not having luck.
Hyndman's dataframe exists in the fable or fabletools or feasts or tsibble or tsibbledata packages. (I can't remember which one.)
Here is my dataframe:
structure(list(Date = structure(c(19114, 19115, 19116, 19117,
19118, 19119, 19120, 19121, 19122, 19123, 19124, 19125, 19126,
19127, 19128, 19129, 19130, 19131, 19132, 19133, 19134, 19135,
19136, 19137, 19138, 19139, 19140, 19141, 19142, 19143, 19144,
19145, 19146, 19147, 19148, 19149, 19150, 19151, 19152, 19153,
19154, 19155, 19156, 19157, 19158, 19159, 19160, 19161, 19162,
19163, 19164, 19165), class = "Date"), Sales = c(2147350, 1953453,
1930514, 1951737, 2496552, 2091370, 1921364, 2342280, 2224779,
2124766, 2229922, 2501654, 2056751, 1908814, 2109249, 1946929,
2057711, 2001398, 2535514, 2060774, 1793765, 1954603, 2019082,
2077929, 2152838, 2802181, 2314866, 2268680, 2380746, 1887751,
2201204, 2004422, 2783170, 2238542, 2000024, 1777258, 1844045,
2138638, 2387784, 2783170, 1988945, 1749007, 2128774, 2101340,
2122877, 2085712, 2532569, 1995143, 1713529, 2045398, 1781901,
2164901)), class = c("tbl_ts", "tbl_df", "tbl", "data.frame"), row.names = c(NA,
-52L), key = structure(list(.rows = structure(list(1:52), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), 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")))
And here is my code:
library(tsibble)
library(tsibbledata)
library(fable)
library(fabletools)
library(feasts)
my_dcmp_spec <- decomposition_model(
STL(Sales),
ETS(season_adjust ~ season("N"))
)
fit <- time_series_sample %>%
model(
stl_ets = my_dcmp_spec,
`Seasonal naïve` = SNAIVE(Sales),
holt_winters = ETS(Sales ~ error("A") + trend("N") + season("N"))
) %>%
mutate(combination = (stl_ets + `Seasonal naïve` + holt_winters) / 3)
This final bit I can't recreate.
fc <- fit %>% forecast(h = 21)
Here is the error message I receive.
Error in UseMethod("forecast") :
no applicable method for 'forecast' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')"
So I tried again using this function to coerce the tibble to a mable:
fit %>% as_mable()
I get another error message:
Error in `build_mable()`:
! A mable must contain at least one model.
Backtrace:
1. fit %>% as_mable()
3. fabletools:::as_mable.data.frame(.)
4. fabletools:::build_mable(x, key = !!enquo(key), model = !!enquo(model))
This error message doesn't make sense to me because my tibble does contain models,
as you can see in the screenshot.
I also tried adding pivot_longer both with and without the as_mable()
call, but that doesn't work either.
fit <- time_series_sample %>%
model(
stl_ets = my_dcmp_spec,
`Seasonal naïve` = SNAIVE(Sales),
holt_winters = ETS(Sales ~ error("A") + trend("N") + season("N"))
) %>%
mutate(combination = (stl_ets + `Seasonal naïve` + holt_winters) / 3) %>%
pivot_longer(everything(), names_to = "Model name",
values_to = "Sales")
# as_mable()
As long as I include the step where I create a combination model, which is the mutate(combination(...
line, I end up with a tibble and not a mable.
Dr. Hyndman's code doesn't seem to coerce his models into a tibble, but keeps them as a mable, while my code seems to coerce to a tibble. What can I do to fix this?
If I run the code without the combination part, I can get this graph
fit <- time_series_sample %>%
model(
stl_ets = my_dcmp_spec,
seasonal_naive = SNAIVE(Sales),
holt_winters = ETS(Sales ~ error("A") + trend("N") + season("N"))
)
fc <- fit %>% forecast(h = 21)
fc %>% autoplot(time_series_sample)
But I'm definitely missing the combination forecast, which is what I'm really hoping to add.
Is all this a bug? The mutate works to create the combination forecast, which is a linear combination of models - I can see that it worked because I see an S3: lst_model in the cell... but the mable is coerced to a tibble and I can't use forecast on it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论