-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from RConsortium/8
brm_summary() in the brms prototype
- Loading branch information
Showing
11 changed files
with
333 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
#' @title Summarize an MMRM. | ||
#' @export | ||
#' @family results | ||
#' @description Summarize a basic MMRM model fit. | ||
#' @details Currently assumes the response variable is `CHG` | ||
#' (change from baseline) and not `AVAL` (raw response). | ||
#' @return A `tibble` with summary statistics of the marginal posterior. | ||
#' @inheritParams brm_formula | ||
#' @param model Fitted `brms` model object from [brm_model()]. | ||
#' @param control Character of length 1, name of the control arm | ||
#' in the `group` column in the data. | ||
#' @examples | ||
#' set.seed(0L) | ||
#' sim <- brm_simulate() | ||
#' data <- sim$data | ||
#' 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 | ||
#' ) | ||
#' ) | ||
#' ) | ||
#' ) | ||
#' brm_summary( | ||
#' model = model, | ||
#' group = "group", | ||
#' time = "time", | ||
#' patient = "patient", | ||
#' control = 1 | ||
#' ) | ||
brm_summary <- function( | ||
model, | ||
base = "BASE", | ||
group = "TRT01P", | ||
time = "AVISIT", | ||
patient = "USUBJID", | ||
covariates = character(0), | ||
control = "Placebo" | ||
) { | ||
assert_chr(base, "base arg must be a nonempty character string") | ||
assert_chr(group, "group arg must be a nonempty character string") | ||
assert_chr(time, "time arg must be a nonempty character string") | ||
assert_chr(patient, "patient arg must be a nonempty character string") | ||
assert_chr_vec(covariates, "covariates arg must be a character vector") | ||
assert( | ||
control, | ||
is.atomic(.), | ||
length(.) == 1L, | ||
!anyNA(.), | ||
message = "control arg must be a length-1 non-missing atomic value" | ||
) | ||
assert(is.data.frame(model$data)) | ||
data <- model$data | ||
assert( | ||
group %in% colnames(data), | ||
message = "group arg must be a data column name" | ||
) | ||
assert( | ||
time %in% colnames(data), | ||
message = "time arg must be a data column name" | ||
) | ||
assert( | ||
patient %in% colnames(data), | ||
message = "patient arg must be a data column name" | ||
) | ||
assert( | ||
covariates %in% colnames(data), | ||
message = "all covariates must be data column names" | ||
) | ||
assert( | ||
control %in% data[[group]], | ||
message = "control arg must be in data[[group]]" | ||
) | ||
nuisance <- c(base, patient, covariates) | ||
emmeans_response <- emmeans::emmeans( | ||
object = model, | ||
specs = as.formula(sprintf("~%s:%s", time, group)), | ||
weights = "proportional", | ||
nuisance = nuisance | ||
) | ||
table_response <- brm_summary_response( | ||
data = data, | ||
emmeans_response = emmeans_response | ||
) | ||
table_diff <- brm_summary_diff( | ||
data = data, | ||
emmeans_response = emmeans_response, | ||
group = group, | ||
time = time, | ||
nuisance = nuisance, | ||
control = control | ||
) | ||
dplyr::left_join( | ||
x = table_response, | ||
y = table_diff, | ||
by = c(group, time) | ||
) | ||
} | ||
|
||
brm_summary_response <- function(data, emmeans_response) { | ||
out <- tibble::as_tibble(emmeans_response) | ||
out$response_mean <- out$emmean | ||
out$response_lower <- out$lower.HPD | ||
out$response_upper <- out$upper.HPD | ||
out$emmean <- NULL | ||
out$lower.HPD <- NULL | ||
out$upper.HPD <- NULL | ||
out | ||
} | ||
|
||
brm_summary_diff <- function( | ||
data, | ||
emmeans_response, | ||
group, | ||
time, | ||
nuisance, | ||
control | ||
) { | ||
reference <- tibble::as_tibble(emmeans_response) | ||
contrasts_diff <- list() | ||
for (level_group in setdiff(sort(unique(reference[[group]])), control)) { | ||
for (level_time in sort(unique(reference[[time]]))) { | ||
contrast_treatment <- as.integer( | ||
(reference[[group]] == level_group) & | ||
(reference[[time]] == level_time) | ||
) | ||
contrast_control <- as.integer( | ||
(reference[[group]] == control) & | ||
(reference[[time]] == level_time) | ||
) | ||
contrast <- contrast_treatment - contrast_control | ||
contrasts_diff[[length(contrasts_diff) + 1L]] <- contrast | ||
} | ||
} | ||
emmeans_diff <- emmeans::contrast( | ||
emmeans_response, | ||
method = contrasts_diff, | ||
adjust = "none", | ||
nuisance = nuisance | ||
) | ||
subset_reference <- reference[reference[[group]] != control, ] | ||
out <- tibble::as_tibble(emmeans_diff) | ||
out[[group]] <- subset_reference[[group]] | ||
out[[time]] <- subset_reference[[time]] | ||
out$contrast <- NULL | ||
out$diff_mean <- out$estimate | ||
out$diff_lower <- out$lower.HPD | ||
out$diff_upper <- out$upper.HPD | ||
out$estimate <- NULL | ||
out$lower.HPD <- NULL | ||
out$upper.HPD <- NULL | ||
out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,5 @@ reference: | |
- title: Results | ||
contents: | ||
- '`brm_convergence`' | ||
- '`brm_summary`' | ||
- '`brm_plot`' | ||
- '`brm_summary`' |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.