tests/testthat/test-diagnostics.R

diagnostic_result <- function(eta, num_teach = length(eta), persistence = "CP") {
  n <- length(eta)
  structure(list(
    X = matrix(1, n, 1, dimnames = list(NULL, "intercept")),
    Z = diag(n), vinv = diag(seq_len(n), n), G = diag(n),
    eta.hat = eta, num.teach = num_teach, persistence = persistence,
    student.side = "R"), class = "GPvam")
}

quiet_bias <- function(object, ...) {
  bias.test.custom(object, ..., plot = FALSE, verbose = FALSE)
}

test_that("singleton and empty teacher strata never sample numeric values", {
  for (eta in list(7, -3, 0, 0.2)) {
    result <- quiet_bias(diagnostic_result(eta), n_perms = 5)
    expect_equal(result$permutation_results$Nu_Prime_Eta, eta)
    expect_equal(result$permutation_results$Permutation_P_Value, 1)
    expect_equal(result$permutation_results$Permutation_Percentile, 50)
    expect_equal(result$permutation_results$Permutation_Mean, eta)
    expect_equal(result$plot_list[[1]]$data$permutation_record, rep(eta, 5))
    expect_no_warning(ggplot2::ggplot_build(result$plot_list[[1]]))
  }
  result <- quiet_bias(diagnostic_result(c(2, 7), c(0, 1, 0, 1)), n_perms = 5)
  expect_equal(result$permutation_results$Nu_Prime_Eta, 16 / 3)
  expect_equal(result$permutation_results$Permutation_P_Value, 1)
})

test_that("permutation contrasts agree with dense GLS and a finite reference set", {
  object <- diagnostic_result(c(1, 2, 4))
  object$X <- cbind(intercept = 1, trend = c(-1, 0, 1))
  k <- c(2, -1)
  # Independent dense GLS expression; no GPvam covariance helper is used.
  nu <- as.vector(k %*% solve(crossprod(object$X, object$vinv %*% object$X),
                              crossprod(object$X, object$vinv %*% object$Z)))
  reference_orders <- rbind(c(1, 2, 3), c(1, 3, 2), c(2, 1, 3),
                           c(2, 3, 1), c(3, 1, 2), c(3, 2, 1))
  reference <- apply(reference_orders, 1, function(i) sum(nu * object$eta.hat[i]))
  set.seed(7814)
  result <- quiet_bias(object, k_vectors = k, n_perms = 99)
  draws <- result$plot_list[[1]]$data$permutation_record
  expect_equal(result$permutation_results$Nu_Prime_Eta, reference[1])
  expect_true(all(vapply(draws, function(x) min(abs(x - reference)) < 1e-12,
                        logical(1))))
  expect_equal(result$permutation_results$Permutation_P_Value,
               (1 + sum(abs(draws) >= abs(reference[1]))) / 100)
  set.seed(7814)
  repeated <- quiet_bias(object, k_vectors = k, n_perms = 99)
  expect_identical(draws, repeated$plot_list[[1]]$data$permutation_record)
})

test_that("Monte Carlo p-values include the observed ordering", {
  object <- diagnostic_result(c(1, 2, 4))
  # The observed order uniquely maximizes the positive weighted sum.
  set.seed(2)
  result <- quiet_bias(object, n_perms = 1)
  expect_lt(result$plot_list[[1]]$data$permutation_record,
            result$permutation_results$Nu_Prime_Eta)
  expect_equal(result$permutation_results$Permutation_P_Value, 1 / 2)
  expect_equal(result$permutation_results$Permutation_Percentile, 100)
})

test_that("directional percentiles handle finite ties, sign and response units", {
  object <- diagnostic_result(c(1, 3))
  # The only possible diagnostics are 5/3 and 7/3, with observed value 7/3.
  # Every swap contributes 100 percentile points, every unchanged draw 50.
  set.seed(701)
  positive <- quiet_bias(object, k_vectors = 1, n_perms = 199)
  draws <- positive$plot_list[[1]]$data$permutation_record
  n_swapped <- sum(draws < 2)
  expected_percentile <- (100 * n_swapped + 50 * (199 - n_swapped)) / 199
  expect_equal(positive$permutation_results$Permutation_Percentile,
               expected_percentile)
  expect_equal(positive$permutation_results$Permutation_Mean,
               (5 * n_swapped + 7 * (199 - n_swapped)) / (3 * 199))
  set.seed(701)
  negative <- quiet_bias(object, k_vectors = -1, n_perms = 199)
  expect_equal(negative$permutation_results$Permutation_Percentile,
               100 - expected_percentile)
  expect_equal(negative$permutation_results$Permutation_Mean,
               -positive$permutation_results$Permutation_Mean)
  for (scale in c(1e-15, 1e15)) {
    scaled <- object
    scaled$eta.hat <- scale * object$eta.hat
    set.seed(701)
    result <- quiet_bias(scaled, k_vectors = 1, n_perms = 199)
    expect_equal(result$permutation_results$Permutation_Percentile,
                 expected_percentile)
  }
  expect_identical(names(positive$permutation_results)[1:3],
                   c("Fixed_Effect", "Nu_Prime_Eta", "Permutation_P_Value"))
})

