ggdemetra: extending ggplot2 to perform seasonal adjustment with RJDemetra

Alain Quartier-la-Tente

2019-04-18

ggdemetra is an extension of ggplot2 to add seasonal adjustment statistics to your plots. The seasonal adjustment process is done with RJDemetra that is an R interface to JDemetra+, the seasonal adjustment software officially recommended 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+ and X-12ARIMA/X-13ARIMA-SEATS.

There are 4 main functionnalities in ggdemetra depending of what you want to add in the graphic:

Seasonal adjustment specification

All the functions have some common parameters and especially those to defined the seasonal adjustment method:

In the following examples, the default seasonal adjustment specification will be used on the French industrial production index. The seasonal adjustment will then be processed with X-13ARIMA with a pre-defined specification "RSA5c (automatic log detection, automatic ARIMA and outliers detection and trading day and easter adjustment), even if this is not the specification that should be used for this series. Indeed, in the industrial production th working day effect has more economic sense than the trading day effect and a gradual effect for easter does not make economic sense for the aggregated series. The specification that should be used with X-13ARIMA is spec = RJDemetra::x13_spec("RSA3", tradingdays.option = "WorkingDays") but, to simplify the examples that follow, we will use the default one.

library(ggplot2)
library(ggdemetra)
p_ipi_fr <- ggplot(data = ipi_c_eu_df, mapping = aes(x = date, y = FR)) +
    geom_line() +
    labs(title = "Seasonal adjustment of the French industrial production index",
         x = "time", y = NULL)
p_ipi_fr

Add the result of the seasonal adjusment

By default geom_sa() adds the seasonal adjusted time series:

p_ipi_fr +
    geom_sa(color = "red")
#> Frenquency used: 12

To add other components of the seasonal adjustment, use the component parameter of geom_sa() (see ?RJDemetra::user_defined_variables() for the availables parameters). For example, to add the forecasts of the input data and of the seasonal adjusted series:

p_sa <- p_ipi_fr +
    geom_sa(component = "y_f", linetype = 2, message = FALSE) + 
    geom_sa(component = "sa", color = "red", message = FALSE) +
    geom_sa(component = "sa_f", color = "red", linetype = 2, message = FALSE)
p_sa

Add the outliers to the plot

There are four differents geometrics to add to the plot the names of the outliers used in the pre-adjustment process:

In our example, there are 3 outliers:

p_sa + geom_outlier(geom = "label",
                    message = FALSE)

They can be plotted in more readable way using the parameters of ggrepel::geom_label_repel:

p_sa + 
    geom_outlier(geom = "label_repel",
                 message = FALSE,
                 vjust = 4,
                 ylim = c(NA, 65), force = 10,
                 arrow = arrow(length = unit(0.03, "npc"),
                               type = "closed", ends = "last"))

Use the parameters first_date and last_date to only have the outliers in a precise time interval. For example, to only plot the outliers from 2009 use first_date = 2009:

p_sa + 
    geom_outlier(geom = "label_repel",
                 message = FALSE,
                 first_date = 2009,
                 vjust = 4,
                 ylim = c(NA, 65), force = 10,
                 arrow = arrow(length = unit(0.03, "npc"),
                               type = "closed", ends = "last"))

Add the ARIMA model

The ARIMA model used pre-adjustment process can be added to the plot with geom_ arima(). The parameter geom = "label" draws a rectangle behind the ARIMA model, making it easier to read:

p_sa + 
    geom_arima(geom = "label",
               x_arima = -Inf, y_arima = -Inf, 
               vjust = -1, hjust = -0.1,
               message = FALSE)

Add a table with some diagnostics

A table with some diagnostics on the seasonal adjustment process can be added with geom_diagnostics(). The desired diagnostics have to be added to the diagnostics parameter (see ?RJDemetra::user_defined_variables() for the availables diagnostics). For example, to add the result of the X-11 combined test and the p-values of the residual seasonality qs and f tests:

diagnostics <- c("diagnostics.combined.all.summary", "diagnostics.qs", "diagnostics.ftest")
p_sa + 
    geom_diagnostics(diagnostics = diagnostics,
                     ymin = 58, ymax = 72, xmin = 2010,
                     table_theme = gridExtra::ttheme_default(base_size = 8),
                     message = FALSE)

To customize the names of the diagnostics in the plot, pass a named vector to the diagnostics parameter:

diagnostics <- c(`Combined test` = "diagnostics.combined.all.summary",
                 `Residual qs-test (p-value)` = "diagnostics.qs",
                 `Residual f-test (p-value)` = "diagnostics.ftest")
p_sa + 
    geom_diagnostics(diagnostics = diagnostics,
                     ymin = 58, ymax = 72, xmin = 2010,
                     table_theme = gridExtra::ttheme_default(base_size = 8),
                     message = FALSE)

To add the table below the plot, you can for example use gridExtra::grid.arrange():

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),
                     message = FALSE) + 
    theme_void()
    
gridExtra::grid.arrange(p_sa, p_diag,
             nrow = 2, heights  = c(4, 1))