tests/testthat/test-simulate-bias.R

test_that("residual generation uses the covariance and original observation order", {
  Q <- Matrix::Matrix(matrix(c(3,.4,.2,0,.4,2.5,.3,.1,.2,.3,2,.5,0,.1,.5,1.7),4),sparse=TRUE)
  U <- GPvam:::.gpvam_simulation_residual_factor(list(R.inv=Q))
  B <- as.matrix(solve(U, Matrix::Diagonal(4)))
  expect_equal(B %*% t(B), solve(as.matrix(Q)), tolerance=1e-12)
  expect_gt(max(abs(t(B) %*% B - solve(as.matrix(Q)))),1e-3)
})

test_that("all persistence structures preserve the generated error decomposition", {
  d <- simulation_fixture()
  d$y[c(2,17,49,81)] <- NA_real_
  d$teacher[c(7,52)] <- NA_character_
  rownames(d) <- paste0("row",seq_len(nrow(d)))
  configurations <- expand.grid(persistence=c("GP","rGP","VP","CP","ZP"),
                                 REML=c(FALSE,TRUE),stringsAsFactors=FALSE)
  configurations$student.side <- "R"
  configurations <- rbind(configurations,data.frame(persistence="GP",REML=c(FALSE,TRUE),student.side="G"))
  # A 1e-5 likelihood stopping criterion keeps these covariance/refit tests
  # bounded. Pointwise dense-GLS identities use separate 1e-7 tolerances;
  # convergence here does not claim high-precision covariance estimation.
  for (ii in seq_len(nrow(configurations))) {
    cfg <- configurations[ii,]
    info <- paste(cfg$persistence,cfg$student.side,cfg$REML)
    fit <- GPvam(d,~as.factor(year)+x+0,persistence=cfg$persistence,student.side=cfg$student.side,
                 REML=cfg$REML,max.iter.EM=500,tol1=1e-5,verbose=FALSE)
    expect_true(fit$converged,info=info)
    if (!fit$converged) next
    p <- ncol(fit$X); n <- nrow(fit$X)
    U <- GPvam:::.gpvam_simulation_residual_factor(fit)
    B <- as.matrix(solve(U, Matrix::Diagonal(n)))
    expect_equal(B %*% t(B), solve(as.matrix(fit$R.inv)), tolerance=1e-10, info=info)
    K <- list(x=as.numeric(colnames(fit$X)=="x"))
    # Store two Gaussian draws to make direct refits exactly reproducible.
    # The generating residual law is centered at the original fitted covariance.
    set.seed(72000L+ii)
    errors <- t(chol(as.matrix(solve(fit$R.inv)))) %*% matrix(rnorm(2*n),n,2)
    err <- as.numeric(errors[,1])
    generator <- function(model,replicate) list(random_effects=as.numeric(model$eta.hat),
                                               residual_errors=as.numeric(errors[,replicate]))
    sim <- simulate_bias(fit,d,nsim=2,k_list=K,generator=generator,centered_errors=TRUE)
    expect_equal(sim$counts$Failed,0,info=info)
    expect_equal(sim$replicates$Total,sim$replicates$Estimation_Error,tolerance=1e-7,info=info)
    expect_equal(sim$replicates$Alignment,
      sim$replicates$Alignment_Teacher+sim$replicates$Alignment_Student,tolerance=1e-10,info=info)
    expect_true(sim$counts$CV_Summary_Available,info=info)
    expect_true(max(sim$fit_status$GLS_Coefficient_Error) < 1e-7,info=info)
    # Dense inversion of V=R+ZGZ' is independent of the implementation's
    # returned vinv and sparse GLS map. Validate the first simulated response.
    h <- as.numeric(fit$Z %*% fit$eta.hat)
    newdata <- d
    newdata$y[fit$observation] <- as.numeric(fit$X %*% coef(fit))+h+err
    other <- GPvam(newdata,fit$fixed_effects,persistence=cfg$persistence,
      student.side=cfg$student.side,REML=cfg$REML,max.iter.EM=500,tol1=1e-5,verbose=FALSE)
    expect_true(other$converged,info=info)
    ref <- dense_reference(other)
    X <- as.matrix(other$X)
    W <- solve(crossprod(X,ref$Vinv %*% X),crossprod(X,ref$Vinv))
    row <- sim$replicates[1,]
    base_ref <- dense_reference(fit)
    X0 <- as.matrix(fit$X)
    W0 <- solve(crossprod(X0,base_ref$Vinv %*% X0),crossprod(X0,base_ref$Vinv))
    expect_equal(row$Residual_CV,as.numeric(K[[1]] %*% (W-W0) %*% err),tolerance=1e-7,info=info)
    expect_equal(row$Alignment,as.numeric(K[[1]] %*% W %*% h),tolerance=1e-7,info=info)
    expect_equal(row$Residual,as.numeric(K[[1]] %*% W %*% err),tolerance=1e-7,info=info)
    expect_equal(row$Internal_Diagnostic,as.numeric(K[[1]] %*% W %*% other$Z %*% ref$eta),
                 tolerance=1e-7,info=info)
    expect_equal(unname(coef(other)),ref$beta,tolerance=1e-7,info=info)
    if (cfg$persistence=="VP") {
      expect_true(max(abs(other$Z-fit$Z)) > 1e-5,info=info)
      # A changed VP design must not replace the generating latent mean.
      expect_true(abs(row$Alignment-as.numeric(K[[1]] %*% W %*% other$Z %*% fit$eta.hat)) > 1e-6,info=info)
    }
    if (cfg$student.side=="G") {
      students <- grepl("^ygam\\.", colnames(fit$Z))
      expect_equal(sum(students), fit$num.student, info=info)
      expect_equal(row$Alignment_Student, as.numeric(K[[1]] %*% W %*%
        fit$Z[,students,drop=FALSE] %*% fit$eta.hat[students]), tolerance=1e-7, info=info)
      expect_equal(row$Alignment_Teacher, as.numeric(K[[1]] %*% W %*%
        fit$Z[,!students,drop=FALSE] %*% fit$eta.hat[!students]), tolerance=1e-7, info=info)
      expect_true(max(abs(sim$replicates$Alignment_Student)) > 1e-6,info=info)
    }
    else expect_equal(sim$replicates$Alignment_Student,c(0,0),info=info)
  }
})