test_that("GP and rGP permutations move complete teacher vectors", {
  for (persistence in c("GP", "rGP")) {
    width <- if (persistence == "GP") 3L else 2L
    eta <- c(seq_len(width), 10 * seq_len(width), 100, 200, 300)
    object <- diagnostic_result(eta, c(2, 1, 1), persistence)
    # Correlated within-teacher blocks, with distinct variances by effect.
    gamma <- diag(seq_len(width)) + matrix(0.1, width, width)
    object$G <- as.matrix(Matrix::bdiag(gamma, gamma, diag(2), 1))
    weights <- seq_along(eta) / sum(seq_along(eta))
    swapped <- c(eta[width + seq_len(width)], eta[seq_len(width)],
                 eta[2 * width + seq_len(3)])
    reference <- c(sum(weights * eta), sum(weights * swapped))
    set.seed(8041)
    result <- quiet_bias(object, n_perms = 60)
    draws <- result$plot_list[[1]]$data$permutation_record
    expect_true(all(vapply(draws, function(x) min(abs(x - reference)) < 1e-12,
                          logical(1))))
    expect_length(unique(round(draws, 10)), 2)
  }
})

test_that("G-side students form a separate permutation stratum", {
  object <- diagnostic_result(c(10, 20, 1, 2), 2, "GP")
  object$student.side <- "G"
  object$num.student <- 2
  orders <- rbind(c(1, 2, 3, 4), c(2, 1, 3, 4),
                 c(1, 2, 4, 3), c(2, 1, 4, 3))
  reference <- apply(orders, 1, function(i) sum((1:4) * object$eta.hat[i]) / 10)
  set.seed(664)
  result <- quiet_bias(object, n_perms = 60)
  draws <- result$plot_list[[1]]$data$permutation_record
  expect_true(all(vapply(draws, function(x) min(abs(x - reference)) < 1e-12,
                        logical(1))))
  expect_length(unique(round(draws, 10)), 4)
})

test_that("G-side diagnostics exclude unused factor levels and unobserved students", {
  d <- vam_fixture(nstudent = 24L, nyear = 3L)
  d$student <- factor(d$student, levels = seq_len(30))
  d$y[d$student == 24] <- NA_real_
  fit <- suppressWarnings(GPvam(d, ~as.factor(year) + x,
                                student.side = "G", persistence = "GP",
                                max.iter.EM = 3, verbose = FALSE))
  expect_equal(fit$num.student, 23)
  expect_equal(ncol(fit$Z), 23 + sum(fit$num.teach * c(3, 2, 1)))
  set.seed(4719)
  result <- quiet_bias(fit, n_perms = 19)
  expect_equal(nrow(result$permutation_results), ncol(fit$X))
  expect_true(all(is.finite(result$permutation_results$Nu_Prime_Eta)))
  expect_true(all(result$permutation_results$Permutation_P_Value >= 1 / 20))
})

test_that("bias diagnostics validate controls and contrast dimensions", {
  object <- diagnostic_result(c(1, 2))
  for (n in list(0, -1, 1.1, NA_real_, Inf, c(1, 2), "2")) {
    expect_error(quiet_bias(object, n_perms = n), "n_perms")
  }
  for (k in list(list(), NA_real_, Inf, c(1, 2), "x", matrix(1))) {
    expect_error(quiet_bias(object, k_vectors = k, n_perms = 1), "k_vectors")
  }
  expect_error(bias.test.custom(object, plot = NA), "plot")
  expect_error(bias.test.custom(object, verbose = 1), "verbose")
  object$num.teach <- 1
  expect_error(quiet_bias(object, n_perms = 1), "block sizes")
  object$num.teach <- -1
  expect_error(quiet_bias(object, n_perms = 1), "num.teach")
  object$num.teach <- 2
  object$X[1] <- Inf
  expect_error(quiet_bias(object, n_perms = 1), "finite matrix")
})

test_that("zero contrasts have probability one and renderable histograms", {
  set.seed(310)
  result <- quiet_bias(diagnostic_result(c(1, -1)), k_vectors = 0, n_perms = 9)
  expect_equal(result$permutation_results$Nu_Prime_Eta, 0)
  expect_equal(result$permutation_results$Permutation_P_Value, 1)
  expect_equal(result$permutation_results$Permutation_Percentile, 50)
  expect_equal(result$permutation_results$Permutation_Mean, 0)
  expect_no_warning(ggplot2::ggplot_build(result$plot_list[[1]]))
})

