我有一个 data.frames/tsibbles 列表,如下所示。这已根据 RSMODE 列中的值从更大的 data.frame 中拆分出来。
library(tsibble)
library(fable)
library(tidyverse)
tsib.list <- list(COMBO = structure(list(RSLITM = c("004", "004", "004", "004",
"004", "004", "004", "004", "004", "004", "004", "004", "004",
"004", "004", "004", "004", "004", "004", "004", "004", "004",
"004", "004", "004", "004", "004", "004", "004", "004", "004",
"004", "004", "004", "004", "004", "004"), RSFMTH = structure(c(17713,
17744, 17775, 17805, 17836, 17866, 17897, 17928, 17956, 17987,
18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231, 18262,
18293, 18322, 18353, 18383, 18414, 18444, 18475, 18506, 18536,
18567, 18597, 18628, 18659, 18687, 18718, 18748, 18779, 18809
), class = c("yearmonth", "vctrs_vctr")), RSFQTY = c(285600,
352200, 273600, 282700, 175800, 138700, 177700, 245900, 165000,
180100, 298000, 173800, 257300, 282800, 164500, 155100, 232300,
175500, 226000, 287100, 221400, 270800, 286200, 394400, 336600,
331000, 224600, 216800, 351600, 374700, 173500, 423700, 357700,
245200, 454700, 361700, 381200), RSSEAS = c("A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A"), RSTREND = c("N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N"), RSMODE = c("COMBO",
"COMBO", "COMBO", "COMBO", "COMBO", "COMBO", "COMBO", "COMBO",
"COMBO", "COMBO", "COMBO", "COMBO", "COMBO", "COMBO", "COMBO",
"COMBO", "COMBO", "COMBO", "COMBO", "COMBO", "COMBO", "COMBO",
"COMBO", "COMBO", "COMBO", "COMBO", "COMBO", "COMBO", "COMBO",
"COMBO", "COMBO", "COMBO", "COMBO", "COMBO", "COMBO", "COMBO",
"COMBO")), row.names = c(NA, -37L), key = structure(list(RSLITM = "004",
RSSEAS = "A", RSTREND = "N", RSMODE = "COMBO", .rows = structure(list(
1:37), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -1L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), index = structure("RSFMTH", ordered = TRUE), index2 = "RSFMTH", interval = structure(list(
year = 0, quarter = 0, month = 1, week = 0, day = 0, 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")), EXP = structure(list(RSLITM = c("005", "005",
"005", "005", "005", "005", "005", "005", "005", "005", "005",
"005", "005", "005", "005", "005", "005", "005", "005", "005",
"005", "005", "005", "005", "005", "005", "005", "005", "005",
"005", "005", "005", "005", "005", "005", "005", "005", "006",
"006", "006", "006", "006", "006", "006", "006", "006", "006",
"006", "006", "006", "006", "006", "006", "006", "006", "006",
"006", "006", "006", "006", "006", "006", "006", "006", "006",
"006", "006", "006", "006", "006", "006", "006", "006", "006"
), RSFMTH = structure(c(17713, 17744, 17775, 17805, 17836, 17866,
17897, 17928, 17956, 17987, 18017, 18048, 18078, 18109, 18140,
18170, 18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414,
18444, 18475, 18506, 18536, 18567, 18597, 18628, 18659, 18687,
18718, 18748, 18779, 18809, 17713, 17744, 17775, 17805, 17836,
17866, 17897, 17928, 17956, 17987, 18017, 18048, 18078, 18109,
18140, 18170, 18201, 18231, 18262, 18293, 18322, 18353, 18383,
18414, 18444, 18475, 18506, 18536, 18567, 18597, 18628, 18659,
18687, 18718, 18748, 18779, 18809), class = c("yearmonth", "vctrs_vctr"
)), RSFQTY = c(79000, 58100, 66300, 52700, 68600, 33000, 76600,
85600, 84100, 49000, 98000, 113500, 83800, 64000, 116800, 72000,
65200, 49800, 33300, 79800, 48000, 81600, 125000, 53500, 97600,
80000, 81900, 80000, 53800, 39000, 73800, 76600, 33700, 60200,
84000, 66600, 32400, 285600, 352200, 273600, 282700, 175800,
138700, 177700, 245900, 165000, 180100, 298000, 173800, 257300,
282800, 164500, 155100, 232300, 175500, 226000, 287100, 221400,
270800, 286200, 394400, 336600, 331000, 224600, 216800, 351600,
374700, 173500, 423700, 357700, 245200, 454700, 361700, 381200
), RSSEAS = c("N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A"),
RSTREND = c("N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N"), RSMODE = c("EXP", "EXP", "EXP",
"EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP",
"EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP",
"EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP",
"EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP",
"EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP",
"EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP",
"EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP",
"EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP", "EXP")), row.names = c(NA,
-74L), key = structure(list(RSLITM = c("005", "006"), RSSEAS = c("N",
"A"), RSTREND = c("N", "N"), RSMODE = c("EXP", "EXP"), .rows = structure(list(
1:37, 38:74), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), index = structure("RSFMTH", ordered = TRUE), index2 = "RSFMTH", interval = structure(list(
year = 0, quarter = 0, month = 1, week = 0, day = 0, 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")))
tsibble/data.frame 包含需求历史和参数信息,这些参数将用于估计指数平滑和其他时间序列模型。对于不同的 RSLITM,这些参数可能不同。
我有几个函数将用于列表中的每个 data.frame,因为列表是在 RSMODE 列上拆分较大的 data.frame 时创建的。请注意,我有一个 ARIMA 函数,尽管在这个测试 data.frame 中,我没有指定任何 RSLITM 用于估计用于预测目的的 ARIMA 模型。
combo.function <- function(tsib){
season.param <- tsib[["RSSEAS"]][1]
trend.param <- tsib[["RSTREND"]][1]
fmla <- as.formula(glue::glue("RSFQTY ~ trend('{trend.param}') +",
" season('{season.param}')"))
print(fmla)
tsib %>% model(EXP = ETS(fmla), ARI = ARIMA(RSFQTY)) %>% mutate(COM = (ARI + EXP)/2) %>% forecast(h = "3 years") %>% filter(.model == "COM")
}
ets.function <- function(tsib){
season.param <- tsib[["RSSEAS"]][1]
trend.param <- tsib[["RSTREND"]][1]
fmla <- as.formula(glue::glue("RSFQTY ~ trend('{trend.param}') +",
" season('{season.param}')"))
print(fmla)
tsib %>% model(EXP = ETS(fmla)) %>% forecast(h = "3 years")
}
arima.function <- function(tsib){
tsib %>% model(ARI = ARIMA(RSFQTY)) %>% forecast(h = "3 years")
}
我正在使用下面的代码来估计列表中每个 data.frame/tsibble 中每个 RSLITM 的适当模型。但是,由于没有指定要使用 ARIMA 模型预测的 RSLITM,因此在这种情况下没有 ARIMA 列表元素会出现问题。(RSMODE 列中没有带有“ARIMA”值的 RLSITM。)因此,它会引发错误。
我相信有一些方法可以使用try()
或类似的功能来处理这个问题,但我不确定如何在map()
.
pmap_dfr(list(tsib = list(tsib.list$ARIMA, tsib.list$COMBO, tsib.list$EXP), func = list(arima.function, combo.function, ets.function)),
function(tsib, func) map(tsib %>% group_split(RSLITM), func))
这是我得到的错误。
Error in UseMethod("group_split") :
no applicable method for 'group_split' applied to an object of class "NULL"