Você está na página 1de 9

A short introduction to time series analysis in R

The key poi nt i n t i me ser i es anal ysi s i s t hat obser vat i ons t end t o
show ser i al t empor al aut ocor r el at i on t hat needs t o be account ed f or
i n st at i st i cal anal yses.

Example 1

For exampl e, l et s cr eat e a t i me ser i es of poi nt s r angi ng f r ompi t o
n*pi i n st eps or 0. 1:

my.ts=sin(seq(pi,10*pi,0.1))

0 50 100 150 200 250
-
1
.
0
-
0
.
5
0
.
0
0
.
5
1
.
0
Index
m
y
.
t
s



To t el l R t hat t hese dat a act ual l y r epr esent a time series, we need
t o r e- def i ne t he sequence t o be a t i me ser i es; l et us assume t hat
t he t i me ser i es st ar t s i n t he year 1800:

my.ts=ts(my.ts,start=1800)
plot(my.ts)

Time
m
y
.
t
s
1800 1850 1900 1950 2000 2050
-
1
.
0
-
0
.
5
0
.
0
0
.
5
1
.
0


Now we know exactly t hat t hese dat a poi nt s ar e per f ect l y
aut ocor r el at ed; t hi s means, f or each poi nt n i t i s known t hat t he
next ( n+k) poi nt s wi l l f ol l ow a si ne f unct i on.

Let us see what Rs bui l t - i n aut ocor r el at i on f unct i on t el l s us:

acf(my.ts,lag.max=100)



What we can see f r omt hi s pl ot i s t hat t he under l yi ng dat a-
gener at i ng si ne f unct i on i s cor r ect l y r epr esent ed i n t he
aut ocor r el at i on f unct i on. We do, however , see a sl i ght decl i ne i n
t he acf wi t h i ncr easi ng l ag due t o smal l pr edi ct i on er r or s. However ,
t hi s aut ocor r el at i on i s hi ghl y si gni f i cant ( i ndi cat ed by al l
ver t i cal l i nes cr ossi ng t he dashed hor i zont al ones) .
0 20 40 60 80 100
-
0
.
5
0
.
0
0
.
5
1
.
0
Lag
A
C
F
Series my.ts

What about t he numer i cal val ues of t hi s aut ocor r el at i on f unct i on?

acf(my.ts,lag.max=100,plot=F)

>Autocorrelations of series 'my.ts', by lag

0 1 2 3 4 5 6 7 8 9 10
1.000 0.995 0.980 0.955 0.922 0.879 0.828 0.768 0.702 0.629 0.551
11 12 13 14 15 16 17 18 19 20 21
0.467 0.380 0.289 0.196 0.102 0.007 -0.086 -0.179 -0.268 -0.355 -0.437
22 23 24 25 26 27 28 29 30 31 32
-0.514 -0.586 -0.651 -0.709 -0.760 -0.803 -0.837 -0.863 -0.880 -0.888 -0.887
33 34 35 36 37 38 39 40 41 42 43
-0.878 -0.860 -0.833 -0.798 -0.756 -0.707 -0.651 -0.589 -0.521 -0.449 -0.373
44 45 46 47 48 49 50 51 52 53 54
-0.294 -0.213 -0.131 -0.048 0.035 0.117 0.197 0.274 0.348 0.418 0.483
55 56 57 58 59 60 61 62 63 64 65
0.542 0.596 0.643 0.683 0.717 0.742 0.760 0.771 0.773 0.768 0.755
66 67 68 69 70 71 72 73 74 75 76
0.735 0.708 0.673 0.633 0.586 0.534 0.478 0.417 0.352 0.284 0.215
77 78 79 80 81 82 83 84 85 86 87
0.144 0.072 0.000 -0.072 -0.141 -0.209 -0.274 -0.336 -0.394 -0.447 -0.496
88 89 90 91 92 93 94 95 96 97 98
-0.538 -0.576 -0.607 -0.632 -0.650 -0.661 -0.666 -0.665 -0.657 -0.642 -0.621
99 100
-0.595 -0.562

The spect r um( ) f unct i on pr ovi des a spect r al decomposi t i on of our
t i me ser i es:


0.0 0.1 0.2 0.3 0.4 0.5
1
e
-
1
3
1
e
-
0
9
1
e
-
0
5
1
e
-
0
1
frequency
s
p
e
c
t
r
u
m
Series: x
Raw Periodogram
bandwidth =0.00100




The spect r umhas a maxi mumval ue at about 0. 03 ( whi ch i s on a l og
scal e) , f or whi ch al so a conf i dence est i mat e i s gi ven ( t he ver t i cal
bar t o t he r i ght ) .

I n summar y, t he per i odogr amshows t hat t he cycl es have a per i od of
about 1/ 0. 03=33 year s. Let us check i f t hi s appr oxi mat i on hol ds:

We know f r omt he autocorrelation function t hat t her e i s a l ocal
mi ni mumi n t he year 1816 ( r oughl y 0. 0) ; t he val ue at 1831 i s al most
per f ect l y negat i vel y cor r el at ed wi t h t he one f r om1800, and t he
cycl e appr oaches zer o agai n i n 1848. The next maxi mumi s i n 1863,
and so on. Thus, our aut ocor r el at i on f unct i on pr edi ct s a per i od of
about ( 1863- 1831) =32 year s.

Thi s i s wel l i n accor dance wi t h our above guess of 33 year s.

