在R中叠加预测时间序列与原始序列及2017年1-4月预测需求
Hey there! Let's walk through how to get your 2017 Jan-Apr forecast results, compare them with your actual values, and plot both the original time series and forecast together in R. Here's the step-by-step solution:
First, let's adjust your original code to avoid variable overwrites and add steps for handling actual values and overlay plotting:
# Read the raw data w_data <- read.csv("C:/Users/admin/Documents/aggrmonth.csv", sep=";", dec=",") # Create time series object for historical data (2015-2016) historical_ts <- ts(w_data$new, frequency = 12, start = c(2015, 1)) # Plot the historical time series plot.ts(historical_ts, main = "Historical Monthly Data (2015-2016)", ylab = "Value") # Load forecast package and fit Holt-Winters model library(forecast) hw_model <- HoltWinters(historical_ts) # Forecast the next 4 months (2017 Jan-Apr) forecast_result <- forecast(hw_model, h = 4) # Store your actual values for 2017 Jan-Apr (replace the Apr value with your real data) actual_2017 <- c(1020, 800, 1130, 950) actual_ts <- ts(actual_2017, frequency = 12, start = c(2017, 1))
You can pull the forecast values directly from the forecast_result object. Here's how to view and extract them:
# View the full forecast output (including confidence intervals) print(forecast_result) # Extract just the numeric forecast values with month labels forecast_values <- as.numeric(forecast_result$mean) names(forecast_values) <- c("Jan-17", "Feb-17", "Mar-17", "Apr-17") cat("\n2017 Jan-Apr Forecast Values:\n") print(forecast_values)
To make a clear side-by-side comparison, create a summary data frame:
comparison_df <- data.frame( Month = c("Jan-17", "Feb-17", "Mar-17", "Apr-17"), Actual_Value = actual_2017, Forecast_Value = forecast_values, Absolute_Error = actual_2017 - forecast_values ) print(comparison_df)
We have two straightforward options to visualize the overlay:
Option 1: Base R Plot (Simple & Lightweight)
# Set plot limits to include 2017 data plot.ts(historical_ts, xlim = c(2015, 2017.3), ylim = range(c(historical_ts, actual_2017, forecast_values)), main = "Historical Data + 2017 Forecast & Actuals", ylab = "Value") # Add forecast line and 95% confidence intervals lines(forecast_result$mean, col = "blue", lwd = 2) lines(forecast_result$lower[,2], col = "blue", lty = 2) lines(forecast_result$upper[,2], col = "blue", lty = 2) # Add actual 2017 values as points connected by lines lines(actual_ts, col = "red", lwd = 2, type = "b") # Add legend for clarity legend("topleft", legend = c("Historical", "Forecast", "95% CI", "2017 Actual"), col = c("black", "blue", "blue", "red"), lty = c(1,1,2,1), lwd = c(1,2,1,2))
Option 2: ggplot2-Based Plot (Polished & Customizable)
Using the autoplot function from the forecast package gives a cleaner look:
library(ggplot2) # Combine historical and 2017 actual data into one time series full_actual_ts <- ts(c(historical_ts, actual_2017), frequency = 12, start = c(2015, 1)) # Generate the overlay plot autoplot(full_actual_ts, series = "Actual Data") + autolayer(forecast_result$mean, series = "Forecast") + autolayer(forecast_result$lower[,2], series = "95% Lower CI", lty = 2) + autolayer(forecast_result$upper[,2], series = "95% Upper CI", lty = 2) + labs(title = "Historical Data + 2017 Jan-Apr Forecast & Actuals", y = "Value", x = "Year") + theme_minimal() + scale_color_manual(values = c("Actual Data" = "red", "Forecast" = "blue", "95% Lower CI" = "darkblue", "95% Upper CI" = "darkblue"))
内容的提问来源于stack exchange,提问作者psysky

