Top Banner
Intro to Forecasting in R Part I Houston R Users Group Ed Goodwin, CFA
17
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Hrug intro to forecasting

Intro to Forecasting in R Part I

Houston R Users Group Ed Goodwin, CFA

Page 2: Hrug intro to forecasting

Why Forecast?• Planning and budgeting

• Making timely decisions

• Improving service

• Solving problems

• Optimization

Page 3: Hrug intro to forecasting

What can you forecast?• How well do we understand the factors that contribute to the

forecast?

• How much data do we have available?

• Does the forecast influence the outcome (complex adaptive systems, reflexivity)

We can forecast when we believe:

• We have data about the past

• We believe that historic patterns will persist in some form

Page 4: Hrug intro to forecasting

Why use R to forecast?• enhanced graphics capabilities

• tools for summary statistics

• ability to handle time series

• easy to use transformations

• residuals and diagnostics

• ready to run forecasting models built in

Page 5: Hrug intro to forecasting

A Wealth of Time Series Classes

Class Features

ts vector or matrix representing equally spaced, numeric* time series; has numeric timestamps

mts multivariate time series class

zoo built on ts with added functionality for annual and quarterly observations (yearmon and yearqtr)

xts built on zoo for the quantmod package for added date/timestamp conversion capabilities

timeSeries class with datetime indices

* limited support for non-numeric data

Page 6: Hrug intro to forecasting

ts - basic time series classSAMPLE TIME SERIES

1949.000 112

1949.083 118

1949.167 132

1949.250 129

use ts( ) function to create a time series

Page 7: Hrug intro to forecasting

Working with time series

Subsetting a time series with window( )

Lagging a time series with lag( )

Page 8: Hrug intro to forecasting

Plotting time series

Plotting with plot( ), plot.ts( ), or ts.plot( )

Page 9: Hrug intro to forecasting

The Forecast Package• Developed by Dr. Rob J. Hyndman

• Implements a number of different forecast models

• Tools to create forecast models and assess their validity

Page 10: Hrug intro to forecasting

How do we validate a forecast model?

• Does it beat random chance?

• Does it beat a simple model?

Page 11: Hrug intro to forecasting

Simple Forecasting Methods(Made even simpler with the forecast package)

Mean forecast - predicted values are the mean of the historical time series

Naive forecast - predicted values are equal to the last known predicted value

Seasonal Naive forecast - predicted values are equal to the same values of the prior seasonal period

Drift forecast - naive forecast with a drift factor based on the trend in the historic data set

Page 12: Hrug intro to forecasting
Page 13: Hrug intro to forecasting

Error measures of forecasts• Mean Absolute Error

(MAE)

• Root Mean Squared Error (RMSE)

• Mean Absolute Percentage Error (MAPE)

• Symmetric Mean Absolute Percentage Error (sMAPE)

• Mean Absolute Scaled Error (MASE)

Page 14: Hrug intro to forecasting

How accurate are the simple methods?

RMSE VALUE

MEAN 230.6

NAIVE 99.3

SNAIVE 50.1

DRIFT 90.7

Page 15: Hrug intro to forecasting

Linear Trend Fitting Models• Linear regression

extrapolated into some future period

• Create model with tslm( )

• use forecast( ) function to roll period

• defaults to 80% and 95% confidence intervals

Page 16: Hrug intro to forecasting

For next time…• Dummy variables

• Data transforms

• Seasonal decompositions

• Exponential smoothing

Page 17: Hrug intro to forecasting

Other Resources• TimeSeries task view

• http://cran.r-project.org/web/views/TimeSeries.html

• Good overview of time series packages, classes, and datasets

• Rmetrics ebook on timeseries https://www.rmetrics.org/ebooks-tseries

• "Forecasting: Principles and Practice” Hyndman & Athanasopoulos https://www.otexts.org/fpp/

• “Introductory Time Series with R” Cowpertwait & Metcalfe