# ---------------------------- 数据预处理 ---------------------------- # rm(list=ls()) # 在 Spaces 中不推荐清空环境变量,每个运行都是独立的 # setwd("/users/songyingxiao/desktop/rworkspace") # 在 Spaces 中不推荐设置工作目录,使用相对路径 # 加载分析所需库 library(zoo) # 时间序列插值 library(forecast) # 时间序列预测 library(tseries) # 平稳性检验 library(ggplot2) # 可视化 library(uroot) # 季节性单位根检验 library(readxl) # 读取Excel数据 library(dplyr) # 数据处理 library(lubridate) # 日期处理 library(prophet) # Prophet模型 library(ggpubr) # 增强的可视化功能 library(patchwork) # 图形组合 library(scales) # 图形比例尺 library(parallel) # 并行计算 library(doParallel) # 并行计算 library(tidyr) # 用于 pivot_longer # 为了解决中文乱码问题,可能需要设置字体 # 如果 Dockerfile 中安装了中文字体,这里可以尝试设置 # if (capabilities("cairo")) { # # For cairo-based devices (e.g., png, svg) # # For specific font files, you might need to use extrafont package. # # For simplicity, if fonts-wqy-zenhei is installed, ggplot2 might pick it up. # # Alternatively, use sysfonts and showtext for font handling in R. # # library(sysfonts) # # library(showtext) # # font_add("SimHei", regular = "/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc") # # showtext_auto() # } # ---------------------------- 1. 数据清洗 ---------------------------- # 1.1 读取数据 df <- read_excel('gmqrkl.xlsx') # 1.2 处理零值:将Value列中的0转换为缺失值 df$Value[df$Value == 0] <- NA # 缺失值插值(线性插值) df$Value <- na.approx(df$Value, rule = 2, method = "linear") # 1.3 异常值检测与处理(Z-score法) z_scores <- abs((df$Value - mean(df$Value, na.rm = TRUE)) / sd(df$Value, na.rm = TRUE)) threshold <- 3 outliers <- df[z_scores > threshold, ] print("检测到的异常值:") print(outliers) # ---------------------------- 2. 时间序列转换 ---------------------------- ts_data <- ts(df$Value, start = c(2023, 1), frequency = 365) # ---------------------------- 3. 平稳性检验与差分 ---------------------------- make_stationary <- function(data, max_diff = 3) { diff_order <- 0 # 初始化差分阶数为 0 current_data <- data # 当前处理的时间序列数据 while(diff_order <= max_diff) { # 循环进行平稳性检验,直到达到最大差分阶数或数据平稳 # 检查数据长度是否足够进行 ADF 检验 if (length(current_data) <= 1) { message("数据长度不足,无法进行ADF检验或进一步差分。") break } adf_test <- adf.test(current_data, alternative = "stationary") # 进行ADF检验 # par(mfrow = c(2,1)) # 在非交互式环境中直接 plot 可能不会显示 # plot(current_data, main = paste("差分阶数 d =", diff_order), family = "SimHei") # 绘制时间序列图 # acf(current_data, main = paste("ACF | d =", diff_order), family = "SimHei") # 绘制自相关函数图 if(adf_test$p.value < 0.05) break # 如果ADF检验的 p 值小于 0.05,认为数据平稳,退出循环 current_data <- diff(current_data) # 对数据进行一次差分 diff_order <- diff_order + 1 # 差分阶数加 1 } # par(mfrow = c(1,1)) # 恢复默认图形布局 return(list(stationary_data = current_data, d = diff_order)) # 返回平稳化后的数据和差分阶数 } # 调用函数 stationary_result <- make_stationary(ts_data) # 传入时间序列数据 ts_data ts_stationary <- stationary_result$stationary_data # 获取平稳化后的数据 d_order <- stationary_result$d # 获取差分阶数 # ---------------------------- 4. 白噪声检验 ---------------------------- # 确保 ts_stationary 有足够的长度 if (length(ts_stationary) < 2) { stop("平稳化后的序列过短,无法进行白噪声检验。") } lb_lag <- min(2*d_order, length(ts_stationary)%/%5) if (lb_lag == 0) { # 避免 lag=0 的情况 lb_lag <- 1 # 至少设置为1 } lb_test <- Box.test(ts_stationary, lag = lb_lag, type = "Ljung-Box") if(lb_test$p.value > 0.05) { stop("序列为白噪声,无需进一步分析") } else { message("通过白噪声检验,p-value = ", round(lb_test$p.value,4)) } #差分为0 if (d_order == 0) { lb_test <- Box.test(ts_stationary, lag = min(10, length(ts_stationary) %/% 5), type = "Ljung-Box") } else { lb_test <- Box.test(ts_stationary, lag = min(2 * d_order, length(ts_stationary) %/% 5), type = "Ljung-Box") } if (lb_test$p.value > 0.05) { stop("序列为白噪声,无需进一步分析") } else { message("通过白噪声检验,p-value = ", round(lb_test$p.value, 4)) } # ---------------------------- 5. 季节性检验与处理 ---------------------------- # 方法1:季节单位根检验(确定是否需要季节差分) # 确保 ts_stationary 有足够的长度和周期 if (length(ts_stationary) < 365 * 2) { # 至少需要两个周期的数据 message("数据长度不足,可能无法进行可靠的季节性检验(年周期)。") ts_obj <- ts(ts_stationary, frequency = 7) # 尝试使用周频率 seasonal_diff_order <- nsdiffs(ts_obj, test = "ch") } else { ts_obj <- ts(ts_stationary, frequency = 365) seasonal_diff_order <- nsdiffs(ts_obj, test = "ch") } if(seasonal_diff_order > 0) { ts_seasonal <- diff(ts_obj, lag = frequency(ts_obj), differences = seasonal_diff_order) message("执行", seasonal_diff_order, "阶季节差分") } else { ts_seasonal <- ts_obj message("无需季节差分") } # 方法2:STL分解处理多重季节性 # 确保数据长度足以进行多重季节性分解 if (length(ts_stationary) < 365 * 2) { message("数据长度不足,可能无法进行可靠的STL多季节性分解(年周期)。跳过此步骤。") stl_plot <- NULL # 设置为NULL避免后续错误 } else { msts_obj <- msts(ts_stationary, seasonal.periods = c(7, 30, 365)) stl_decomp <- mstl(msts_obj) # 可视化分解结果 stl_plot <- autoplot(stl_decomp) + ggtitle("STL多季节性分解(周+月+年)") + theme_bw() + theme(text = element_text(family = "SimHei")) # print(stl_plot) # 在 Spaces 中,最后统一打印或保存 } # 提取季节调整后序列 if (!is.null(stl_plot)) { ts_season_adj <- stl_decomp[, "Trend"] + stl_decomp[, "Remainder"] } else { ts_season_adj <- ts_stationary # 如果跳过STL,则使用原始平稳化数据 } # 方法3:Hegyi检验(显式检验周/年季节性) # 周季节性检验(周期7天) if (length(ts_data) < 7 * 2) { message("数据长度不足,无法进行周季节性Hegyi检验。") hegy_weekly <- NULL } else { ts_weekly <- ts(ts_data, frequency = 7) hegy_weekly <- hegy.test(ts_weekly, deterministic = c(1,0,0)) # 含常数项,无趋势项 summary(hegy_weekly) # 输出检验结果(p<0.05表示存在季节性) } # 月季节性检验(周期30天) if (length(ts_data) < 30 * 2) { message("数据长度不足,无法进行月季节性Hegyi检验。") hegy_month <- NULL } else { ts_month <- ts(ts_data, frequency = 30) hegy_month <- hegy.test(ts_month, deterministic = c(1, 0, 0)) summary(hegy_month) } # 年季节性检验(周期365天) if (length(ts_data) < 365 * 2) { message("数据长度不足,无法进行年季节性Hegyi检验。") hegy_annual <- NULL } else { ts_annual <- ts(ts_data, frequency = 365) hegy_annual <- hegy.test(ts_annual, deterministic = c(1, 0, 0)) summary(hegy_annual) } # ---------------------------- 6. 窗口大小优化 ---------------------------- ts_values <- as.numeric(df$Value) # 评估函数 evaluate_window <- function(window_size) { n <- length(ts_values) if (window_size >= n) return(list(mae = Inf, rmse = Inf)) # 确保训练数据至少有足够的点进行 auto.arima 训练 # auto.arima 至少需要一些点才能运行,例如 10-20 点 if (window_size < 20) { # 设置一个合理的最小窗口大小 return(list(mae = Inf, rmse = Inf)) } errors <- numeric(n - window_size) for (i in 1:(n - window_size)) { train <- ts_values[i:(i + window_size - 1)] test <- ts_values[i + window_size] # 确保训练数据有足够长度进行 ARIMA 建模 if (length(train) < 2) { # auto.arima 通常需要更多数据 errors[i] <- NA # 标记为NA或跳过 next } model <- tryCatch({ forecast::auto.arima( train, d = 0, # 这里固定了 d=0,因为前面已经处理了平稳性 max.p = 3, max.q = 3, stepwise = TRUE ) }, error = function(e) { message("auto.arima error: ", e$message, " for window_size ", window_size, " at iteration ", i) return(NULL) # 返回NULL表示模型训练失败 }) if (is.null(model)) { errors[i] <- NA next } fc <- forecast::forecast(model, h = 1) errors[i] <- test - fc$mean[1] } # 移除NA值后计算 errors <- errors[!is.na(errors)] if (length(errors) == 0) { return(list(mae = Inf, rmse = Inf)) } return(list(mae = mean(abs(errors)), rmse = sqrt(mean(errors^2)))) } # 并行计算优化 num_cores <- detectCores(logical = FALSE) if (num_cores > 1) { cl <- makeCluster(num_cores) registerDoParallel(cl) } else { message("检测到单核CPU,将不使用并行计算。") registerDoSEQ() # 注册顺序执行,以防万一 } window_sizes <- seq(70, 210, by = 7) # 过滤掉过大的窗口大小,避免 window_size >= n 导致循环无法进行 window_sizes <- window_sizes[window_sizes < length(ts_values) - 1] if (length(window_sizes) == 0) { stop("可用的窗口大小范围为空,无法进行窗口优化。") } results <- foreach(ws = window_sizes, .combine = rbind) %dopar% { res <- evaluate_window(ws) c(window_size = ws, mae = res$mae, rmse = res$rmse) } if (exists("cl") && class(cl) == "cluster") { # 检查集群是否已创建再停止 stopCluster(cl) } # 可视化窗口大小与误差关系 results_df <- as.data.frame(results) if (nrow(results_df) == 0) { message("没有有效的窗口优化结果,跳过窗口优化图表。") window_plot <- NULL best_mae_window <- 100 # 设置一个默认值 best_rmse_window <- 100 # 设置一个默认值 } else { window_plot <- ggplot(results_df, aes(x = window_size)) + geom_line(aes(y = mae, color = "MAE"), size = 1) + geom_line(aes(y = rmse, color = "RMSE"), size = 1) + geom_point(aes(y = mae, color = "MAE"), size = 2) + geom_point(aes(y = rmse, color = "RMSE"), size = 2) + labs(title = "窗口大小对预测误差的影响", x = "训练窗口天数", y = "误差值", color = "指标") + theme_minimal() + theme(text = element_text(family = "SimHei", size = 12), legend.position = "top") # print(window_plot) # 输出最优窗口 best_mae_window <- window_sizes[which.min(results_df$mae)] best_rmse_window <- window_sizes[which.min(results_df$rmse)] cat("最优窗口(MAE):", best_mae_window, "天\n") cat("最优窗口(RMSE):", best_rmse_window, "天\n") } # ---------------------------- 7. 动态窗口划分 ---------------------------- dynamic_split <- function(data, current_date, window_size = best_mae_window) { data %>% mutate(Date = lubridate::ymd(Date)) %>% filter(Date >= current_date - days(window_size - 1), Date <= current_date) %>% arrange(Date) %>% list( train = ., test_1w = filter(data, Date > current_date, Date <= current_date + weeks(1)), test_4w = filter(data, Date > current_date, Date <= current_date + weeks(4)) ) } df$Date <- lubridate::ymd(df$Date) # 确保 df$Date 是日期类型 start_date <- min(df$Date) + days(best_mae_window) end_date <- max(df$Date) - weeks(4) # 确保 start_date 不在 end_date 之后 if (start_date > end_date) { stop("数据不足以进行动态窗口划分,请检查数据长度和窗口大小。") } current_dates <- seq(start_date, end_date, by = "week") if (length(current_dates) == 0) { stop("无法生成 current_dates 序列,请检查日期范围和数据长度。") } ############################################################################ # ---------------------------- 8. 模型定义 ---------------------------- # 8.1 SARIMA模型函数 - 修复:添加h参数控制预测长度 sarima_model <- function(train_data, h = 28) { # 确保 train_data$Value 有足够的长度 if (length(train_data$Value) < 2) { message("SARIMA 训练数据不足,返回NA预测。") return(list(mean = rep(NA, h))) } ts_data <- ts(train_data$Value, frequency = 7) # 假设周季节性 model <- tryCatch({ auto.arima(ts_data, seasonal = TRUE) }, error = function(e) { message("SARIMA 模型训练失败: ", e$message) return(NULL) }) if (is.null(model)) { return(list(mean = rep(NA, h))) } fc <- forecast(model, h = h) # 确保返回的预测值长度正确 if (length(fc$mean) < h) { fc$mean <- c(fc$mean, rep(NA, h - length(fc$mean))) } return(fc) } # 8.2 Prophet模型函数(修改后) prophet_model <- function(train_data, test_dates) { df_prophet <- train_data %>% rename(ds = Date, y = Value) # 确保 Prophet 训练数据有足够的行数 if (nrow(df_prophet) < 2) { message("Prophet 训练数据不足,返回NA预测。") return(tibble(Date = test_dates, Value = rep(NA, length(test_dates)))) } # 使用全量数据作为训练集,预测最后四周的测试集 model <- tryCatch({ prophet(df_prophet, yearly.seasonality = TRUE, weekly.seasonality = TRUE) }, error = function(e) { message("Prophet 模型训练失败: ", e$message) return(NULL) }) if (is.null(model)) { return(tibble(Date = test_dates, Value = rep(NA, length(test_dates)))) } future <- make_future_dataframe(model, periods = length(test_dates), freq = "day") fc <- predict(model, future) tibble( Date = test_dates, Value = tail(fc$yhat, length(test_dates)) # 确保长度一致 ) } # 8.3 加权平均组合模型(修改后) weighted_average_model <- function(train_data, test_dates) { # 验证集:从全量数据中提取最后四周 # 确保 train_data 有足够的历史数据来创建验证集 validation_start_date <- max(train_data$Date) - weeks(4) + days(1) # 从倒数四周的开始日期 validation_data <- train_data %>% arrange(Date) %>% filter(Date >= validation_start_date) if (nrow(validation_data) < 28) { message("验证集不足四周(少于28天),无法计算权重。将使用默认权重或跳过。") sarima_weight <- 0.5 prophet_weight <- 0.5 } else { # 使用全量数据训练SARIMA和Prophet sarima_fc_val <- sarima_model(train_data, h = nrow(validation_data)) sarima_values <- as.numeric(sarima_fc_val$mean) prophet_fc_val <- prophet_model(train_data, validation_data$Date) # 确保预测值长度与实际值一致 min_len <- min(length(validation_data$Value), length(sarima_values), length(prophet_fc_val$Value)) if (min_len < 1) { sarima_weight <- 0.5 prophet_weight <- 0.5 message("验证集或预测值长度不足,使用默认权重。") } else { sarima_mae <- mean(abs(validation_data$Value[1:min_len] - sarima_values[1:min_len]), na.rm = TRUE) prophet_mae <- mean(abs(validation_data$Value[1:min_len] - prophet_fc_val$Value[1:min_len]), na.rm = TRUE) # 避免除以零或NaN if (is.na(sarima_mae) || is.na(prophet_mae) || (sarima_mae == 0 && prophet_mae == 0)) { sarima_weight <- 0.5 prophet_weight <- 0.5 } else { total_mae <- sarima_mae + prophet_mae sarima_weight <- 1 - (sarima_mae / total_mae) prophet_weight <- 1 - (prophet_mae / total_mae) total_weight <- sarima_weight + prophet_weight sarima_weight <- sarima_weight / total_weight prophet_weight <- prophet_weight / total_weight } } } cat("权重计算 - SARIMA:", round(sarima_weight, 2), "Prophet:", round(prophet_weight, 2), "\n") # 预测最后四周 full_sarima_fc <- sarima_model(train_data, h = length(test_dates)) full_sarima <- as.numeric(full_sarima_fc$mean) full_prophet <- prophet_model(train_data, test_dates)$Value # 确保预测值长度一致 min_len_pred <- min(length(full_sarima), length(full_prophet), length(test_dates)) if (min_len_pred < 1) { message("最终预测长度不足,返回NA。") result <- tibble(Date = test_dates, Value = rep(NA, length(test_dates))) } else { weighted_avg <- sarima_weight * full_sarima[1:min_len_pred] + prophet_weight * full_prophet[1:min_len_pred] result <- tibble(Date = test_dates[1:min_len_pred], Value = weighted_avg) } attr(result, "sarima_weight") <- sarima_weight attr(result, "prophet_weight") <- prophet_weight return(result) } # ---------------------------- 9. 模型性能评估 ---------------------------- calculate_metrics <- function(actual, predicted) { # 移除NA值,并确保长度一致 common_indices <- intersect(which(!is.na(actual)), which(!is.na(predicted))) if (length(common_indices) == 0) { return(data.frame(MAE = NA, RMSE = NA, MAPE = NA, sMAPE = NA)) } actual <- actual[common_indices] predicted <- predicted[common_indices] mae <- mean(abs(actual - predicted)) rmse <- sqrt(mean((actual - predicted)^2)) # 避免除以零或NaN mape <- ifelse(any(actual == 0), NA, mean(abs((actual - predicted) / actual)) * 100) smape <- 200 * mean(abs(actual - predicted) / (abs(actual) + abs(predicted))) data.frame(MAE = mae, RMSE = rmse, MAPE = mape, sMAPE = smape) } all_metrics <- list() weight_history <- tibble() # 存储权重历史 # 限制循环次数以加快测试或处理大数据量 # 例如,只处理最后几个 `current_dates` # current_dates_to_process <- tail(current_dates, 5) # 只处理最后5个周期 current_dates_to_process <- current_dates for(i in seq_along(current_dates_to_process)) { date <- current_dates_to_process[i] message("Processing date: ", date) # 动态窗口划分(用于 SARIMA 训练,Prophet 使用 full_train_data) window_data <- dynamic_split(df, date, window_size = best_mae_window) # 使用全量数据作为训练集 full_train_data <- df %>% filter(Date <= date) # 保留最后四周作为测试集 test_start_date <- date + days(1) test_end_date <- date + weeks(4) test_dates <- seq(test_start_date, test_end_date, by = "day") if (length(test_dates) == 0) { message("测试日期序列为空,跳过此迭代。") next } # 检查实际值数据是否存在且足够 actual_values_full_range <- df %>% filter(Date >= test_start_date, Date <= test_end_date) %>% pull(Value) if (length(actual_values_full_range) == 0) { message("当前日期 ", date, " 之后的实际值不足,跳过此迭代。") next } # 各模型预测 sarima_fc_result <- sarima_model(window_data$train, h = length(test_dates)) sarima_pred <- as.numeric(sarima_fc_result$mean) prophet_pred_df <- prophet_model(full_train_data, test_dates) prophet_pred <- prophet_pred_df$Value weighted_pred_df <- weighted_average_model(full_train_data, test_dates) weighted_pred <- weighted_pred_df$Value # 提取权重 sarima_weight <- attr(weighted_pred_df, "sarima_weight") prophet_weight <- attr(weighted_pred_df, "prophet_weight") # 存储权重信息 weight_history <- bind_rows(weight_history, tibble(Date = date, SARIMA_Weight = sarima_weight, Prophet_Weight = prophet_weight)) # 计算评估指标 # 确保预测值和实际值长度一致 min_len_metrics <- min(length(actual_values_full_range), length(sarima_pred), length(prophet_pred), length(weighted_pred)) if (min_len_metrics == 0) { message("预测或实际值长度为0,无法计算指标。") next } actual_values <- actual_values_full_range[1:min_len_metrics] sarima_pred_cut <- sarima_pred[1:min_len_metrics] prophet_pred_cut <- prophet_pred[1:min_len_metrics] weighted_pred_cut <- weighted_pred[1:min_len_metrics] sarima_metrics <- calculate_metrics(actual_values, sarima_pred_cut) prophet_metrics <- calculate_metrics(actual_values, prophet_pred_cut) weighted_metrics <- calculate_metrics(actual_values, weighted_pred_cut) all_metrics[[i]] <- list( date = date, sarima = sarima_metrics, prophet = prophet_metrics, weighted = weighted_metrics ) } # 过滤掉空的列表元素 all_metrics <- all_metrics[!sapply(all_metrics, is.null)] # ---------------------------- 10. 模型对比可视化 ---------------------------- # 10.1 提取评估结果 if (length(all_metrics) == 0) { stop("没有计算出任何模型评估指标,无法生成图表。请检查数据和循环设置。") } metrics_df <- bind_rows(lapply(all_metrics, function(x) { bind_rows( x$sarima %>% mutate(Model = "SARIMA", Date = x$date), x$prophet %>% mutate(Model = "Prophet", Date = x$date), x$weighted %>% mutate(Model = "加权平均模型", Date = x$date) ) })) # 10.2 模型权重变化可视化 plot_model_weights <- function(weight_history) { # 确保日期是日期格式 weight_history$Date <- as.Date(weight_history$Date) # 绘制权重变化图 ggplot(weight_history, aes(x = Date)) + geom_line(aes(y = SARIMA_Weight, color = "SARIMA Weight"), size = 1) + geom_line(aes(y = Prophet_Weight, color = "Prophet Weight"), size = 1) + labs(title = "模型权重变化", x = "日期", y = "权重", color = "模型") + theme_minimal() + theme(text = element_text(family = "SimHei", size = 12)) + scale_color_manual(values = c("SARIMA Weight" = "#E41A1C", "Prophet Weight" = "#377EB8")) } # 调用函数绘制图形 p_weights <- plot_model_weights(weight_history) # 10.3 各模型误差指标对比(分面图) metrics_long <- metrics_df %>% pivot_longer(cols = c(MAE, RMSE, MAPE, sMAPE), names_to = "Metric", values_to = "Value") error_plot <- ggplot(metrics_long, aes(x = Date, y = Value, color = Model)) + geom_line(size = 0.8) + facet_wrap(~Metric, scales = "free_y", ncol = 2) + labs(title = "各模型预测误差指标对比", y = "误差值", x = "预测起始日期") + theme_bw() + theme(text = element_text(family = "SimHei"), legend.position = "bottom") + scale_color_brewer(palette = "Set1") # print(error_plot) # 10.4 平均性能对比雷达图 avg_metrics <- metrics_df %>% group_by(Model) %>% summarise( MAE = mean(MAE, na.rm = TRUE), RMSE = mean(RMSE, na.rm = TRUE), MAPE = mean(MAPE, na.rm = TRUE), sMAPE = mean(sMAPE, na.rm = TRUE) ) %>% pivot_longer(cols = -Model, names_to = "Metric", values_to = "Value") radar_plot <- ggplot(avg_metrics, aes(x = Metric, y = Value, group = Model, color = Model)) + geom_polygon(aes(fill = Model), alpha = 0.2, size = 1) + coord_polar() + labs(title = "各模型平均性能对比雷达图") + theme_minimal() + theme(text = element_text(family = "SimHei"), axis.text.x = element_text(size = 10), legend.position = "right") # print(radar_plot) # 10.5 最终预测对比图(最后一个窗口) # 确保 current_dates_to_process 不为空 if (length(current_dates_to_process) == 0) { warning("没有足够的 current_dates 来生成最终预测对比图。") forecast_plot <- NULL } else { last_date <- tail(current_dates_to_process, 1) # 使用处理过的日期序列 window_data <- dynamic_split(df, last_date) test_dates <- seq(last_date + days(1), last_date + weeks(4), by = "day") sarima_fc <- sarima_model(window_data$train, h = length(test_dates))$mean prophet_fc <- prophet_model(window_data$train, test_dates) weighted_fc <- weighted_average_model(window_data$train, test_dates) # 过滤掉NA值,确保长度一致 min_len_final_comp <- min(length(window_data$test_4w$Value), length(sarima_fc), length(prophet_fc$Value), length(weighted_fc$Value)) if (min_len_final_comp == 0) { warning("最终预测对比图数据长度不足,跳过生成。") forecast_plot <- NULL } else { comparison_df <- bind_rows( window_data$test_4w[1:min_len_final_comp,] %>% mutate(Type = "实际值"), tibble(Date = test_dates[1:min_len_final_comp], Value = sarima_fc[1:min_len_final_comp], Type = "SARIMA预测"), prophet_fc[1:min_len_final_comp,] %>% mutate(Type = "Prophet预测"), weighted_fc[1:min_len_final_comp,] %>% mutate(Type = "加权平均预测") ) forecast_plot <- ggplot(comparison_df, aes(x = Date, y = Value, color = Type)) + geom_line(size = 1.2) + geom_point(size = 2) + labs(title = "三种模型未来4周预测对比", subtitle = paste("预测起始日期:", last_date), x = "日期", y = "值") + theme_minimal() + theme(text = element_text(family = "SimHei", size = 12), legend.position = "top") + scale_color_manual(values = c("实际值" = "black", "SARIMA预测" = "red", "Prophet预测" = "blue", "加权平均预测" = "green")) # print(forecast_plot) } } # 10.6 误差分布箱线图 error_dist <- metrics_df %>% select(Model, MAE, RMSE, MAPE, sMAPE) %>% pivot_longer(cols = -Model, names_to = "Metric", values_to = "Value") box_plot <- ggplot(error_dist, aes(x = Model, y = Value, fill = Model)) + geom_boxplot(alpha = 0.7) + facet_wrap(~Metric, scales = "free_y") + labs(title = "各模型误差分布箱线图") + theme_bw() + theme(text = element_text(family = "SimHei"), axis.text.x = element_text(angle = 45, hjust = 1)) + scale_fill_brewer(palette = "Pastel1") # print(box_plot) # 10.7 组合所有可视化结果 # 检查每个图表对象是否为NULL,只有非NULL的才会被组合 plots_to_combine <- list() if (!is.null(p_weights)) plots_to_combine$p_weights <- p_weights if (!is.null(error_plot)) plots_to_combine$error_plot <- error_plot if (!is.null(forecast_plot)) plots_to_combine$forecast_plot <- forecast_plot if (!is.null(radar_plot)) plots_to_combine$radar_plot <- radar_plot if (!is.null(box_plot)) plots_to_combine$box_plot <- box_plot if (!is.null(stl_plot)) plots_to_combine$stl_plot <- stl_plot # 添加 STL 分解图 # 使用 patchwork 动态组合图表 if (length(plots_to_combine) > 0) { # 根据可用图表的数量和类型,选择合适的布局 # 这是一个通用的组合,你可以根据实际生成的图表调整布局 combined_plots <- wrap_plots(plots_to_combine) + plot_annotation(title = "时间序列预测模型综合比较", theme = theme(plot.title = element_text(hjust = 0.5, size = 16, family = "SimHei"))) print(combined_plots) # 保存综合图表 ggsave("forecast_comparison.png", combined_plots, width = 16, height = 20, dpi = 300) } else { message("没有足够的图表可以组合。") } # 输出平均性能 cat("\n各模型平均性能对比:\n") avg_perf <- metrics_df %>% group_by(Model) %>% summarise( MAE = mean(MAE, na.rm = TRUE), RMSE = mean(RMSE, na.rm = TRUE), MAPE = mean(MAPE, na.rm = TRUE), sMAPE = mean(sMAPE, na.rm = TRUE) ) print(avg_perf) #---------------------------第11部分代码---------------------------# # 11.1 获取最后一个预测窗口数据 # 确保 current_dates_to_process 不为空 if (length(current_dates_to_process) == 0) { warning("没有足够的 current_dates 来进行最后的预测可视化。") } else { last_date <- tail(current_dates_to_process, 1) window_data <- dynamic_split(df, last_date) test_dates <- seq(last_date + days(1), last_date + weeks(4), by = "day") actual_data <- window_data$test_4w # 11.2 生成各模型预测 sarima_fc <- sarima_model(window_data$train, h = 28)$mean prophet_fc <- prophet_model(window_data$train, test_dates) weighted_fc <- weighted_average_model(window_data$train, test_dates) # 11.3 创建结果数据框 # 确保所有预测结果长度一致 min_len_results <- min(length(actual_data$Value), length(sarima_fc), length(prophet_fc$Value), length(weighted_fc$Value)) if (min_len_results == 0) { warning("最终结果数据框数据长度不足,无法创建。") results_df <- NULL } else { results_df <- bind_rows( actual_data[1:min_len_results,] %>% mutate(Model = "实际值"), tibble(Date = as.Date(test_dates[1:min_len_results]), Value = sarima_fc[1:min_len_results], Model = "SARIMA预测"), prophet_fc[1:min_len_results,] %>% mutate(Model = "Prophet预测"), weighted_fc[1:min_len_results,] %>% mutate(Model = "加权平均预测") ) # 确保所有数据框中的 Date 列都是 Date 类型 results_df$Date <- as.Date(results_df$Date) prophet_fc$Date <- as.Date(prophet_fc$Date) weighted_fc$Date <- as.Date(weighted_fc$Date) # ————————————————————————————提取一周预测数据————————————————————————————# first_week_df <- results_df %>% filter(Date <= last_date + weeks(1)) # 再次确保 first_week_df 中的 Date 列是 Date 类型 first_week_df$Date <- as.Date(first_week_df$Date) # 11.5 可视化:第一周预测对比 first_week_plot <- ggplot(first_week_df, aes(x = Date, y = Value, color = Model, linetype = Model)) + geom_line(size = 1.2) + geom_point(data = filter(first_week_df, Model == "实际值"), size = 2) + labs( title = "第一周预测结果对比", subtitle = paste("预测起始日期:", format(last_date, "%Y-%m-%d")), x = "日期", y = "值" ) + theme_minimal() + theme( text = element_text(family = "SimHei", size = 12), legend.position = "top", axis.text.x = element_text(angle = 45, hjust = 1) ) + scale_color_manual( values = c( "实际值" = "black", "SARIMA预测" = "#E41A1C", "Prophet预测" = "#377EB8", "加权平均预测" = "#4DAF4A" ) ) + scale_linetype_manual( values = c( "实际值" = "solid", "SARIMA预测" = "dashed", "Prophet预测" = "dotted", "加权平均预测" = "longdash" ) ) + scale_x_date( date_labels = "%m-%d", date_breaks = "1 day" ) print(first_week_plot) #---------------------------对比图---------------------------# # 11.6 计算第一周的误差指标 # 计算每个模型的 MAE、MAPE 和 RMSE # 提取实际值(第一周) actual_values_1w <- first_week_df %>% filter(Model == "实际值") %>% pull(Value) # 提取各模型预测值(第一周) sarima_predictions_1w <- first_week_df %>% filter(Model == "SARIMA预测") %>% pull(Value) prophet_predictions_1w <- first_week_df %>% filter(Model == "Prophet预测") %>% pull(Value) weighted_predictions_1w <- first_week_df %>% filter(Model == "加权平均预测") %>% pull(Value) # 检查数据长度是否一致 min_len_error_1w <- min(length(actual_values_1w), length(sarima_predictions_1w), length(prophet_predictions_1w), length(weighted_predictions_1w)) if (min_len_error_1w == 0) { warning("第一周误差计算数据长度不足。") error_df_long_1w <- data.frame() # 创建空数据框以避免后续错误 } else { # 截取到最小长度 actual_values_1w <- actual_values_1w[1:min_len_error_1w] sarima_predictions_1w <- sarima_predictions_1w[1:min_len_error_1w] prophet_predictions_1w <- prophet_predictions_1w[1:min_len_error_1w] weighted_predictions_1w <- weighted_predictions_1w[1:min_len_error_1w] # 计算误差指标 sarima_error_1w <- calculate_error_metrics(actual_values_1w, sarima_predictions_1w) prophet_error_1w <- calculate_error_metrics(actual_values_1w, prophet_predictions_1w) weighted_error_1w <- calculate_error_metrics(actual_values_1w, weighted_predictions_1w) # 创建误差指标数据框 error_df_1w <- data.frame( Model = c("SARIMA预测", "Prophet预测", "加权平均预测"), MAE = c(sarima_error_1w$MAE, prophet_error_1w$MAE, weighted_error_1w$MAE), MAPE = c(sarima_error_1w$MAPE, prophet_error_1w$MAPE, weighted_error_1w$MAPE), RMSE = c(sarima_error_1w$RMSE, prophet_error_1w$RMSE, weighted_error_1w$RMSE) ) # 将误差指标数据框转换为长格式 error_df_long_1w <- error_df_1w %>% pivot_longer(cols = c(MAE, MAPE, RMSE), names_to = "Metric", values_to = "Value") # 11.7 可视化:第一周误差结果对比 # 创建误差指标对比图 error_plot_1w <- ggplot(error_df_long_1w, aes(x = Model, y = Value, fill = Model)) + geom_bar(stat = "identity", position = position_dodge()) + geom_text(aes(label = ifelse(Metric == "MAPE", paste0(round(Value, 2), "%"), round(Value, 2))), position = position_dodge(width = 0.9), vjust = -0.5) + labs( title = "第一周预测误差结果对比", x = "模型", y = "误差值" ) + facet_wrap(~Metric, scales = "free_y") + theme_minimal() + theme( text = element_text(family = "SimHei", size = 12), legend.position = "none", strip.text = element_text(face = "bold") ) print(error_plot_1w) } # # ————————————————————————————提取四周预测数据————————————————————————————# # 时间序列对比图 # 确保 Date 列是 Date 类型 results_df$Date <- as.Date(results_df$Date) # 确保 last_date 是 Date 类型 last_date <- as.Date(last_date) four_week_plot <- ggplot(results_df, aes(x = Date, y = Value, color = Model, linetype = Model)) + geom_line(size = 1.2) + geom_point(data = filter(results_df, Model == "实际值"), size = 2) + labs( title = "最后四周预测结果对比", subtitle = paste("预测起始日期:", format(last_date, "%Y-%m-%d")), # 格式化日期 x = "日期", y = "值" ) + theme_minimal() + theme( text = element_text(family = "SimHei", size = 12), legend.position = "top", axis.text.x = element_text(angle = 45, hjust = 1) ) + scale_color_manual( values = c( "实际值" = "black", "SARIMA预测" = "#E41A1C", "Prophet预测" = "#377EB8", "加权平均预测" = "#4DAF4A" ) ) + scale_linetype_manual( values = c( "实际值" = "solid", "SARIMA预测" = "dashed", "Prophet预测" = "dotted", "加权平均预测" = "longdash" ) ) + scale_x_date( date_labels = "%m-%d", date_breaks = "3 days" ) print(four_week_plot) # 11.8 提取四周预测数据 four_weeks_df <- results_df # 11.9 计算四周的误差指标 # 提取实际值(四周) actual_values_4w <- four_weeks_df %>% filter(Model == "实际值") %>% pull(Value) # 提取各模型预测值(四周) sarima_predictions_4w <- four_weeks_df %>% filter(Model == "SARIMA预测") %>% pull(Value) prophet_predictions_4w <- four_weeks_df %>% filter(Model == "Prophet预测") %>% pull(Value) weighted_predictions_4w <- four_weeks_df %>% filter(Model == "加权平均预测") %>% pull(Value) # 根据实际值数量调整预测值数量 n_actual <- length(actual_values_4w) min_len_error_4w <- min(n_actual, length(sarima_predictions_4w), length(prophet_predictions_4w), length(weighted_predictions_4w)) if (min_len_error_4w == 0) { warning("四周误差计算数据长度不足。") error_df_long_4w <- data.frame() } else { sarima_predictions_4w <- sarima_predictions_4w[1:min_len_error_4w] prophet_predictions_4w <- prophet_predictions_4w[1:min_len_error_4w] weighted_predictions_4w <- weighted_predictions_4w[1:min_len_error_4w] actual_values_4w <- actual_values_4w[1:min_len_error_4w] # 确保 actual 也被截取 # 计算误差指标 sarima_error_4w <- calculate_error_metrics(actual_values_4w, sarima_predictions_4w) prophet_error_4w <- calculate_error_metrics(actual_values_4w, prophet_predictions_4w) weighted_error_4w <- calculate_error_metrics(actual_values_4w, weighted_predictions_4w) # 创建误差指标数据框 error_df_4w <- data.frame( Model = c("SARIMA预测", "Prophet预测", "加权平均预测"), MAE = c(sarima_error_4w$MAE, prophet_error_4w$MAE, weighted_error_4w$MAE), MAPE = c(sarima_error_4w$MAPE, prophet_error_4w$MAPE, weighted_error_4w$MAPE), RMSE = c(sarima_error_4w$RMSE, prophet_error_4w$RMSE, weighted_error_4w$RMSE) ) # 将误差指标数据框转换为长格式 error_df_long_4w <- error_df_4w %>% pivot_longer(cols = c(MAE, MAPE, RMSE), names_to = "Metric", values_to = "Value") # 11.10 可视化:四周误差结果对比 # 创建误差指标对比图 error_plot_4w <- ggplot(error_df_long_4w, aes(x = Model, y = Value, fill = Model)) + geom_bar(stat = "identity", position = position_dodge()) + geom_text(aes(label = ifelse(Metric == "MAPE", paste0(round(Value, 2), "%"), round(Value, 2))), position = position_dodge(width = 0.9), vjust = -0.5) + labs( title = "四周预测误差结果对比", x = "模型", y = "误差值" ) + facet_wrap(~Metric, scales = "free_y") + theme_minimal() + theme( text = element_text(family = "SimHei", size = 12), legend.position = "none", strip.text = element_text(face = "bold") ) print(error_plot_4w) } }