suppressPackageStartupMessages(library(lme4))
suppressPackageStartupMessages(library(fastglmm))
script_dir <- local({
args <- commandArgs(trailingOnly = FALSE)
file_arg <- sub("^--file=", "", args[grep("^--file=", args)])
dirname(normalizePath(if (length(file_arg)) file_arg else "."))
})
source(file.path(script_dir, "_oracle.R"))
data(cbpp)
cbpp$prop <- cbpp$incidence / cbpp$size
fit <- fastglmm(prop ~ period + (1 | herd), cbpp,
family = binomial(), weights = size)
cat("converged:", fit$converged, " singular:", fit$singular, "\n")
summary(fit)
vc <- VarCorr(fit)
cat("\nherd stddev:", attr(vc$herd, "stddev")[1], "\n")
cat("loglik:", fit$loglik, "\n")
cat("\noracle cross-check vs goldens/cbpp_agq_k1.json (manifest rung 5, nAGQ=1):\n")
g <- load_golden("cbpp_agq_k1")
est <- g$estimates
for (i in seq_along(g$coef_names)) {
check_rel(sprintf("beta[%s]", g$coef_names[i]), unname(fixef(fit)[i]), est$beta[i], TOL_BETA_REL)
}
for (i in seq_along(g$coef_names)) {
check_rel(sprintf("se_hessian[%s]", g$coef_names[i]), unname(fit$se[i]), est$se_hessian[i], TOL_SE_HESSIAN_REL)
}
check_rel("herd stddev", attr(vc$herd, "stddev")[1], est$varcomp$stddev[[1]][1], TOL_STDDEV_REL)
check_abs("loglik", fit$loglik, est$loglik, TOL_LOGLIK_ABS_GLMM)