Skip to content

Commit

Permalink
finish testing brm_marginals()
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau-lilly committed Jun 5, 2023
1 parent 0992d78 commit 6108d34
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions tests/testthat/test-brm_marginals.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
)
}
}
})

0 comments on commit 6108d34

Please sign in to comment.