Search code examples
rloopsdata-manipulationforecast

How to automatically create more models in my environment changing 3 parameters at a time?


A fully reproducible example.

library(forecast)
date = seq(as.Date("2019/01/01"), by = "month", length.out = 48)

productB = rep("B",48)
productB = rep("B",48)
productA = rep("A",48)
productA = rep("A",48)

subproducts1=rep("1",48)
subproducts2=rep("2",48)
subproductsx=rep("x",48)
subproductsy=rep("y",48)

b1 <- c(rnorm(30,5), rep(0,18))
b2 <- c(rnorm(30,5), rep(0,18))
b3 <-c(rnorm(30,5), rep(0,18))
b4 <- c(rnorm(30,5), rep(0,18))

Created the dataframe below

dfone <- data.frame("date"= rep(date,4),
            "product"= c(rep(productB,2),rep(productA,2)),
            "subproduct"= 
c(subproducts1,subproducts2,subproductsx,subproductsy),
            "actuals"= c(b1,b2,b3,b4))

export_df <- split(dfone[1:4], dfone[3])

Creation of data frames based off UNIQUE SUBPRODUCTS

dummy_list <- split(dfone[1:4], dfone[3]) %>% lapply( function(x) 
x[(names(x) %in% c("date", "actuals"))])
dummy_list <-  lapply(dummy_list, function(x) { x["date"] <- NULL; x })


list_dfs <- list()
for (i in 1:length(unique(dfone$subproduct))) {
  #assign(paste0("df", i), as.data.frame(dummy_list[[i]]))
  list_dfs <-append(list_dfs,dummy_list[[i]])
}

combined_dfs <- Reduce(function(x, y) merge(x, y, all = TRUE,  
by='date'), list(list_dfs))

Creating the time series

list_ts <- lapply(list_dfs, function(t) 
ts(t,start=c(2019,1),end=c(2021,6), frequency = 12)) %>%
  lapply( function(t) ts_split(t,sample.out=(0.2*length(t))))    # 
creates my train test split
list_ts <- do.call("rbind", list_ts)  #Creates a list of time series

Question. This doesn't give me more than 9 models. I'd want a model for n1 =.1 n2=.99 and n3= .3 for example so we would have way more than 9 models for this.

n1 <- seq(0.1, 0.99, by = 0.1)
n2 <- seq(0.1, 0.99, by = 0.1)
n3 <- seq(0.1, 0.99, by = 0.1)

out<- lapply(seq_along(n1), function(i) {
   cw_triple_holtwinters_additive <- lapply(list_ts[1: 
(length(list_ts)/2)], function(x) 
       forecast::forecast(ses(x,h=24,alpha = 
n1[i],beta=n2[i],gamma=n3[i])))
    cw_triple_holtwinters_additive <- 
 lapply(cw_triple_holtwinters_additive, "[", "mean")
  assign(paste0("cw_triple_holtwinters_additive", i), 
cw_triple_holtwinters_additive, envir = .GlobalEnv)
 cw_triple_holtwinters_additive})

Additional question: for order=c(1,1,1) and order=c(0,1,0) can I create a list of values like these and loop through them both at the same time like Akrun's solution?

cw_seasonal_autoregressive_integratedmovingaverage1 <- lapply(list_ts[1: 
(length(list_ts)/2)], function(x)
 forecast::forecast(arima(x,order=c(1,1,1),seasonal=list(order=c(0,1,0),
period=12)) ,h=24))

cw_seasonal_autoregressive_integratedmovingaverage1 <- 
lapply(cw_seasonal_autoregressive_integratedmovingaverage1, "[",  
c("mean"))

Solution

  • We can use expand.grid to get all the combinations

    dat_n <- expand.grid(n1 = n1, n2= n2, n3 = n3) 
    

    Then, we loop over the sequence of rows of 'dat_n'

    out<- lapply(seq_len(nrow(dat_n)), function(i) {
       cw_triple_holtwinters_additive <- lapply(list_ts[1: 
    (length(list_ts)/2)], function(x) 
           forecast::forecast(ses(x,h=24,alpha = 
    dat_n$n1[i],beta=dat_n$n2[i],gamma=dat_n$n3[i])))
        cw_triple_holtwinters_additive <- 
     lapply(cw_triple_holtwinters_additive, "[", "mean")
      assign(paste0("cw_triple_holtwinters_additive", i), 
    cw_triple_holtwinters_additive, envir = .GlobalEnv)
     cw_triple_holtwinters_additive})
    

    -checking

     ls(pattern = "cw_triple")
      [1] "cw_triple_holtwinters_additive1"   "cw_triple_holtwinters_additive10"  "cw_triple_holtwinters_additive100" "cw_triple_holtwinters_additive101"
      [5] "cw_triple_holtwinters_additive102" "cw_triple_holtwinters_additive103" "cw_triple_holtwinters_additive104" "cw_triple_holtwinters_additive105"
      [9] "cw_triple_holtwinters_additive106" "cw_triple_holtwinters_additive107" "cw_triple_holtwinters_additive108" "cw_triple_holtwinters_additive109"
     [13] "cw_triple_holtwinters_additive11"  "cw_triple_holtwinters_additive110" "cw_triple_holtwinters_additive111" "cw_triple_holtwinters_additive112"
     [17] "cw_triple_holtwinters_additive113" "cw_triple_holtwinters_additive114" "cw_triple_holtwinters_additive115" "cw_triple_holtwinters_additive116"
     [21] "cw_triple_holtwinters_additive117" 
    ...