Nothing
test_that("optimize() marginal MAP and Satterthwaite DF work correctly", {
set.seed(1)
Y1 <- rnorm(40, mean = 10, sd = 2)
Y2 <- rnorm(40, mean = 11, sd = 2)
mdl_tt <- rtmb_ttest(Y1, Y2, prior = prior_jzs(r = 0.707))
fit_tt <- mdl_tt$optimize(
marginal = "auto",
df_method = "satterthwaite"
)
expect_s3_class(fit_tt, "map_fit")
expect_true(all(c("total_mean", "delta") %in% fit_tt$marginal_vars))
expect_true(all(c("total_mean", "delta") %in% fit_tt$laplace_random_vars))
expect_true(fit_tt$show_df)
tab_tt <- fit_tt$summary()
expect_true("df" %in% colnames(tab_tt))
# Verify DF for derived quantities
for (nm in c("diff", "mean0", "mean1")) {
df_val <- tab_tt[nm, "df"]
expect_false(is.na(df_val))
expect_true(df_val < 1e12)
}
# delta should be Inf or large finite due to low sensitivity in this balanced case
expect_true(is.infinite(tab_tt["delta", "df"]))
})
test_that("JZS unequal-variance t-test uses an explicit delta parameter", {
set.seed(2)
Y1 <- rnorm(20, mean = 10, sd = 1)
Y2 <- rnorm(25, mean = 11, sd = 2)
mdl_tt <- rtmb_ttest(Y1, Y2, prior = prior_jzs(r = 0.707), var.equal = FALSE)
expect_true(all(c("total_mean", "sd", "delta") %in% names(mdl_tt$par_list)))
expect_false(any(c("mean0", "mean1") %in% names(mdl_tt$par_list)))
expect_true(all(c("total_mean", "delta") %in% mdl_tt$extra$marginal))
fit_null <- mdl_tt$optimize(fixed = list(delta = 0), se_method = "none")
expect_s3_class(fit_null, "map_fit")
})
test_that("Mixed model metadata separation and profile() logic", {
skip_if_not_installed("lme4")
data("sleepstudy", package = "lme4")
mdl_mm <- rtmb_lmer(Reaction ~ Days + (1 | Subject), data = sleepstudy)
fit_mm <- mdl_mm$optimize(laplace = TRUE, marginal = "auto")
expect_true(all(c("Intercept", "b") %in% fit_mm$marginal_vars))
expect_true(all(c("Intercept", "b", "r_re") %in% fit_mm$laplace_random_vars))
expect_gt(length(fit_mm$laplace_random_vars), length(fit_mm$marginal_vars))
# Marginalized vars should be blocked from profiling
expect_error(fit_mm$profile(pars = "Intercept"), "marginalized")
# Non-marginalized fixed parameter should be profile-able in a non-marginalized fit
fit_mm_none <- mdl_mm$optimize(marginal = "none")
prof_mm <- fit_mm_none$profile(pars = "b[Days]", quiet = TRUE)
expect_false(is.null(prof_mm))
})
test_that("classic() lmer fixed-effect SE uses GLS covariance", {
old_silent <- options(BayesRTMB.silent = TRUE)
on.exit(options(old_silent), add = TRUE)
fit <- rtmb_lmer(sat ~ talk + perf + (1 | group), data = debate)$classic()
X <- cbind(Intercept = 1, fit$model$data$X)
sigma <- as.numeric(fit$par$sigma)
tau <- as.numeric(fit$par$sd)
group_idx <- as.integer(fit$model$data$group_idx)
Z <- as.matrix(fit$model$data$Z_mat)
V <- diag(rep(sigma^2, nrow(X)))
for (g in unique(group_idx)) {
idx <- which(group_idx == g)
Z_g <- Z[idx, , drop = FALSE]
V[idx, idx] <- V[idx, idx, drop = FALSE] + tau^2 * tcrossprod(Z_g)
}
V_beta <- solve(crossprod(X, solve(V, X)))
rows <- c("Intercept", "b[talk]", "b[perf]")
expect_equal(
unname(fit$fit[rows, "Std. Error"]),
unname(sqrt(diag(V_beta))),
tolerance = 1e-6
)
expect_equal(
unname(fit$vcov[rows, rows]),
unname(V_beta),
tolerance = 1e-6
)
expect_equal(
unname(fit$vcov_unc[seq_along(rows), seq_along(rows)]),
unname(V_beta),
tolerance = 1e-6
)
})
test_that("se_method = 'none' and df_method = 'bw' rejection", {
Y1 <- rnorm(20); Y2 <- rnorm(20)
mdl_tt <- rtmb_ttest(Y1, Y2)
expect_warning(
fit_none <- mdl_tt$optimize(se_method = "none", df_method = "satterthwaite"),
"df_method is ignored"
)
tab_none <- fit_none$summary()
expect_false(fit_none$show_df)
expect_true(all(is.na(tab_none$`Std. Error`)))
expect_false("df" %in% colnames(tab_none))
expect_error(mdl_tt$optimize(df_method = "bw"), "not supported")
})
test_that("sampling SE works with a one-dimensional covariance matrix", {
old_silent <- options(BayesRTMB.silent = TRUE)
on.exit(options(old_silent), add = TRUE)
code_binom <- rtmb_code(
parameters = {
theta <- Dim(lower = 0, upper = 1)
},
model = {
Y ~ binomial(Trial, theta)
theta ~ beta(1, 1)
}
)
mdl_binom <- rtmb_model(
data = list(Trial = 10, Y = 6),
code = code_binom,
silent = TRUE
)
fit_map <- mdl_binom$optimize(
se_method = "sampling",
num_samples = 20,
seed = 1
)
tab <- fit_map$summary()
expect_s3_class(fit_map, "map_fit")
expect_true("theta" %in% rownames(tab))
expect_true(is.finite(tab["theta", "Std. Error"]))
})
test_that("classic() regression and prior restriction", {
mdl_lm <- rtmb_lm(mpg ~ wt, data = mtcars)
fit_c <- mdl_lm$classic()
expect_s3_class(fit_c, "Classic_Fit")
mdl_p <- rtmb_lm(mpg ~ wt, data = mtcars, prior = prior_normal())
expect_error(mdl_p$classic(), "prior_flat")
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.