-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.Rmd
146 lines (114 loc) · 6.6 KB
/
README.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
fig.align = "center",
fig.dim = c(7,4)*1.4,
out.width = "100%",
warning = FALSE
)
```
# ggdemetra <img src="man/figures/logo.png" align="right" alt="" />
[![R-CMD-check](https://github.com/AQLT/ggdemetra/workflows/R-CMD-check/badge.svg)](https://github.com/AQLT/ggdemetra/actions)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/ggdemetra)](https://cran.r-project.org/package=ggdemetra)
[![CRAN last release](http://www.r-pkg.org/badges/last-release/ggdemetra)](https://cran.r-project.org/package=ggdemetra)
[![CRAN monthly downloads](http://cranlogs.r-pkg.org/badges/ggdemetra?color=lightgrey)](https://cran.r-project.org/package=ggdemetra)
[![CRAN downloads](http://cranlogs.r-pkg.org/badges/grand-total/ggdemetra?color=lightgrey)](https://cran.r-project.org/package=ggdemetra)
## Overview
ggdemetra is an extension of [ggplot2](https://github.com/tidyverse/ggplot2) to add seasonal adjustment statistics to your plots. The seasonal adjustment process is done with [RJDemetra](https://github.com/jdemetra/rjdemetra) that is an R interface to [JDemetra+](https://github.com/jdemetra/jdemetra-app), the seasonal adjustment software [officially recommended](https://wayback.archive-it.org/12090/20240102173448/https://cros-legacy.ec.europa.eu/system/files/Jdemetra_%20release.pdf) to the members of the European Statistical System (ESS) and the European System of Central Banks. RJDemetra implements the two leading seasonal adjustment methods [TRAMO/SEATS+](https://gretl.sourceforge.net/tramo/tramo-seats.html) and [X-12ARIMA/X-13ARIMA-SEATS](https://www.census.gov/data/software/x13as.html).
There are 4 main functionnalities in `ggdemetra` depending of what you want to add in the graphic:
- `geom_sa()`: to add a time series compute during the seasonal adjustment (the trend, the seasonal adjusted time series, etc.).
- `geom_outlier()`: to add the outliers used in the pre-adjustment process of the seasonal adjustment.
- `geom_arima()`: to add the ARIMA model used in the pre-adjustment process of the seasonal adjustment.
- `geom_diagnostics()`: to add a table containing some diagnostics on the seasonal adjustment process.
## Installation
Since RJDemetra requires Java SE 8 or later version, the same requirements are also needed for ggdemetra.
```{r, eval = FALSE}
# Install release version from CRAN
install.packages("ggdemetra")
# Install development version from GitHub
# install.packages("devtools")
devtools::install_github("AQLT/ggdemetra")
```
If you have troubles with the installation of RJDemetra, check the [installation manual](https://github.com/jdemetra/rjdemetra/wiki/Installation-manual).
## Usage
By default, the seasonal adjustment is made with X-13-ARIMA with the pre-defined specification "RSA5c" (automatic log detection, automatic ARIMA and outliers detection and trading day and easter adjustment).
If no new data or seasonal adjustment specification is specified (method or specification), these parameters is inherited from the previous defined: therefore you only need to specify the specification once.
In the following examples, the seasonal adjustment will be perform with X-13-ARIMA with working day adjustment and no gradual easter effect adjustment (it is the specification that has the most economic sense for the industrial production index).
To add the seasonal adjusted series and the forecasts of the input data and of the seasonal adjusted series:
```{r sa, warning=FALSE, message=FALSE}
library(ggplot2)
library(ggdemetra)
spec <- RJDemetra::x13_spec("RSA3", tradingdays.option = "WorkingDays")
p_ipi_fr <- ggplot(data = ipi_c_eu_df, mapping = aes(x = date, y = FR)) +
geom_line(color = "#F0B400") +
labs(title = "Seasonal adjustment of the French industrial production index",
x = NULL, y = NULL)
p_sa <- p_ipi_fr +
geom_sa(component = "y_f", linetype = 2,
spec = spec, frequency = 12, color = "#F0B400") +
geom_sa(component = "sa", color = "#155692") +
geom_sa(component = "sa_f", color = "#155692", linetype = 2)
p_sa
```
To add the outliers at the bottom of the plot with an arrow to the data point and the estimated coefficients:
```{r sa-out}
p_sa +
geom_outlier(geom = "label_repel",
coefficients = TRUE,
ylim = c(NA, 65),
arrow = arrow(length = unit(0.03, "npc"),
type = "closed", ends = "last"),
digits = 2)
```
To add the ARIMA model:
```{r sa-arima}
p_sa +
geom_arima(geom = "label",
x_arima = -Inf, y_arima = -Inf,
vjust = -1, hjust = -0.1)
```
To add a table of diagnostics below the plot:
```{r sa-diag}
diagnostics <- c(`Combined test` = "diagnostics.combined.all.summary",
`Residual qs-test (p-value)` = "diagnostics.qs",
`Residual f-test (p-value)` = "diagnostics.ftest")
p_diag <- ggplot(data = ipi_c_eu_df, mapping = aes(x = date, y = FR)) +
geom_diagnostics(diagnostics = diagnostics,
table_theme = gridExtra::ttheme_default(base_size = 8),
spec = spec, frequency = 12) +
theme_void()
gridExtra::grid.arrange(p_sa, p_diag,
nrow = 2, heights = c(4, 1.5))
```
See the [vignette](https://aqlt.github.io/ggdemetra/articles/ggdemetra.html) for more details.
Note that `ts` objects cannot be directly used in `ggplot2`.
To convert `ts` or `mts` object to `data.frame`, you can use the `ts2df()` function.
For example, the data `ipi_c_eu_df` used in this package is obtained by applying the `ts2df()`function to the `ipi_c_eu` data available in RJDemetra:
```{r, eval = FALSE}
ipi_c_eu_df <- ts2df(ipi_c_eu)
```
## Existing models
ggdemetra offers several function that can be used to manipulate existing models.
The different components of seasonal adjustment models can be extracted through `calendar()`, `calendaradj()`, `irregular()`, `trendcycle()`, `seasonal()`, `seasonaladj()`, `trendcycle()` and `raw()`.
If you already have a seasonally adjusted model you can also used the function `init_ggplot()` :
```{r sa-init}
spec <- RJDemetra::x13_spec("RSA3", tradingdays.option = "WorkingDays")
mod <- RJDemetra::x13(ipi_c_eu[,"FR"], spec)
init_ggplot(mod) +
geom_line(color = "#F0B400") +
geom_sa(component = "sa", color = "#155692")
```
There is also an `autoplot()` function:
```{r autoplot}
autoplot(mod)
```
SI-ratio plots can be plotted with `siratioplot` and `ggsiratioplot`:
```{r ggsiratio}
ggsiratioplot(mod)
```