## Load the libraries
library(fpp3)
library(tidyverse)
library(readxl)
### Set the current working directory, Update the directory based on the Excel file location
getwd()
setwd(“C:/Users/Ahmet.Kandakoglu/Desktop/UO_Ahmet/05_Teaching/ADM4307 Business Forecasting Analytics – F2021/02_Lecture Notes/Week-3”)
### Read the data from Excel
pax <- read_excel("airline-passengers.xlsx") pax view(pax) ### Convert to time series pax_ts <- pax %>% mutate(Month = yearmonth(Month)) %>% as_tsibble(index = Month)
pax_ts
view(pax_ts)
### Plot the data
autoplot(pax_ts, vars(Passengers))
pax_ts %>% autoplot(vars(Passengers)) + ylab(“Number of passengers in thousands”) + xlab(“Month”) + ggtitle(“Airline Passengers”)
pax_ts %>% gg_season(Passengers)
pax_ts %>% gg_season(Passengers, polar=TRUE)
pax_ts %>% gg_subseries(Passengers)
### Split the dataset into train and test
train <- pax_ts %>% filter(year(Month) < 1958) test <- pax_ts %>% filter(year(Month) >= 1958)
### Fit a model
fit_mean <- train %>% model(Mean = MEAN(Passengers))
fit_drift <- train %>% model(Drift = RW(Passengers ~ drift()))
fit_naive <- train %>% model(Naive = NAIVE(Passengers))
fit_snaive <- train %>% model(Seasonal_Naive = SNAIVE(Passengers))
fit_trend <- train %>% model(Trend = TSLM(Passengers ~ trend()))
### Fit models in one step
pax_fit <- train %>%
model(
Mean = MEAN(Passengers),
Drift = RW(Passengers ~ drift()),
Naive = NAIVE(Passengers),
Seasonal_Naive = SNAIVE(Passengers),
Trend = TSLM(Passengers ~ trend())
)
### Forecast
forecast_mean <- fit_mean %>% forecast(h = 36)
forecast_mean
forecast_drift <- fit_drift %>% forecast(h = 36)
forecast_naive <- fit_naive %>% forecast(h = 36)
forecast_snaive <- fit_snaive %>% forecast(h = 36)
forecast_trend <- fit_trend %>% forecast(h = 36)
forecast_pax <- pax_fit %>% forecast(h = 36)
### Visualize the forecast
forecast_mean %>% autoplot(train, level = NULL) + autolayer(test, colour = “green”)
forecast_drift %>% autoplot(train, level = NULL) + autolayer(test, colour = “green”)
forecast_naive %>% autoplot(train, level = NULL) + autolayer(test, colour = “green”)
forecast_snaive %>% autoplot(train, level = NULL) + autolayer(test, colour = “green”)
forecast_trend %>% autoplot(train, level = NULL) + autolayer(test, colour = “green”)
forecast_pax %>% autoplot(train, level = NULL) + autolayer(test, colour = “green”)
forecast_drift %>% autoplot(train)
forecast_snaive %>% autoplot(train)
forecast_trend %>% autoplot(train)
### The fitted values and residuals
augment(pax_fit)
fit_snaive %>% gg_tsresiduals()
pax_fit %>% select(Seasonal_Naive) %>% gg_tsresiduals()
pax_fit %>% select(Mean) %>% gg_tsresiduals()
pax_fit %>% select(Trend) %>% gg_tsresiduals()
### Calculate the accuracy of the models
accuracy(forecast_pax, pax_ts)
accuracy(forecast_pax, test)
### Prediction intervals
forecast_snaive %>% hilo()
forecast_pax %>% hilo()
### STL decomposition
pax_comp <- pax_ts %>% model(stl = STL(Passengers))
pax_comp
components(pax_comp)
components(pax_comp) %>% autoplot()
components(pax_comp) %>% autoplot(trend)
components(pax_comp) %>% autoplot(season_year)
components(pax_comp) %>% gg_subseries()
### Transform the data first
pax_ts %>% autoplot(log(Passengers))
pax_comp <- pax_ts %>% mutate(Passengers_Log = log(Passengers)) %>% model(stl = STL(Passengers_Log))
components(pax_comp) %>% autoplot()
lambda <- pax_ts %>% features(Passengers, features = guerrero) %>%
pull(lambda_guerrero)
pax_ts %>% autoplot(box_cox(Passengers, lambda))
pax_comp <- pax_ts %>% mutate(Passengers_Log = box_cox(Passengers, lambda)) %>% model(stl = STL(Passengers_Log))
components(pax_comp) %>% autoplot()