rjdverse / rjdemetra

R interface to JDemetra+ v 2.x
https://rjdverse.github.io/rjdemetra
52 stars 16 forks source link

x11.mode can not be multiplicative when an outlier is not prespecified #91

Closed LevonIpdjian closed 3 years ago

LevonIpdjian commented 3 years ago

Hello,

I'm not sure this is an anomaly but I have an irritating issue about x11.mode. Sometimes when outliers are not prespecified, the decomposition mode is fixed to additive, even if I specified multiplicative manually. In fact, I can specifiy anything but PseudoAdditive, the applied decomposition will always be additive.

You can find below a reproductible example. In my console, the print sequences show "Multiplicative", "Additive", "Multiplicative" while all of them should be Multiplicative.


if(!"RJDemetra" %in% installed.packages())install.packages("RJDemetra")
if(packageVersion("RJDemetra")<"0.1.5")install.packages("RJDemetra")
library(RJDemetra)
# Simulation d'une série multiplicative --------------------------------

N <- 15*12 #>48

Tendance <- 100:(100+(N-1))
Irregulier <- runif(N,0.8,1.2)
Saisonnalite <- seq(0.725,1.3,by=0.05)

BRUT <- ts(Tendance * Irregulier * Saisonnalite,start=c(2010,1),frequency=12)

# Introduction d'une série avec crise ------------------------------------

BRUT_CRISE <- BRUT
BRUT_CRISE[48] <- BRUT_CRISE[48]/10 #Introduction d'un point atypique

# Graphique ------------------------------------

plot(BRUT_CRISE)
lines(BRUT,col="red")

SPEC <- x13_spec(x11.mode = "Multiplicative")
SPEC_AVEC_OUTLIER <- x13_spec(x11.mode = "Multiplicative" #) # Avec ou sans outliers, cela ne change pas le mode de décomposition choisi
                 ,usrdef.outliersEnabled = T, usrdef.outliersType = "AO",usrdef.outliersDate = "2013-12-01")

# Desaisonnalisation ------------------------------------

K <- RJDemetra::x13(BRUT,SPEC)
K_CRISE <- RJDemetra::x13(BRUT_CRISE,SPEC)
K_O_CRISE <- RJDemetra::x13(BRUT_CRISE,SPEC_AVEC_OUTLIER)

paste0("Sans crise, sans outlier prédéfini : ",ifelse(any(K$final$series[,"s"]<0),"Additive","Multiplicative")) #Multiplicative
paste0("Avec crise, sans outlier prédéfini : ",ifelse(any(K_CRISE$final$series[,"s"]<0),"Additive","Multiplicative")) #Additive ?!
paste0("Avec crise, avec outlier prédéfini : ",ifelse(any(K_O_CRISE$final$series[,"s"]<0),"Additive","Multiplicative")) #Multiplicative

SA <- cbind("NO_NO"=K$final$series[,"sa"],"O_NO"=K_CRISE$final$series[,"sa"])
zoo::plot.zoo(SA, plot.type = "single",col=c("blue","red"))#
SA <- cbind("NO_NO"=K$final$series[,"sa"],"O_O"=K_O_CRISE$final$series[,"sa"])
zoo::plot.zoo(SA, plot.type = "single",col=c("blue","red"))#

SA <- cbind("O_NO"=K_CRISE$final$series[,"sa"],"O_O"=K_O_CRISE$final$series[,"sa"])
zoo::plot.zoo(SA, plot.type = "single",col=c("blue","red"))#comparaison de resultats : le rouge est le bon

#Confirmation additive
abs(K_CRISE$final$series[,"t"] + K_CRISE$final$series[,"i"]) - K_CRISE$final$series[,"sa"]) > 10e-10
AQLT commented 3 years ago

Hello, Instead of x11.mode = "Multiplicative" option, you should use transform.function = "Log". Indeed, the first option only changes the decomposition mode used in the X11 algorithm. However, like it is said in the help associated to this parameter, it "could be changed by the program, if needed", that is to say when the decomposition used in the pre-adjustment (regarima) is not compatible and this is the case when the series is not log-transformed and you use the Multiplicative decomposition. The second option changes the transformation used in the pre-adjustment and uses a compatible x11 decomposition:

