Sie sind auf Seite 1von 14

Practice 1 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.

6000

10000

14000

www <- "http://elena.aut.ac.nz/~pcowpert/ts/ApprovActiv.dat"


Build.dat <- read.table(www, header=T) ; attach(Build.dat)
App.ts <- ts(Approvals, start = c(1996,1), freq=4)
Act.ts <- ts(Activity, start = c(1996,1), freq=4)
ts.plot(App.ts, Act.ts, lty = c(1,3))

1996

1998

2000

2002
Time

2004

2006

acf(ts.union(App.ts, Act.ts))

0.6
0.2

0.6

App.ts & Act.ts

0.2

ACF

App.ts

0.0 0.5 1.0 1.5 2.0 2.5 3.0

Act.ts & App.ts

Act.ts

0.2

0.6

Lag

0.6

Lag

0.2

ACF

0.0 0.5 1.0 1.5 2.0 2.5 3.0

3.0

2.0

1.0

0.0

0.0 0.5 1.0 1.5 2.0 2.5 3.0

Lag

Lag

app.ran <- decompose(App.ts)$random # generate NAs


app.ran.ts <- window (app.ran, start = c(1996, 3),end=c(2005,2) )
act.ran <- decompose (Act.ts)$random
act.ran.ts <- window (act.ran, start = c(1996, 3),end=c(2005,2) )
acf (ts.union(app.ran.ts, act.ran.ts))

0.5
0.5

0.5

app.ran.ts & act.ran.ts

0.5

ACF

app.ran.ts

0.5

1.0

1.5

2.0

2.5

3.0

0.0

0.5

1.0

1.5

2.0

act.ran.ts & app.ran.ts

act.ran.ts

2.5

3.0

2.5

3.0

0.5

0.5

Lag

0.5

Lag

0.5

ACF

0.0

3.0

2.0

1.0

0.0

0.0

Lag

0.5

1.0

1.5
Lag

ccf (app.ran.ts, act.ran.ts)

2.0

0.2
0.4

0.0

ACF

0.4

0.6

app.ran.ts & act.ran.ts

Lag

T79 <- 1:10


