tests/testthat/test-interface.R

test_that("malformed inputs fail before estimation", {
  d <- vam_fixture(nyear = 2L)
  expect_error(GPvam(as.matrix(d)), "data frame")
  expect_error(GPvam(d[FALSE, ]), "nonempty")
  expect_error(GPvam(d[, setdiff(names(d), "teacher")]), "required columns.*teacher")
  expect_error(GPvam(rbind(d, d[1, ])), "one row per year")
  for (args in list(list(student.side = 1), list(student.side = "Q"),
                   list(persistence = NA_character_), list(persistence = c("CP", "GP")),
                   list(hes.method = "Richardson"), list(max.iter.EM = 0),
                   list(max.iter.EM = 1.2), list(max.iter.EM = Inf),
                   list(tol1 = NA_real_), list(tol1 = -1), list(hessian = 1),
                   list(verbose = c(TRUE, FALSE)), list(REML = NA))) {
    expect_error(do.call(GPvam, c(list(vam_data = d), args)))
  }
  expect_error(GPvam(d, y ~ x), "one-sided")
  expect_error(GPvam(d, ~offset(x)), "Offsets")
  expect_error(GPvam(d, ~0), "at least one column")
  expect_error(GPvam(d, student.side = "G", persistence = "CP"), "requires")
  for (field in c("student", "year")) {
    x <- d
    x[[field]][1] <- NA
    expect_error(GPvam(x), "missing|finite|nonmissing")
  }
  x <- d
  x$y[1] <- Inf
  expect_error(GPvam(x), "finite")
  x$y <- NA_real_
  expect_error(GPvam(x), "no observed")
  x <- d
  x$x[1] <- NA
  expect_error(GPvam(x, ~x), "missing")
  x$x[1] <- Inf
  expect_error(GPvam(x, ~x), "finite")
  x <- d
  x$x2 <- x$x
  expect_error(GPvam(x, ~x+x2), "full column rank")
  x$y[x$year == 1] <- 2
  expect_error(GPvam(x), "positive observed score variance")
})

test_that("input data are not mutated and unused missing covariates are harmless", {
  d <- vam_fixture(nyear = 2L)
  d$unused <- NA_real_
  original <- d
  fit <- suppressWarnings(GPvam(d, ~as.factor(year)+x, persistence = "CP",
                                max.iter.EM = 2, verbose = FALSE))
  expect_identical(d, original)
  expect_s3_class(fit, "GPvam")
  expect_identical(fit$REML, TRUE)
  expect_identical(fit$student.side, "R")
  expect_identical(fit$persistence, "CP")
  expect_false(fit$converged)
  expect_warning(GPvam(d, persistence = "CP", max.iter.EM = 1, verbose = FALSE),
                 "did not converge")
})

test_that("factor IDs, arbitrary year labels and missing teachers are handled", {
  d <- vam_fixture(nyear = 2L)
  d$student <- factor(d$student)
  d$teacher <- factor(paste0("NANCY", d$teacher))
  d$year <- c(2010, 2012)[d$year]
  d$teacher[1] <- NA
  fit <- suppressWarnings(GPvam(d, persistence = "CP", max.iter.EM = 2, verbose = FALSE))
  expect_true(all(fit$teach.effects$teacher_year %in% c(2010, 2012)))
  expect_true(all(grepl("^NANCY", fit$teach.effects$teacher)))
  expect_equal(sum(fit$num.teach), 12)
  expect_false(any(grepl("^NA\\(year", fit$teach.effects$teacher)))
})

test_that("unused factor levels do not make a valid fixed design rank deficient", {
  d <- vam_fixture(nyear = 2L)
  d$category <- factor(rep(c("A", "B"), length.out = nrow(d)), levels = c("A", "B", "unused"))
  fit <- suppressWarnings(GPvam(d, ~category+x, persistence = "CP",
                                max.iter.EM = 2, verbose = FALSE))
  expect_equal(ncol(fit$X), 3)
  d$category[1] <- "unused"
  d$y[1] <- NA
  fit <- suppressWarnings(GPvam(d, ~category+x, persistence = "CP",
                                max.iter.EM = 2, verbose = FALSE))
  expect_equal(ncol(fit$X), 3)
})

test_that("standard methods retain precision and observed input order", {
  d <- vam_fixture(nyear = 2L)
  d <- d[rev(seq_len(nrow(d))), ]
  d$y[c(3, 9)] <- NA
  fit <- suppressWarnings(GPvam(d, ~as.factor(year)+x, persistence = "CP",
                                max.iter.EM = 3, verbose = FALSE))
  expect_equal(unname(coef(fit)), as.numeric(fit$ybetas))
  expect_equal(unname(vcov(fit)), unname(as.matrix(fit$betacov)))
  expect_equal(nobs(fit), sum(!is.na(d$y)))
  expect_identical(names(fitted(fit)), rownames(d)[!is.na(d$y)])
  expect_equal(unname(fitted(fit) + residuals(fit)), d$y[!is.na(d$y)])
  expect_equal(unname(fitted(fit, "marginal") + residuals(fit, "marginal")),
               d$y[!is.na(d$y)])
  expect_equal(unname(residuals(fit, "scaled")),
               as.numeric(fit$sresid)[order(fit$observation)])
  expect_s3_class(logLik(fit), "logLik")
  expect_identical(attr(logLik(fit), "df"), nrow(fit$parameters))
  expect_equal(as.numeric(logLik(fit)), fit$loglik)
  expect_equal(stats::AIC(fit), -2*fit$loglik + 2*nrow(fit$parameters))
  expect_error(residuals(fit, "unknown"), "arg")
})
test_that("REML is the default and explicit ML remains available", {
  d <- vam_fixture(nstudent = 24L, nyear = 2L)
  default <- suppressWarnings(GPvam(d, ~as.factor(year)+x, persistence = "CP",
                                    max.iter.EM = 3, verbose = FALSE))
  explicit <- suppressWarnings(GPvam(d, ~as.factor(year)+x, persistence = "CP", REML = TRUE,
                                     max.iter.EM = 3, verbose = FALSE))
  ml <- suppressWarnings(GPvam(d, ~as.factor(year)+x, persistence = "CP", REML = FALSE,
                               max.iter.EM = 3, verbose = FALSE))
  expect_true(default$REML)
  expect_identical(default$loglik, explicit$loglik)
  expect_false(ml$REML)
  expect_equal(ml$loglik, dense_reference(ml)$loglik, tolerance = 1e-8)
})

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.