summary_fixture <- function() {
  structure(list(loglik = -12, parameters = matrix(0, 5, 2), num.obs = 50,
                 num.year = 1L, num.student = 25L, num.teach = 2L,
                 key = matrix(c(2001, 1), 1, 2), iter = 10L,
                 teach.cov = list(matrix(1)), R_i = matrix(2), stu.cov = NA_real_,
                 mresid = c(-1, 0, 1), cresid = c(-1, 0, 1),
                 sresid = c(-1, 0, 1), REML = FALSE), class = "GPvam")
}

test_that("information criteria use explicit counts and guard finite correction", {
  object <- summary_fixture()
  result <- summary(object)
  expect_equal(result$AIC, 34)
  expect_equal(result$AICc, 34 + 60 / 44)
  object$REML <- TRUE
  result <- summary(object)
  expect_equal(result$AIC, 34)
  expect_true(is.na(result$AICc))
  object$REML <- FALSE
  for (n in c(5, 6)) {
    object$num.obs <- n
    expect_true(is.na(summary(object)$AICc))
  }
})

test_that("print methods return their object invisibly", {
  object <- summary_fixture()
  capture.output(printed <- withVisible(print(object)))
  expect_identical(printed$value, object)
  expect_false(printed$visible)
  object$REML <- TRUE
  summarized <- summary(object)
  output <- capture.output(printed <- withVisible(print(summarized)))
  expect_identical(printed$value, summarized)
  expect_false(printed$visible)
  expect_true(any(grepl("restricted log-likelihood", output, fixed = TRUE)))
})

test_that("small-scale covariance summaries preserve precision and finite correlations", {
  d <- vam_fixture(nstudent = 24L, nyear = 2L)
  d$y <- d$y / 1000
  for (side in c("R", "G")) {
    fit <- suppressWarnings(GPvam(d, ~as.factor(year) + x, student.side = side,
                                  persistence = if (side == "R") "CP" else "GP",
                                  max.iter.EM = 3, verbose = FALSE))
    expect_no_warning(capture.output(print(summary(fit))))
    expect_no_warning(capture.output(print(fit)))
    offset <- if (side == "G") fit$num.student else 0L
    expect_equal(fit$teach.effects$EBLUP,
                 as.numeric(fit$eta.hat)[offset + seq_len(nrow(fit$teach.effects))],
                 tolerance = 1e-12)
    for (g in seq_along(fit$num.teach)) {
      indices <- offset + seq_len(fit$Kg[g])
      expect_equal(unname(as.matrix(fit$teach.cov[[g]])),
                   unname(as.matrix(fit$G[indices, indices, drop = FALSE])),
                   tolerance = 1e-12)
      expect_true(all(is.finite(stats::cov2cor(as.matrix(fit$teach.cov[[g]])))))
      offset <- offset + fit$num.teach[g] * fit$Kg[g]
    }
    if (side == "R") {
      covariance <- solve(as.matrix(fit$R.inv))
      expect_equal(unname(as.matrix(fit$R_i)), covariance[1:2, 1:2], tolerance = 1e-12)
      expect_true(all(is.finite(stats::cov2cor(as.matrix(fit$R_i)))))
    }
  }
})

plot_fixture <- function() {
  object <- summary_fixture()
  object$teach.effects <- data.frame(
    teacher_year = c("2001", "2001", "2001", "2001", "2003"),
    effect_year = c("2001", "2001", "2003", "2003", "2003"),
    teacher = c("A", "B", "A", "B", "C"),
    EBLUP = c(-1, 1, -0.5, 0.5, 0), std_error = rep(.2, 5))
  object$persistence <- "GP"
  object$yhat <- object$yhat.s <- object$yhat.m <- c(1, 2, 3)
  object
}

test_that("plots respect original year labels and restore device interaction", {
  grDevices::pdf(tempfile(fileext = ".pdf"))
  on.exit(grDevices::dev.off(), add = TRUE)
  grDevices::devAskNewPage(TRUE)
  object <- plot_fixture()
  expect_no_warning(result <- plot(object, ask = FALSE, col = "navy", cex = .8))
  expect_identical(result, object)
  expect_true(grDevices::devAskNewPage())
  expect_error(plot(object, alpha = 1, ask = FALSE), "alpha")
  expect_true(grDevices::devAskNewPage())
  expect_error(plot(object, ask = NA), "ask")
  # Trigger an error after changing the device setting.
  object$teach.effects <- 1
  expect_error(plot(object, ask = FALSE))
  expect_true(grDevices::devAskNewPage())
})

Try the GPvam package in your browser

Any scripts or data that you put into this service are public.

GPvam documentation built on Oct. 23, 2026, 1:06 a.m.