I have a tsibble as shown below.
test.data <- 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", "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"), 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(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, 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), 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", "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("004", "005"), RSSEAS = c("A",
"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"))
I would like to apply a modified ETS function using the saved parameters from the tsibble. For instance, whatever is in the RSSEAS and RSTREND columns will be used to estimate the ETS model.
The following works:
test.data %>% model(EXP = ETS(RSFQTY ~ trend("N") + season("A")))
However, when I try to use a function below to extract the parameters for each SKU (since presumably they could be different for each SKU), I get an error message.
ets.function <- function(tsib){
season.param <- as.character(tsib[1, "RSSEAS"])
trend.param <- as.character(tsib[1, "RSTREND"])
tsib %>% model(EXP = ETS(RSFQTY ~ trend(trend.param) + season(season.param))) %>% forecast(h = "3 years")
}
If I call ets.function(test.data)
R returns a fable but it is NULL/NA since the model is not being estimated with the specified parameters.
Calling map_dfr(test.data, ets.function)
gives me the following error:
Error in tsib[1, "RSSEAS"] : incorrect number of dimensions
This doesn't make sense to me since if I run the code for season.param or trend.param in my console, I get "A" or "N" as appropriate, which is exactly the value the trend and season specials take inside the ETS function.
Basically I am trying to figure out a way to map ETS over my tsibble using prespecified parameters for each unique key combination. I am open to other ideas about how to achieve this (pmap_dfr for vectors of parameters, etc).