在R中如何将数据框/数值对象转为forecast对象以适配autoplot?
Convert Prophet Forecasts to
forecast Objects for autoplot() Got it, let's tackle this problem! The error you're seeing happens because autoplot() from the forecast package only works with objects of class forecast—and Prophet's predictions come as a data frame (or numeric vectors like yhat), which don't fit that mold. Here's how to convert your Prophet forecast into a forecast object that plays nicely with autoplot():
Step 1: Understand the forecast Object Structure
A valid forecast object is a list with specific required elements:
x: The historical time series (as atsobject)mean: The predicted values (as atsobject)lower/upper: Prediction intervals (as matrix-styletsobjects)- It also needs the
forecastclass assigned.
Step 2: Full Conversion Example
Let's walk through a complete example using sample data (you can swap this with your own dataset):
# Load required libraries library(forecast) library(prophet) library(dplyr) # 1. Prepare your time series data (using AirPassengers as an example) df <- data.frame( ds = as.Date(time(AirPassengers)), y = as.numeric(AirPassengers) ) # 2. Run Prophet model and generate forecasts m <- prophet(df) future <- make_future_dataframe(m, periods = 12, freq = "month") prophet_forecast <- predict(m, future) # 3. Convert historical data to a ts object # Adjust `frequency` and `start` to match your data (e.g., 12 for monthly, 365 for daily) historical_ts <- ts(df$y, frequency = 12, start = c(1949, 1)) # 4. Create a base forecast object and update it with Prophet's results prophet_forecast_obj <- forecast(historical_ts) # Replace base predictions with Prophet's values prophet_forecast_obj$mean <- ts( prophet_forecast$yhat, start = start(historical_ts), frequency = frequency(historical_ts) ) # Add Prophet's prediction intervals (adjust column names if needed) prophet_forecast_obj$lower <- ts( cbind(`95%` = prophet_forecast$yhat_lower), start = start(historical_ts), frequency = frequency(historical_ts) ) prophet_forecast_obj$upper <- ts( cbind(`95%` = prophet_forecast$yhat_upper), start = start(historical_ts), frequency = frequency(historical_ts) ) # 5. Now use autoplot() as usual! autoplot(prophet_forecast_obj)
Step 3: Optional - Forecast Only (No Historical Data)
If you only want to plot the forecasted values (without historical data), you can construct a minimal forecast object:
# Extract only the forecast period values forecast_period <- tail(prophet_forecast, 12) # Adjust 12 to match your forecast periods # Create ts objects for predictions and intervals forecast_ts <- ts(forecast_period$yhat, start = c(1961, 1), frequency = 12) lower_ts <- ts(forecast_period$yhat_lower, start = c(1961, 1), frequency = 12) upper_ts <- ts(forecast_period$yhat_upper, start = c(1961, 1), frequency = 12) # Build the minimal forecast object forecast_only_obj <- list( mean = forecast_ts, lower = cbind(`95%` = lower_ts), upper = cbind(`95%` = upper_ts), x = NULL, # No historical data series = "Prophet Forecast", model = NULL ) # Assign the forecast class class(forecast_only_obj) <- "forecast" # Plot it! autoplot(forecast_only_obj)
Key Notes
- Always make sure the
frequencyandstartparameters of yourtsobjects match your data's temporal frequency (monthly, daily, quarterly, etc.). - Prophet's default prediction interval is 95%, but you can add 80% intervals too if you generated them when fitting the model.
内容的提问来源于stack exchange,提问作者Alex Kinman
相关产品推荐
相关产品推荐

