tests/testthat/test-estimation-kernels.R

# The oracle constructs the full joint precision and marginal covariance directly.
test_that('E-step moments equal dense Gaussian and Henderson equations', {
  X <- cbind(1, c(-1, 0, 1, -1, 0, 1))
  Z <- matrix(c(1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1), 6, 2)
  y <- c(1.2, 3.1, 2.4, -0.5, 1.8, 3.7)
  G <- matrix(c(1.5, 0.2, 0.2, 0.8), 2)
  R <- diag(c(0.7, 0.9, 1.1, 0.7, 0.9, 1.1))
  R[1, 2] <- R[2, 1] <- 0.15
  Ri <- solve(R)
  V <- R + Z %*% G %*% t(Z)
  Vi <- solve(V)
  b <- as.vector(solve(crossprod(X, Vi %*% X), crossprod(X, Vi %*% y)))
  u <- G %*% crossprod(Z, Vi %*% (y - X %*% b))
  H <- crossprod(Z, Ri %*% Z) + solve(G)
  M <- rbind(cbind(crossprod(X, Ri %*% X), crossprod(X, Ri %*% Z)),
             cbind(crossprod(Z, Ri %*% X), H))
  C <- solve(M)
  ld <- function(a) as.numeric(determinant(a, logarithm = TRUE)$modulus)
  for (reml in c(FALSE, TRUE)) {
    ans <- GPvam:::.gpvam_estep(Matrix::Matrix(X), y, Matrix::Matrix(Z),
                               Matrix::Matrix(Ri), b, Matrix::Matrix(G), reml)
    expected <- if (reml) C[3:4, 3:4] else solve(H)
    expect_equal(unname(as.vector(ans)), as.vector(expected), tolerance = 1e-11)
    expect_equal(as.vector(attr(ans, 'eta')), as.vector(u), tolerance = 1e-11)
    expect_equal(as.vector(attr(ans, 'ybetas')), b, tolerance = 1e-11)
    expect_equal(attr(ans, 'betacov'), unname(C[1:2, 1:2]), ignore_attr = TRUE,
                 tolerance = 1e-11)
    expect_equal(attr(ans, 'C12'), unname(C[1:2, 3:4]), ignore_attr = TRUE,
                 tolerance = 1e-11)
    loglik <- -0.5 * (6 * log(2 * pi) + ld(V) +
                      as.numeric(crossprod(y - X %*% b, Vi %*% (y - X %*% b))))
    if (reml) loglik <- loglik + log(2 * pi) - 0.5 * ld(crossprod(X, Vi %*% X))
    expect_equal(attr(ans, 'likelihood'), loglik, tolerance = 1e-11)
    observed <- GPvam:::.gpvam_residual_diagonal(X, Z, y, b, u, expected,
                                                C[1:2, 1:2], C[1:2, 3:4], reml)
    correction <- if(reml) cbind(X, Z) %*% C %*% t(cbind(X, Z)) else Z %*% solve(H) %*% t(Z)
    expect_equal(observed, as.vector(y - X %*% b - Z %*% u)^2 + diag(correction),
                 tolerance = 1e-11)
  }
})

test_that('native residual kernels match explicit block moments and row weights', {
  X <- Matrix::Matrix(cbind(1, c(-1, 1, -1, 1)), sparse = TRUE)
  Z <- Matrix::Matrix(matrix(c(1, 0, 1, 0, 0, 1, 0, 1), 4, 2), sparse = TRUE)
  beta <- matrix(c(0.4, -0.3))
  eta <- matrix(c(0.2, -0.6))
  y <- matrix(c(1, 2, -1, 0.5))
  covariance <- matrix(c(0.8, 0.1, 0.1, 0.7), 2)
  second <- covariance + tcrossprod(eta)
  betacov <- matrix(c(0.5, 0.1, 0.1, 0.3), 2)
  C12 <- matrix(c(-0.1, 0.02, -0.03, -0.05), 2)
  weights <- matrix(c(1, 2, 0.5, 3, 1.5, 0.7))
  rows <- matrix(c(1L, 3L, 4L, 6L))
  args <- list(invsqrtW_ = weights, JYp_ = y, loopsize_ = 2L,
               patternlength_ = 2L, rownumber_ = rows, ybetas_ = beta,
               etahat_ = eta, tempmatR_ = second,
               JXpi_ = as.matrix(X@i), JXpp_ = as.matrix(X@p),
               JXpx_ = as.matrix(X@x), JXpdim_ = as.matrix(X@Dim),
               JZpi_ = as.matrix(Z@i), JZpp_ = as.matrix(Z@p),
               JZpx_ = as.matrix(Z@x), JZpdim_ = as.matrix(Z@Dim))
  oracle <- function(reml) {
    result <- matrix(0, 2, 2)
    for (i in list(1:2, 3:4)) {
      Xi <- as.matrix(X[i, , drop = FALSE]); Zi <- as.matrix(Z[i, , drop = FALSE])
      r <- y[i, , drop = FALSE] - Xi %*% beta - Zi %*% eta
      moment <- tcrossprod(r) + Zi %*% covariance %*% t(Zi)
      if(reml) moment <- moment + Xi %*% betacov %*% t(Xi) +
        Xi %*% C12 %*% t(Zi) + Zi %*% t(C12) %*% t(Xi)
      W <- diag(as.vector(weights[rows[i], ]), nrow = 2)
      result <- result + W %*% moment %*% W
    }
    result
  }
  expect_equal(do.call(GPvam:::R_mstep2, args), oracle(FALSE), tolerance = 1e-12)
  args$betacov_ <- betacov; args$C12_ <- C12
  expect_equal(do.call(GPvam:::REML_Rm, args), oracle(TRUE), tolerance = 1e-12)
  expect_equal(do.call(GPvam:::R_mstep2, args), oracle(TRUE), tolerance = 1e-12)
  args$rownumber_[1] <- 100L
  expect_error(do.call(GPvam:::REML_Rm, args))
  args$betacov_ <- args$C12_ <- NULL
  expect_error(do.call(GPvam:::R_mstep2, args))
})

test_that('residual M-steps recover known covariance moments at any scale', {
  truth <- matrix(c(1.2, 0.3, 0.3, 0.8), 2)
  key <- rbind(c(0, 1), c(1, 0), c(1, 1))
  counts <- list(4, 3, 8)
  moments <- list(matrix(counts[[1]] * truth[2, 2]),
                  matrix(counts[[2]] * truth[1, 1]), counts[[3]] * truth)
  for (scale in c(1e-6, 1, 1e6)) {
    result <- GPvam:::.gpvam_update_residual(Matrix::Matrix(diag(2) * scale),
        lapply(moments, function(x) x * scale), key, counts, 1:3)
    expect_equal(as.matrix(result) / scale, truth, tolerance = 2e-5)
    exact <- GPvam:::.gpvam_update_residual(Matrix::Matrix(diag(2) * scale),
        lapply(moments, function(x) x * scale), key, counts, 3L)
    expect_equal(as.matrix(exact) / scale, truth, tolerance = 1e-12)
  }
})

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.