且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

适应R中几个变量的预测代码

更新时间:2023-02-18 13:12:46

您只需要从变量对/组中获取一个新组

You just need to get a new group from the pair Variable/group at the beginning of "Step 1" and the code should work:

# load libraries
load_pkgs <- c("forecast", "zoo", "timetk", "tidyverse", "lubridate") 
sapply(load_pkgs, function(x) suppressPackageStartupMessages(library(x, character.only = T)))

# get new group from pair variable/group
df <- df %>%
  unite_("group", c("Variable", "group"))

下一步,运行步骤1-步骤3中的代码,并在步骤1中进行小幅更新3:将分别拟合的预测结果汇总在一起时,为了提取出 Variable group 列,您需要按如下所示更新步骤3的第二部分:

Next, run the code from Step 1 - Step 3, with a small update in Step 3: when bringing together the fitted respectively forecasting results, in order to extract back the Variable and group columns you need to update the second part of Step 3 as follows:

# bring together the fitted respectively forecasting results
output_fit_all <- lapply(lts_all, function(x) x[[1]])
output_fit_all <- bind_rows(output_fit_all)  %>%
  separate(group, c("Variable", "group"))

output_fcst_all <- lapply(lts_all, function(x) x[[2]])
output_fcst_all <- bind_rows(output_fcst_all)   %>%
  separate(group, c("Variable", "group"))