library(RJDemetra)
set.seed(100)
N <- 15*12 #>48

Tendance <- 100:(100+(N-1))
Irregulier <- runif(N,0.8,1.2)
Saisonnalite <- seq(0.725,1.3,by=0.05)

BRUT <- ts(Tendance * Irregulier * Saisonnalite,start=c(2010,1),frequency=12)

BRUT_CRISE <- BRUT
BRUT_CRISE[48] <- BRUT_CRISE[48]/10 #Introduction d'une crise

SPEC <- x13_spec(x11.mode = "Multiplicative")
SPEC_AVEC_OUTLIER <- x13_spec(x11.mode = "Multiplicative" #) # Avec ou sans outliers, cela ne change pas le mode de décomposition choisi
                              ,usrdef.outliersEnabled = T, usrdef.outliersType = "AO",
                              usrdef.outliersDate = "2013-12-01")
K <- RJDemetra::x13(BRUT,SPEC)
K_CRISE <- RJDemetra::x13(BRUT_CRISE,SPEC)
K_O_CRISE <- RJDemetra::x13(BRUT_CRISE,SPEC_AVEC_OUTLIER)

sprintf("Sans crise, sans outlier prédéfini : %s - Decomposition %s",
       ifelse(K$regarima$model$spec_rslt["Log transformation"],"Log transformed","No transformation"),
        K$decomposition$mode)
#> [1] "Sans crise, sans outlier prédéfini : Log transformed - Decomposition Multiplicative"
sprintf("Avec crise, sans outlier prédéfini : %s - Decomposition %s",
        ifelse(K_CRISE$regarima$model$spec_rslt["Log transformation"],"Log transformed","No transformation"),
        K_CRISE$decomposition$mode)
#> [1] "Avec crise, sans outlier prédéfini : No transformation - Decomposition Additive"
sprintf("Avec crise, avec outlier prédéfini : %s - Decomposition %s",
        ifelse(K_O_CRISE$regarima$model$spec_rslt["Log transformation"],"Log transformed","No transformation"),
        K_O_CRISE$decomposition$mode)
#> [1] "Avec crise, avec outlier prédéfini : Log transformed - Decomposition Multiplicative"

SPEC2 <- x13_spec(transform.function = "Log")
SPEC_AVEC_OUTLIER2 <- x13_spec(transform.function = "Log" #) # Avec ou sans outliers, cela ne change pas le mode de décomposition choisi
                              ,usrdef.outliersEnabled = T, usrdef.outliersType = "AO",
                              usrdef.outliersDate = "2013-12-01")
K2 <- RJDemetra::x13(BRUT,SPEC2)
K_CRISE2 <- RJDemetra::x13(BRUT_CRISE,SPEC2)
K_O_CRISE2 <- RJDemetra::x13(BRUT_CRISE,SPEC_AVEC_OUTLIER2)

sprintf("Sans crise, sans outlier prédéfini : %s - Decomposition %s",
        ifelse(K2$regarima$model$spec_rslt["Log transformation"],"Log transformed","No transformation"),
        K2$decomposition$mode)
#> [1] "Sans crise, sans outlier prédéfini : Log transformed - Decomposition Multiplicative"
sprintf("Avec crise, sans outlier prédéfini : %s - Decomposition %s",
        ifelse(K_CRISE2$regarima$model$spec_rslt["Log transformation"],"Log transformed","No transformation"),
        K_CRISE2$decomposition$mode)
#> [1] "Avec crise, sans outlier prédéfini : Log transformed - Decomposition Multiplicative"
sprintf("Avec crise, avec outlier prédéfini : %s - Decomposition %s",
        ifelse(K_O_CRISE2$regarima$model$spec_rslt["Log transformation"],"Log transformed","No transformation"),
        K_O_CRISE2$decomposition$mode)
#> [1] "Avec crise, avec outlier prédéfini : Log transformed - Decomposition Multiplicative"

Created on 2021-08-02 by the reprex package (v2.0.0)

LevonIpdjian commented 3 years ago

Thank you !