home/modeling/application/r/time series

Time Series in R

Time Series models are used for forecasting values by analyzing the historical data listed in time order. This topic has been discussed in detail in the theory blog of Time Series. To demonstrate time series model in R we will be using a dataset of passenger movement of an airline.

Preparation

Importing Dataset

We will import the above-mentioned dataset using read_excel command.

r
library(readxl)
time <- read_excel('C:/Users/user/Desktop/Data Sets/Time_Series/AirPassengersData.xls')

Indexing Data

We will now convert our variable into R Time series objects using ts function. In the following function, frequency defines the number of observations per unit time. It will be equal to 4 for quarterly, 12 for monthly and so on.

r
time_passengers <- ts(time$`No. of Passengers`, start=c(1949,1),
end=c(1960,12), frequency=12)

We can now view the indexed data.

r
print(time_passengers)
print(time_passengers) output

Graphical Representation

To see the type of dataset we will use the above-indexed output to plot the graph. This graph is used to see trend or seasonality in our dataset.

r
plot(time_passengers,col='dodgerblue3',lwd=2)
Air passengers time series plot showing trend and seasonality

Clearly, it's a trend and seasonal graph. Let us now look at different techniques for predicting the number of passengers for the next 12 months.

Averaging Techniques

There are mainly three types of averaging techniques - Simple Average, Moving Average and Weighted Average. These methods have been discussed in detail in the theory blog of Averaging Techniques. We will now demonstrate the Moving Average Technique using R.

Moving Average Technique

We can compute moving average using the aggregate function in R. This will compute average using the data for the previous one year and plot the graph for the same. First, we will convert the data to log values to eliminate trend/seasonality. This is done in order to forecast the values for future months.

r
time_passengers2 <- log(time_passengers)

Using aggregate function to compute moving average.

r
moving_avg <- aggregate(time_passengers,FUN=mean)
plot(time_passengers,col='dodgerblue3',lwd=2)
lines(moving_avg,col='orange',lwd=2)
Time series with moving average overlay

Smoothing Techniques and Time Series Decomposition

Seasonal Trend Decomposition

We will use stl function for decomposition. This will deconstruct the time series into three components namely trend, seasonality and remainder.

r
stl_passengers2 <- stl(time_passengers2,s.window="periodic")

Checking Accuracy of the Model (STL Function-Decomposition)

Here, we compute the accuracy for the forecast model generated using STL function. We first start off by importing forecast library.

r
install.packages("forecast")
library(forecast)

Accuracy for All Variables

Here h is the number of periods for forecasting.

r
accuracy(forecast(stl_passengers2, h=12))
STL accuracy metrics output

Plotting Graph

After computing accuracy of the model of STL function, we plot the STL components generated for each variable.

r
plot(stl_passengers2,lwd=1)
STL decomposition 4-panel plot

We will now make use of the forecast function for forecasting variables.

r
require(forecast)
forecast(stl_passengers2, h=12)
STL forecast values table

Graphical Representation of Forecasted Variables

We will now plot the above output.

r
plot(forecast(stl_passengers2, h=12))
STL forecast plot

Exponential Smoothing

ets function is used to fit exponential smoothing models.

r
ets_passengers2 <- ets(time_passengers2)

Calculating accuracy of ets for each variable.

r
accuracy(ets_passengers2$fitted,time_passengers2)
ETS accuracy metrics output

Now we will forecast using ets outputs.

r
forecast(ets_passengers2,h=12)
ETS forecast values table

We plot the forecasted values by ets function (Smoothing Technique).

r
plot(forecast(ets_passengers2,h=12))
ETS forecast plot

ARIMA Models

Initializing ARIMA Model

An automated way of forecasting is by using ARIMA models. Note that in R, we are using an automated ARIMA and hence don't specify the order tuple p,d,q which is Number of AR (Auto-Regressive) terms (p), Number of MA (Moving Average) terms (q), Number of non-seasonal Differences (d) like we did in Python. ARIMA is a function of forecast package.

r
arima_passengers <- auto.arima(time_passengers2)

Accuracy Score of ARIMA Models

Let us now compute the accuracy score of ARIMA models.

r
accuracy(arima_passengers)
ARIMA accuracy metrics output

Forecasting Using ARIMA Model

We will now forecast values for each variable using ARIMA model.

r
forecast(arima_passengers,h=12)
ARIMA forecast values table

Finally, we represent the graphs for forecasted values from ARIMA model.

r
plot(forecast(arima_passengers,h=12))
ARIMA forecast plot

In this blog post, many of the forecasting techniques were explored. The same techniques have also been explored in the blog Time Series in Python.

ESC
100 pages indexed · Esc to close