You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于R语言fable包的时间序列混合模型最优组合筛选方法

实现思路
  • 预训练所有候选单模型:你原有代码已经完成该步骤,无需在遍历过程中重复拟合,可大幅降低计算开销
  • 生成所有待验证的模型子集:如果仅需对比混合模型,可直接过滤掉仅含1个模型的子集
  • 逐一遍历子集:对每个子集生成等权重混合模型,在验证集上计算指定精度指标(如MAE)
  • 排序输出结果:按你指定的精度指标排序,返回性能最优的组合

补充说明:如果你的候选模型数量超过15个,全遍历的算力开销会明显上升,此时可改用逐步向前/向后选择的启发式方法,无需穷举所有组合也能找到近似最优解。

代码示例

你原有代码已经跑完了fit、count_data等核心对象,直接运行以下代码即可实现自动遍历:

library(purrr)
library(dplyr)
library(fabletools)

# 1. 定义候选单模型列表
model_names <- c("ets", "arima", "snaive", "croston", "ave", "naive", "neural", "lm")
n_models <- length(model_names)

# 2. 生成所有非空模型子集,仅需混合模型则把下方的1改为2
all_combinations <- map(1:n_models, ~combn(model_names, .x, simplify = FALSE)) %>% 
  flatten()

# 3. 预提取所有单模型的7步预测结果,避免重复计算
single_model_fc <- fit %>% 
  select(all_of(model_names)) %>% 
  forecast(h = 7)

# 4. 遍历所有组合计算精度
combination_performance <- map_dfr(all_combinations, function(current_comb) {
  # 生成当前组合的等权重混合预测
  mixed_fc <- single_model_fc %>% 
    as_tibble() %>% 
    mutate(Count = rowMeans(select(., all_of(current_comb)))) %>% 
    mutate(.model = paste0("mixed_", paste(current_comb, collapse = "_"))) %>% 
    select(.model, Date, Count) %>% 
    as_fable(index = Date, response = "Count", distribution = Count)
  
  # 计算精度指标,可自行增减需要的指标
  acc <- accuracy(mixed_fc, count_data, measures = list(MAE = MAE, RMSE = RMSE, MAPE = MAPE))
  
  # 返回组合信息和对应精度
  tibble(
    model_combination = list(current_comb),
    comb_size = length(current_comb),
    MAE = acc$MAE,
    RMSE = acc$RMSE,
    MAPE = acc$MAPE
  )
})

# 5. 按MAE从小到大排序,取最优组合
best_comb <- combination_performance %>% 
  arrange(MAE) %>% 
  slice(1)

# 输出最优结果
cat("最优混合模型包含的单模型:\n")
print(best_comb$model_combination[[1]])
cat("\n最优组合精度:\n")
print(paste0("MAE:", round(best_comb$MAE, 2), " | RMSE:", round(best_comb$RMSE, 2), " | MAPE:", round(best_comb$MAPE, 2)))

# 可查看所有组合的精度排名
# View(combination_performance %>% arrange(MAE))

自定义调整说明

  • 若要更换评价指标,仅需修改accuracy函数中的measures参数,同时调整排序时对应的列名即可
  • 若不需要等权重混合,可在生成混合预测时加入权重优化逻辑(比如用线性回归拟合最优权重)

内容的提问来源于stack exchange,提问作者metaltoaster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 04:54:00