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
Thanks for a very good documentation. However, in my case the forecast date is indicated as the starting date of the time series. How do I fix this? Also, my ts object contains dates in Unix format and the forecast function cannot handle it even when I use the following command.
date <- as.POSIXct(1546387205, origin = "1970-01-01", tz = "UTC"). Please advice.
Hi S. Kandaswamy…The issue you’re facing is likely due to how `forecast()` in R handles the time index in a `ts` object, which is typically based on a numeric time index rather than Unix timestamps or POSIX date objects. To ensure your forecasted values align with the correct dates, try the following steps:
1. **Convert Unix Timestamps to Date Format:** Ensure your data is in a format `forecast()` can handle, such as `zoo` or `xts` objects, which support date indices. You can also use `ts()` but only if your data has a regular interval (e.g., daily, monthly).
2. **Using `zoo` or `xts`:** The `zoo` or `xts` packages can help bridge this issue by allowing POSIXct dates while keeping your time series compatible with forecasting.
Here’s a general example for your scenario:
“`R
library(forecast)
library(zoo)
# Convert your Unix timestamps to POSIXct
dates <- as.POSIXct(c(1546387205, 1546473605, 1546560005), origin = "1970-01-01", tz = "UTC") values <- c(10, 20, 30) # Replace with your actual values # Create a zoo object with dates time_series <- zoo(values, order.by = dates) # Convert zoo to ts object if needed, or use forecast directly on zoo object forecasted_values <- forecast(auto.arima(time_series)) # Plot forecast with correct date axis plot(forecasted_values) ``` 3. **Directly Specifying Dates for Plotting:** If using `zoo` or `xts` objects doesn’t work, another approach is to use `ts()` with an index that maps to dates post-forecast. After forecasting, you can adjust the `forecast()` output's dates manually if necessary. 4. **Set `origin` Correctly**: Confirm the `origin` in `as.POSIXct` matches the data's format (usually "1970-01-01" for Unix timestamps).
Thanks but my problem remains unresolved. My forecast command yields the following:
> Date2Forecast Date2Forecast
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
126403201 140.1199 134.2213 146.0184 131.0988 149.1409
126489601 140.2964 132.3122 148.2807 128.0856 152.5073
126576001 140.4730 130.8338 150.1122 125.7312 155.2148
126662401 140.6496 129.5911 151.7080 123.7371 157.5620
126748801 140.8261 128.5022 153.1500 121.9784 159.6739
126835201 141.0027 127.5238 154.4816 120.3886 161.6168
126921601 141.1793 126.6295 155.7290 118.9274 163.4312
127008001 141.3558 125.8019 156.9098 117.5681 165.1435
127094401 141.5324 125.0287 158.0360 116.2922 166.7725
127180801 141.7090 124.3011 159.1168 115.0860 168.3319
127267201 141.8855 123.6123 160.1588 113.9390 169.8321
127353601 142.0621 122.9569 161.1673 112.8432 171.2810
Two problems: #1. Point needs to be converted to Dates. #2. When I manually convert the Point to dates, I get 2023-01-5, 2023-1-6 which are NOT future dates. Dates in my time series start with 1/2/2019 and end with 10/23/2024. So, the first forecast should have been for the future date of 10/24/2024. How do I resolve these two problems? Would very much appreciate your help. My knowledge is limited to R and I don’t know Python.
Hi S. Kandaswamy…It sounds like you have two main issues:
1. **Converting Point Forecast indices to actual dates**: In R, it’s possible to transform these indices into dates by utilizing the starting and frequency parameters from your original time series.
2. **Starting the forecast from the next date after your data ends**: You’ll need to set the starting point of the forecast to match the last date in your dataset.
Here’s a step-by-step approach to address both issues:
### Step 1: Make sure your time series is set up with dates
If your original time series (e.g., `ts_data`) starts on a specific date with daily observations, you’ll want to make sure it’s defined with the correct start date and frequency. For example, if your data starts on January 2, 2019, with daily frequency:
“`r
ts_data <- ts(your_data, start = c(2019, 2), frequency = 365) ``` In this case: - The `start` parameter (2019, 2) indicates the year and day of the year (e.g., January 2). - `frequency = 365` specifies daily data. ### Step 2: Use the `forecast` package When you call the `forecast()` function, it should respect the start and frequency of your `ts_data`. The dates should automatically extend from the last date in your dataset. For example: ```r library(forecast) model <- auto.arima(ts_data) forecast_data <- forecast(model, h = 12) # Forecast for 12 periods ``` ### Step 3: Extract forecasted dates and format the output After forecasting, convert the index to a date format starting from the last date of your time series: ```r # Convert the forecast to a data frame for easier manipulation forecast_df <- data.frame(Date = time(forecast_data), forecast_data$mean, forecast_data$lower, forecast_data$upper) # Use the date format for clarity forecast_df$Date <- as.Date(forecast_df$Date, origin = "1970-01-01") print(forecast_df) ``` This should ensure your forecasts begin from the date following the last data point in your series, displaying proper future dates. Let me know if you encounter further issues, and we can refine the approach.
Thanks for your continued willingness to support. Even after using your suggestions and input from META AI, I’m unable to solve the forecast problem. I encountered two problems: Problem #1. When I create a new object with the ts class out of the original dataframe, the new dates do not match with the original date in my dataframe. Problem #2. When I use the forecast command, there are two forecasts one for the Date (which is silly and I don’t want) and another for the variable I’m interested in. As the object belongs to the ts class, I’m unable select only the variable of my interest. Also, the dates appear to be in Unix code in the forecast.
Only four commands were used by me which are below. (I also provide intermediate results.) My first object/dataframe was created from importing a dataset from an Excel file containing trading data on NVDIA from 1/2/2019 to 10/23/2024. Note the dates are not consecutive as they are trading days-centric.
Code #1. > NVIDIA_5YEAR_Data NVIDIA_closing head(NVIDIA_closing)
# A tibble: 6 × 2
Date Close
1 2019-01-02 00:00:00 3.38
2 2019-01-03 00:00:00 3.17
3 2019-01-04 00:00:00 3.38
4 2019-01-07 00:00:00 3.56
5 2019-01-08 00:00:00 3.47
6 2019-01-09 00:00:00 3.54
Code #3. >NVIDIA_ts_closing head(NVIDIA_ts_closing)
Time Series:
Start = 1
End = 6
Frequency = 1
Date Close
1 1546387200 3.3783
2 1546473600 3.1742
3 1546560000 3.3776
4 1546819200 3.5564
5 1546905600 3.4678
6 1546992000 3.5360
Code #4. >NVIDIA_forecast_data <-forecast(NVIDIA_ts_closing, h=12) # Problem 2. This statement forecasts Date as well and the date is in Unix code (my guess). As the class of the object is ts, I cannot select only the Close variable.
Date
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
3482 1729789333 1729705847 1729872819 1729661653 1729917013
3483 1729914838 1729828942 1730000734 1729783471 1730046205
And more
Close
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
3482 140.1199 134.2213 146.0184 131.0988 149.1409
3483 140.2964 132.3122 148.2807 128.0856 152.5073
And more
Not empty words – very kind of you to take the time and effort to help me.
You’re encountering two issues with your time series (`ts`) object and forecast, primarily related to how R handles time series data. Let’s address both problems step by step.
—
### **Problem 1: Dates Do Not Match When Creating `ts` Object**
The `ts` class in R does not inherently handle irregular time series (e.g., trading days-centric data). Instead, it assumes regular intervals (e.g., daily, monthly). Since your trading data has irregular dates, this mismatch is causing issues.
#### **Solution**
Use the `xts` or `zoo` package, which supports irregular time series, instead of the `ts` class.
Here’s how you can convert your data:
“`R
library(xts)
# Convert your data into an xts object
NVIDIA_xts <- xts(NVIDIA_closing$Close, order.by = as.Date(NVIDIA_closing$Date)) # View the head to confirm head(NVIDIA_xts) ``` This preserves your original dates and aligns them correctly. --- ### **Problem 2: Forecast Includes Dates and Uses Unix Timestamps** When using the `forecast` function, it creates predictions for both the time index and the variable. The Unix timestamps in your forecast indicate that the time series class (`ts`) is not appropriate for your data. Moreover, `forecast` is designed to predict future values, not dates. #### **Solution** 1. **Forecast Only the Close Variable** Forecasting tools typically focus on the values (e.g., `Close`) rather than the `Date` column. ```R # Use the `auto.arima` function with the `xts` object library(forecast) # Fit an ARIMA model NVIDIA_model <- auto.arima(NVIDIA_xts) # Forecast 12 steps ahead NVIDIA_forecast <- forecast(NVIDIA_model, h = 12) # Plot the forecast plot(NVIDIA_forecast) ``` This will only forecast the `Close` variable. 2. **Add Dates to the Forecast** If you want to include proper dates for the forecast, you can manually compute them based on your trading calendar. For example: ```R # Generate future dates manually (e.g., for 12 future trading days) future_dates <- seq(max(index(NVIDIA_xts)), by = "days", length.out = 12) # Combine forecast with future dates NVIDIA_forecast_df <- data.frame( Date = future_dates, Point_Forecast = as.numeric(NVIDIA_forecast$mean), Lo_80 = as.numeric(NVIDIA_forecast$lower[,1]), Hi_80 = as.numeric(NVIDIA_forecast$upper[,1]), Lo_95 = as.numeric(NVIDIA_forecast$lower[,2]), Hi_95 = as.numeric(NVIDIA_forecast$upper[,2]) ) # View the forecast with dates print(NVIDIA_forecast_df) ``` --- ### Key Takeaways - **Use `xts` or `zoo` for irregular time series data** instead of the `ts` class. - Forecasting tools focus on the variable of interest (e.g., `Close`). Dates can be added manually if needed. - The `forecast` package works well with regular intervals but can be adapted for irregular data by preprocessing it appropriately. Let me know if you need further clarification or code refinements!
I did sent a personal “Thank You Note” to James Carmichael for the outstanding support in helping me to resolve the issues. Not sure whether he got it. Regards.
Hi Subramaniam…Thank you for your feedback and support! Let us know if we can ever help with anything else!