如何创建许多对参数进行不同更改的新对象?

How can I create many new objects with different changes to the parameters?

一个完全可重现的例子。

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))

创建了下面的数据框

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])

根据独特的子产品创建数据框

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))

创建时间序列

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

问题:如果我想创建 10 个如下所示的新模型对象,其中 alpha = 0.1,0.2,0.3.... 到 1.0。有没有一种方法可以自动执行以下过程,而无需在代码中创建 10 个单独的 model_tune_ses1?

model_tune_ses1 <- lapply(list_ts[1:(length(list_ts)/2)], function(x) 
forecast::forecast(ses(x,h=24,alpha=0.1))) 
model_tune_ses1 <- lapply(model_tune_ses1, "[",  c("mean"))

model_tune_ses2 <- lapply(list_ts[1:(length(list_ts)/2)], function(x) 
forecast::forecast(ses(x,h=24,alpha=0.2))) 
model_tune_ses2 <- lapply(model_tune_ses2, "[",  c("mean"))

etc...

编辑:这并没有给我超过 9 个模型。例如,我想要一个 n1 =.1 n2=.99 和 n3= .3 的模型,因此我们将有超过 9 个模型。

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})

我们可以有一个嵌套循环

n1 <- seq(0.1, 0.5, by = 0.1)

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

检查创建的对象

ls(pattern = 'model_tune_ses')
[1] "model_tune_ses1" "model_tune_ses2" "model_tune_ses3" "model_tune_ses4" "model_tune_ses5"