From 6108d34c87db7475f518ee6a45a8b454593bc6dc Mon Sep 17 00:00:00 2001 From: wlandau-lilly Date: Mon, 5 Jun 2023 11:26:38 -0400 Subject: [PATCH] finish testing brm_marginals() --- tests/testthat/test-brm_marginals.R | 75 +++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/tests/testthat/test-brm_marginals.R b/tests/testthat/test-brm_marginals.R index 914620da..d601f719 100644 --- a/tests/testthat/test-brm_marginals.R +++ b/tests/testthat/test-brm_marginals.R @@ -88,3 +88,78 @@ test_that("brm_marginals() on response", { } } }) + +test_that("brm_marginals() on change", { + set.seed(0L) + sim <- brm_simulate() + data <- sim$data + data$group <- paste("treatment", data$group) + data$time <- paste("visit", data$time) + formula <- brm_formula( + response = "response", + group = "group", + time = "time", + patient = "patient", + effect_base = FALSE, + interaction_base = FALSE + ) + tmp <- utils::capture.output( + suppressMessages( + suppressWarnings( + model <- brm_model( + data = data, + formula = formula, + chains = 1, + iter = 100, + refresh = 0 + ) + ) + ) + ) + suppressWarnings( + out <- brm_marginals( + model = model, + group = "group", + time = "time", + patient = "patient", + control = "treatment 1", + baseline = "visit 1", + outcome = "change" + ) + ) + fields <- c("response", "difference") + columns_df <- expand.grid( + group = sort(unique(data$group)), + time = sort(unique(data$time)), + stringsAsFactors = FALSE + ) + columns <- paste(columns_df$group, columns_df$time, sep = ", ") + expect_equal(sort(names(out)), sort(fields)) + for (field in fields) { + x <- out[[field]] + expect_true(tibble::is_tibble(x)) + expect_true(all(colnames(x) %in% c(columns, names_mcmc))) + expect_false(any(unlist(lapply(x, anyNA)))) + expect_equal(nrow(x), 50) + } + expect_equal( + sort(colnames(out$response)), + sort(c(columns, names_mcmc)) + ) + columns_df <- columns_df[columns_df$group != "treatment 1", ] + columns <- paste(columns_df$group, columns_df$time, sep = ", ") + expect_equal( + sort(colnames(out$difference)), + sort(c(columns, names_mcmc)) + ) + for (group in setdiff(unique(data$group), "treatment 1")) { + for (time in unique(data$time)) { + name1 <- paste("treatment 1", time, sep = ", ") + name2 <- paste(group, time, sep = ", ") + expect_equal( + out$difference[[name2]], + out$response[[name2]] - out$response[[name1]] + ) + } + } +})