Skip to content

Commit

Permalink
Sketch neat summaries
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau-lilly committed Jun 2, 2023
1 parent 8094046 commit 00f3f13
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
66 changes: 62 additions & 4 deletions R/brm_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#' response variable is the raw outcome variable (such as AVAL)
#' or `"change"` if the response variable is change from baseline
#' (e.g. CHG).
#' @param level Numeric of length 1 between 0 and 1, credible level
#' for the credible intervals in the output.
#' @examples
#' set.seed(0L)
#' sim <- brm_simulate()
Expand Down Expand Up @@ -61,7 +63,8 @@ brm_summary <- function(
covariates = character(0),
control = "Placebo",
baseline = "Baseline",
response_type = "change"
response_type = "change",
level = 0.95
) {
assert_chr(base, "base arg must be a nonempty character string")
assert_chr(group, "group arg must be a nonempty character string")
Expand Down Expand Up @@ -90,6 +93,8 @@ brm_summary <- function(
!anyNA(.),
message = "baseline arg must be a length-1 non-missing atomic value"
)
assert_num(level, "level arg must be a length-1 numeric between 0 and 1")
assert(level, . >= 0, . <= 1, message = "level arg must be between 0 and 1")
assert(is.data.frame(model$data))
data <- model$data
assert(
Expand Down Expand Up @@ -182,9 +187,19 @@ brm_summary <- function(
control = control
)
}

browser()

table_response <- summarize_marginals(draws_response, level)
table_change <- if_any(
response_type == "response",
summarize_marginals(draws_change, level),
NULL
)
table_diff <- summarize_marginals(draws_diff, level)
dplyr::bind_rows(
response = table_response,
change = table_change,
difference = table_diff,
.id = "marginal"
)
}

subtract_baseline <- function(draws, groups, times, baseline) {
Expand Down Expand Up @@ -214,3 +229,46 @@ subtract_control <- function(draws, groups, times, control) {
marginal_name <- function(group, time) {
sprintf("%s, %s", group , time)

Check warning on line 230 in R/brm_summary.R

View workflow job for this annotation

GitHub Actions / lint

file=R/brm_summary.R,line=230,col=26,[commas_linter] Commas should never have a space before.
}

summarize_marginals <- function(draws, level) {
level_lower <- (1 - level) / 2
level_upper <- 1 - level_lower
draws[[".chain"]] <- NULL
draws[[".iteration"]] <- NULL
draws[[".draw"]] <- NULL
value <- tibble::tibble(
group = gsub(",.*$", "", colnames(draws)),
time = gsub("^.*, ", "", colnames(draws)),
mean = purrr::map_dbl(draws, mean),
median = purrr::map_dbl(draws, median),
sd = purrr::map_dbl(draws, sd),
lower = purrr::map_dbl(draws, ~quantile(.x, level_lower)),
upper = purrr::map_dbl(draws, ~quantile(.x, level_upper))
)
mcse <- tibble::tibble(
group = gsub(",.*$", "", colnames(draws)),
time = gsub("^.*, ", "", colnames(draws)),
mean = purrr::map_dbl(draws, posterior::mcse_mean),
median = purrr::map_dbl(draws, posterior::mcse_median),
sd = purrr::map_dbl(draws, posterior::mcse_sd),
lower = purrr::map_dbl(draws, ~posterior::mcse_quantile(.x, level_lower)),
upper = purrr::map_dbl(draws, ~posterior::mcse_quantile(.x, level_upper))
)
value <- tidyr::pivot_longer(
data = value,
cols = -any_of(c("group", "time")),
names_to = "statistic",
values_to = "value"
)
mcse <- tidyr::pivot_longer(
data = mcse,
cols = -any_of(c("group", "time")),
names_to = "statistic",
values_to = "mcse"
)
dplyr::left_join(
x = value,
y = mcse,
by = c("group", "time", "statistic")
)
}
6 changes: 5 additions & 1 deletion man/brm_summary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 00f3f13

Please sign in to comment.