## ============================================================================= ## arimasel tests ## ============================================================================= context("hqic") test_that("hqic returns a finite numeric scalar for a valid Arima object", { fit <- arima(.x_stat, order = c(1, 0, 0)) val <- hqic(fit) expect_true(is.numeric(val)) expect_length(val, 1L) expect_true(is.finite(val)) }) test_that("hqic errors on non-Arima objects", { expect_error(hqic(lm(y ~ x, data = data.frame(y = 1:5, x = 1:5))), "class 'Arima'") }) test_that("hqic is larger than AIC for small n (stronger penalty for ln ln n < 2)", { fit <- arima(.x_short, order = c(1, 0, 0)) # ln(ln(15)) ~ 0.996 so penalty is 2 * k * 0.996; AIC penalty is 2 * k # so HQIC < AIC for n < e^e ~ 15.15 # Just check both finite expect_true(is.finite(hqic(fit))) expect_true(is.finite(AIC(fit))) }) ## --------------------------------------------------------------------------- # context("cp_sets") test_that("cp_sets returns correct element count", { res <- cp_sets(0:1, 0:1, 0:1) expect_equal(res$n_models, 2L * 2L * 2L) }) test_that("cp_sets returns cartProduct class", { res <- cp_sets(0:2, 0:1, 0:2) expect_s3_class(res, "cartProduct") }) test_that("cp_sets errors on negative values", { expect_error(cp_sets(p_set = -1:1), ">= 0") }) test_that("cp_sets model_strings has correct format", { res <- cp_sets(0:1, 1, 0:1) expect_true(all(grepl("^ARIMA\\(\\d,\\d,\\d\\)$", res$model_strings))) }) ## --------------------------------------------------------------------------- # context("arima_weights") test_that("arima_weights returns weights summing to 1", { ic <- c(200, 202, 205) w <- arima_weights(ic) expect_equal(sum(w$Weight), 1, tolerance = 1e-6) }) test_that("arima_weights: all weights non-negative", { ic <- c(100, 110, 120) w <- arima_weights(ic) expect_true(all(w$Weight >= 0)) }) test_that("arima_weights: best model has Weight closest to 1", { ic <- c(100, 200, 300) w <- arima_weights(ic) expect_equal(w$Weight[1L], max(w$Weight)) }) test_that("arima_weights errors on length-1 input", { expect_error(arima_weights(123), "length >= 2") }) test_that("arima_weights returns correct Delta for best model", { ic <- c(A = 198, B = 200, C = 202) w <- arima_weights(ic) expect_equal(w$Delta[1L], 0) }) ## --------------------------------------------------------------------------- # context("cart_arima") test_that("cart_arima returns cartARIMA class", { res <- cart_arima(.x_stat, p_set = 0:1, d_set = 0L, q_set = 0:1) expect_s3_class(res, "cartARIMA") }) test_that("cart_arima full_table is ranked in ascending order by primary criterion", { res <- cart_arima(.x_stat, p_set = 0:2, d_set = 0:1, q_set = 0:2, criterion = "AIC") aic_vals <- res$full_table$AIC expect_true(all(diff(aic_vals) >= 0)) }) test_that("cart_arima best_model_str matches top row of full_table", { res <- cart_arima(.x_stat, p_set = 0:1, d_set = 0L, q_set = 0:1) expect_equal(res$best_model_str, res$full_table$Model[1L]) }) test_that("cart_arima n_total equals |P| x |D| x |Q|", { res <- cart_arima(.x_stat, p_set = 0:2, d_set = 0:1, q_set = 0:2) expect_equal(res$n_total, 3L * 2L * 3L) }) test_that("cart_arima n_converged <= n_total", { res <- cart_arima(.x_stat, p_set = 0:2, d_set = 0:1, q_set = 0:2) expect_lte(res$n_converged, res$n_total) }) test_that("cart_arima top_n limits table rows", { res <- cart_arima(.x_stat, p_set = 0:2, d_set = 0:1, q_set = 0:2, top_n = 3L) expect_lte(nrow(res$table), 3L) }) test_that("cart_arima with BIC criterion gives BIC-ranked table", { res <- cart_arima(.x_stat, p_set = 0:2, d_set = 0:1, q_set = 0:2, criterion = "BIC") expect_true(all(diff(res$full_table$BIC) >= 0)) expect_equal(res$criterion, "BIC") }) test_that("cart_arima with HQIC criterion works", { res <- cart_arima(.x_stat, p_set = 0:1, d_set = 0L, q_set = 0:1, criterion = "HQIC") expect_true(all(diff(res$full_table$HQIC) >= 0)) }) test_that("cart_arima errors on series with < 10 obs", { expect_error(cart_arima(rnorm(8)), "at least 10") }) test_that("cart_arima works on I(1) series with d_set including 1", { res <- cart_arima(.x_I1, p_set = 0:1, d_set = 1L, q_set = 0:1) expect_s3_class(res, "cartARIMA") expect_true(all(res$full_table$d == 1L)) }) test_that("cart_arima Akaike weights in full_table sum to 1", { res <- cart_arima(.x_stat, p_set = 0:1, d_set = 0:1, q_set = 0:1) expect_equal(sum(res$full_table$Weight), 1, tolerance = 1e-4) }) ## --------------------------------------------------------------------------- # context("S3 methods for cartARIMA") test_that("coef() returns named numeric vector", { res <- cart_arima(.x_stat, p_set = 0:1, d_set = 0L, q_set = 0:1) cfs <- coef(res) expect_true(is.numeric(cfs)) expect_true(!is.null(names(cfs))) }) test_that("residuals() has length equal to n_obs", { res <- cart_arima(.x_stat, p_set = 0:1, d_set = 0L, q_set = 0:1) expect_equal(length(residuals(res)), res$n_obs) }) test_that("fitted() returns finite values", { res <- cart_arima(.x_stat, p_set = 0:1, d_set = 0L, q_set = 0:1) fv <- fitted(res) expect_true(is.numeric(fv)) expect_true(any(is.finite(fv))) }) test_that("AIC() is numeric and finite", { res <- cart_arima(.x_stat, p_set = 0:1, d_set = 0L, q_set = 0:1) expect_true(is.finite(AIC(res))) }) test_that("BIC() is numeric and finite", { res <- cart_arima(.x_stat, p_set = 0:1, d_set = 0L, q_set = 0:1) expect_true(is.finite(BIC(res))) }) test_that("logLik() returns a logLik object", { res <- cart_arima(.x_stat, p_set = 0:1, d_set = 0L, q_set = 0:1) expect_s3_class(logLik(res), "logLik") }) ## --------------------------------------------------------------------------- # context("arima_table") test_that("arima_table returns a data.frame", { res <- cart_arima(.x_stat, p_set = 0:2, d_set = 0:1, q_set = 0:2) tbl <- arima_table(res) expect_s3_class(tbl, "data.frame") }) test_that("arima_table with BIC criterion re-ranks correctly", { res <- cart_arima(.x_stat, p_set = 0:2, d_set = 0:1, q_set = 0:2, criterion = "AIC") tbl <- arima_table(res, criterion = "BIC") expect_true(all(diff(tbl$BIC) >= 0)) }) test_that("arima_table top_n limits rows", { res <- cart_arima(.x_stat, p_set = 0:2, d_set = 0:1, q_set = 0:2) tbl <- arima_table(res, top_n = 4L) expect_lte(nrow(tbl), 4L) }) ## --------------------------------------------------------------------------- # context("stationarity_test") test_that("stationarity_test returns data.frame with >=1 rows", { res <- stationarity_test(.x_stat) expect_s3_class(res, "data.frame") expect_gte(nrow(res), 1L) }) test_that("stationarity_test has required columns", { res <- stationarity_test(.x_I1) expect_true(all(c("Test", "Statistic", "p_value", "Conclusion") %in% names(res))) }) test_that("stationarity_test has consensus attribute", { res <- stationarity_test(.x_stat) expect_true(!is.null(attr(res, "consensus"))) }) ## --------------------------------------------------------------------------- # context("suggest_d") test_that("suggest_d returns an integer in 0:2 for stationary series", { d <- suggest_d(.x_stat) expect_true(d %in% 0:2) }) test_that("suggest_d returns d >= 1 for I(1) series", { d <- suggest_d(.x_I1) expect_gte(d, 0L) # may be 0 or 1 depending on test }) test_that("suggest_d is an integer", { expect_true(is.integer(suggest_d(.x_stat))) }) ## --------------------------------------------------------------------------- # context("arima_forecast") test_that("arima_forecast returns a list with correct structure", { res <- cart_arima(.x_stat, p_set = 0:1, d_set = 0L, q_set = 0:1) fc <- arima_forecast(res, h = 5L, plot = FALSE) expect_true(is.list(fc)) expect_true(all(c("mean", "lower", "upper", "level") %in% names(fc))) }) test_that("arima_forecast mean has length h", { res <- cart_arima(.x_stat, p_set = 0:1, d_set = 0L, q_set = 0:1) fc <- arima_forecast(res, h = 6L, plot = FALSE) expect_length(fc$mean, 6L) }) test_that("arima_forecast lower <= upper for all PIs", { res <- cart_arima(.x_stat, p_set = 0:1, d_set = 0L, q_set = 0:1) fc <- arima_forecast(res, h = 5L, plot = FALSE, level = c(80, 95)) expect_true(all(fc$lower <= fc$upper, na.rm = TRUE)) }) test_that("arima_forecast ensemble works without error", { res <- cart_arima(.x_stat, p_set = 0:1, d_set = 0L, q_set = 0:1) fc <- arima_forecast(res, h = 4L, ensemble = TRUE, top_k = 2L, plot = FALSE) expect_false(is.null(fc$ensemble_mean)) expect_length(fc$ensemble_mean, 4L) }) ## --------------------------------------------------------------------------- # context("compare_arima") test_that("compare_arima returns a list with comparison_table", { res <- compare_arima(.x_stat, p_set = 0:1, d_set = 0L, q_set = 0:1) expect_true(is.list(res)) expect_true("comparison_table" %in% names(res)) }) test_that("compare_arima comparison_table has Method column", { res <- compare_arima(.x_stat, p_set = 0:1, d_set = 0L, q_set = 0:1) expect_true("Method" %in% names(res$comparison_table)) })