R Shiny价格预测应用数据过滤问题求助
解决R Shiny价格预测应用的服务器端逻辑问题
核心修正方向
针对三个维度(曲线名称、峰值类型、交割期)的过滤逻辑进行精准约束,再基于过滤后的时序数据拟合ARIMA模型并生成预测图表。
具体实现步骤
1. 修正数据过滤逻辑
确保仅筛选与用户选择完全匹配的数据,同时保证时序数据的连续性:
filtered_data <- reactive({ # 强制要求所有输入参数已选择 req(input$curve_name, input$peak_type, input$delivery_period) # 多维度精准过滤 df %>% filter( curve_name == input$curve_name, peak_type == input$peak_type, delivery_period == input$delivery_period ) %>% arrange(date) # 按日期排序,满足ARIMA模型对时序连续性的要求 })
2. ARIMA模型拟合与预测
基于过滤后的时序数据自动选择最优ARIMA参数,并生成指定周期的预测结果:
arima_forecast <- reactive({ data <- filtered_data() # 转换为时间序列对象,frequency根据数据频率调整(月度用12,日度用365) ts_data <- ts(data$price, frequency = 12) # 自动拟合最优ARIMA模型 optimal_arima <- auto.arima(ts_data) # 生成未来12个周期的预测,可按需修改h值 forecast(optimal_arima, h = 12) })
3. 生成预测可视化图表
整合原始数据与预测结果,绘制带置信区间的趋势图:
output$price_pred_plot <- renderPlot({ pred_result <- arima_forecast() # 整理原始数据 original_df <- data.frame( date = filtered_data()$date, price = filtered_data()$price, type = "原始价格" ) # 整理预测数据(含置信区间) forecast_df <- data.frame( date = seq(max(filtered_data()$date), by = "month", length.out = 12), price = as.numeric(pred_result$mean), type = "预测价格", lower = as.numeric(pred_result$lower[,2]), upper = as.numeric(pred_result$upper[,2]) ) # 绘图 ggplot() + geom_line(data = original_df, aes(x = date, y = price, color = type), size = 1) + geom_line(data = forecast_df, aes(x = date, y = price, color = type), size = 1, linetype = "dashed") + geom_ribbon(data = forecast_df, aes(x = date, ymin = lower, ymax = upper), alpha = 0.2, fill = "#2196F3") + scale_color_manual(values = c("原始价格" = "#333333", "预测价格" = "#2196F3")) + labs( title = paste(input$curve_name, "-", input$peak_type, "-", input$delivery_period, "价格预测"), x = "日期", y = "价格" ) + theme_minimal() })
完整服务器端代码示例
server <- function(input, output) { # 数据过滤 filtered_data <- reactive({ req(input$curve_name, input$peak_type, input$delivery_period) df %>% filter( curve_name == input$curve_name, peak_type == input$peak_type, delivery_period == input$delivery_period ) %>% arrange(date) }) # ARIMA预测 arima_forecast <- reactive({ data <- filtered_data() ts_data <- ts(data$price, frequency = 12) optimal_arima <- auto.arima(ts_data) forecast(optimal_arima, h = 12) }) # 渲染预测图表 output$price_pred_plot <- renderPlot({ pred_result <- arima_forecast() original_df <- data.frame( date = filtered_data()$date, price = filtered_data()$price, type = "原始价格" ) forecast_df <- data.frame( date = seq(max(filtered_data()$date), by = "month", length.out = 12), price = as.numeric(pred_result$mean), type = "预测价格", lower = as.numeric(pred_result$lower[,2]), upper = as.numeric(pred_result$upper[,2]) ) ggplot() + geom_line(data = original_df, aes(x = date, y = price, color = type), size = 1) + geom_line(data = forecast_df, aes(x = date, y = price, color = type), size = 1, linetype = "dashed") + geom_ribbon(data = forecast_df, aes(x = date, ymin = lower, ymax = upper), alpha = 0.2, fill = "#2196F3") + scale_color_manual(values = c("原始价格" = "#333333", "预测价格" = "#2196F3")) + labs( title = paste(input$curve_name, "-", input$peak_type, "-", input$delivery_period, "价格预测"), x = "日期", y = "价格" ) + theme_minimal() }) }
关键注意点
- 确保数据集包含
date(日期列)、price(价格列)、curve_name、peak_type、delivery_period五个核心字段 - 若数据频率非月度,需修改
ts()函数的frequency参数(如日度数据设为365) - 如需支持多组数据同时预测,可将过滤逻辑改为分组循环处理
内容的提问来源于stack exchange,提问作者Miguel.H
相关产品推荐
相关产品推荐