test_that("simulation input matching, failure reporting and paired CV are explicit", {
  d <- simulation_fixture()
  d$y[2] <- NA_real_
  rownames(d) <- paste0("input",seq_len(nrow(d)))
  fit <- GPvam(d,~as.factor(year)+x+0,persistence="CP",REML=FALSE,
               max.iter.EM=500,tol1=1e-5,verbose=FALSE)
  expect_true(fit$converged)
  if (!fit$converged) return()
  bad <- d; bad$teacher[2] <- "changed"
  expect_error(simulate_bias(fit,bad,2),"keys")
  bad <- d; bad$y[1] <- bad$y[1]+1
  expect_error(simulate_bias(fit,bad,2),"responses")
  bad <- d; bad$x[1] <- bad$x[1]+1
  expect_error(simulate_bias(fit,bad,2),"design")
  bad <- d; bad$y[2] <- 1
  expect_error(simulate_bias(fit,bad,2),"missing-score")
  bad <- d; bad$student <- factor(bad$student,levels=rev(unique(bad$student)))
  expect_error(simulate_bias(fit,bad,2),"keys")
  old <- fit; old$simulation_spec <- NULL
  expect_error(simulate_bias(old,d,2),"Refit this model")
  other_locale <- fit; other_locale$simulation_spec$collation <- "different-locale"
  expect_error(simulate_bias(other_locale,d,2),"same collation")
  expect_error(simulate_bias(fit,d,2,generator=function(...) stop("bad generator")),"bad generator")
  expect_error(simulate_bias(fit,d,2,generator=function(...) list()),"Generator replicate")
  expect_error(simulate_bias(fit,d,1),"at least 2")
  expect_error(simulate_bias(fit,d,2,refit_control=list(REML=TRUE)),"only named")
  dependent <- fit; dependent$fixed_effects <- ~y
  expect_error(simulate_bias(dependent,d,2),"Response-dependent")
  dependent$fixed_effects <- ~outside
  expect_error(simulate_bias(dependent,d,2),"external vectors")
  bad <- d; bad$irrelevant <- seq_len(nrow(bad))
  set.seed(20261024)
  sim <- simulate_bias(fit,bad,nsim=3,k_list=list(x=as.numeric(colnames(fit$X)=="x")))
  expect_equal(sim$counts$Failed,0)
  expect_true(sim$counts$CV_Summary_Available)
  expect_equal(sim$replicates$Total_CV,sim$replicates$Alignment+sim$replicates$Residual_CV,tolerance=1e-12)
  v <- sim$replicates$Residual_CV
  expect_equal(subset(sim$summary,Component=="Residual_CV")$Mean,mean(v),tolerance=1e-12)
  expect_equal(subset(sim$summary,Component=="Residual_CV")$MCSE,sd(v)/sqrt(3),tolerance=1e-12)
  set.seed(20261024)
  same <- simulate_bias(fit,d,nsim=3,k_list=list(x=as.numeric(colnames(fit$X)=="x")))
  expect_equal(sim$replicates,same$replicates,tolerance=0)
  expect_false(any(c("data","model","fits","responses") %in% names(sim)))
  expect_output(printed <- print(sim), "conditional response-refit")
  expect_identical(printed,sim)
  expect_identical(getS3method("print","GPvam_simulation"),GPvam:::print.GPvam_simulation)
  set.seed(20261024)
  expect_warning(failed <- simulate_bias(fit,d,nsim=2,refit_control=list(max.iter.EM=1)),"summaries withheld")
  expect_equal(failed$counts$Failed,2)
  expect_true(all(failed$fit_status$Status=="nonconverged"))
  expect_true(all(is.na(failed$summary$Mean)))
  expect_false(failed$counts$CV_Summary_Available)
  fitter <- GPvam::GPvam
  calls <- 0L
  testthat::local_mocked_bindings(GPvam=function(...) {
    calls <<- calls+1L
    if (calls==2L) stop("deliberate refit failure")
    fitter(...)
  },.package="GPvam")
  set.seed(20261024)
  expect_warning(mixed <- simulate_bias(fit,d,nsim=2),"summaries withheld")
  expect_equal(mixed$counts$Success,1)
  expect_equal(mixed$counts$Failed,1)
  expect_true(all(is.finite(subset(mixed$replicates,Status=="ok")$Residual_CV)))
  expect_true(all(is.finite(subset(mixed$summary,Component=="Residual")$Mean)))
  expect_true(all(is.na(subset(mixed$summary,Component %in% c("Residual_CV","Total_CV"))$Mean)))
  expect_true(all(is.na(subset(mixed$summary,Component %in% c("Residual_CV","Total_CV"))$MCSE)))
})

test_that("ill-conditioning flag includes zero blocks but not every scalar boundary", {
  base <- list(teach.cov=list(matrix(0,1,1)),student.side="R",R_i=diag(2))
  expect_true(GPvam:::.gpvam_simulation_boundary(base))
  base$teach.cov <- list(matrix(1e-20,1,1))
  expect_false(GPvam:::.gpvam_simulation_boundary(base))
})

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.