Skip to content

Forecasting Time Series

AW

What is time series?

Screenshot

Methodology

  1. What model explains the observed series? (IDENTIFY)
  2. How do we estimate this model? (ESTIMATE)
  3. How do we forecast the observed series? (FORECAST)

Basic Approaches

  1. Smoothing
  2. No formal model
  3. Averages of past values
  4. An algorithm to plot a smooth curve through the data
  5. Fairly common. Easy to use and not bad in some situations
  6. Reasonable situations to uses moothing:
    • Very few observations
    • Huge number of variables to forecast
    • Slowly evolving mean with measurement error
  7. ARIMA Models
    • Most widely used time series models
    • Typically start by transforming observed series Wt=f(Xt)
    • Appropriate for a model with slowly evolving mean and a time trend
    • Fit a model with AR(p)and MA(q)components: Screenshot
    • Xt may be a vector. Will be able to use other variables to help forecast. VAR model
  8. Regression models with time dependent errors
  9. Suppose you have a linear regression model where the variables are time series: Screenshot
  10. εt is typically not “nice”
  11. The error process typically has dependence.It may follow its own ARMA model
  12. How do we estimate this type of model?
  13. How do we forecast with this type of model?

Smoothing

Exponential Smoothing – series with no trend and no seasonal

Screenshot Screenshot Screenshot Screenshot Screenshot

Setup and Plot

# Clear the work space
rm(list = ls())

# Load the needed libraries
if (!require('readxl')) install.packages('readxl')
library('readxl')
if (!require('writexl')) install.packages('writexl')
library('writexl')
if (!require('ggplot2')) install.packages('ggplot2')
library('ggplot2')
if (!require('ggfortify')) install.packages('ggfortify')
library('ggfortify')
if (!require('forecast')) install.packages('forecast')
library('forecast')
if (!require('lubridate')) install.packages('lubridate')
library('lubridate')
if (!require('zoo')) install.packages('zoo')
library('zoo')

# Load the data into R
data_from_excel <- read_excel("NG.xlsx")

# Get familiar with your data, variables, size, etc. (str=structure)
str( data_from_excel )

# Make NG a monthly time series
NG <- ts(data_from_excel, start=c(1997,1), frequency = 12, names =c("Natural Gas Price") )
str(NG)
summary(NG)
autoplot(NG, color = 'red') +
labs(x = "Observation Date", y = "Natural Gas Price")

# Plot with ggplot2
df_data <- data.frame(date=as.Date(as.yearmon(time(NG))), Y=as.matrix(NG))
ggplot(data=df_data, mapping=aes(x=date, y=Natural.Gas.Price))+geom_point()+geom_line(aes(x=date, y=Natural.Gas.Price, color="red"))

Create forecasts using simple exponential smoothing

# sts=error,trend,seasonal
# options: N - none; A - addictive; M - multiplicative; Z - automatically selected
SES <- ets(NG, model = "ANN", alpha = .2)
SES
# Structure of the output
str(SES)
# Plot the output
plot(SES)
# Create predictions
SES.pred <- forecast(SES, h = 18, level = c(95))
SES.pred
# Look at the data structure of the predictions
str(SES.pred)
# Plot the predictions
plot(SES.pred)

Do the same things with the automatic procedure

SES_auto <- ets(NG, model="ANN")
SES_auto
SES_auto.pred <- forecast(SES_auto, h = 18, level = c(95))
plot(SES_auto.pred)

Write the forecasted value and the confidence interval to an excel file

col_names <- as.yearmon(time(SES.pred$mean), "%m-%Y")
my_df <- data.frame( as.Date(col_names), SES.pred$mean, SES.pred$lower, SES.pred$upper)
colnames(my_df) <- c("Date", "Forecast", "95% Lower", "95% Upper")
write_xlsx(my_df, "NG_prediction.xlsx")

Nice focused plot of the forecasts

ggplot(data = my_df, aes(x = Date, y = as.double(Forecast) )) +
geom_point( color="blue" ) +
geom_line(aes(x = Date, y = `95% Lower`), color="red" ) +
11
geom_line(aes(x = Date, y = `95% Upper`), color="red" ) +
geom_point(data=df_data, mapping=aes(x=date, y=Natural.Gas.Price))+
labs(x = "Date", y = "Natural Gas Price")

Holt Smoothing – series with a trend and no seasonal

Screenshot Screenshot Screenshot

# Load the data into R
data_from_excel <- read_excel("Annual_Hot_Water.xlsx")
str( data_from_excel )

