Você está na página 1de 18

Practice 2 from Introductory Time Series with R

YIK LUN, KEI


allen29@ucla.edu
This paper is a practice from the book called Introductory Time Series with R
by Cowpertwait, Paul SP, and Andrew V. Metcalfe. All R codes and comments
below are belonged to the book and authors.

0
2

set.seed(1)
w <- rnorm(100)
plot(w, type = "l")

20

40

60
Index

x <- seq(-3,3, length = 1000)


hist(rnorm(100), prob = T)
points(x, dnorm(x), type = "l")

80

100

0.3
0.2
0.0

0.1

Density

0.4

0.5

Histogram of rnorm(100)

0
rnorm(100)

set.seed(2)
acf(rnorm(100))

0.2 0.4 0.6 0.8 1.0


0.2

ACF

Series rnorm(100)

10
Lag

x <- w <- rnorm(1000)


for (t in 2:1000) x[t] <- x[t - 1] + w[t]
plot(x, type = "l")

15

20

80
60
40
0

20

200

400

600
Index

acf(x)

800

1000

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series x

10

15
Lag

acf(diff(x))

20

25

30

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series diff(x)

10

15

20

25

Lag

www <- "http://staff.elena.aut.ac.nz/Paul-Cowpertwait/ts/pounds_nz.dat"


Z <- read.table(www, header = T)
Z.ts <- ts(Z, st = 1991, fr = 4)
plot(Z.ts,xlab = "years",ylab = "Quarterly exchange rate in $NZ / pound")

30

1992
1994
1996

years

acf(diff(Z.ts))

1998
2000

2.2 2.4 2.6 2.8 3.0 3.2 3.4

Quarterly exchange rate in $NZ / pound

0.2 0.4 0.6 0.8 1.0


0.2

ACF

xrate

2
Lag

Z.hw <- HoltWinters(Z.ts, alpha = 1, gamma = FALSE)


acf(resid(Z.hw))

0.2 0.4 0.6 0.8 1.0


0.2

ACF

object$x

2
Lag

Z.hw$alpha

## [1] 1
Z.hw$beta
## [1] 0.167018
www <- "http://staff.elena.aut.ac.nz/Paul-Cowpertwait/ts/HP.txt"
HP.dat <- read.table(www, header = T) ; attach(HP.dat)
plot (as.ts(Price))

45
40
35
30
20

25

as.ts(Price)

100

200

300
Time

DP <- diff(Price)
plot (as.ts(DP))

10

400

500

600

3
2
1
0
2

as.ts(DP)

100

200

300
Time

acf (DP)

11

400

500

600

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series DP

10

15
Lag

mean(DP) + c(-2, 2) * sd(DP)/sqrt(length(DP))


## [1] 0.004378275 0.075353468
rho <- function(k, alpha) alpha^k
plot(0:10, rho(0:10, 0.7), type = "b")

12

20

25

1.0
0.8
0.6
0.4
0.0

0.2

rho(0:10, 0.7)

6
0:10

plot(0:10, rho(0:10, -0.7), type = "b")

13

10

1.0
0.5
0.0
0.5

rho(0:10, 0.7)

6
0:10

set.seed(1)
x <- w <- rnorm(100)
for (t in 2:100) x[t] <- 0.7 * x[t - 1] + w[t]
plot(x, type = "l")

14

10

3
2
1
1

20

40

60
Index

acf(x)

15

80

100

0.4
0.2

0.2

ACF

0.6

0.8

1.0

Series x

10
Lag

pacf(x)

16

15

20

0.2
0.2

0.0

Partial ACF

0.4

0.6

Series x

10
Lag

x.ar <- ar(x, method = "mle")


x.ar$order
## [1] 1
x.ar$ar
## [1] 0.6009459
x.ar$ar + c(-2, 2) * sqrt(x.ar$asy.var)
## [1] 0.4404031 0.7614886
Z.ar <- ar(Z.ts)
mean(Z.ts)
## [1] 2.823251
Z.ar$order
## [1] 1

17

15

20

Z.ar$ar
## [1] 0.890261
Z.ar$ar + c(-2, 2) * sqrt(Z.ar$asy.var)

## [1] 0.7405097 1.0400123


acf(Z.ar$res[-1])

0.2 0.4 0.6 0.8 1.0


0.2

ACF

Series Z.ar$res[1]

10

15

Lag

Reference:
Cowpertwait, Paul SP, and Andrew V. Metcalfe. Introductory time
series withR. Springer Science & Business Media, 2009.

18

Você também pode gostar