Intro To Time Series
Intro To Time Series
Intro To Time Series
Introduction
Time series is a set of observations, each one being recorded at a specific time. (e.g.,
Annual GDP of a country, Sales figure, etc.)
Discrete time series is one in which the set of time points at which observations are
made is a discrete set. (e.g., All above including irregularly spaced data)
Continuous time series are obtained when observations are made continuously over
some time intervals. (e.g., ECG graph)
Forecasting is very difficult, since it’s about the future! (e.g., forecasts of daily cases
of COVID-19)
Time Series Data
A time series is a sequence of observations over time. What makes it distinguishable from other
statistical analyses is the explicit recognition of the importance of the order in which the
observations are made. Also, unlike many other problems where observations are independent, in
time series observations are most often dependent.
What is the effect over time on electronics good consumption of a hike in the
excise duty?
Rates of inflation and unemployment in the country can be observed only over
time!
A Forecasting Problem: India / U.S. Foreign
Exchange Rate (EXINUS)
Source: FRED ECONOMICS DATA (Shaded areas indicate US recessions)
Units: Indian Rupees to One U.S. Dollar, Not Seasonally Adjusted
Frequency: Monthly (Averages of daily figures)
Forecasting: Assumptions
Time series Forecasting: Data collected at regular intervals of time (e.g.,
Weather and Electricity Forecasting).
Assumptions: (a) Historical information is available;
(b) Past patterns will continue in the future.
Time Series Components
Trend (𝑇𝑡 ) : pattern exists when there is a long-term increase or decrease in the data.
Seasonal (𝑆𝑡 ) : pattern exists when a series is influenced by seasonal factors (e.g., the
quarter of the year, the month, or day of the week).
Cyclic (𝐶𝑡 ) : pattern exists when data exhibit rises and falls that are not of fixed period
(duration usually of at least 2 years).
Decomposition : 𝑌𝑡 = 𝑓(𝑇𝑡 ; 𝑆𝑡 ; 𝐶𝑡 ; 𝐼𝑡 ) , where 𝑌𝑡 is data at period t and 𝐼𝑡 is irregular
component at period t.
Additive decomposition: : 𝑌𝑡 = 𝑇𝑡 + 𝑆𝑡 + 𝐶𝑡 + 𝐼𝑡
Multiplicative decomposition: 𝑌𝑡 = 𝑇𝑡 ∗ 𝑆𝑡 ∗ 𝐶𝑡 ∗ 𝐼𝑡
Time series data are data collected on the same observational unit at multiple
time periods
How to estimate?
A time series 𝑌𝑡 is stationary if its probability distribution does not change over
time, that is, if the joint distribution of (Yi+1, Yi+2, …, 𝑌𝑖+𝑇 ) does not depend on i.
𝐶𝑜𝑣(𝑌𝑡 , 𝑌𝑡−𝑗 )
𝜌𝑗 =
𝜎𝑌𝑡 𝜎𝑌𝑡−𝑗
Where, 𝐶𝑜𝑣(𝑌𝑡 , 𝑌𝑡−𝑗 ) is the j-th autocovariance.
For the given data, say ρ1 = 0.84
This implies that the Dollars per Pound is highly serially correlated
AR: autoregressive (lagged observations as inputs) I: integrated (differencing to make series stationary)
MA: moving average (lagged errors as inputs).
The model is expressed as ARIMA 𝑝, 𝑑, 𝑞 where 𝑝, 𝑑 𝑎𝑛𝑑 𝑞 are integer parameter values that decide the
structure of the model.
More precisely, 𝑝 𝑎𝑛𝑑 𝑞 are the order of the AR model and the MA model respectively, and parameter d is
the level of differencing applied to the data.
where 𝑦𝑡 is the actual value, 𝜀𝑡 is the random error at time t, 𝜙𝑖 and 𝜃𝑗 are the coefficients of the model.
It is assumed that 𝜀𝑡−1 𝜀𝑡−1 = 𝑦𝑡−1 − 𝑦𝑡−1 has zero mean with constant variance, and satisfies the i.i.d.
condition.
Three basic Steps: Model identification, Parameter Estimation, and Diagnostic Checking.
Differencing in ARIMA Model
ARIMA model
ACF / PACF Plots
ACF / PACF Plots : Example
Forecast Evaluation
Performance metrics such as mean absolute error (MAE), root mean square error
(RMSE), and mean absolute percent error (MAPE) are used to evaluate the
performances of different forecasting models for the unemployment rate data sets:
𝑛
1 2;
𝑅𝑀𝑆𝐸 = 𝑦𝑖 − 𝑦𝑖
𝑛
𝑖=1
𝑛
1
𝑀𝐴𝐸 = 𝑦𝑖 − 𝑦𝑖 ;
𝑛
𝑖=1
1 𝑛 𝑦𝑖 −𝑦𝑖
𝑀𝐴𝑃𝐸 = 𝑛 𝑖=1 ,
𝑦𝑖
Where 𝑦𝑖 is the actual output, 𝑦𝑖 is the predicted output, and n denotes the number
of data points.
By definition, the lower the value of these performance metrics, the better is the
performance of the concerned forecasting model.
Time Series
Analysis using R
Time Series Plot:
The graphical representation of time series data by taking time on x axis & data on y
axis.
A plot of data over time
Example
The demand for a commodity E15 for last 20 months from April 2012 to October 2013
is given in E15demand.csv file. Draw the time series plot
Month Demand Month Demand
1 139 11 193
2 137 12 207
3 174 13 218
4 142 14 229
5 141 15 225
6 162 16 204
7 180 17 227
8 164 18 223
9 171 19 242
10 206 20 239
24
Reading data to R
mydata <- read.csv("E15demand.csv")
E15 = ts(mydata$Demand, start = c(2012,4), end = c(2013,10), frequency = 12)
E15
plot(E15, type = "b")
Year GDP
1993 94.43
1994 100.00
1995 107.25
1996 115.13
1997 124.16
1998 130.11
1999 138.57
2000 146.97
2001 153.40
2002 162.28
2003 168.73
Seasonal Pattern:
The time series data exhibiting rises and falls influenced by seasonal factors
Example: The data on monthly sales of a branded jackets
Example : The data on daily shipments is given in shipment.csv. Check whether the
data is stationary
R code
mydata <- read.csv("shipment.csv")
shipments = ts(mydata$Shipments)
plot(shipments, type = "b")
Test for checking series is Stationary: Unit root test in R
ADF Test
R Code
install.packages("tseries")
library("tseries")
adf.test(shipments)
Statistic Value
Dickey-Fuller -3.2471
P value 0.09901
Since p value = 0.099 < 0.1, the data is stationary at 10% significant
level
Test for checking series is Stationary : Unit root test in R
KPSS test
R Code
kpss.test(shipments)
Statistic Value
KPSS Level 0.24322
P value > 0.1
Since p value > 0.1 >= 0.1, the data is stationary at 10% level of
significance
Differencing: A method for making series stationary
A differenced series is the series of difference between each observation 𝑌𝑡 and the
previous observation 𝑌𝑡−1
𝑌𝑡′ = 𝑌𝑡 − 𝑌𝑡−1
R Code
>mydata = ts(GDP$GDP)
> plot(mydata, type = "b")
Conclusion
Series has a linear trend
KPSS test (p value < 0.05) shows data is not stationary
Differencing: A method for making data stationary
R Code
install.packages("forecast")
library(forecast)
ndiffs(GDP)
Differencing required is 1
Yt’ = Yt – Yt-1
R code
Reading and plotting the data
mydata <- read.csv("Amount.csv")
amount = ts(mydata$Amount)
plot(amount, type ="b")
Example: The data on ad revenue from an advertising agency for the last 12 months is
given below. Forecast the ad revenue from the agency in the future month
using single exponential smoothing method with best value of ?
R code
Checking whether series is stationary
library(forecast)
adf.test(amount)
kpss.test(amount)
R code
Fitting the model
mymodel = HoltWinters(amount, beta = FALSE, gamma = FALSE)
mymodel
R code
Example: The data on ad revenue from an advertising agency for the last 12 months is
given below. Forecast the ad revenue from the agency in the future month
using single exponential smoothing method with best value of ?
Model diagnostics – R Code
abs_res = abs(res)
res_sq = res^2
pae = abs_res/ amount
50
Example: The data on ad revenue from an advertising agency for the last 12 months is
given below. Forecast the ad revenue from the agency in the future month
using single exponential smoothing method with best value of ?
Model diagnostics
Criteria
Prediction Interval z
90% 1.645
95% 1.960
99% 2.576
R Code
mydata <- read.csv("Trens_GDP.csv")
GDP <- ts(mydata$GDP, start = 1993, end = 2003)
acf(GDP, 3)
acf(GDP)
Auto Regressive Integrated Moving Average Models (ARIMA (p,d,q))
General Form
yt = c + 1yt-1+ 2yt-2 + - - - + 1et-1+ + 2et-2 - - - -
Where
c: constant
1, 2, 1, 2 , - - - are model parameters
et-1 = yt-1 – st-1, et are called errors or residuals
st-1 : predicted value for the t-1th observation (yt-1)
Auto Regressive Integrated Moving Average Models (ARIMA (p,d,q))
Step 1:
Draw time series plot and check for trend, seasonality, etc
Step 2:
Draw Auto Correlation Function (ACF) and Partially Auto Correlation Function
(PACF) graphs to identify auto correlation structure of the series
Step 3:
Check whether the series is stationary using unit root test (ADF test, KPSS test)
If series is non stationary do differencing or transform the series
Auto Regressive Integrated Moving Average Models (ARIMA (p,d,q))
Step 4:
Identify the model using ACF and PACF or automatically
The best model is one which minimizes AIC or BIC or both
Step 5:
Estimate the model parameters using maximum likelihood method (MLE)
Auto Regressive Integrated Moving Average Models (ARIMA (p,d,q))
Step 6:
Do model diagnostic checks
The errors or residuals should be white noise and should not be auto
correlated
Do Portmanteau and Ljung & Box tests. If p value > 0.05, then there is no
autocorrelation in residuals and residuals are purely white noise.
The model is a good fit
Auto Regressive Integrated Moving Average Models (ARIMA (p,d,q))
Statistic Value
Minimum 45
Quartile 1 144.5
Median 271.5
Mean 243.6
Quartile 3 313
Maximum 417
Auto Regressive Integrated Moving Average Models (ARIMA (p,d,q))
Potential Models
ARMA(1,0) since acf at lag 1 is crossing 95% confidence interval
ARMA(0,1) since pacf at lag 1 is crossing 95% confidence interval
ARMA(1,1) since both acf and pacf at lag 1 is crossing 95% confidence interval
Auto Regressive Integrated Moving Average Models (ARIMA (p,d,q))
Conclusion:
The best model which minimizes AIC & BIC is p=1, q=0 or ARIMA(1,0,0)
Identified automatically
Auto Regressive Integrated Moving Average Models (ARIMA (p,d,q))
Statistic p value
0.96445 0.4004
Ljung-Box Test
Since the p value 0.05, The residuals are not auto correlated
The residuals are white noise
Auto Regressive Integrated Moving Average Models (ARIMA (p,d,q))
https://arxiv.org/abs/2010.05079