Skip to content

Commit

Permalink
Small modification of the seasonality tests
Browse files Browse the repository at this point in the history
  • Loading branch information
palatej committed Sep 25, 2024
1 parent 1d60277 commit 1854648
Show file tree
Hide file tree
Showing 32 changed files with 210 additions and 87 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export(seasonality_combined)
export(seasonality_f)
export(seasonality_friedman)
export(seasonality_kruskalwallis)
export(seasonality_modified_qs)
export(seasonality_periodogram)
export(seasonality_qs)
export(set_arima)
Expand Down
1 change: 1 addition & 0 deletions R/arima.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ sarima_decompose<-function(model, rmod=0, epsphi=0){
#' @export
#'
#' @examples
#' model <- arima_model("trend", ar=c(1,-.8), delta = c(1,-1), ma=c(1,-.5), var=100)
arima_model<-function(name="arima", ar=1, delta=1, ma=1, variance=1){
return(structure(list(name=name, ar=ar, delta=delta, ma=ma, var=variance), class="JD3_ARIMA"))
}
Expand Down
9 changes: 4 additions & 5 deletions R/differencing.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ NULL

#' Automatic stationary transformation
#'
#' Stationary transformation of a series by simple differencing of lag 1.
#' Automatic processing (identification of the order of the differencing) based on auto-correlations and on mean correction.
#' The series should not be seasonal.
#' Source: Tramo
Expand Down Expand Up @@ -50,13 +49,13 @@ do_stationary<-function(data, period){

#' Automatic differencing
#'
#' The series is differentiated till its variance is decreasing.
#' The series is differenced till its variance is decreasing.
#'
#' @param data Series being differenced.
#' @param period Period considered in the automatic differencing.
#' @param mad Use of MAD in the computation of the variance (true by default).
#' @param centile Percentage of the data used for computing the variance (90 by default).
#' @param k tolerance in the decrease of the variance. The algorithm stops if the new varance is higher than k*the old variance.
#' @param k tolerance in the decrease of the variance. The algorithm stops if the new variance is higher than k*the old variance. k should be equal or slightly higher than 1 (1.2 by default)
#'
#' @return
#' Stationary transformation
Expand Down Expand Up @@ -87,7 +86,7 @@ differencing_fast<-function(data, period, mad=TRUE, centile=90, k=1.2){
#'
#' @param data The series to be differenced.
#' @param lags Lags of the differencing.
#' @param mean Mean correction.
#' @param mean Apply a mean correction at the end of the differencing process.
#'
#' @return The differenced series.
#' @export
Expand Down Expand Up @@ -126,7 +125,7 @@ differences.data.frame<-function(data, lags=1, mean=TRUE){
#' Range-Mean Regression
#'
#' Function to perform a range-mean regression, trimmed to avoid outlier distortion.
#' The slope is used in TRAMO to select whether the original series will be transformed into log or maintain in level.
#' The can be used to select whether the original series will be transformed into log or maintain in level.
#'
#' @param data data to test.
#' @param period periodicity of the data.
Expand Down
12 changes: 7 additions & 5 deletions R/tests_regular.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,18 @@ kurtosis<-function(data){
return(.jd2r_test(jtest))
}

#' Title
#' Compute a robust median absolute deviation (MAD)
#'
#' @param data
#' @param centile
#' @param medianCorrected
#' @param data The data for which we compute the robust deviation
#' @param centile The centile used to exclude extreme values (only the "centile" part of the data are is to compute the mad)
#' @param medianCorrected TRUE if the series is corrected for its median, FALSE if the median is supposed to be 0
#'
#' @return
#' @return The median absolute deviation
#' @export
#'
#' @examples
#' y<-rnorm(1000)
#' m<-rjd3toolkit::mad(y, centile=70)
mad<-function(data, centile=50, medianCorrected=TRUE){
return(.jcall("jdplus/toolkit/base/r/stats/Tests", "D", "mad",as.numeric(data), as.numeric(centile), as.logical(medianCorrected)))
}
76 changes: 54 additions & 22 deletions R/tests_seasonality.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,55 @@
NULL


#' QS Seasonality Test
#'
#' QS (modified seasonal Ljung-Box) test.
#' QS (seasonal Ljung-Box) test.
#'
#' @param data the input data.
#' @param period Tested periodicity.
#' @param nyears Number of number of periods number of cycles considered in the test, at the end of the series:
#' @param period Tested periodicity. Can be missing if the input is a time series
#' @param nyears Number of periods or number of cycles considered in the test, at the end of the series:
#' in periods (positive value) or years (negative values).
#' By default (\code{nyears = 0}), the entire sample is used.
#' @param type 1 for positive autocorrelations, -1 for negative autocorrelations,
#' 0 for all autocorrelations. By default (\code{type = 1})
#'
#' @return A `c("JD3_TEST", "JD3")` object (see [statisticaltest()] for details).
#' @export
#'
#' @examples
#' seasonality_qs(ABS$X0.2.09.10.M, 12)
#' s<-do_stationary(log(ABS$X0.2.09.10.M))$ddata
#' seasonality_qs(s)
#' seasonality_qs(random_t(2, 1000), 7)
seasonality_qs<-function(data, period, nyears=0){
seasonality_qs<-function(data, period=NA, nyears=0, type=1){
if (is.ts(data) && missing(period))
period <- frequency(data)
jtest<-.jcall("jdplus/sa/base/r/SeasonalityTests", "Ljdplus/toolkit/base/api/stats/StatisticalTest;", "qsTest",
as.numeric(data), as.integer(period), as.integer(nyears))
as.numeric(data), as.integer(period), as.integer(nyears), as.integer((type)))
return(.jd2r_test(jtest))
}

#' Modified QS Seasonality Test (Maravall)
#'
#'
#' @param data the input data.
#' @param period Tested periodicity. Can be missing if the input is a time series
#' @param nyears Number of periods or number of cycles considered in the test, at the end of the series:
#' in periods (positive value) or years (negative values).
#' By default (\code{nyears = 0}), the entire sample is used.
#'
#' @return The value of the test
#' @export
#'
#' @examples
#' s<-do_stationary(log(ABS$X0.2.09.10.M))$ddata
#' seasonality_modified_qs(s)
seasonality_modified_qs<-function(data, period=NA, nyears=0){
if (is.ts(data) && missing(period))
period <- frequency(data)
test<-.jcall("jdplus/sa/base/r/SeasonalityTests", "D", "modifiedQsTest",
as.numeric(data), as.integer(period), as.integer(nyears))
return(test)
}


#' Kruskall-Wallis Seasonality Test
#'
#'
Expand All @@ -36,7 +61,8 @@ seasonality_qs<-function(data, period, nyears=0){
#' @export
#'
#' @examples
#' seasonality_kruskalwallis(ABS$X0.2.09.10.M, 12)
#' s<-do_stationary(log(ABS$X0.2.09.10.M))$ddata
#' seasonality_kruskalwallis(s)
#' seasonality_kruskalwallis(random_t(2, 1000), 7)
seasonality_kruskalwallis<-function(data, period, nyears=0){
if (is.ts(data) && missing(period))
Expand All @@ -55,9 +81,10 @@ seasonality_kruskalwallis<-function(data, period, nyears=0){
#' @export
#'
#' @examples
#' seasonality_periodogram(ABS$X0.2.09.10.M, 12)
#' s<-do_stationary(log(ABS$X0.2.09.10.M))$ddata
#' seasonality_periodogram(s)
#' seasonality_periodogram(random_t(2, 1000), 7)
seasonality_periodogram<-function(data, period, nyears=0){
seasonality_periodogram<-function(data, period=NA, nyears=0){
if (is.ts(data) && missing(period))
period <- frequency(data)
jtest<-.jcall("jdplus/sa/base/r/SeasonalityTests", "Ljdplus/toolkit/base/api/stats/StatisticalTest;", "periodogramTest",
Expand All @@ -74,7 +101,10 @@ seasonality_periodogram<-function(data, period, nyears=0){
#' @export
#'
#' @examples
seasonality_friedman<-function(data, period, nyears=0){
#' s<-do_stationary(log(ABS$X0.2.09.10.M))$ddata
#' seasonality_friedman(s)
#' seasonality_friedman(random_t(2, 1000), 12)
seasonality_friedman<-function(data, period=NA, nyears=0){
if (is.ts(data) && missing(period))
period <- frequency(data)
jtest<-.jcall("jdplus/sa/base/r/SeasonalityTests", "Ljdplus/toolkit/base/api/stats/StatisticalTest;", "friedmanTest",
Expand All @@ -91,10 +121,10 @@ seasonality_friedman<-function(data, period, nyears=0){
#' @export
#'
#' @examples
#' seasonality_f(ABS$X0.2.09.10.M, 12)
#' seasonality_f(ABS$X0.2.09.10.M, model="D1")
#' seasonality_f(random_t(2, 1000), 7)
seasonality_f<-function(data,
period,
period=NA,
model=c("AR", "D1", "WN"),
nyears=0){
if (is.ts(data) && missing(period))
Expand All @@ -117,9 +147,10 @@ seasonality_f<-function(data,
#' @export
#'
#' @examples
#' seasonality_combined(ABS$X0.2.09.10.M, 12)
#' s<-do_stationary(log(ABS$X0.2.09.10.M))$ddata
#' seasonality_combined(s)
#' seasonality_combined(random_t(2, 1000), 7)
seasonality_combined<-function(data, period, firstperiod=cycle(data)[1], mul=TRUE){
seasonality_combined<-function(data, period=NA, firstperiod=cycle(data)[1], mul=TRUE){
if (is.ts(data) && missing(period))
period <- frequency(data)
jctest<-.jcall("jdplus/sa/base/r/SeasonalityTests", "Ljdplus/sa/base/core/tests/CombinedSeasonality;", "combinedTest",
Expand All @@ -138,8 +169,8 @@ seasonality_combined<-function(data, period, firstperiod=cycle(data)[1], mul=TRU
#' @inheritParams seasonality_qs
#' @param periods Periodicities.
#' @param lag1 Lagged variable in the regression model.
#' @param kernel Kernel used to compute the robust covariance matrix.
#' @param order The truncation parameter used to compute the robust covariance matrix.
#' @param kernel Kernel used to compute the robust Newey-West covariance matrix.
#' @param order The truncation parameter used to compute the robust Newey-West covariance matrix.
#' @param original `TRUE` for original algorithm, `FALSE` for solution proposed by T. Proietti (based on Ox code).
#'
#' @export
Expand All @@ -166,12 +197,13 @@ seasonality_canovahansen_trigs<-function(data, periods, lag1=TRUE,
#' @inheritParams seasonality_qs
#' @param trigs TRUE for trigonometric variables, FALSE for seasonal dummies.
#' @param lag1 Lagged variable in the regression model.
#' @param kernel Kernel used to compute the robust covariance matrix.
#' @param order The truncation parameter used to compute the robust covariance matrix.
#' @param kernel Kernel used to compute the robust Newey-West covariance matrix.
#' @param order The truncation parameter used to compute the robust Newey-West covariance matrix.
#' @param start Position of the first observation of the series
#' @return list with the joint test and with details for the different seasonal variables
#' @return list with the FTest on seasonal variables, the joint test and the details for the stability of the different seasonal variables
#' @export
#'
#'
#' @examples
#' s<-log(ABS$X0.2.20.10.M)
#' seasonality_canovahansen(s, 12, trigs = FALSE)
Expand All @@ -187,5 +219,5 @@ seasonality_canovahansen<-function(data, period, trigs=TRUE, lag1=TRUE,
as.logical(trigs), as.logical(lag1),
kernel, as.integer(order), as.integer(start-1))
last<-length(q)
return(list(joint=q[last], details=q[-last]))
return(list(seasonality=list(value=q[last-1], pvalue=q[last]), joint=q[last-2], details=q[-c(last-2, last-1, last)]))
}
4 changes: 2 additions & 2 deletions R/tests_td.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ td_f<-function(s, model=c("D1", "DY", "DYD1", "WN", "AIRLINE", "R011", "R100"),
#' @param kernel Kernel used to compute the robust covariance matrix.
#' @param order The truncation parameter used to compute the robust covariance matrix.
#'
#' @return list with the joint test and with details for the different days (starting with Mondays).
#' @return list with the ftest on td, the joint test and the details for the stability of the different days (starting with Mondays).
#' @export
#'
#' @examples
Expand All @@ -76,5 +76,5 @@ td_canovahansen<-function(s, differencing, kernel=c("Bartlett", "Square", "Welch
jts, .jarray(as.integer(differencing)), kernel, as.integer(order))

last<-length(q)
return(list(joint=q[last], details=q[-last]))
return(list(td=list(value=q[last-1], pvalue=q[last]), joint=q[last-2], details=q[-c(last-2, last-1, last)]))
}
23 changes: 11 additions & 12 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,19 @@ test_anova<-function(ssm, dfm, ssr, dfr){
return(statisticaltest(val, pval, desc))
}

#' Title
#' Information on the (log-)likelihood
#'
#' @param nobs
#' @param neffectiveobs
#' @param nparams
#' @param ll
#' @param adjustedll
#' @param aic
#' @param aicc
#' @param bic
#' @param bicc
#' @param ssq
#' @param nobs Number of observation
#' @param neffectiveobs Number of effective observations. NA if it is the same as nobs.
#' @param nparams Number of hyper-parameters
#' @param ll Log-likelihood
#' @param adjustedll Adjusted log-likelihood when the series has been transformed
#' @param aic AIC
#' @param aicc AICC
#' @param bic BIC
#' @param bicc BIC corrected for the length
#' @param ssq Sum of the squared residuals
#'
#' @return
#' @export
#'
#' @examples
Expand Down
Binary file not shown.
Binary file modified inst/java/jdplus-sa-base-core-3.2.5-SNAPSHOT.jar
Binary file not shown.
Binary file not shown.
Binary file modified inst/java/jdplus-sa-base-r-3.2.5-SNAPSHOT.jar
Binary file not shown.
Binary file not shown.
Binary file modified inst/java/jdplus-toolkit-base-core-3.2.5-SNAPSHOT.jar
Binary file not shown.
Binary file not shown.
Binary file modified inst/java/jdplus-toolkit-base-r-3.2.5-SNAPSHOT.jar
Binary file not shown.
3 changes: 3 additions & 0 deletions man/arima_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/differences.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/differencing_fast.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/do_stationary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions man/likelihood.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions man/mad.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1854648

Please sign in to comment.