Time Series Forecasting with “AirPassengers” Dataset.
R
# Load the "AirPassengers" dataset data ( "AirPassengers" ) # Convert the dataset to a time series object ts_data <- ts (AirPassengers, frequency = 12, start = c (1949, 1)) # Split the data into training and testing sets train_data <- window (ts_data, start = c (1949, 1), end = c (1958, 12)) test_data <- window (ts_data, start = c (1959, 1)) # Train an ARIMA model arima_model <- forecast:: auto.arima (train_data) # Forecast 12 months ahead forecast_result <- forecast:: forecast (arima_model, h = 12) summary (arima_model) |
Output:
Series: train_data
ARIMA(1,1,0)(0,1,0)[12]
Coefficients:
ar1
-0.2397
s.e. 0.0935
sigma^2 = 103.6: log likelihood = -399.64
AIC=803.28 AICc=803.4 BIC=808.63
Training set error measures:
ME RMSE MAE MPE MAPE MASE ACF1
Training set -0.01614662 9.567988 7.120167 -0.03346415 2.90195 0.2491828 0.00821521
This line converts the loaded dataset into a time series object (ts_data). The ts() function is used to specify that the data has a monthly frequency (frequency = 12) and starts in January 1949 (start = c(1949, 1)).
Plot The Forecast
R
# Plot the forecast plot (forecast_result, xlab = "Year" , ylab = "Passenger Count" , main = "Airline Passengers Forecast" ) |
Output:
- The data(“AirPassengers”) command loads the built-in “AirPassengers” dataset, which contains the monthly number of airline passengers from 1949 to 1960.
- The ts_data <- ts(AirPassengers, frequency = 12, start = c(1949, 1)) line converts the loaded dataset into a time series object. The frequency argument is set to 12, indicating that the data has a monthly frequency, and the start argument specifies the starting date as January 1949.
- The train_data and test_data objects are created by using the window() function to split the time series into two parts. The training data contains observations from January 1949 to December 1958, and the testing data contains observations from January 1959 onward.
- The arima_model <- forecast::auto.arima(train_data) line trains an ARIMA (AutoRegressive Integrated Moving Average) model on the training data. The auto.arima() function automatically selects the best ARIMA model based on the data.
- The forecast_result <- forecast::forecast(arima_model, h = 12) line generates a forecast for the next 12 months using the trained ARIMA model. The h parameter specifies the number of periods (in this case, months) to forecast ahead.
- Finally, the plot() function is used to create a plot of the forecasted values. The x-axis represents the year, the y-axis represents the passenger count, and the title of the plot is set to “Airline Passengers Forecast.”
Machine Learning for Time Series Data in R
Machine learning (ML) is a subfield of artificial intelligence (AI) that focuses on the development of algorithms and models that enable computers to learn and make predictions or decisions without being explicitly programmed. In R Programming Language it’s a way for computers to learn from data and improve their performance on a specific task over time. Here are some key concepts in machine learning.
Time Series Data in R
Time series data is a sequence of observations or measurements collected or recorded at specific time intervals. This type of data is commonly found in various domains, including finance, economics, meteorology, and more. R provides several packages and functions to work with time series data effectively.
Time Series Components
Time series data is characterized by several key components that impact its behaviour and modelling. Understanding these components is crucial for accurate time series forecasting. The primary components are.
- Trend: The trend component represents the long-term movement or direction in the data. It reveals the overall pattern or behaviour over an extended period. Trends can be upward, downward, or relatively stable.
- Seasonality: Seasonality refers to periodic fluctuations or patterns that occur at regular intervals. These intervals could be daily, weekly, monthly, or yearly. For example, sales data often exhibits seasonality with higher sales during specific times of the year.
- Cyclic Patterns: Cyclic patterns are long-term wave-like movements that are not strictly periodic like seasonality. They typically have irregular durations and amplitudes. Identifying cyclic patterns can be challenging.
- Residuals: Residuals represent the random noise or irregular variations in the data that cannot be attributed to the trend, seasonality, or cyclic patterns. Accurate time series modelling involves minimizing these residuals.
Important steps required for Machine Learning for Time Series Data in R
Data: Machine learning algorithms require data to learn from. This data typically consists of features (input variables) and labels (output or target variables). For example, in image recognition, features might be pixel values, and labels would be the object classes.
Training: In the training phase, a machine learning model is presented with a dataset containing known inputs and outputs. The model learns to map inputs to outputs by adjusting its internal parameters.
Model: A machine learning model is a mathematical representation of a relationship between inputs and outputs. There are various types of ML models, including regression models, decision trees, neural networks, and more.
Learning: Learning refers to the process of adjusting the model’s parameters during training to minimize the difference between its predictions and the actual labels in the training data. This process is guided by a loss function that quantifies the model’s error.
Prediction: Once trained, a machine learning model can be used to make predictions or decisions on new, unseen data. It applies the learned patterns to new inputs to produce outputs or predictions.