You are on page 1of 10

Harmonic Seasonal Models

YIK LUN, KEI


allen29@ucla.edu
allenkei.weebly.com

Example 1
(1)Simulation
set.seed(1)
TIME <- 1:(10 * 12)
w <- rnorm(10 * 12, sd = 0.5)

10

15

Trend <- 0.1 + 0.005 * TIME + 0.001 * TIME^2


Seasonal <- sin(2*pi*TIME/12) +
0.2*sin(2*pi*2*TIME/12) +
0.1*sin(2*pi*4*TIME/12) +
0.1*cos(2*pi*4*TIME/12)
x <- Trend + Seasonal + w;plot(x, type = "l")

20

40

60
Index

80

100

120

(2)Fit to simulated series


SIN <- COS <- matrix(nr = length(TIME), nc = 6)
for (i in 1:6) {
COS[, i] <- cos(2 * pi * i * TIME/12)
SIN[, i] <- sin(2 * pi * i * TIME/12)
}
x.lm1 <- lm(x ~ TIME + I(TIME^2) + COS[, 1] + SIN[, 1] +
COS[, 2] + SIN[, 2] + COS[, 3] + SIN[, 3] + COS[, 4] +
SIN[, 4] + COS[, 5] + SIN[, 5] + COS[, 6] + SIN[, 6])
summary(x.lm1)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

Call:
lm(formula = x ~ TIME + I(TIME^2) + COS[, 1] + SIN[, 1] + COS[,
2] + SIN[, 2] + COS[, 3] + SIN[, 3] + COS[, 4] + SIN[, 4] +
COS[, 5] + SIN[, 5] + COS[, 6] + SIN[, 6])
Residuals:
Min
1Q
-1.0782 -0.2871

Median
0.0045

3Q
0.2547

Max
1.1128

Coefficients:

Estimate Std. Error t value Pr(>|t|)


(Intercept) 1.558e-01 1.258e-01
1.239 0.218080
TIME
5.392e-03 4.793e-03
1.125 0.263112
I(TIME^2)
9.951e-04 3.837e-05 25.933 < 2e-16 ***
COS[, 1]
1.908e-02 5.822e-02
0.328 0.743761
SIN[, 1]
9.015e-01 5.838e-02 15.442 < 2e-16 ***
COS[, 2]
-3.008e-02 5.845e-02 -0.515 0.607890
SIN[, 2]
2.025e-01 5.875e-02
3.447 0.000817 ***
COS[, 3]
1.350e-02 5.826e-02
0.232 0.817147
SIN[, 3]
-4.102e-02 5.837e-02 -0.703 0.483774
COS[, 4]
1.328e-02 5.835e-02
0.228 0.820378
SIN[, 4]
6.131e-02 5.825e-02
1.053 0.294981
COS[, 5]
-6.785e-02 5.900e-02 -1.150 0.252713
SIN[, 5]
5.063e-02 5.906e-02
0.857 0.393199
COS[, 6]
-1.473e-02 4.753e-02 -0.310 0.757167
SIN[, 6]
1.080e+12 2.823e+12
0.382 0.702950
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.4509 on 105 degrees of freedom
Multiple R-squared: 0.9912, Adjusted R-squared:
0.99
F-statistic: 846.7 on 14 and 105 DF, p-value: < 2.2e-16

x.lm2 <- lm(x ~ I(TIME^2) + SIN[, 1] + SIN[, 2])


summary(x.lm2)
##
## Call:
2

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

lm(formula = x ~ I(TIME^2) + SIN[, 1] + SIN[, 2])


Residuals:
Min
1Q Median
-1.2741 -0.3093 -0.0151

3Q
0.3074

Max
1.2774

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.804e-01 6.059e-02
4.628 9.69e-06 ***
I(TIME^2)
1.036e-03 9.325e-06 111.144 < 2e-16 ***
SIN[, 1]
9.002e-01 5.702e-02 15.786 < 2e-16 ***
SIN[, 2]
1.989e-01 5.690e-02
3.495 0.000673 ***
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.4405 on 116 degrees of freedom
Multiple R-squared: 0.9907, Adjusted R-squared: 0.9905
F-statistic: 4137 on 3 and 116 DF, p-value: < 2.2e-16

AIC(x.lm1);AIC(x.lm2)
## [1] 165.3527
## [1] 149.7256
plot(x, type = "l")
lines(TIME,predict(x.lm2),col="red")

15
10
0

20

40

60

80

100

120

Index

Example 2: Resid = 0.0001095086 + 0.4448 et1 + 0.2612 et2 + z


Global <- scan("http://staff.elena.aut.ac.nz/Paul-Cowpertwait/ts/global.dat")
Global.ts <- ts(Global, st = c(1856, 1), end = c(2005,12), fr = 12)
temp <- window(Global.ts, start = 1970)
plot(temp,type="l")

0.8
0.6
0.4
0.2
0.4

0.0

temp

1970

1975

1980

1985

1990

1995

Time

SIN <- COS <- matrix(nr = length(temp), nc = 6)


