The R Project SVN R

Rev

Rev 20445 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 20445 Rev 30453
Line 1... Line 1...
1
library(ts)
1
library(ts)
2
.proctime00 <- proc.time()
2
.proctime00 <- proc.time()
3
 
3
 
4
### ar
4
### ar
5
data(lh)
-
 
6
ar(lh)
5
ar(lh)
7
ar(lh, method = "burg")
6
ar(lh, method = "burg")
8
ar(lh, method = "ols")
7
ar(lh, method = "ols")
9
ar(lh, FALSE, 4) # fit ar(4)
8
ar(lh, FALSE, 4) # fit ar(4)
10
ar.ols(lh)
9
ar.ols(lh)
11
ar.ols(lh, FALSE, 4) # fit ar(4) by OLS
10
ar.ols(lh, FALSE, 4) # fit ar(4) by OLS
12
 
11
 
13
data(LakeHuron)
-
 
14
ar(LakeHuron)
12
ar(LakeHuron)
15
ar(LakeHuron, method = "burg")
13
ar(LakeHuron, method = "burg")
16
ar(LakeHuron, method = "ols")
14
ar(LakeHuron, method = "ols")
17
ar(LakeHuron, method = "mle")
15
ar(LakeHuron, method = "mle")
18
 
16
 
19
data(sunspot)
-
 
20
ar(sunspot.year, method = "yw")
17
ar(sunspot.year, method = "yw")
21
ar(sunspot.year, method = "burg")
18
ar(sunspot.year, method = "burg")
22
ar(sunspot.year, method = "ols")
19
ar(sunspot.year, method = "ols")
23
ar(sunspot.year, method = "mle")
20
ar(sunspot.year, method = "mle")
24
 
21
 
25
 
22
 
26
### tests using presidents
-
 
27
data(presidents) # contains missing values
23
### tests using presidents, contains missing values
28
acf(presidents, na.action = na.pass)
24
acf(presidents, na.action = na.pass)
29
pacf(presidents, na.action = na.pass)
25
pacf(presidents, na.action = na.pass)
30
## graphs in example(acf) suggest order 1 or 3
26
## graphs in example(acf) suggest order 1 or 3
31
(fit1 <- arima(presidents, c(1, 0, 0)))
27
(fit1 <- arima(presidents, c(1, 0, 0)))
32
tsdiag(fit1)
28
tsdiag(fit1)
33
(fit3 <- arima(presidents, c(3, 0, 0)))  # smaller AIC
29
(fit3 <- arima(presidents, c(3, 0, 0)))  # smaller AIC
34
tsdiag(fit3)
30
tsdiag(fit3)
35
 
31
 
36
 
32
 
37
### tests of arima:
33
### tests of arima:
38
data(lh); data(USAccDeaths)
-
 
39
arima(USAccDeaths, order = c(0,1,1), seasonal = list(order=c(0,1,1)))
34
arima(USAccDeaths, order = c(0,1,1), seasonal = list(order=c(0,1,1)))
40
arima(USAccDeaths, order = c(0,1,1), seasonal = list(order=c(0,1,1)),
35
arima(USAccDeaths, order = c(0,1,1), seasonal = list(order=c(0,1,1)),
41
      method = "CSS") # drops first 13 observations.
36
      method = "CSS") # drops first 13 observations.
42
 
37
 
43
## test fitting with NAs
38
## test fitting with NAs
Line 68... Line 63...
68
arima(x = LakeHuron, order = c(2, 0, 0), xreg = trend,
63
arima(x = LakeHuron, order = c(2, 0, 0), xreg = trend,
69
      fixed = c(NA, NA, 580, 0))
64
      fixed = c(NA, NA, 580, 0))
70
 
65
 
71
 
66
 
72
### model selection from WWWusage
67
### model selection from WWWusage
73
data(WWWusage)
-
 
74
aics <- matrix(, 6, 6, dimnames=list(p=0:5, q=0:5))
68
aics <- matrix(, 6, 6, dimnames=list(p=0:5, q=0:5))
75
for(q in 1:5) aics[1, 1+q] <- arima(WWWusage, c(0,1,q),
69
for(q in 1:5) aics[1, 1+q] <- arima(WWWusage, c(0,1,q),
76
    optim.control = list(maxit = 500))$aic
70
    optim.control = list(maxit = 500))$aic
77
for(p in 1:5)
71
for(p in 1:5)
78
   for(q in 0:5) aics[1+p, 1+q] <- arima(WWWusage, c(p,1,q),
72
   for(q in 0:5) aics[1+p, 1+q] <- arima(WWWusage, c(p,1,q),
Line 80... Line 74...
80
round(aics - min(aics, na.rm=TRUE), 2)
74
round(aics - min(aics, na.rm=TRUE), 2)
81
 
75
 
82
 
76
 
83
 
77
 
84
### nottem
78
### nottem
85
data(nottem)
-
 
86
nott <- window(nottem, end=c(1936,12))
79
nott <- window(nottem, end=c(1936,12))
87
fit <- arima(nott,order=c(1,0,0), list(order=c(2,1,0), period=12))
80
fit <- arima(nott,order=c(1,0,0), list(order=c(2,1,0), period=12))
88
nott.fore <- predict(fit, n.ahead=36)
81
nott.fore <- predict(fit, n.ahead=36)
89
ts.plot(nott, nott.fore$pred, nott.fore$pred+2*nott.fore$se,
82
ts.plot(nott, nott.fore$pred, nott.fore$pred+2*nott.fore$se,
90
        nott.fore$pred-2*nott.fore$se, gpars=list(col=c(1,1,4,4)))
83
        nott.fore$pred-2*nott.fore$se, gpars=list(col=c(1,1,4,4)))
91
 
84
 
92
### StructTS
85
### StructTS
93
data(UKgas)
-
 
94
(fit <- StructTS(log10(UKgas), type = "BSM"))
86
(fit <- StructTS(log10(UKgas), type = "BSM"))
95
(fit <- StructTS(log10(UKgas), type = "BSM", fixed=c(0, NA, NA, NA)))
87
(fit <- StructTS(log10(UKgas), type = "BSM", fixed=c(0, NA, NA, NA)))
96
(fit <- StructTS(log10(UKgas), type = "BSM", fixed=c(NA, 0, NA, NA)))
88
(fit <- StructTS(log10(UKgas), type = "BSM", fixed=c(NA, 0, NA, NA)))
97
(fit <- StructTS(log10(UKgas), type = "BSM", fixed=c(NA, NA, NA, 0)))
89
(fit <- StructTS(log10(UKgas), type = "BSM", fixed=c(NA, NA, NA, 0)))
98
 
90
 
99
### from AirPassengers
91
### from AirPassengers
100
data(AirPassengers)
-
 
101
## The classic `airline model', by full ML
92
## The classic `airline model', by full ML
102
(fit <- arima(log10(AirPassengers), c(0, 1, 1),
93
(fit <- arima(log10(AirPassengers), c(0, 1, 1),
103
              seasonal = list(order=c(0, 1 ,1), period=12)))
94
              seasonal = list(order=c(0, 1 ,1), period=12)))
104
update(fit, method = "CSS")
95
update(fit, method = "CSS")
105
update(fit, x=window(log10(AirPassengers), start = 1954))
96
update(fit, x=window(log10(AirPassengers), start = 1954))