rjdverse / rjdemetra

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

Seasonality Test for Original Series #147

Closed JohannesBBB closed 1 month ago

JohannesBBB commented 3 months ago

I would like to get the values for the seasonality tests (particularly the p-values) from the section "Original (transformed) Series".

In JDemetra+, one can start a seasonal adjustment and under section Diagnostics -> Seasonality Test -> Original (transformed) series

I see a summary like this

Original series

Summary Data have been differenced and corrected for mean

  1. Auto-correlations at seasonal lags | YES
  2. Friedman (non parametric) | YES
  3. Kruskall-Wallis (non parametric) | YES
  4. Spectral peaks | YES
  5. Periodogram | YES
  6. Seasonal dummies | YES 6bis. Seasonal dummies (AMI) | YES

Below, I then get some p-values; I would like to get these values with RJDemetra; Is it possible that this option is not available?

I first looked into the userdefined variables and did not find anything. Afterwards, I just chose all userdefined values by defining

ts_data <- ipi_c_eu[,"FR"] m <- tramoseats(ts_data, user_defined_variables("TRAMO-SEATS"))

However, I searched across all variables but did not find it either - is it possible that is not has been implemented? Is there some kind of workaround?

AQLT commented 1 month ago

Hello, Indeed they are not implemented in RJDemetra, however you have the tests on the linearised series (which can be more relevant when you have several outliers). Did you try using the code in #50? You can get all the results you want (only the tukey test is not implemented in the example):

library(rJava)
library(RJDemetra)
y <- ipi_c_eu[, "FR"]
sa_ts <- jtramoseats(y)
if (!get_indicators(sa_ts, "mode")[[1]]=="Additive") {
  y <- log(y)
}

jd_seasonality<-function(s, test="QS", differencing=1, mean=TRUE){
  jd_s<-RJDemetra:::ts_r2jd(s)
  jd_seas<-.jcall("ec/tstoolkit/modelling/arima/tramo/SeasonalityTests",
                  "Lec/tstoolkit/modelling/arima/tramo/SeasonalityTests;",
                  "seasonalityTest", jd_s, as.integer(differencing), mean, TRUE)
  if (test=="QS"){
    jd_qs<-.jcall(jd_seas, "Lec/tstoolkit/stats/StatisticalTest;","getQs")
    val<-.jcall(jd_qs, "D", "getValue")
    pval<-.jcall(jd_qs, "D", "getPValue")
    all<-c(val, pval)
    jd_dist<-.jcall(jd_qs, "Lec/tstoolkit/dstats/IDistribution;", "getDistribution")
    desc<-.jcall(jd_dist, "S", "toString")
    attr(all, "description")<-desc
    all
  }else if (test=="FRIEDMAN"){
    jd_qs<-.jcall(jd_seas, "Lec/satoolkit/diagnostics/FriedmanTest;","getNonParametricTest")
    val<-.jcall(jd_qs, "D", "getValue")
    pval<-.jcall(jd_qs, "D", "getPValue")
    all<-c(val, pval)
    jd_dist<-.jcall(jd_qs, "Lec/tstoolkit/dstats/IDistribution;", "getDistribution")
    desc<-.jcall(jd_dist, "S", "toString")
    attr(all, "description")<-desc
    all
  }else if (test=="KRUSKALWALLIS"){
    jd_diff<-.jcall(jd_seas, "Lec/tstoolkit/modelling/DifferencingResults;", "getDifferencing")
    jd_ds<-.jcall(jd_diff, "Lec/tstoolkit/timeseries/simplets/TsData;", "getDifferenced")
    jd_kw<-.jnew("ec/satoolkit/diagnostics/KruskalWallisTest", jd_ds)
    val<-.jcall(jd_kw, "D", "getValue")
    pval<-.jcall(jd_kw, "D", "getPValue")
    all<-c(val, pval)
    jd_dist<-.jcall(jd_kw, "Lec/tstoolkit/dstats/IDistribution;", "getDistribution")
    desc<-.jcall(jd_dist, "S", "toString")
    attr(all, "description")<-desc
    all
  }else if (test=="PERIODOGRAM"){
    jd_qs<-.jcall(jd_seas, "Lec/tstoolkit/stats/StatisticalTest;","getPeriodogramTest")
    val<-.jcall(jd_qs, "D", "getValue")
    pval<-.jcall(jd_qs, "D", "getPValue")
    all<-c(val, pval)
    jd_dist<-.jcall(jd_qs, "Lec/tstoolkit/dstats/IDistribution;", "getDistribution")
    desc<-.jcall(jd_dist, "S", "toString")
    attr(all, "description")<-desc
    all
  }
}
jd_seasftest<-function(s, ami=FALSE){
  jd_s<-RJDemetra:::ts_r2jd(s)
  jd_f<-.jnew("ec/satoolkit/diagnostics/FTest");
  if (ami){
    .jcall(jd_f, "Z", "testAMI", jd_s)
  }else{
    .jcall(jd_f, "Z", "test", jd_s)
  }
  jd_test<-.jcall(jd_f, "Lec/tstoolkit/stats/StatisticalTest;","getFTest")
  val<-.jcall(jd_test, "D", "getValue")
  pval<-.jcall(jd_test, "D", "getPValue")
  all<-c(val, pval)
  jd_dist<-.jcall(jd_test, "Lec/tstoolkit/dstats/IDistribution;", "getDistribution")
  desc<-.jcall(jd_dist, "S", "toString")
  attr(all, "description")<-desc
  all
}
jd_seasonality(y,test = "QS")
#> [1] 583.9888   0.0000
#> attr(,"description")
#> [1] "Chi2(2)"
jd_seasonality(y,test = "FRIEDMAN")
#> [1] 259.6716   0.0000
#> attr(,"description")
#> [1] "Chi2(11)"
jd_seasonality(y,test = "KRUSKALWALLIS")
#> [1] 290.8866   0.0000
#> attr(,"description")
#> [1] "Chi2(11)"
jd_seasonality(y,test = "PERIODOGRAM")
#> [1]  2.616813e+02 7.728866e-161
#> attr(,"description")
#> [1] "F(11,348)"
# For some reason the first value has to be removed to get the exact test of JDemetra+
jd_seasftest(ts(y[-1], end = end(y), frequency = 12), ami = FALSE)
#> [1]  2.364115e+02 1.922905e-156
#> attr(,"description")
#> [1] "F(11,357)"
jd_seasftest(ts(y[-1], end = end(y), frequency = 12), ami = TRUE)
#> [1]  2.388967e+02 6.895608e-157
#> attr(,"description")
#> [1] "F(11,356)"