How to Use the forecast() Function in R


Often you may want to forecast future values for a specific time series in R.

One of the easiest ways to do so is by using the forecast() function from the forecast package in R, which is designed to perform this exact task.

The forecast() function uses the following syntax:

forecast(object, h, level, …)

where:

  • object: Name of the object to forecast future values for
  • h: Number of periods for forecasting
  • level: Confidence level for prediction intervals

Note: By default, this function will produce 80% and 95% confidence intervals for the forecasted values. Keep in mind that the larger value that you supply for the level argument, the wider the confidence interval will be.

The following examples show how to use the forecast() function to predict future values of a given time series in R.

Example: How to Use the forecast() Function in R

We can use the following syntax to generate a time series that contains values ranging from October 2023 to May 2025:

#define time series values
data <- c(6, 7, 7, 7, 8, 5, 8, 9, 4, 9, 12, 14, 14, 15, 18, 24, 20, 15, 24, 26)

#create time series object from vector
ts_data <- ts(data, start=c(2023, 10), frequency=12)

#view time series object
ts_data

     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2023                                       6   7   7
2024   7   8   5   8   9   4   9  12  14  14  15  18
2025  24  20  15  24  26

Suppose that we would like to predict the future values of this time series for the next 12 months.

We can use the forecast() function from the forecast package to do so:

library(forecast)

#forecast values for 12 periods in future of time series
forecast(ts_data, h=12)

         Point Forecast     Lo 80    Hi 80       Lo 95    Hi 95
Jun 2025       23.74305 13.629491 33.85662   8.2756948 39.21041
Jul 2025       23.74305 12.062173 35.42393   5.8786883 41.60742
Aug 2025       23.74305 10.638219 36.84789   3.7009389 43.78517
Sep 2025       23.74305  9.313813 38.17229   1.6754342 45.81067
Oct 2025       23.74305  8.062465 39.42364  -0.2383363 47.72444
Nov 2025       23.74305  6.866724 40.61938  -2.0670649 49.55317
Dec 2025       23.74305  5.714367 41.77174  -3.8294430 51.31555
Jan 2026       23.74305  4.596437 42.88967  -5.5391701 53.02528
Feb 2026       23.74305  3.506129 43.97998  -7.2066512 54.69276
Mar 2026       23.74305  2.438127 45.04798  -8.8400200 56.32613
Apr 2026       23.74305  1.388172 46.09793 -10.4457875 57.93189
May 2026       23.74305  0.352787 47.13332 -12.0292721 59.51538

Note: We used the argument h=12 to specify that we would like to forecast the values for 12 periods in the future.

The output displays the predicted values for the next 12 months along with the lower and upper limits of both the 80% and 95% confidence intervals.

We can see that the boundaries of the 95% confidence intervals are much larger since this is the very definition of confidence intervals – the higher the confidence level, the wider the interval since it allows us to be more confident that the point estimate will fall in the range.

Note that we could also use the level argument of the forecast() function to specify an exact confidence level to use.

For example, we can use the following syntax to produce lower and upper limits for 90% confidence intervals instead:

library(forecast)

#forecast values for 12 periods in future of time series
forecast(ts_data, h=12, level=0.9)

         Point Forecast      Lo 90    Hi 90
Jun 2025       23.74305 10.7624368 36.72367
Jul 2025       23.74305  8.7508056 38.73530
Aug 2025       23.74305  6.9231807 40.56293
Sep 2025       23.74305  5.2233235 42.26278
Oct 2025       23.74305  3.6172367 43.86887
Nov 2025       23.74305  2.0825193 45.40359
Dec 2025       23.74305  0.6034850 46.88262
Jan 2026       23.74305 -0.8313632 48.31747
Feb 2026       23.74305 -2.2307575 49.71686
Mar 2026       23.74305 -3.6015238 51.08763
Apr 2026       23.74305 -4.9491264 52.43523
May 2026       23.74305 -6.2780285 53.76414

The output displays the predicted values for the next 12 months along with the lower and upper limits of the 90% confidence intervals.

The Lo 90 column displays the lower boundary of the confidence interval for the future predicted value while the Hi 90 column displays the upper boundary.

Feel free to use any value you’d like for the level argument of the forecast() function when predicting future values of your own time series.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Create a Time Series in R
How to Convert Data Frame to Time Series in R
How to Use the predict() Function with lm() in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *