r - Using nested time series columns as input into function, return value to nested df -
i use ts.union()
create column in nested data frame, inputs ts.union()
being 2 nested time series in nested data frame. see set below.
library(tidyverse) library(forecast) ts_special <- function(df){ts(df,start = c(2010,01), frequency = 4)} ets_spec <- function(df){ets(df, mod="mmm", opt.crit="lik", damped=null)} x <- cumsum(rnorm(48)) grp <- rep(c("a","b"), 24) dtf <- cbind(x, grp) %>% data.frame dtf <- dtf %>% group_by(grp) %>% nest dtf <- dtf %>% mutate(ts = map(data, ts_special)) dtf <- dtf %>% mutate(ets = map(ts, ets_spec)) dtf <- dtf %>% mutate(ets_fcast = map(ets, forecast)) dtf <- dtf %>% mutate(pred= map(dtf$ets_fcast, ~ .x[["mean"]])) # below works want dplyr way , nest result in column in dtf ts.union(dtf$ts[[1]], dtf$pred[[1]])
i'd have result of ts.union
each group's time series , prediction combination stored in nested column in data frame, dtf
.
you can use purrr::map2
map on inputs ts.union
in parallel:
dtf %>% mutate(union = map2(ts, pred, ts.union))
Comments
Post a Comment