-
Notifications
You must be signed in to change notification settings - Fork 2
/
04-01-Forecasting-Basic-Methods.Rmd
87 lines (58 loc) · 2.6 KB
/
04-01-Forecasting-Basic-Methods.Rmd
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
84
85
86
87
# Forecasting {-}
## Basic Graphical Methods for Time Series {-}
```{r a1, comment=NA, warning=FALSE, message=FALSE, fig.height=6, fig.width=9}
library(forecast)
library(lubridate)
data("AirPassengers")
## Plot of Air Passengers with increasing variance
plot(AirPassengers, main = "Air Passengers 1949 to 1961", ylab = "Passengers")
## Seasonal plot by year
seasonplot(AirPassengers, s = 12, main = "Seasonal Plot", ylab = "Passengers", year.labels = TRUE, col = 1:12)
## Seasonal subseries plot
monthplot(AirPassengers, ylab = "Passengers", main = "Seasonal Deviation", labels = month.abb)
## Autocorrelation
acf(AirPassengers, main = "Autocorrelation")
```
#### Simple Forecasting Methods {-}
```{r a2, comment=NA, warning=FALSE, message=FALSE, fig.height=6, fig.width=9}
## Average Method
method.avg = meanf(AirPassengers, h = 36)
## Naive Method
method.naive = naive(AirPassengers, h = 36)
## Seasonal Naive
method.snaive = snaive(AirPassengers, h = 36)
## Random Walk with Drift
method.rwf = rwf(AirPassengers, h = 36, drift = TRUE)
## Plot of the different methods
plot(AirPassengers, main = "Air Passengers 1949 to 1961", xlim = c(1949, 1964))
lines(method.avg$mean, col = 2)
lines(method.naive$mean, col = 3)
lines(method.snaive$mean, col = 4)
lines(method.rwf$mean, col = 5)
legend("topleft", lty = 1, col = c(2, 3, 4, 5),
legend = c("Average", "Naive", "Seasonal Naive", "Random Walk"))
```
#### Stabalizing Variance {-}
```{r a3, comment=NA, warning=FALSE, message=FALSE, fig.height=8, fig.width=9}
## Box Cox Transformation
(lambda = BoxCox.lambda(AirPassengers))
## Remove variation from months, create a data.frame first
Air = data.frame(Passengers = AirPassengers)
Air$Date = seq.Date(from = as.Date("1949-01-01"), by = "month", length.out = 144)
## Calculate days per month
Air$Days.per.month = days_in_month(Air$Date)
## Calculate average number of passengers per month removing variation due to number of days
Air$month.avg = with(Air, Passengers / Days.per.month)
## Plots of original data and monthly average under the Box Cox Transformation
par(mfrow = c(2, 1))
plot(BoxCox(Air$Passengers, lambda), ylab = "Passengers", main = "Air Passengers (BCT)")
plot(BoxCox(Air$month.avg, BoxCox.lambda(Air$month.avg)), ylab = "Passengers",
main = "Passengers Per Month (BCT)")
```
```{r a4, comment=NA, warning=FALSE, message=FALSE, fig.height=6, fig.width=10}
## Decompose average monthly passengers
plot(decompose(Air$month.avg))
## Decompose average monthly passenegers under Box Cox
## Results better random error
plot(decompose(BoxCox(Air$month.avg, BoxCox.lambda(Air$month.avg))), ylab = "Passengers")
```