Forecast Function In R

The forecast function in R Programming Language is part of the forecast package, which provides functions and tools for forecasting time series data. This function allows users to generate forecasts for future time points based on historical data and time series models.

The basic syntax of the forecast function is:

forecast(model, h = ...)
  • model: The time series model object fitted to historical data. This could be an object generated by various time series modeling functions, such as auto.arima, ets, stlf, etc.
  • h: The number of future time points for which forecasts are to be generated.

Let’s demonstrate the usage of the forecast function with a simple example:

R
# Load required libraries
library(forecast)
# Generate example time series data
set.seed(123)
ts_data <- ts(rnorm(100), start = 2020)
# Fit an ARIMA model to the data
model <- auto.arima(ts_data)
# Generate forecasts for the next 10 time points
forecast_values <- forecast(model, h = 10)
# Print the forecasted values
print(forecast_values)

Output:

     Point Forecast     Lo 80    Hi 80     Lo 95    Hi 95
2120 0 -1.169709 1.169709 -1.788915 1.788915
2121 0 -1.169709 1.169709 -1.788915 1.788915
2122 0 -1.169709 1.169709 -1.788915 1.788915
2123 0 -1.169709 1.169709 -1.788915 1.788915
2124 0 -1.169709 1.169709 -1.788915 1.788915
2125 0 -1.169709 1.169709 -1.788915 1.788915
2126 0 -1.169709 1.169709 -1.788915 1.788915
2127 0 -1.169709 1.169709 -1.788915 1.788915
2128 0 -1.169709 1.169709 -1.788915 1.788915
2129 0 -1.169709 1.169709 -1.788915 1.788915

Additional Parameters

The forecast function offers various additional parameters for customizing the forecasting process, such as:

  • level: Confidence level for prediction intervals (e.g., 0.95 for a 95% confidence interval).
  • fan: Whether to compute prediction intervals for each future time point separately (default is FALSE).
  • simulate: Whether to generate simulated paths for the future values (default is FALSE).
  • bootstrap: Whether to use the bootstrap method to estimate prediction intervals (default is FALSE).
  • PI: Whether to include prediction intervals in the output (default is TRUE).

Visualizing Forecasts

To visualize the forecasted values along with prediction intervals, you can use the plot function:

R
# Plot forecasted values with prediction intervals
plot(forecast_values)

Output:

Forecast Function In R

This will generate a plot displaying the forecasted values along with upper and lower bounds of prediction intervals.

Conclusion

The forecast function in R, provided by the forecast package, is a valuable tool for generating forecasts for time series data. By fitting time series models to historical data and utilizing the forecast function, users can obtain forecasts for future time points, along with prediction intervals to quantify uncertainty. Experimenting with different time series models and parameters allows for the development of accurate and reliable forecasts, aiding in decision-making and planning in various domains.