Tdelt <- (1:100) / 10
Sales <- c(840,1470,2110,4000, 7590, 10950, 10530, 9470, 7790, 5890)
Cusales <- cumsum(Sales)
Bass.nls <- nls(Sales ~ M * ( ((P+Q)^2 / P) * exp(-(P+Q) * T79) ) / (1+(Q/P)*exp(-(P+Q)*T79))^2, start =
summary(Bass.nls)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

Formula: Sales ~ M * (((P + Q)^2/P) * exp(-(P + Q) * T79))/(1 + (Q/P) *


exp(-(P + Q) * T79))^2
Parameters:
Estimate Std. Error t value Pr(>|t|)
M 6.798e+04 3.128e+03
21.74 1.10e-07
P 6.594e-03 1.430e-03
4.61 0.00245
Q 6.381e-01 4.140e-02
15.41 1.17e-06
--Signif. codes: 0 '***' 0.001 '**' 0.01

***
**
***
'*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 727.2 on 7 degrees of freedom


Number of iterations to convergence: 8
Achieved convergence tolerance: 7.338e-06

6000
2000

Sales per year

10000

Bcoef <- coef(Bass.nls)


m <- Bcoef[1]
p <- Bcoef[2]
q <- Bcoef[3]
ngete <- exp(-(p+q) * Tdelt)
Bpdf <- m * ( (p+q)^2 / p ) * ngete / (1 + (q/p) * ngete)^2
plot(Tdelt, Bpdf, xlab = "Year from 1979",
ylab = "Sales per year", type='l')
points(T79, Sales)

Year from 1979

Bcdf <- m * (1 - ngete)/(1 + (q/p)*ngete)


plot(Tdelt, Bcdf, xlab = "Year from 1979",
ylab = "Cumulative sales", type='l')
points(T79, Cusales)

10

50000
30000
0 10000

Cumulative sales

Year from 1979

www <- "http://elena.aut.ac.nz/~pcowpert/ts/motororg.dat"


Motor.dat <- read.table(www, header = T)
attach(Motor.dat)
Comp.ts <- ts(complaints, start = c(1996, 1), freq = 12)
plot(Comp.ts, xlab = "Time / months", ylab = "Complaints")

10

35
30
25
20
15
5

10

Complaints

1996

1997

1998

1999

2000

Time / months

Comp.hw1 <- HoltWinters(complaints, beta = FALSE, gamma = FALSE)


Comp.hw1
##
##
##
##
##
##
##
##
##
##
##
##
##

Holt-Winters exponential smoothing without trend and without seasonal component.


Call:
HoltWinters(x = complaints, beta = FALSE, gamma = FALSE)
Smoothing parameters:
alpha: 0.1429622
beta : FALSE
gamma: FALSE
Coefficients:
[,1]
a 17.70343

plot(Comp.hw1)

25
20
15
5

10

Observed / Fitted

30

35

HoltWinters filtering

10

20

30

40

Time
Comp.hw1$SSE
## [1] 2502.028
Comp.hw2 <- HoltWinters(complaints, alpha = 0.2, beta=FALSE, gamma=FALSE)
Comp.hw2
##
##
##
##
##
##
##
##
##
##
##
##
##

Holt-Winters exponential smoothing without trend and without seasonal component.


Call:
HoltWinters(x = complaints, alpha = 0.2, beta = FALSE, gamma = FALSE)
Smoothing parameters:
alpha: 0.2
beta : FALSE
gamma: FALSE
Coefficients:
[,1]
a 17.97913

Comp.hw2$SSE
## [1] 2526.39
8

100 200 300 400 500 600

sales (1000 litres)

www <- "http://elena.aut.ac.nz/~pcowpert/ts/wine.dat"


wine.dat <- read.table(www, header = T)
attach (wine.dat)
sweetw.ts <- ts(sweetw, start = c(1980,1), freq = 12)
plot(sweetw.ts, xlab= "Time (months)", ylab = "sales (1000 litres)")

1980

1985

1990

1995

Time (months)
sweetw.hw <- HoltWinters (sweetw.ts, seasonal = "mult")
sweetw.hw
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

Holt-Winters exponential smoothing with trend and multiplicative seasonal component.


Call:
HoltWinters(x = sweetw.ts, seasonal = "mult")
Smoothing parameters:
alpha: 0.4086698
beta : 0
gamma: 0.4929402
Coefficients:
[,1]
a
285.6890314
b
1.3509615
s1
0.9498541
s2
0.9767623
9

##
##
##
##
##
##
##
##
##
##

s3
s4
s5
s6
s7
s8
s9
s10
s11
s12

1.0275900
1.1991924
1.5463100
0.6730235
0.8925981
0.7557814
0.8227500
0.7241711
0.7434861
0.9472648

sweetw.hw$coef
##
a
## 285.6890314
##
s5
##
1.5463100
##
s11
##
0.7434861

b
1.3509615
s6
0.6730235
s12
0.9472648

s1
0.9498541
s7
0.8925981

s2
0.9767623
s8
0.7557814

sweetw.hw$SSE
## [1] 477693.9
sqrt(sweetw.hw$SSE/length(sweetw))
## [1] 50.54219
sd(sweetw)
## [1] 121.3908
plot (sweetw.hw$fitted)

10

s3
1.0275900
s9
0.8227500

s4
1.1991924
s10
0.7241711

0.8

1.2

season
0.8 1.2 1.6 100

trend
300

level
100 300 500

xhat

sweetw.hw$fitted

1985
1990

Time

plot (sweetw.hw)

11
1995

100 200 300 400 500 600

Observed / Fitted

HoltWinters filtering

1985

1990
Time

data(AirPassengers)
AP <- AirPassengers
AP.hw <- HoltWinters(AP, seasonal = "mult")
plot(AP.hw)

12

1995

500
400
300
100

200

Observed / Fitted

600

HoltWinters filtering

1950

1952

1954

1956
Time

AP.predict <- predict(AP.hw, n.ahead = 4 * 12)


ts.plot(AP, AP.predict, lty = 1:2)

13

1958

1960

700
500
300
100

1950

1955

1960

1965

Time

AP.hw$alpha; AP.hw$beta; AP.hw$gamma


##
alpha
## 0.2755925
##
beta
## 0.03269295
##
gamma
## 0.8707292

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

14

Das könnte Ihnen auch gefallen