-
Notifications
You must be signed in to change notification settings - Fork 39
/
ggseas.Rmd
79 lines (65 loc) · 2.56 KB
/
ggseas.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
---
title: "ggplot2 extensions: ggseas"
---
### ggseas
<https://github.com/ellisp/ggseas>
Seasonal adjustment on the fly extension for ggplot2. Convenience functions that let you easily do seasonal adjustment on the fly with ggplot. Depends on the [`seasonal` package](https://cran.r-project.org/web/packages/seasonal/index.html) to give you access to X13-SEATS-ARIMA.
```{r, message=FALSE,warning=FALSE}
# Example from https://github.com/ellisp/ggseas
library(ggplot2)
library(ggnet)
library(ggseas)
```
#### Usage
So far there are three types of seasonal adjustment possible
##### X13-SEATS-ARIMA
```{r, message=FALSE,warning=FALSE}
# make demo data
ap_df <- data.frame(
x = as.numeric(time(AirPassengers)),
y = as.numeric(AirPassengers)
)
# SEATS with defaults
ggplot(ap_df, aes(x = x, y = y)) +
geom_line(colour = "grey80") +
stat_seas(start = c(1949, 1), frequency = 12) +
ggtitle("SEATS seasonal adjustment - international airline passengers") +
ylab("International airline passengers per month")
# X11 with no outlier treatment
ggplot(ap_df, aes(x = x, y = y)) +
geom_line(colour = "grey80") +
stat_seas(start = c(1949, 1), frequency = 12, x13_params = list(x11 = "", outlier = NULL)) +
ggtitle("X11 seasonal adjustment - international airline passengers") +
ylab("International airline passengers per month")
ggplot(ldeaths_df, aes(x = YearMon, y = deaths, colour = sex)) +
geom_point(colour = "grey50") +
geom_line(colour = "grey50") +
facet_wrap(~sex) +
stat_seas(start = c(1974, 1), frequency = 12, size = 2) +
ggtitle("Seasonally adjusted lung deaths in the UK 1974 - 1979") +
ylab("Deaths") +
xlab("(light grey shows original data;\ncoloured line is seasonally adjusted)") +
theme(legend.position = "none")
```
##### STL (LOESS-based decomposition)
```{r, message=FALSE,warning=FALSE}
# periodic if fixed seasonality; doesn't work well:
ggplot(ap_df, aes(x = x, y = y)) +
geom_line(colour = "grey80") +
stat_stl(frequency = 12, s.window = "periodic")
# seasonality varies a bit over time, works better:
ggplot(ap_df, aes(x = x, y = y)) +
geom_line(colour = "grey80") +
stat_stl(frequency = 12, s.window = 7)
```
#### Classical decomposition
```{r, message=FALSE,warning=FALSE}
# default additive decomposition (doesn't work well in this case!):
ggplot(ap_df, aes(x = x, y = y)) +
geom_line(colour = "grey80") +
stat_decomp(frequency = 12)
# multiplicative decomposition, more appropriate:
ggplot(ap_df, aes(x = x, y = y)) +
geom_line(colour = "grey80") +
stat_decomp(frequency = 12, type = "multiplicative")
```