0% found this document useful (0 votes)
11 views38 pages

Decomposition

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views38 pages

Decomposition

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Exploratory Data Analysis: Time Series

Decomposition
Time Series and Forecasting

Maria Eduarda Silva

Faculdade de Economia, Universidade do Porto

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 1 / 38
Outline

1 Decomposition and filtering

2 Time Series Decomposition

3 The difference filter

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 2 / 38
Section 1

Decomposition and filtering

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 3 / 38
Decomposition and filtering
Depending on the purpose of our study, our interest may be
the trend - long-term variations (not necessarily linear)
the seasonal component - oscillations with fixed period associated with
calendar efects
cycles - oscillations with periods not associated with calendar effects
the random component
all together
Approaches
I Decomposition methods - estimate the trend /seasonal component
F with deterministic functions
F smooth functions- moving averages
F STL
I Filtering methods - Difference and Seasonal Difference

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 4 / 38
Section 2

Time Series Decomposition

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 5 / 38
Time Series Decomposition

Trend-Cycle- aperiodic changes in level over time +Seasonal - (almost)


periodic changes in level due to seasonal factors
Additive decomposition

yt = Tt + St + Rt

Multiplicative decomposition

yt = Tt × St × Rt =⇒ log(yt ) = log(Tt ) + log(St ) + log(Rt )

yt data at time t
Tt trend-cycle component at time t
St seasonal component at time t
Rt remainder component at time t

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 6 / 38
Classical Time Series decomposition

Calculate yhe Trend Tt (moving average, polinomial regression . . . .)


Remove the trend: yt − Tt
Calculate the seasonal component St ( seasonal indices)
Calculate the residuals series Rt = yt − Tt − St

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 7 / 38
Example - classical decomposition
420
400
380
co2

360
340
320

1960 1970 1980 1990 2000 2010 2020

Time

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 8 / 38
Example of Classical Decomposition

Decomposition of additive time series


observed
400
360
400 320
trend
360
3320
seasonal
1
−1
1.0 −3
random
0.0
−1.0

1960 1970 1980 1990 2000 2010 2020

Time

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 9 / 38
The components

## List of 6
## $ x : Time-Series [1:764] from 1959 to 2023: 316 316
## $ seasonal: Time-Series [1:764] from 1959 to 2023: 0.0861
## $ trend : Time-Series [1:764] from 1959 to 2023: NA NA N
## $ random : Time-Series [1:764] from 1959 to 2023: NA NA N
## $ figure : num [1:12] 0.0861 0.7221 1.4505 2.5939 3.0435
## $ type : chr "additive"
## - attr(*, "class")= chr "decomposed.ts"

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 10 / 38
The trend
420
400
380
out$trend

360
340
320

1960 1970 1980 1990 2000 2010 2020

Time

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 11 / 38
The seasonal component
3
2
1
out$seasonal

0
−1
−2
−3

1960 1970 1980 1990 2000 2010 2020

Time

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 12 / 38
Limitations of classical decomposition

Assumes de seasonal component to be the same over time- from year


to year
not robust to usual values

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 13 / 38
STL decomposition (Seasonal-Trend decomposition
by Lowess/loess)

Loess/Lowess (Cleveland, 1979): robust local regression