# Make Hot Water Heater Sales a monthly time series
PROD_A <- ts(data_from_excel, start=c(2004), frequency = 1, names =c("Sales of Product A") )
str(PROD_A)
summary(PROD_A)
autoplot(PROD_A, color = 'red') +
labs(x = "Observation Date", y = "Sales of Product A")

# Plot with ggplot2
df_data <- data.frame(date=as.Date(as.yearqtr(time(PROD_A))), Y=as.matrix(PROD_A))
ggplot(data=df_data, mapping=aes(x=date, y=Sales.of.Product.A))+geom_point()

Create forecasts using Holt smoothing

HOLT <- ets(PROD_A, model = "AAN", alpha = .2, beta = .15)
HOLT
str(HOLT)
plot(HOLT)
HOLT.pred <- forecast(HOLT, h = 3, level = c(95))
HOLT.pred
str(HOLT.pred)

Do the same things with the automatic procedure

HOLT_auto <- ets(PROD_A, model = "AAN")
HOLT_auto
plot(HOLT_auto)
HOLT_auto.pred <- forecast(HOLT_auto, h = 3, level = c(95))
plot(HOLT_auto.pred)

Write the forecasted value and the confidence interval to an excel file

col_names <- as.yearmon(time(HOLT.pred$mean))
my_df <- data.frame( as.Date(col_names), HOLT.pred$mean, HOLT.pred$lower, HOLT.pred$upper)
colnames(my_df) <- c("Date", "Forecast", "95% Lower", "95% Upper" )
write_xlsx(my_df, "Annual_Sales_A_prediction.xlsx")

Nice focused plot of the forecasts

ggplot(data = my_df, aes(x = Date, y = as.double(Forecast) )) +
geom_point( color="blue" ) +
geom_line(aes(x = Date, y = `95% Lower`), color="red" ) +
geom_line(aes(x = Date, y = `95% Upper`), color="red" ) +
geom_point(data=df_data, mapping=aes(x=date, y=Sales.of.Product.A))+
labs(x = "Date", y = "Sales of Product A")

Holt-Winter’s Smoothing – for seasonal data and possibly a trend

Screenshot Screenshot

# Load the data into R
data_from_excel <- read_excel("WEEK0QUARTERLY.xlsx")
str( data_from_excel )

# Make Hot Water Heater Sales a monthly time series
PROD_B <- ts(data_from_excel, start=c(2000,1), frequency = 4, names =c("Sales of Product B") )
str(PROD_B)
summary(PROD_B)
autoplot(PROD_B, colour = 'red') +
labs(x = "Observation Date", y = "Sales of Product B")

# Plot with ggplot2
df_data <- data.frame(date=as.Date(as.yearmon(time(PROD_B))), Y=as.matrix(PROD_B))
ggplot(data=df_data, mapping=aes(x=date, y=Sales.of.Product.B))+geom_line()

Create forecasts using Holt smoothing

Holt_Winters <- ets(PROD_B, model = "MAM", alpha = .4, beta = .15, gamma=.3)
Holt_Winters
plot(Holt_Winters)
Holt_Winters.pred <- forecast(Holt_Winters, h = 6, level = c(95))
Holt_Winters.pred
str(Holt_Winters.pred)
plot(Holt_Winters.pred)

Do the same things with the automatic procedure

Holt_Winters_auto <- ets(PROD_B, model = "MAM")
Holt_Winters_auto
plot(Holt_Winters_auto)
Holt_Winters_auto.pred <- forecast(Holt_Winters_auto, h = 6, level = c(95))
plot(Holt_Winters_auto.pred)

Write the forecasted value and the confidence interval to an excel file

col_names <- as.yearmon(time(Holt_Winters.pred$mean))
my_df <- data.frame( as.Date(col_names), Holt_Winters.pred$mean, Holt_Winters.pred$lower, Holt_Winters.colnames(my_df) <- c("Date", "Forecast", "95% Lower", "95% Upper" )
write_xlsx(my_df, "Quarterly_Sales_B_prediction.xlsx")

Nice focused plot of the forecasts

ggplot(data = my_df, aes(x = Date, y = as.double(Forecast) )) +
geom_point( color="blue" ) +
geom_line(data = my_df, aes(x = Date, y = `95% Lower`), color="red" ) +
geom_line(data = my_df, aes(x = Date, y = `95% Upper`), color="red" ) +
geom_line(data=df_data, mapping=aes(x=date, y=Sales.of.Product.B))+
labs(x = "Date", y = "Sales of Product B")

W1