An i nspect i on of t he f i r st f ew val ues f r omt he 1817 onwar d gi ves:

par(tck=1)
plot(my.ts,type="l",xlim=c(1817,1851))

Time
m
y
.
t
s
1820 1825 1830 1835 1840 1845 1850
-
1
.
0
-
0
.
5
0
.
0
0
.
5
1
.
0


Let us now i nspect t he partial aut ocor r el at i on f unct i on:

plot(acf(my.ts,type="p"))

5 10 15 20
-
1
.
0
-
0
.
5
0
.
0
0
.
5
1
.
0
Lag
P
a
r
t
i
a
l

A
C
F
Series my.ts



acf(my.ts,type="p",plot=F)

>Partial autocorrelations of series 'my.ts', by lag

1 2 3 4 5 6 7 8 9 10 11
0.995 -0.985 -0.266 -0.075 -0.020 -0.004 0.001 0.002 0.003 0.003 0.002
12 13 14 15 16 17 18 19 20 21 22
0.002 0.002 0.001 0.001 0.001 0.000 0.000 -0.001 -0.001 -0.001 -0.002
23 24
-0.002 -0.002

We see t hat at l ags 2 and 3 t he par t i al acf i s si gni f i cant ,
but af t er t hat not hi ng mor e goes on.

One i mpor t ant t hi ng t o r emember her e i s t hat i t may be
di f f i cul t t o i nf er about mechani sms by l ooki ng at t he dynami cs
of a syst em. Thus, whi l e we can wel l i nspect t he dynami cal
behavi or of our t i me ser i es cycl es, mat hemat i cal t ool s woul d
be needed t o det ect t he under l yi ng f unct i on t hat has gener at ed
t he t i me ser i es.
______________________________________________________________

Example 2

Now l et us exami ne a t i me ser i es t hat has a l i near t r end. We
can cr eat e t he t r end ar t i f i ci al l y l i ke t hi s:

x=seq(pi,10*pi,0.1)
my.ts=0.1*x+sin(x)


We t hus have a si ne f unct i on t o whi ch a l i near f unct i on has
been added. The t wo component s ar e:

a) y=si n( x)




b) 0. 1*x



l et us now i nspect t he combi ned " pse


5 10 15 20 25 30
-1.0
-0.5
0.0
0.5
1.0
x
y

y=

5 10 15 20 25 30
0.5
1.0
1.5
2.0
2.5
3.0
x
y
So udo t i me ser i es" :


my.ts=0.1*x+sin(x)
my.ts=ts(my.ts,start=1800)
plot(my.ts)

Time
m
y
.
t
s
1800 1850 1900 1950 2000 2050
0
1
2
3

Fi r st of al l , we can agai n check f or aut ocor r el at i on i n t hi s


r t her , we coul d l ook at a spect r t i me


w l et us t est f or a l i near t r end ( of

t i me ser i es:

plot(acf(my.ts,lag.max=100))

Series my.ts

Fu al decomposi t i on of t he
ser i es:

spectrum(my.ts)



No i n t hi s t i me ser i es
whi ch we know i t i s t her e) :

model1=lm(my.ts~I(1801:2083))
summary(model1)

0 20 40 60 80 100
-0.2
0.0
0.2
0.4
0.6
0.8
1.0
Lag
A
C
F
Series: x
Raw Periodogram
0.0 0.1 0.2 0.3 0.4 0.5
1e-13
1e-09
1e-05
1e-01
frequency
s
p
e
c
t
r
u
m
bandwidth =0.00100
plot(my.ts)
abline(model1)




Fi nal l y, t he summary(model1) command gi ves:

Cal l :
l m( f or mul a = my. t s ~ I ( 1801: 2083) )

Resi dual s:
Mi n 1Q Medi an 3Q Max
- 0. 9295 - 0. 6860 - 0. 0790 0. 6985 1. 0706

Coef f i ci ent s:
Est i mat e St d. Er r or t val ue Pr ( >| t | )
( I nt er cept ) - 1. 776e+01 9. 981e- 01 - 17. 79 <2e- 16 ***
I ( 1801: 2083) 9. 995e- 03 5. 135e- 04 19. 46 <2e- 16 ***
- - -
Si gni f . codes: 0 ' ***' 0. 001 ' **' 0. 01 ' *' 0. 05 ' . ' 0. 1 ' ' 1

Resi dual st andar d er r or : 0. 7057 on 281 degr ees of f r eedom
Mul t i pl e R- Squar ed: 0. 5741, Adj ust ed R- squar ed: 0. 5726
F- st at i st i c: 378. 9 on 1 and 281 DF, p- val ue: < 2. 2e- 16


We concl ude t hat t he model has an i nt er cept of - 1. 77 and a
sl ope of 0. 01. Not e t hat t hese val ues ar e f or t he t i me ser i es
st ar t i ng at 1800.

Time
m
y
.
t
s
1800 1900 2000
0
1
2
3

Now, f i nal l y, l et us de- t r end t he t i me ser i es:

detrended=my.ts-
predict(model1)

plot(detrended)




Whi ch l eaves us back wi t h t he t wo component s we have used t o
cr eat e t he t i me ser i es i ni t i al l y.


Time
d
e
t
r
e
n
d
e
d
1800 1900 2000
-1.0
-0.5
0.0
0.5
1.0

Você também pode gostar