Time series forecasting is a common technique used in economics, business, and environmental science for prediction purposes. In this project, a classical time series dataset is used, and the Prophet time series forecasting system developed by Meta is employed for the purpose of prediction.
Prophet is a procedure for forecasting time series data with long-term trends and seasonalities that is simple to use.
In this project, the time series dataset used is the AirPassengers dataset.
The dataset contains monthly passenger counts for international airline travel between 1949 and 1960.
## [1] 1949 1
## [1] 1960 12
## [1] 12
The series contains 144 observations with a frequency of 12, indicating monthly data.
We first visualise the time series to understand its structure.
plot(AirPassengers,
main="Monthly Airline Passenger Numbers",
xlab="Year",
ylab="Passengers (thousands)")The plot reveals two important characteristics:
The seasonal pattern suggests that passenger numbers increase during certain months of the year.
The variability of the data increases as passenger numbers grow. To stabilise the variance we apply a logarithmic transformation.
log_passengers <- log(AirPassengers)
plot(log_passengers,
main="Log Transformed Passenger Data",
xlab="Year",
ylab="Log(Passengers)")The transformation reduces the increasing spread of the data and helps produce a more stable time series.
Prophet requires a dataframe with two columns named ds (date) and y (value).
## Loading required package: Rcpp
## Loading required package: rlang
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
time_values <- time(AirPassengers)
date_values <- as.Date(as.yearmon(time_values))
passenger_dataframe <- data.frame(
ds = date_values,
y = as.numeric(AirPassengers)
)
head(passenger_dataframe)## ds y
## 1 1949-01-01 112
## 2 1949-02-01 118
## 3 1949-03-01 132
## 4 1949-04-01 129
## 5 1949-05-01 121
## 6 1949-06-01 135
We now estimate a Prophet forecasting model using the passenger data.
## Disabling weekly seasonality. Run prophet with weekly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
The model automatically estimates trend and seasonal effects.
We generate predictions for the next 24 months.
future_dates <- make_future_dataframe(prophet_model,
periods = 24,
freq = "month")
forecast_results <- predict(prophet_model, future_dates)
head(forecast_results)## ds trend additive_terms additive_terms_lower additive_terms_upper
## 1 1949-01-01 107.8473 -21.2774203 -21.2774203 -21.2774203
## 2 1949-02-01 109.9819 -29.9397070 -29.9397070 -29.9397070
## 3 1949-03-01 111.9100 -0.4437455 -0.4437455 -0.4437455
## 4 1949-04-01 114.0446 -4.8747224 -4.8747224 -4.8747224
## 5 1949-05-01 116.1104 -3.6060409 -3.6060409 -3.6060409
## 6 1949-06-01 118.2451 34.3718704 34.3718704 34.3718704
## yearly yearly_lower yearly_upper multiplicative_terms
## 1 -21.2774203 -21.2774203 -21.2774203 0
## 2 -29.9397070 -29.9397070 -29.9397070 0
## 3 -0.4437455 -0.4437455 -0.4437455 0
## 4 -4.8747224 -4.8747224 -4.8747224 0
## 5 -3.6060409 -3.6060409 -3.6060409 0
## 6 34.3718704 34.3718704 34.3718704 0
## multiplicative_terms_lower multiplicative_terms_upper yhat_lower yhat_upper
## 1 0 0 59.20008 115.9395
## 2 0 0 52.03125 108.1210
## 3 0 0 84.87915 140.3400
## 4 0 0 80.41623 137.4957
## 5 0 0 83.95233 142.8517
## 6 0 0 124.73955 180.7018
## trend_lower trend_upper yhat
## 1 107.8473 107.8473 86.56984
## 2 109.9819 109.9819 80.04221
## 3 111.9100 111.9100 111.46624
## 4 114.0446 114.0446 109.16992
## 5 116.1104 116.1104 112.50439
## 6 118.2451 118.2451 152.61695
The plot displays the predicted passenger counts along with uncertainty intervals.
The forecast suggests continued growth in international airline travel.
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## ℹ The deprecated feature was likely used in the prophet package.
## Please report the issue at <https://github.com/facebook/prophet/issues>.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
The model separates the series into:
The seasonal component highlights recurring yearly travel patterns.
In this project, we worked with airline passenger data. The data shows strong growth trends that are characteristic of transportation demand.
The data has been successfully modelled with the trend as well as the seasonality. The data is showing a trend that the number of passengers will continue to grow based on the current trends.
This is a practical example of how we can use current forecasting tools to work with time-series data.
Prophet Documentation
https://facebook.github.io/prophet/
R Markdown Guide
https://rmarkdown.rstudio.com