Reputation: 822
I'm working with multiple time series models, and hoping to manage them with data.table:
pkg <- c("data.table", "magrittr", "forecast")
sapply(pkg, library, character.only = TRUE)
df <- data.frame(
group = rep(c("a", "b", "c"), each = 10),
val = sample(1:10, 30, replace = TRUE)
) %>% as.data.table
I can successfully generate the model (and a time series ready for the next step):
t1 <- df[, list(
tsAll = list(val %>% as.ts),
mod1 = list(val %>% as.ts %>% window(1, 7) %>% ets)
), by = group]
I'm now trying to update the ets model, which involves passing 'tsAll' into the ets function and providing the model to use (mod1).
This does not work:
t1[, lapply(tsAll, ets, model = mod1)]
I've also tried:
t1[, lapply(tsAll, ets, model = mod1[[1]])]
This runs, but it looks like the same model is returned to every row.
Possibly I have solved the question but run into this issue:
Why is using update on a lm inside a grouped data.table losing its model data?
Can anyone help with the next step?
Upvotes: 2
Views: 121
Reputation: 49448
Is this what you want:
t1[, list(lapply(tsAll, ets, model = mod1[[1]])), by = group]$V1
I put the result in a list, so that the data type is preserved, as opposed to being converted into a vector and did the operation by group (since each group has its own model).
Upvotes: 1