TIME <- (time(temp) - mean(time(temp)))/sd(time(temp))
for (i in 1:6) {
COS[, i] <- cos(2 * pi * i * TIME)
SIN[, i] <- sin(2 * pi * i * TIME)
}
temp.lm1 <- lm(temp
COS[,1] + SIN[,1] +
COS[,3] + SIN[,3] +
COS[,5] + SIN[,5] +
summary(temp.lm1)
##
##
##
##
##
##
##
##
##
##

~ TIME + I(TIME^2) +
COS[,2] + SIN[,2] +
COS[,4] + SIN[,4] +
COS[,6] + SIN[,6])

Call:
lm(formula = temp ~ TIME + I(TIME^2) + COS[, 1] + SIN[, 1] +
COS[, 2] + SIN[, 2] + COS[, 3] + SIN[, 3] + COS[, 4] + SIN[,
4] + COS[, 5] + SIN[, 5] + COS[, 6] + SIN[, 6])
Residuals:
Min
1Q
-0.34970 -0.06080

Median
0.01032

3Q
0.06051

Max
0.30392

2000

2005

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

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.166498
0.007653 21.755 < 2e-16 ***
TIME
0.182793
0.005096 35.868 < 2e-16 ***
I(TIME^2)
0.008240
0.005955
1.384 0.167202
COS[, 1]
0.011978
0.007500
1.597 0.110981
SIN[, 1]
0.031247
0.007244
4.313 2.01e-05 ***
COS[, 2]
0.029817
0.007241
4.118 4.61e-05 ***
SIN[, 2]
0.011700
0.007214
1.622 0.105621
COS[, 3]
0.058641
0.007206
8.138 4.67e-15 ***
SIN[, 3]
-0.041532
0.007241 -5.736 1.87e-08 ***
COS[, 4]
0.043672
0.007236
6.035 3.51e-09 ***
SIN[, 4]
0.016601
0.007181
2.312 0.021271 *
COS[, 5]
0.024563
0.007184
3.419 0.000690 ***
SIN[, 5]
-0.025692
0.007233 -3.552 0.000426 ***
COS[, 6]
0.021111
0.007201
2.932 0.003556 **
SIN[, 6]
0.005659
0.007151
0.791 0.429214
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.1049 on 417 degrees of freedom
Multiple R-squared: 0.7859, Adjusted R-squared: 0.7787
F-statistic: 109.3 on 14 and 417 DF, p-value: < 2.2e-16

temp.lm2 <- lm(temp ~ TIME+SIN[,1]+COS[,2]+COS[,3]+SIN[,3]+COS[,4]+SIN[,5])


summary(temp.lm2)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

Call:
lm(formula = temp ~ TIME + SIN[, 1] + COS[, 2] + COS[, 3] + SIN[,
3] + COS[, 4] + SIN[, 5])
Residuals:
Min
1Q
-0.34410 -0.06407

Median
0.01111

3Q
0.06032

Max
0.32233

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.173416
0.005191 33.410 < 2e-16 ***
TIME
0.183225
0.005195 35.272 < 2e-16 ***
SIN[, 1]
0.030436
0.007379
4.125 4.47e-05 ***
COS[, 2]
0.029933
0.007418
4.035 6.47e-05 ***
COS[, 3]
0.060070
0.007366
8.155 3.99e-15 ***
SIN[, 3]
-0.043848
0.007378 -5.943 5.83e-09 ***
COS[, 4]
0.041661
0.007395
5.634 3.22e-08 ***
SIN[, 5]
-0.027424
0.007371 -3.720 0.000226 ***
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.1078 on 424 degrees of freedom
Multiple R-squared: 0.7702, Adjusted R-squared: 0.7664
F-statistic:
203 on 7 and 424 DF, p-value: < 2.2e-16

0.2
0.4

0.0

temp

0.4

0.6

0.8

plot(TIME,temp,type="l")
lines(as.numeric(TIME),predict(temp.lm2),col="red",type="l")

1.5

1.0

0.5

0.0
TIME

acf(resid(temp.lm2))

0.5

1.0

1.5

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series resid(temp.lm2)

10

15
Lag

pacf(resid(temp.lm2)) #AR(2)

20

25

0.3
0.1

0.1

Partial ACF

0.5

Series resid(temp.lm2)

10

15

20

Lag

res.ar <- ar(resid(temp.lm2), method = "mle")


res.ar$order
## [1] 2
Box.test(resid(temp.lm2),lag=log(length(resid(temp.lm2))),type='Ljung')
##
## Box-Ljung test
##
## data: resid(temp.lm2)
## X-squared = 419.85, df = 6.0684, p-value < 2.2e-16
m1=arima(resid(temp.lm2),order=c(2,0,0))
m1
##
##
##
##
##
##
##
##

Call:
arima(x = resid(temp.lm2), order = c(2, 0, 0))
Coefficients:
ar1
ar2
0.4448 0.2612
s.e. 0.0466 0.0468

intercept
0.0004
0.0134
9

25

##
## sigma^2 estimated as 0.006804:

log likelihood = 464.62,

aic = -921.24

which((1-pnorm(abs(m1$coef)/sqrt(diag(m1$var.coef))))*2 > 0.05)


## intercept
##
3

Constant Term for Residual Model(?)


(1-m1$coef[1]-m1$coef[2])*m1$coef[3] # constant
##
ar1
## 0.0001095086

10

You might also like