【发布时间】:2021-09-14 18:57:59
【问题描述】:
一个完全可重现的例子。
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])
根据 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))
创建时间序列
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
如何自动创建它以便在全局环境中自动创建 m1 到 m6?注意第一个参数 order = 是相同的,而第二个参数是不同的。在我们用完所有 2 阶的值之后,我们继续按照第一个参数的顺序移动到下一个元素。
m1<- 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))
m1<- lapply(m1, "[", c("mean"))
m2<- lapply(list_ts[1:
(length(list_ts)/2)], function(x)
forecast::forecast(arima(x,order=c(1,1,1),seasonal=list(order=c(1,0,0),
period=12)) ,h=24))
m2<- lapply(m2"[", c("mean"))
m3<- lapply(list_ts[1:
(length(list_ts)/2)], function(x)
forecast::forecast(arima(x,order=c(1,1,1),seasonal=list(order=c(0,0,0),
period=12)) ,h=24))
m3<- lapply(m3"[", c("mean"))
m4<- lapply(list_ts[1:
(length(list_ts)/2)], function(x)
forecast::forecast(arima(x,order=c(0,0,0),seasonal=list(order=c(0,1,0),
period=12)) ,h=24))
m4<- lapply(m4, "[", c("mean"))
m5<- lapply(list_ts[1:
(length(list_ts)/2)], function(x)
forecast::forecast(arima(x,order=c(0,0,0),seasonal=list(order=c(1,0,0),
period=12)) ,h=24))
m5<- lapply(m5"[", c("mean"))
m6<- lapply(list_ts[1:
(length(list_ts)/2)], function(x)
forecast::forecast(arima(x,order=c(0,0,0),seasonal=list(order=c(0,0,0),
period=12)) ,h=24))
m6<- lapply(m6"[", c("mean"))
我想用这个做点什么
n1 <- ((0,0,0),(1,1,1))
where each element of n1 is (0,0,0)... etc
n2 <- ((0,1,0),(1,0,1),(0,0,0))
out<- lapply(seq_along(n1), function(i) {
m<- lapply(list_ts[1:
(length(list_ts)/2)], function(x)
forecast::forecast(arima(x,order=c(0,0,0),seasonal=list(order=c(1,0,0),
period=12)),h=24)
m1<-
lapply(m1, "[", "mean")
assign(paste0("m1", i),
m1, envir = .GlobalEnv)
m1})
【问题讨论】:
-
请查看更新后的解决方案
-
这工作再次感谢。我会尝试强制一些错误来看看会发生什么,当我这样做时,我会支持你的答案。
标签: r loops data-manipulation forecast