Decomposition based on local robust regression
More robust and more flexible decomposition
Note: trend estimate unstable at the ennds of the series
Decomposes a time series into seasonal, trend and irregular
components using loess
Loess is a robust weighted regression smoothing method close to
nearest neighbour regression

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 14 / 38
STL in R
The Seasonal Decomposition of Time Series by Loess is implemented
in R in the stl() function
Parameters:
I t (in R: t.window) - the span of the loess window for trend extraction;
should be odd
I s (in R: s.window) - the span of the loess window for seasonal extraction;
should be odd and ≥ 7
I If s.window = “periodic” smoothing is effectively replaced by taking the
mean
Iterative process:
I The seasonal component is found by loess smoothing the seasonal
sub-series (the series of all January values, . . . )
I The seasonal values are removed, and the remainder smoothed to find
the trend
I The overall level is removed from the seasonal component and added to
the trend component. This process is iterated a few times
I The remainder component is the residuals from the seasonal plus trend
fit.
Maria Eduarda Silva (Faculdade de Economia,
Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 15 / 38
STL decomposition of Cardox - fixed periodicity
data
400
360
320

3
seasonal

1
−1
−3
400
trend
360
320
remainder

0.5
−0.5
1960 1970 1980 1990 2000 2010 2020

time

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 16 / 38
STL decomposition of Cardox - not fixed periodicity
data
400
360
320

3
seasonal

1
−1
−3
400
trend
360
320
remainder

0.5
−0.5
1960 1970 1980 1990 2000 2010 2020

time

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 17 / 38
STL components

head(cardox.stlper$time.series)

## seasonal trend remainder


## Mar 1958 1.4039091 314.8115 -0.50536160
## Apr 1958 2.5512431 314.9140 -0.01528735
## May 1958 2.9864460 315.0166 -0.50308202
## Jun 1958 2.2977101 315.1059 -0.30364134
## Jul 1958 0.6929085 315.1952 -0.02813505
## Aug 1958 -1.4385111 315.2765 1.09200191

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 18 / 38
Investigate the correlation behaviour of the
remainder.
cardox.stl$time.series[, 3](t)

cardox.stl$time.series[, 3](t)

cardox.stl$time.series[, 3](t)
0.22 0.02 −0.14
0.5

0.5

0.5
−0.5

−0.5

−0.5
−0.5 0.0 0.5 1.0 −0.5 0.0 0.5 1.0 −0.5 0.0 0.5 1.0
cardox.stl$time.series[, 3](t)

cardox.stl$time.series[, 3](t)

cardox.stl$time.series[, 3](t)
cardox.stl$time.series[, 3](t−1) cardox.stl$time.series[, 3](t−2) cardox.stl$time.series[, 3](t−3)

−0.17 −0.09 −0.07


0.5

0.5

0.5
−0.5

−0.5

−0.5
−0.5 0.0 0.5 1.0 −0.5 0.0 0.5 1.0 −0.5 0.0 0.5 1.0
cardox.stl$time.series[, 3](t)

cardox.stl$time.series[, 3](t)

cardox.stl$time.series[, 3](t)
cardox.stl$time.series[, 3](t−4) cardox.stl$time.series[, 3](t−5) cardox.stl$time.series[, 3](t−6)

−0.04 0.00 0.05


0.5

0.5

0.5
−0.5

−0.5

−0.5
−0.5 0.0 0.5 1.0 −0.5 0.0 0.5 1.0 −0.5 0.0 0.5 1.0
cardox.stl$time.series[, 3](t)

cardox.stl$time.series[, 3](t)

cardox.stl$time.series[, 3](t)
cardox.stl$time.series[, 3](t−7) cardox.stl$time.series[, 3](t−8) cardox.stl$time.series[, 3](t−9)

−0.05 −0.07 −0.17


0.5

0.5

0.5
−0.5

−0.5

−0.5
−0.5 0.0 0.5 1.0 −0.5 0.0 0.5 1.0 −0.5 0.0 0.5 1.0
cardox.stl$time.series[, 3](t−10) cardox.stl$time.series[, 3](t−11) cardox.stl$time.series[, 3](t−12)

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 19 / 38
STL decomposition

Versatile and Robust


Seasonal component allowed to change overtime and rate of change
controllled by the user
Smoothness of trend-cycle controlled by the user
Optionally robust to outliers
No calendar adjustments
Only additive
You need to log the data if multiplicative decomposition is needed

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 20 / 38
Section 3

The difference filter

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 21 / 38
The difference filter

xt denotes the observation at time t


B is called the backshift or lag operator: Bxt = xt−1
B 12 xt = xt−12
∇ = 1 − B the difference operator:
∇xt = (1 − B)xt = xt − Bxt = xt − xt−1

yt = ∇xt = xt − xt−1
represents the increments or change of the variable x on consecutive
time points
Applying the difference operator is said Differencing and can help
stabilise the mean of a time series by removing changes in the level of
a time series and therefore eliminating (or reducing) trend

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 22 / 38
The difference operator applied to the Monthly
Price of Chicken
cents per pound
90
60

2005 2010 2015


Time
cents per pound

Price of chicken differenced


1 3
−2

2005 2010 2015


Time
Maria Eduarda Silva (Faculdade de Economia,
Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 23 / 38
Seasonal difference operator

Seasonal difference operator ∇S = 1 − B S , where S is the seasonality


yt = ∇S xt = xt − xt−S represents the increments or change of the
variable x over consecutive seasonal periods.
If S = 12 then yt represents the increments from one year to the next
Applying the seasonal difference operator can help stabilise the mean
of a time series by removing changes in the level due to seasonality

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 24 / 38
SOI (Southern Oscillation Index)

Southern Oscillation Index


0.5
−1.0

1950 1960 1970 1980


0.5
soi
−1.0

J F M A M J J A S O N D

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 25 / 38
SOI - lagplots
soi(t)

soi(t)

soi(t)
0.60 0.37 0.21
−1.0

−1.0

−1.0
−1.0 −0.5 0.0 0.5 1.0 −1.0 −0.5 0.0 0.5 1.0 −1.0 −0.5 0.0 0.5 1.0
soi(t−1) soi(t−2) soi(t−3)
soi(t)

soi(t)

soi(t)
0.05 −0.11 −0.19
−1.0

−1.0

−1.0
−1.0 −0.5 0.0 0.5 1.0 −1.0 −0.5 0.0 0.5 1.0 −1.0 −0.5 0.0 0.5 1.0
soi(t−4) soi(t−5) soi(t−6)
soi(t)

soi(t)

soi(t)
−0.18 −0.10 0.05
−1.0

−1.0

−1.0
−1.0 −0.5 0.0 0.5 1.0 −1.0 −0.5 0.0 0.5 1.0 −1.0 −0.5 0.0 0.5 1.0
soi(t−7) soi(t−8) soi(t−9)
soi(t)

soi(t)

soi(t)
0.22 0.36 0.41
−1.0

−1.0

−1.0
−1.0 −0.5 0.0 0.5 1.0 −1.0 −0.5 0.0 0.5 1.0 −1.0 −0.5 0.0 0.5 1.0
soi(t−10) soi(t−11) soi(t−12)

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 26 / 38
Seasonaly differenced SOI (Southern Oscillation
Index)
Southern Oscillation Index seasonally differenced
0.5
−1.5

1950 1960 1970 1980


diff(soi, 12)
0.5
−1.5

J F M A M J J A S O N D

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 27 / 38
Seasonaly differenced SOI (Southern Oscillation
Index) - lagplots
diff(soi, 12)(t) diff(soi, 12)(t) diff(soi, 12)(t) diff(soi, 12)(t)

diff(soi, 12)(t) diff(soi, 12)(t) diff(soi, 12)(t) diff(soi, 12)(t)

diff(soi, 12)(t) diff(soi, 12)(t) diff(soi, 12)(t) diff(soi, 12)(t)


0.45 0.36 0.38
−1.5

−1.5

−1.5
−1.5 −0.5 0.5 1.0 −1.5 −0.5 0.5 1.0 −1.5 −0.5 0.5 1.0
diff(soi, 12)(t−1) diff(soi, 12)(t−2) diff(soi, 12)(t−3)

0.33 0.22 0.17


−1.5

−1.5

−1.5
−1.5 −0.5 0.5 1.0 −1.5 −0.5 0.5 1.0 −1.5 −0.5 0.5 1.0
diff(soi, 12)(t−4) diff(soi, 12)(t−5) diff(soi, 12)(t−6)

0.08 −0.04 −0.07


−1.5

−1.5

−1.5
−1.5 −0.5 0.5 1.0 −1.5 −0.5 0.5 1.0 −1.5 −0.5 0.5 1.0
diff(soi, 12)(t−7) diff(soi, 12)(t−8) diff(soi, 12)(t−9)

−0.06 −0.15 −0.44


−1.5

−1.5

−1.5
−1.5 −0.5 0.5 1.0 −1.5 −0.5 0.5 1.0 −1.5 −0.5 0.5 1.0
diff(soi, 12)(t−10) diff(soi, 12)(t−11) diff(soi, 12)(t−12)

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 28 / 38
Differencing time series with trend and seasonality
cardox(t)

cardox(t)

cardox(t)
1.00 0.99 0.98
320

320

320
320 360 400 320 360 400 320 360 400
cardox(t−1) cardox(t−2) cardox(t−3)
cardox(t)

cardox(t)

cardox(t)
0.98 0.97 0.97
320

320

320
320 360 400 320 360 400 320 360 400
cardox(t−4) cardox(t−5) cardox(t−6)
cardox(t)

cardox(t)

cardox(t)
0.96 0.96 0.96
320

320

320
320 360 400 320 360 400 320 360 400
cardox(t−7) cardox(t−8) cardox(t−9)
cardox(t)

cardox(t)

cardox(t)
0.95 0.95 0.95
320

320

320
320 360 400 320 360 400 320 360 400
cardox(t−10) cardox(t−11) cardox(t−12)

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 29 / 38
Simple difference of CARDOX
1 simple difference of Cardox
2
diff(cardox, 1)
1
0
−1
−2

1960 1970 1980 1990 2000 2010 2020


Time

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 30 / 38
Cardox simple differenced - lagplots
diff(cardox, 1)(t)diff(cardox, 1)(t)diff(cardox, 1)(t)diff(cardox, 1)(t)

diff(cardox, 1)(t)diff(cardox, 1)(t)diff(cardox, 1)(t)diff(cardox, 1)(t)

diff(cardox, 1)(t)diff(cardox, 1)(t)diff(cardox, 1)(t)diff(cardox, 1)(t)


0.70 0.24 −0.21
−2

−2

−2
−2 −1 0 1 2 −2 −1 0 1 2 −2 −1 0 1 2
diff(cardox, 1)(t−1) diff(cardox, 1)(t−2) diff(cardox, 1)(t−3)

−0.46 −0.51 −0.50


−2

−2

−2
−2 −1 0 1 2 −2 −1 0 1 2 −2 −1 0 1 2
diff(cardox, 1)(t−4) diff(cardox, 1)(t−5) diff(cardox, 1)(t−6)

−0.51 −0.45 −0.19


−2

−2

−2
−2 −1 0 1 2 −2 −1 0 1 2 −2 −1 0 1 2
diff(cardox, 1)(t−7) diff(cardox, 1)(t−8) diff(cardox, 1)(t−9)

0.24 0.71 0.92


−2

−2

−2
−2 −1 0 1 2 −2 −1 0 1 2 −2 −1 0 1 2
diff(cardox, 1)(t−10) diff(cardox, 1)(t−11) diff(cardox, 1)(t−12)

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 31 / 38
Differencing time series with trend and seasonality -
CARDOX

Simple differences, changes in carbon dioxide from month to month, show a


seasonal cycle

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 32 / 38
Seasonal differenced - CARDOX
Seasonal difference of Cardox
4
diff(cardox, 12)
3
2
1
0

1960 1970 1980 1990 2000 2010 2020


Time

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 33 / 38
Cardox seasonally differenced - lagplots diff(cardox, 12)(t)

diff(cardox, 12)(t)

diff(cardox, 12)(t)
0 3

0 3

0 3
0.83 0.77 0.71

0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
diff(cardox, 12)(t)

diff(cardox, 12)(t)

diff(cardox, 12)(t)
diff(cardox, 12)(t−1) diff(cardox, 12)(t−2) diff(cardox, 12)(t−3)
0 3

0 3

0 3
0.68 0.65 0.60

0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
diff(cardox, 12)(t)

diff(cardox, 12)(t)

diff(cardox, 12)(t)
diff(cardox, 12)(t−4) diff(cardox, 12)(t−5) diff(cardox, 12)(t−6)
0 3

0 3

0 3
0.56 0.52 0.48

0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
diff(cardox, 12)(t)

diff(cardox, 12)(t)

diff(cardox, 12)(t)
diff(cardox, 12)(t−7) diff(cardox, 12)(t−8) diff(cardox, 12)(t−9)
0 3

0 3

0 3
0.40 0.35 0.24

0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
diff(cardox, 12)(t−10) diff(cardox, 12)(t−11) diff(cardox, 12)(t−12)

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 34 / 38
Differencing time series with trend and seasonality

Seasonal differences, changes in carbon dioxide from one month to the same
month next year , show some trend

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 35 / 38
Differencing time series with trend and seasonality
diff(diff(cardox, 12), 1)

Seasonal and simple differenced


−1.5 0.5

1960 1970 1980 1990 2000 2010 2020


Time

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 36 / 38
Cardox simple and seasonally differenced - lagplots 12), 1)(t)

