-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path04-02-Forecasting-Generating-TS.Rmd
More file actions
83 lines (56 loc) · 1.98 KB
/
04-02-Forecasting-Generating-TS.Rmd
File metadata and controls
83 lines (56 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
## Generate Time Series Data {-}
### Manually Generated Series {-}
```{r b1, comment=NA, warning=FALSE, message=FALSE, fig.width=9}
library(forecast)
## Set parameters
n = 500
w = rnorm(n, sd = 1)
x.t = 0
## graphical parameters
par(mfrow = c(1,2))
## White Noise
for (i in 2:n) x.t[i] = w[i]
plot.ts(x.t, main = "White Noise")
Acf(x.t, main = "ACF Plot")
## Random Walk, AR(1)
for (i in 2:n) x.t[i] = x.t[i-1] + w[i]
plot.ts(x.t, main = "AR(1) Random Walk")
## Random Walk, AR(2)
for (i in 3:n) x.t[i] = .5*x.t[i-1] - .4*x.t[i-2] + w[i]
plot.ts(x.t, main = "AR(2)")
## MA(1)
for (i in 2:n) x.t[i] = w[i] + w[i-1]
plot.ts(x.t, main = "MA(1)")
for (i in 3:n) x.t[i] = w[i] + w[i-1] + w[i-2]
plot.ts(x.t, main = "MA(2)")
## graphical parameters
par(mfrow = c(1,1))
```
### Auto Generated Series {-}
```{r b2, comment=NA, warning=FALSE, message=FALSE, fig.width=9}
## Generated Time Series
plot(arima.sim(n = n, model = list(order = c(0, 0, 0))), main = "White Noise", ylab = "")
par(mfrow = c(2, 2))
## AR(1) (correlation between observations) p(h) = phi^h
for (i in c(-.9, .01, .7, .9)) {
plot(arima.sim(n = n, model = list(ar = i)), main = bquote(AR(1)~~~phi==.(i)), ylab = "")
}
## AR(2)
plot(arima.sim(n = n, model = list(order = c(2, 0, 0), ar = c(.2, .1))),
main = "AR(2)", ylab = "")
## AR(2) with drift
plot(arima.sim(n = n, model = list(order = c(2, 0, 0), ar = c(.2, .1), d = 1.5)),
main = "AR(2) with drift", ylab = "")
## MA(3)
plot(arima.sim(n = n, model = list(order = c(0, 0, 3), ma = c(.3, .3, .3))),
main = "MA(3)", ylab = "")
## MA(3) with drift
plot(arima.sim(n = n, model = list(order = c(0, 0, 3), ma = c(.1, .8, .1), d = 2)),
main = "MA(3) with drift", ylab = "")
## ARMA(2,3)
plot(arima.sim(n = n, model = list(order = c(2, 0, 3), ar = c(.4, .2), ma = c(.1, .8, .1))),
main = "ARMA(2,3)", ylab = "")
## ARIMA(2,1,3)
plot(arima.sim(n = n, model = list(order = c(2, 1, 3), ar = c(.4, .2), ma = c(.1, .8, .1))),
main = "ARIMA(2,1,3)", ylab = "")
```