12), 1)(t)

12), 1)(t)
diff(diff(cardox,

diff(diff(cardox,

diff(diff(cardox,
−0.32 0.01 −0.08
−1.5

−1.5

−1.5
12), 1)(t)

12), 1)(t)

12), 1)(t)
−1.5 −0.5 0.5 1.5 −1.5 −0.5 0.5 1.5 −1.5 −0.5 0.5 1.5
diff(diff(cardox, 12), 1)(t−1) diff(diff(cardox, 12), 1)(t−2) diff(diff(cardox, 12), 1)(t−3)
diff(diff(cardox,

diff(diff(cardox,

diff(diff(cardox,
−0.02 0.06 −0.02
−1.5

−1.5

−1.5
12), 1)(t)

12), 1)(t)

12), 1)(t)
−1.5 −0.5 0.5 1.5 −1.5 −0.5 0.5 1.5 −1.5 −0.5 0.5 1.5
diff(diff(cardox, 12), 1)(t−4) diff(diff(cardox, 12), 1)(t−5) diff(diff(cardox, 12), 1)(t−6)
diff(diff(cardox, diff(diff(cardox,

diff(diff(cardox, diff(diff(cardox,

diff(diff(cardox, diff(diff(cardox,
0.00 −0.01 0.11
−1.5

−1.5

−1.5
12), 1)(t)

12), 1)(t)

12), 1)(t)
−1.5 −0.5 0.5 1.5 −1.5 −0.5 0.5 1.5 −1.5 −0.5 0.5 1.5
diff(diff(cardox, 12), 1)(t−7) diff(diff(cardox, 12), 1)(t−8) diff(diff(cardox, 12), 1)(t−9)

−0.07 0.16 −0.46


−1.5

−1.5

−1.5
−1.5 −0.5 0.5 1.5 −1.5 −0.5 0.5 1.5 −1.5 −0.5 0.5 1.5
diff(diff(cardox, 12), 1)(t−10) diff(diff(cardox, 12), 1)(t−11) diff(diff(cardox, 12), 1)(t−12)

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 37 / 38
Differencing time series with trend and seasonality

Be careful with over-differencing - we will see (algebrically) the results


of overdifferencing
When we apply both operators, simple and seasonal the filtered data
data no longer presents trend or seasonality.
Why is this important?

Maria Eduarda Silva (Faculdade de Economia,


Exploratory
Universidade
Datado
Analysis:
Porto) Time Series Decomposition 38 / 38

You might also like