| Title: | Utility Functions to Support and Extend the 'rbmi' Package |
|---|---|
| Description: | Provides utility functions that extend the capabilities of the reference-based multiple imputation package 'rbmi'. It supports clinical trial analysis workflows with functions for managing imputed datasets, applying analysis methods across imputations, and tidying results for reporting. |
| Authors: | Mark Baillie [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-5618-0667>), Tobias Muetze [aut] (ORCID: <https://orcid.org/0000-0002-4111-1941>), Jack Talboys [aut], Lukas A. Widmer [ctb] (ORCID: <https://orcid.org/0000-0003-1471-3493>) |
| Maintainer: | Mark Baillie <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.3.0.9000 |
| Built: | 2026-06-03 08:38:49 UTC |
| Source: | https://github.com/openpharma/rbmiutils |
A simplified example of a simulated trial dataset, with missing data.
ADEFFADEFF
ADEFF
A data frame with 1,000 rows and 10 columns:
Unique subject identifier
Primary outcome variable
Planned treatment
Stratification at randomisation
Stratification by region
Stratification by region, numeric code
Baseline value of primary outcome variable
Change from baseline
Visit number
Analysis parameter name
A simplified example of a simulated trial ADMI dataset
ADMIADMI
ADMI
A data frame with 100,000 rows and 12 columns:
Unique patient identifier
Stratification at randomisation
Stratification by region
Stratification by region, numeric code
Planned treatment
Baseline value of primary outcome variable
Change from baseline
Visit number
Imputation number identifier
Responder criteria (binary)
Responder criteria (categorical)
Responder criteria (definition)
This function applies an analysis function (e.g., ANCOVA) to imputed datasets and stores the results for later pooling. It is designed to work with multiple imputed datasets and apply a given analysis function to each imputation iteration.
analyse_mi_data( data = NULL, vars = NULL, method = NULL, fun = rbmi::ancova, delta = NULL, ... )analyse_mi_data( data = NULL, vars = NULL, method = NULL, fun = rbmi::ancova, delta = NULL, ... )
data |
A data frame containing the imputed datasets. The data frame should include a variable (e.g., |
vars |
A list specifying key variables used in the analysis (e.g., |
method |
A method object specifying the imputation method used (e.g., Bayesian imputation).
Created using |
fun |
A function that will be applied to each imputed dataset. Defaults to rbmi::ancova.
Other options include |
delta |
A |
... |
Additional arguments passed to the analysis function |
The function loops through distinct imputation datasets (identified by IMPID), applies the provided analysis function fun, and stores the results for later pooling. If a delta dataset is provided, it will be merged with the imputed data to apply the specified delta adjustment before analysis.
Workflow:
Prepare imputed data using get_imputed_data() or expand_imputed_data()
Define variables using rbmi::set_vars()
Call analyse_mi_data() to apply analysis to each imputation
Pool results using rbmi::pool()
Tidy results using tidy_pool_obj()
An object of class analysis containing the results from applying the analysis function to each imputed dataset.
Pass this to rbmi::pool() to obtain pooled estimates.
rbmi::analyse() which this function wraps
rbmi::pool() for pooling the analysis results
tidy_pool_obj() to format pooled results for publication
get_imputed_data() to extract imputed datasets from rbmi objects
expand_imputed_data() to reconstruct full imputed data from reduced form
gcomp_responder_multi() for binary outcome analysis
validate_data() to check data before imputation
# Example usage with an ANCOVA function library(dplyr) library(rbmi) library(rbmiUtils) set.seed(123) data("ADMI") # Convert key columns to factors ADMI$TRT <- factor(ADMI$TRT, levels = c("Placebo", "Drug A")) ADMI$USUBJID <- factor(ADMI$USUBJID) ADMI$AVISIT <- factor(ADMI$AVISIT) # Define key variables for ANCOVA analysis vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") # Covariates for adjustment ) # Specify the imputation method (Bayesian) - need for pool step method <- rbmi::method_bayes( n_samples = 20, control = rbmi::control_bayes( warmup = 20, thin = 1 ) ) # Perform ANCOVA Analysis on Each Imputed Dataset ana_obj_ancova <- analyse_mi_data( data = ADMI, vars = vars, method = method, fun = ancova, # Apply ANCOVA delta = NULL # No sensitivity analysis adjustment )# Example usage with an ANCOVA function library(dplyr) library(rbmi) library(rbmiUtils) set.seed(123) data("ADMI") # Convert key columns to factors ADMI$TRT <- factor(ADMI$TRT, levels = c("Placebo", "Drug A")) ADMI$USUBJID <- factor(ADMI$USUBJID) ADMI$AVISIT <- factor(ADMI$AVISIT) # Define key variables for ANCOVA analysis vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") # Covariates for adjustment ) # Specify the imputation method (Bayesian) - need for pool step method <- rbmi::method_bayes( n_samples = 20, control = rbmi::control_bayes( warmup = 20, thin = 1 ) ) # Perform ANCOVA Analysis on Each Imputed Dataset ana_obj_ancova <- analyse_mi_data( data = ADMI, vars = vars, method = method, fun = ancova, # Apply ANCOVA delta = NULL # No sensitivity analysis adjustment )
Combines tidy result tibbles from multiple analyses (e.g., different endpoints or subgroups) into a single table with an identifying column.
combine_results(..., results_list = NULL, id_col = "analysis")combine_results(..., results_list = NULL, id_col = "analysis")
... |
Named arguments where each is a tidy result tibble from |
results_list |
Alternative to |
id_col |
Character string specifying the name of the identifier column. Default is "analysis". |
A tibble with all results combined, with an additional column identifying the source analysis.
tidy_pool_obj() to create tidy results from pooled objects
format_results() to format combined results for reporting
library(rbmi) library(dplyr) # Assuming you have multiple pooled results # results_week24 <- tidy_pool_obj(pool_obj_week24) # results_week48 <- tidy_pool_obj(pool_obj_week48) # Combine them # combined <- combine_results( # "Week 24" = results_week24, # "Week 48" = results_week48 # )library(rbmi) library(dplyr) # Assuming you have multiple pooled results # results_week24 <- tidy_pool_obj(pool_obj_week24) # results_week48 <- tidy_pool_obj(pool_obj_week48) # Combine them # combined <- combine_results( # "Week 24" = results_week24, # "Week 48" = results_week48 # )
Adds an IMPID column to a list of imputed datasets, converting them to a
single stacked data.frame suitable for use with analyse_mi_data().
create_impid(imputed_list, id_prefix = "")create_impid(imputed_list, id_prefix = "")
imputed_list |
A list of data.frames, where each element represents one imputed dataset. |
id_prefix |
Optional character prefix for IMPID values. Default is empty string. |
This function is useful when you have imputed datasets from a source other than rbmi (e.g., from mice or another MI package) and want to use them with rbmiUtils analysis functions.
A single data.frame with all imputed datasets stacked, with an IMPID
column identifying the source imputation.
get_imputed_data() to extract imputed data from rbmi objects
analyse_mi_data() to analyse stacked imputed data
# Create example imputed datasets imp1 <- data.frame(USUBJID = c("S1", "S2"), CHG = c(1.5, 2.5)) imp2 <- data.frame(USUBJID = c("S1", "S2"), CHG = c(1.8, 2.2)) imp3 <- data.frame(USUBJID = c("S1", "S2"), CHG = c(1.6, 2.4)) # Stack with IMPID stacked <- create_impid(list(imp1, imp2, imp3)) print(stacked)# Create example imputed datasets imp1 <- data.frame(USUBJID = c("S1", "S2"), CHG = c(1.5, 2.5)) imp2 <- data.frame(USUBJID = c("S1", "S2"), CHG = c(1.8, 2.2)) imp3 <- data.frame(USUBJID = c("S1", "S2"), CHG = c(1.6, 2.4)) # Stack with IMPID stacked <- create_impid(list(imp1, imp2, imp3)) print(stacked)
Extracts structured metadata from an rbmi draws object, including method,
formula, sample count, failures, covariance structure, and (for Bayesian
methods) MCMC convergence diagnostics. Returns an S3 object with an
informative print() method.
describe_draws(draws_obj)describe_draws(draws_obj)
draws_obj |
A |
For conditional mean methods, the sample count is displayed as "1 + N" matching the rbmi convention where the first sample is the primary (full-data) fit and the remaining N are jackknife or bootstrap resamples.
For Bayesian methods, MCMC convergence diagnostics (ESS, Rhat) are extracted
from the stanfit object when rstan is available. The converged flag
uses the Rhat < 1.1 threshold matching rbmi's own convention.
An S3 object of class c("describe_draws", "list") containing:
Human-readable method name (e.g., "Bayesian (MCMC via Stan)")
Raw class name: "bayes", "approxbayes", or "condmean"
Total number of samples
Number of failed samples
Deparsed model formula string
Covariance structure (e.g., "us")
Logical; whether same covariance is used across groups
(condmean only) "jackknife" or "bootstrap"
(condmean only) Always 1
(condmean only) Number of resampled draws
(bayes only) List with warmup, thin, chains, seed
(bayes with stanfit only) List with rhat, ess, max_rhat, min_ess, n_params, converged
rbmi::draws() to create draws objects
rbmi::method_condmean(), rbmi::method_bayes(), rbmi::method_approxbayes()
for method specification
## Not run: library(rbmi) library(dplyr) data("ADEFF", package = "rbmiUtils") # Prepare ADEFF data for rbmi pipeline ADEFF <- ADEFF |> mutate( TRT = factor(TRT01P, levels = c("Placebo", "Drug A")), USUBJID = factor(USUBJID), AVISIT = factor(AVISIT, levels = c("Week 24", "Week 48")) ) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") ) dat <- ADEFF |> select(USUBJID, STRATA, REGION, TRT, BASE, CHG, AVISIT) draws_obj <- draws( data = dat, vars = vars, method = method_bayes(n_samples = 100) ) # Inspect the draws object desc <- describe_draws(draws_obj) print(desc) # Programmatic access to metadata desc$method desc$n_samples desc$formula ## End(Not run)## Not run: library(rbmi) library(dplyr) data("ADEFF", package = "rbmiUtils") # Prepare ADEFF data for rbmi pipeline ADEFF <- ADEFF |> mutate( TRT = factor(TRT01P, levels = c("Placebo", "Drug A")), USUBJID = factor(USUBJID), AVISIT = factor(AVISIT, levels = c("Week 24", "Week 48")) ) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") ) dat <- ADEFF |> select(USUBJID, STRATA, REGION, TRT, BASE, CHG, AVISIT) draws_obj <- draws( data = dat, vars = vars, method = method_bayes(n_samples = 100) ) # Inspect the draws object desc <- describe_draws(draws_obj) print(desc) # Programmatic access to metadata desc$method desc$n_samples desc$formula ## End(Not run)
Extracts structured metadata from an rbmi imputation object, including
method, number of imputations (M), reference arm mappings, subject count,
and a missingness breakdown by visit and treatment arm. Returns an S3 object
with an informative print() method.
describe_imputation(impute_obj)describe_imputation(impute_obj)
impute_obj |
An |
The missingness table is built by cross-tabulating
impute_obj$data$is_missing by visit and treatment group. Each row shows
the total number of subjects in that group, how many had missing data at
that visit, and the percentage missing.
An S3 object of class c("describe_imputation", "list") containing:
Human-readable method name (e.g., "Bayesian (MCMC via Stan)")
Raw class name: "bayes", "approxbayes", or "condmean"
Number of imputations (M)
Named character vector of reference arm mappings, or NULL
Total number of unique subjects
Character vector of visit names
A data.frame with columns: visit, group, n_total, n_miss, pct_miss
rbmi::impute() to create imputation objects
describe_draws() for inspecting draws objects
## Not run: library(rbmi) library(dplyr) data("ADEFF", package = "rbmiUtils") ADEFF <- ADEFF |> mutate( TRT = factor(TRT01P, levels = c("Placebo", "Drug A")), USUBJID = factor(USUBJID), AVISIT = factor(AVISIT, levels = c("Week 24", "Week 48")) ) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") ) dat <- ADEFF |> select(USUBJID, STRATA, REGION, TRT, BASE, CHG, AVISIT) draws_obj <- draws( data = dat, vars = vars, method = method_bayes(n_samples = 100) ) impute_obj <- impute( draws_obj, references = c("Placebo" = "Placebo", "Drug A" = "Placebo") ) # Inspect the imputation desc <- describe_imputation(impute_obj) print(desc) # Programmatic access desc$n_imputations desc$missingness desc$references ## End(Not run)## Not run: library(rbmi) library(dplyr) data("ADEFF", package = "rbmiUtils") ADEFF <- ADEFF |> mutate( TRT = factor(TRT01P, levels = c("Placebo", "Drug A")), USUBJID = factor(USUBJID), AVISIT = factor(AVISIT, levels = c("Week 24", "Week 48")) ) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") ) dat <- ADEFF |> select(USUBJID, STRATA, REGION, TRT, BASE, CHG, AVISIT) draws_obj <- draws( data = dat, vars = vars, method = method_bayes(n_samples = 100) ) impute_obj <- impute( draws_obj, references = c("Placebo" = "Placebo", "Drug A" = "Placebo") ) # Inspect the imputation desc <- describe_imputation(impute_obj) print(desc) # Programmatic access desc$n_imputations desc$missingness desc$references ## End(Not run)
Takes an rbmi pool object and produces a publication-ready gt table in the style of CDISC/ICH Table 14.2.x. The table displays least squares means by treatment arm, treatment differences, confidence intervals, and p-values, organized by visit row groups.
efficacy_table( pool_obj, title = NULL, subtitle = NULL, digits = 2, ci_level = NULL, arm_labels = NULL, pval_digits = 3, pval_threshold = 0.001, font_family = NULL, font_size = NULL, row_padding = NULL, ... )efficacy_table( pool_obj, title = NULL, subtitle = NULL, digits = 2, ci_level = NULL, arm_labels = NULL, pval_digits = 3, pval_threshold = 0.001, font_family = NULL, font_size = NULL, row_padding = NULL, ... )
pool_obj |
A pooled analysis object of class |
title |
Optional character string for the table title. |
subtitle |
Optional character string for the table subtitle. |
digits |
Integer. Number of decimal places for estimates and standard errors. Default is 2. |
ci_level |
Numeric. Confidence level for CI column labeling. If |
arm_labels |
Named character vector with elements |
pval_digits |
Integer. Number of decimal places for p-values. Default is 3. |
pval_threshold |
Numeric. P-values below this threshold are displayed as "< threshold". Default is 0.001. |
font_family |
Optional character string specifying the font family for
the table. When |
font_size |
Optional numeric value specifying the table font size in
pixels. When |
row_padding |
Optional numeric value specifying the vertical padding
for data rows in pixels. When |
... |
Additional arguments passed to |
This function assumes a single-parameter-per-visit pool object (the standard
output from an rbmi ANCOVA or MMRM pipeline). It internally calls
tidy_pool_obj() to parse the pool object, then constructs the gt table.
Arm labels: Use the arm_labels parameter to customize arm names in the
table. For example, arm_labels = c(ref = "Placebo", alt = "Drug A") will
display "LS Mean (Placebo)" and "LS Mean (Drug A)" instead of the defaults.
Customization: The returned gt object can be further customized using
standard gt piping, e.g., efficacy_table(pool_obj) |> gt::tab_options(...).
Example output:
A gt table object of class gt_tbl.
tidy_pool_obj() for the underlying data transformation
format_pvalue() for p-value formatting rules
rbmi::pool() to create pool objects
if (requireNamespace("gt", quietly = TRUE)) { library(rbmi) data("ADMI", package = "rbmiUtils") ADMI$TRT <- factor(ADMI$TRT, levels = c("Placebo", "Drug A")) ADMI$USUBJID <- factor(ADMI$USUBJID) ADMI$AVISIT <- factor(ADMI$AVISIT) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") ) method <- method_bayes( n_samples = 20, control = control_bayes(warmup = 20, thin = 1) ) ana_obj <- analyse_mi_data(ADMI, vars, method, fun = ancova) pool_obj <- pool(ana_obj) # Basic table tbl <- efficacy_table(pool_obj) # Publication-styled table efficacy_table( pool_obj, title = "Table 14.2.1: ANCOVA of Change from Baseline", subtitle = "Mixed Model for Repeated Measures", arm_labels = c(ref = "Placebo", alt = "Drug A"), font_size = 12, row_padding = 4 ) }if (requireNamespace("gt", quietly = TRUE)) { library(rbmi) data("ADMI", package = "rbmiUtils") ADMI$TRT <- factor(ADMI$TRT, levels = c("Placebo", "Drug A")) ADMI$USUBJID <- factor(ADMI$USUBJID) ADMI$AVISIT <- factor(ADMI$AVISIT) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") ) method <- method_bayes( n_samples = 20, control = control_bayes(warmup = 20, thin = 1) ) ana_obj <- analyse_mi_data(ADMI, vars, method, fun = ancova) pool_obj <- pool(ana_obj) # Basic table tbl <- efficacy_table(pool_obj) # Publication-styled table efficacy_table( pool_obj, title = "Table 14.2.1: ANCOVA of Change from Baseline", subtitle = "Mixed Model for Repeated Measures", arm_labels = c(ref = "Placebo", alt = "Drug A"), font_size = 12, row_padding = 4 ) }
Reconstructs the full imputed dataset from a reduced form by merging imputed
values back with the original observed data. This is the inverse operation of
reduce_imputed_data().
expand_imputed_data(reduced_data, original_data, vars)expand_imputed_data(reduced_data, original_data, vars)
reduced_data |
A data.frame containing only the imputed values, as
returned by |
original_data |
A data.frame containing the original dataset before imputation, with missing values in the outcome column. |
vars |
A |
For each imputation (identified by IMPID), this function:
Starts with the original data (observed values)
Replaces missing outcome values with the corresponding imputed values
Stacks all imputations together
A data.frame containing the full imputed dataset with one complete
dataset per IMPID value. The structure matches the output of
get_imputed_data().
rbmi::impute() which creates the imputed datasets this function operates on
reduce_imputed_data() to create the reduced dataset
get_imputed_data() to extract imputed data from an rbmi imputation object
library(rbmi) library(dplyr) # Example with package data data("ADMI", package = "rbmiUtils") data("ADEFF", package = "rbmiUtils") # Prepare original data to match ADMI structure original <- ADEFF |> mutate( TRT = TRT01P, USUBJID = as.character(USUBJID) ) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG" ) # Reduce and then expand reduced <- reduce_imputed_data(ADMI, original, vars) expanded <- expand_imputed_data(reduced, original, vars) # Verify expansion cat("Original ADMI rows:", nrow(ADMI), "\n") cat("Expanded rows:", nrow(expanded), "\n")library(rbmi) library(dplyr) # Example with package data data("ADMI", package = "rbmiUtils") data("ADEFF", package = "rbmiUtils") # Prepare original data to match ADMI structure original <- ADEFF |> mutate( TRT = TRT01P, USUBJID = as.character(USUBJID) ) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG" ) # Reduce and then expand reduced <- reduce_imputed_data(ADMI, original, vars) expanded <- expand_imputed_data(reduced, original, vars) # Verify expansion cat("Original ADMI rows:", nrow(ADMI), "\n") cat("Expanded rows:", nrow(expanded), "\n")
Convenience function to extract only least squares mean estimates from tidy results.
extract_lsm(results, visit = NULL, arm = NULL)extract_lsm(results, visit = NULL, arm = NULL)
results |
A tibble from |
visit |
Optional character vector of visits to filter. If NULL (default), returns results for all visits. |
arm |
Optional character: "ref" for reference arm, "alt" for alternative arm, or NULL (default) for both. |
A tibble containing only LSM rows.
tidy_pool_obj() to create tidy results
extract_trt_effects() to extract treatment effects
library(rbmi) # Assuming you have a tidy result # tidy_result <- tidy_pool_obj(pool_obj) # all_lsm <- extract_lsm(tidy_result) # ref_lsm <- extract_lsm(tidy_result, arm = "ref")library(rbmi) # Assuming you have a tidy result # tidy_result <- tidy_pool_obj(pool_obj) # all_lsm <- extract_lsm(tidy_result) # ref_lsm <- extract_lsm(tidy_result, arm = "ref")
Convenience function to extract only treatment comparison estimates from tidy results, filtering out least squares means.
extract_trt_effects(results, visit = NULL)extract_trt_effects(results, visit = NULL)
results |
A tibble from |
visit |
Optional character vector of visits to filter. If NULL (default), returns results for all visits. |
A tibble containing only treatment effect rows.
tidy_pool_obj() to create tidy results
extract_lsm() to extract least squares means
library(rbmi) # Assuming you have a tidy result # tidy_result <- tidy_pool_obj(pool_obj) # trt_effects <- extract_trt_effects(tidy_result) # trt_week24 <- extract_trt_effects(tidy_result, visit = "Week 24")library(rbmi) # Assuming you have a tidy result # tidy_result <- tidy_pool_obj(pool_obj) # trt_effects <- extract_trt_effects(tidy_result) # trt_week24 <- extract_trt_effects(tidy_result, visit = "Week 24")
Formats a point estimate with its confidence interval in standard publication format: "estimate (lower, upper)".
format_estimate(estimate, lower, upper, digits = 2, sep = ", ")format_estimate(estimate, lower, upper, digits = 2, sep = ", ")
estimate |
Numeric vector of point estimates. |
lower |
Numeric vector of lower confidence interval bounds. |
upper |
Numeric vector of upper confidence interval bounds. |
digits |
Integer. Number of decimal places for rounding. Default is 2. |
sep |
Character. Separator between lower and upper bounds. Default is ", ". |
The function formats estimates as "X.XX (X.XX, X.XX)" by default. All three
input vectors must have the same length. NA values in any position result
in NA_character_ for that element.
A character vector of formatted estimates with confidence intervals.
# Single estimate format_estimate(1.5, 0.8, 2.2) #> "1.50 (0.80, 2.20)" # Multiple estimates format_estimate( estimate = c(-2.5, -1.8), lower = c(-4.0, -3.2), upper = c(-1.0, -0.4) ) #> "-2.50 (-4.00, -1.00)" "-1.80 (-3.20, -0.40)" # More decimal places format_estimate(0.234, 0.123, 0.345, digits = 3) #> "0.234 (0.123, 0.345)" # Different separator format_estimate(1.5, 0.8, 2.2, sep = " to ") #> "1.50 (0.80 to 2.20)"# Single estimate format_estimate(1.5, 0.8, 2.2) #> "1.50 (0.80, 2.20)" # Multiple estimates format_estimate( estimate = c(-2.5, -1.8), lower = c(-4.0, -3.2), upper = c(-1.0, -0.4) ) #> "-2.50 (-4.00, -1.00)" "-1.80 (-3.20, -0.40)" # More decimal places format_estimate(0.234, 0.123, 0.345, digits = 3) #> "0.234 (0.123, 0.345)" # Different separator format_estimate(1.5, 0.8, 2.2, sep = " to ") #> "1.50 (0.80 to 2.20)"
Formats p-values according to common publication standards, with configurable thresholds and decimal places.
format_pvalue(x, digits = 3, threshold = 0.001, html = FALSE)format_pvalue(x, digits = 3, threshold = 0.001, html = FALSE)
x |
A numeric vector of p-values. |
digits |
Integer. Number of decimal places for rounding. Default is 3. |
threshold |
Numeric. P-values below this threshold are displayed as "< threshold". Default is 0.001. |
html |
Logical. If |
The function applies the following rules:
P-values below threshold are formatted as "< 0.001" (or HTML equivalent)
P-values >= threshold are rounded to digits decimal places
NA values are preserved as NA_character_
Values > 1 or < 0 return NA_character_ with a warning
A character vector of formatted p-values.
# Basic usage format_pvalue(0.0234) #> "0.023" format_pvalue(0.00005) #> "< 0.001" # Vector input pvals <- c(0.5, 0.05, 0.001, 0.0001, NA) format_pvalue(pvals) #> "0.500" "0.050" "0.001" "< 0.001" NA # Custom threshold format_pvalue(0.005, threshold = 0.01) #> "< 0.01" # HTML output format_pvalue(0.0001, html = TRUE) #> "< 0.001"# Basic usage format_pvalue(0.0234) #> "0.023" format_pvalue(0.00005) #> "< 0.001" # Vector input pvals <- c(0.5, 0.05, 0.001, 0.0001, NA) format_pvalue(pvals) #> "0.500" "0.050" "0.001" "< 0.001" NA # Custom threshold format_pvalue(0.005, threshold = 0.01) #> "< 0.01" # HTML output format_pvalue(0.0001, html = TRUE) #> "< 0.001"
Formats a tidy results tibble for publication-ready reporting, with options for rounding, confidence interval formatting, and column selection.
format_results( results, digits = 2, ci_format = c("parens", "brackets", "dash"), pval_digits = 3, include_se = FALSE )format_results( results, digits = 2, ci_format = c("parens", "brackets", "dash"), pval_digits = 3, include_se = FALSE )
results |
A tibble from |
digits |
Integer specifying the number of decimal places for estimates. Default is 2. |
ci_format |
Character string specifying CI format. Options are: "parens" for "(LCI, UCI)", "brackets" for "\[LCI, UCI\]", or "dash" for "LCI - UCI". Default is "parens". |
pval_digits |
Integer specifying decimal places for p-values. Default is 3. |
include_se |
Logical indicating whether to include standard error column. Default is FALSE. |
A tibble with formatted columns suitable for reporting.
tidy_pool_obj() to create tidy results
combine_results() to combine multiple analyses
library(rbmi) # Assuming you have a tidy result # tidy_result <- tidy_pool_obj(pool_obj) # formatted <- format_results(tidy_result, digits = 3, ci_format = "brackets")library(rbmi) # Assuming you have a tidy result # tidy_result <- tidy_pool_obj(pool_obj) # formatted <- format_results(tidy_result, digits = 3, ci_format = "brackets")
Adds formatted columns to a tidy results table, creating publication-ready output with properly formatted estimates, confidence intervals, and p-values.
format_results_table( data, est_col = "est", lci_col = "lci", uci_col = "uci", pval_col = "pval", est_digits = 2, pval_digits = 3, pval_threshold = 0.001, ci_sep = ", " )format_results_table( data, est_col = "est", lci_col = "lci", uci_col = "uci", pval_col = "pval", est_digits = 2, pval_digits = 3, pval_threshold = 0.001, ci_sep = ", " )
data |
A data.frame or tibble, typically output from |
est_col |
Character. Name of the estimate column. Default is "est". |
lci_col |
Character. Name of the lower CI column. Default is "lci". |
uci_col |
Character. Name of the upper CI column. Default is "uci". |
pval_col |
Character. Name of the p-value column. Default is "pval". |
est_digits |
Integer. Decimal places for estimates. Default is 2. |
pval_digits |
Integer. Decimal places for p-values. Default is 3. |
pval_threshold |
Numeric. Threshold for p-value formatting. Default is 0.001. |
ci_sep |
Character. Separator for CI bounds. Default is ", ". |
This function is designed to work with output from tidy_pool_obj() but
can be used with any data.frame containing estimate, CI, and p-value columns.
The original columns are preserved; new formatted columns are added.
A tibble with additional formatted columns:
Formatted estimate with confidence interval
Formatted p-value
library(dplyr) # Create example results results <- tibble::tibble( parameter = c("trt_Week24", "lsm_ref_Week24", "lsm_alt_Week24"), description = c("Treatment Effect", "LS Mean (Reference)", "LS Mean (Treatment)"), est = c(-2.45, 5.20, 2.75), se = c(0.89, 0.65, 0.71), lci = c(-4.20, 3.93, 1.36), uci = c(-0.70, 6.47, 4.14), pval = c(0.006, NA, NA) ) # Format for publication formatted <- format_results_table(results) print(formatted[, c("description", "est_ci", "pval_fmt")])library(dplyr) # Create example results results <- tibble::tibble( parameter = c("trt_Week24", "lsm_ref_Week24", "lsm_alt_Week24"), description = c("Treatment Effect", "LS Mean (Reference)", "LS Mean (Treatment)"), est = c(-2.45, 5.20, 2.75), se = c(0.89, 0.65, 0.71), lci = c(-4.20, 3.93, 1.36), uci = c(-0.70, 6.47, 4.14), pval = c(0.006, NA, NA) ) # Format for publication formatted <- format_results_table(results) print(formatted[, c("description", "est_ci", "pval_fmt")])
Wrapper function for targeting a marginal treatment effect using g-computation using the beeca package. Intended for binary endpoints.
gcomp_binary( data, outcome = "CRIT1FLN", treatment = "TRT", covariates = c("BASE", "STRATA", "REGION"), reference = "Placebo", contrast = "diff", method = "Ge", type = "HC0", ... )gcomp_binary( data, outcome = "CRIT1FLN", treatment = "TRT", covariates = c("BASE", "STRATA", "REGION"), reference = "Placebo", contrast = "diff", method = "Ge", type = "HC0", ... )
data |
A data.frame containing the analysis dataset. |
outcome |
Name of the binary outcome variable (as string). |
treatment |
Name of the treatment variable (as string). |
covariates |
Character vector of covariate names to adjust for. |
reference |
Reference level for the treatment variable (default: "Placebo"). |
contrast |
Type of contrast to compute (default: "diff"). |
method |
Marginal estimation method for variance (default: "Ge"). |
type |
Variance estimator type (default: "HC0"). |
... |
Additional arguments passed to |
A named list with treatment effect estimate, standard error, and degrees of freedom (if applicable).
# Load required packages library(rbmiUtils) library(beeca) # for get_marginal_effect() library(dplyr) # Load example data data("ADMI") # Ensure correct factor levels ADMI <- ADMI |> mutate( TRT = factor(TRT, levels = c("Placebo", "Drug A")), STRATA = factor(STRATA), REGION = factor(REGION) ) # Apply g-computation for binary responder result <- gcomp_binary( data = ADMI, outcome = "CRIT1FLN", treatment = "TRT", covariates = c("BASE", "STRATA", "REGION"), reference = "Placebo", contrast = "diff", method = "Ge", # from beeca: GEE robust sandwich estimator type = "HC0" # from beeca: heteroskedasticity-consistent SE ) # Print results print(result)# Load required packages library(rbmiUtils) library(beeca) # for get_marginal_effect() library(dplyr) # Load example data data("ADMI") # Ensure correct factor levels ADMI <- ADMI |> mutate( TRT = factor(TRT, levels = c("Placebo", "Drug A")), STRATA = factor(STRATA), REGION = factor(REGION) ) # Apply g-computation for binary responder result <- gcomp_binary( data = ADMI, outcome = "CRIT1FLN", treatment = "TRT", covariates = c("BASE", "STRATA", "REGION"), reference = "Placebo", contrast = "diff", method = "Ge", # from beeca: GEE robust sandwich estimator type = "HC0" # from beeca: heteroskedasticity-consistent SE ) # Print results print(result)
Performs logistic regression and estimates marginal effects for binary outcomes.
gcomp_responder( data, vars, reference_levels = NULL, var_method = "Ge", type = "HC0", contrast = "diff" )gcomp_responder( data, vars, reference_levels = NULL, var_method = "Ge", type = "HC0", contrast = "diff" )
data |
A data.frame with one visit of data. |
vars |
A list containing |
reference_levels |
Optional vector specifying reference level(s) of the treatment factor. |
var_method |
Marginal variance estimation method (default: "Ge"). |
type |
Type of robust variance estimator (default: "HC0"). |
contrast |
Type of contrast to compute (default: "diff"). |
A named list containing estimates and standard errors for treatment comparisons and within-arm means.
library(dplyr) library(rbmi) library(rbmiUtils) data("ADMI") # Prepare data for a single visit ADMI <- ADMI |> mutate( TRT = factor(TRT, levels = c("Placebo", "Drug A")), STRATA = factor(STRATA), REGION = factor(REGION) ) dat_single <- ADMI |> filter(AVISIT == "Week 24") vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CRIT1FLN", covariates = c("BASE", "STRATA", "REGION") ) result <- gcomp_responder( data = dat_single, vars = vars, reference_levels = "Placebo" ) print(result)library(dplyr) library(rbmi) library(rbmiUtils) data("ADMI") # Prepare data for a single visit ADMI <- ADMI |> mutate( TRT = factor(TRT, levels = c("Placebo", "Drug A")), STRATA = factor(STRATA), REGION = factor(REGION) ) dat_single <- ADMI |> filter(AVISIT == "Week 24") vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CRIT1FLN", covariates = c("BASE", "STRATA", "REGION") ) result <- gcomp_responder( data = dat_single, vars = vars, reference_levels = "Placebo" ) print(result)
Applies gcomp_responder() separately for each unique visit in the data.
gcomp_responder_multi(data, vars, reference_levels = NULL, ...)gcomp_responder_multi(data, vars, reference_levels = NULL, ...)
data |
A data.frame containing multiple visits. |
vars |
A list specifying analysis variables. |
reference_levels |
Optional reference level for the treatment variable. |
... |
Additional arguments passed to |
A named list of estimates for each visit and treatment group.
library(dplyr) library(rbmi) library(rbmiUtils) data("ADMI") ADMI <- ADMI |> mutate( TRT = factor(TRT, levels = c("Placebo", "Drug A")), STRATA = factor(STRATA), REGION = factor(REGION) ) # Note: method must match the original used for imputation method <- method_bayes( n_samples = 100, control = control_bayes(warmup = 20, thin = 2) ) vars_binary <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CRIT1FLN", covariates = c("BASE", "STRATA", "REGION") ) ana_obj_prop <- analyse_mi_data( data = ADMI, vars = vars_binary, method = method, fun = gcomp_responder_multi, reference_levels = "Placebo", contrast = "diff", var_method = "Ge", type = "HC0" ) pool(ana_obj_prop)library(dplyr) library(rbmi) library(rbmiUtils) data("ADMI") ADMI <- ADMI |> mutate( TRT = factor(TRT, levels = c("Placebo", "Drug A")), STRATA = factor(STRATA), REGION = factor(REGION) ) # Note: method must match the original used for imputation method <- method_bayes( n_samples = 100, control = control_bayes(warmup = 20, thin = 2) ) vars_binary <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CRIT1FLN", covariates = c("BASE", "STRATA", "REGION") ) ana_obj_prop <- analyse_mi_data( data = ADMI, vars = vars_binary, method = method, fun = gcomp_responder_multi, reference_levels = "Placebo", contrast = "diff", var_method = "Ge", type = "HC0" ) pool(ana_obj_prop)
This function takes an imputed dataset and a mapping variable to return a dataset with the original IDs mapped back and renamed appropriately.
get_imputed_data(impute_obj)get_imputed_data(impute_obj)
impute_obj |
The imputation object from which the imputed datasets are extracted. |
A data frame with the original subject IDs mapped and renamed.
library(dplyr) library(rbmi) library(rbmiUtils) set.seed(1974) # Load example dataset data("ADEFF") # Prepare data ADEFF <- ADEFF |> mutate( TRT = factor(TRT01P, levels = c("Placebo", "Drug A")), USUBJID = factor(USUBJID), AVISIT = factor(AVISIT) ) # Define variables for imputation vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") ) # Define Bayesian imputation method method <- method_bayes( n_samples = 100, control = control_bayes(warmup = 200, thin = 2) ) # Generate draws and perform imputation draws_obj <- draws(data = ADEFF, vars = vars, method = method) impute_obj <- impute(draws_obj, references = c("Placebo" = "Placebo", "Drug A" = "Placebo")) # Extract imputed data with original subject IDs admi <- get_imputed_data(impute_obj) head(admi)library(dplyr) library(rbmi) library(rbmiUtils) set.seed(1974) # Load example dataset data("ADEFF") # Prepare data ADEFF <- ADEFF |> mutate( TRT = factor(TRT01P, levels = c("Placebo", "Drug A")), USUBJID = factor(USUBJID), AVISIT = factor(AVISIT) ) # Define variables for imputation vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") ) # Define Bayesian imputation method method <- method_bayes( n_samples = 100, control = control_bayes(warmup = 200, thin = 2) ) # Generate draws and perform imputation draws_obj <- draws(data = ADEFF, vars = vars, method = method) impute_obj <- impute(draws_obj, references = c("Placebo" = "Placebo", "Drug A" = "Placebo")) # Extract imputed data with original subject IDs admi <- get_imputed_data(impute_obj) head(admi)
Takes an rbmi pool object and produces a publication-quality, three-panel forest plot using ggplot2 and patchwork. The plot displays treatment effect point estimates with confidence interval whiskers, an aligned table panel showing formatted estimates, and a p-value panel.
plot_forest( pool_obj, display = c("trt", "lsm"), ref_value = NULL, ci_level = NULL, arm_labels = NULL, title = NULL, text_size = 3.5, point_size = 3.5, show_pvalues = TRUE, font_family = NULL, panel_widths = NULL )plot_forest( pool_obj, display = c("trt", "lsm"), ref_value = NULL, ci_level = NULL, arm_labels = NULL, title = NULL, text_size = 3.5, point_size = 3.5, show_pvalues = TRUE, font_family = NULL, panel_widths = NULL )
pool_obj |
A pooled analysis object of class |
display |
Character string specifying the display mode. |
ref_value |
Numeric. The reference value for the vertical reference
line. Default is |
ci_level |
Numeric. Confidence level for CI labeling. If |
arm_labels |
Named character vector with elements |
title |
Optional character string for the plot title. |
text_size |
Numeric. Text size for the table and p-value panels. Default is 3.5. |
point_size |
Numeric. Point size for the forest plot. Default is 3.5. |
show_pvalues |
Logical. Whether to display the p-value panel on the
right side of the plot. Default is |
font_family |
Optional character string specifying the font family for
all text in the plot. When |
panel_widths |
Optional numeric vector controlling the relative widths
of the plot panels. When |
The function calls tidy_pool_obj() internally to parse the pool object,
then constructs a three-panel composition:
Left panel: Visit labels and formatted estimate with CI text
Middle panel: Forest plot with point estimates and CI whiskers
Right panel: Formatted p-values
Display modes:
"trt" – Treatment differences with a reference line at zero
(or custom ref_value). Significant results (CI excludes reference)
are shown as filled circles; non-significant as open circles.
"lsm" – LS mean estimates by treatment arm, color-coded using
the Okabe-Ito colorblind-friendly palette (blue for reference,
vermilion for treatment). Points are dodged vertically within
each visit.
Customization: The returned patchwork object supports & theme() for
applying theme changes to all panels. For example:
plot_forest(pool_obj) & theme(text = element_text(size = 14)).
Suggested dimensions for regulatory documents: For A4 or US Letter page
sizes, width = 10, height = 3 + 0.4 * n_visits (in inches) provides good
results when saving with ggplot2::ggsave(). For example, a 5-visit plot
works well at 10 x 5 inches.
Example output (treatment difference mode):
A patchwork/ggplot object that can be further customized using
& theme() to modify all panels simultaneously.
rbmi::pool() for creating pool objects
tidy_pool_obj() for the underlying data transformation
efficacy_table() for tabular presentation of the same data
format_pvalue() for p-value formatting rules
format_estimate() for estimate with CI formatting
if (requireNamespace("ggplot2", quietly = TRUE) && requireNamespace("patchwork", quietly = TRUE)) { library(rbmi) data("ADMI", package = "rbmiUtils") ADMI$TRT <- factor(ADMI$TRT, levels = c("Placebo", "Drug A")) ADMI$USUBJID <- factor(ADMI$USUBJID) ADMI$AVISIT <- factor(ADMI$AVISIT) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") ) method <- method_bayes( n_samples = 20, control = control_bayes(warmup = 20, thin = 1) ) ana_obj <- analyse_mi_data(ADMI, vars, method, fun = ancova) pool_obj <- pool(ana_obj) # Treatment difference forest plot plot_forest(pool_obj, arm_labels = c(ref = "Placebo", alt = "Drug A")) # LSM display with custom panel widths plot_forest( pool_obj, display = "lsm", arm_labels = c(ref = "Placebo", alt = "Drug A"), title = "LS Mean Estimates by Visit", panel_widths = c(3, 5, 1.5) ) }if (requireNamespace("ggplot2", quietly = TRUE) && requireNamespace("patchwork", quietly = TRUE)) { library(rbmi) data("ADMI", package = "rbmiUtils") ADMI$TRT <- factor(ADMI$TRT, levels = c("Placebo", "Drug A")) ADMI$USUBJID <- factor(ADMI$USUBJID) ADMI$AVISIT <- factor(ADMI$AVISIT) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") ) method <- method_bayes( n_samples = 20, control = control_bayes(warmup = 20, thin = 1) ) ana_obj <- analyse_mi_data(ADMI, vars, method, fun = ancova) pool_obj <- pool(ana_obj) # Treatment difference forest plot plot_forest(pool_obj, arm_labels = c(ref = "Placebo", alt = "Drug A")) # LSM display with custom panel widths plot_forest( pool_obj, display = "lsm", arm_labels = c(ref = "Placebo", alt = "Drug A"), title = "LS Mean Estimates by Visit", panel_widths = c(3, 5, 1.5) ) }
Converts an rbmi pool object to the pharmaverse Analysis Results Dataset (ARD) standard using the cards package. The ARD format is a long-format data frame where each row represents a single statistic for a given parameter, with grouping columns for visit, parameter type, and least-squares-mean type.
pool_to_ard(pool_obj, analysis_obj = NULL, conf.level = NULL)pool_to_ard(pool_obj, analysis_obj = NULL, conf.level = NULL)
pool_obj |
A pooled analysis object of class |
analysis_obj |
An optional analysis object (output of
|
conf.level |
Confidence level used for CI labels (e.g., |
The function works by:
Tidying the pool object via tidy_pool_obj()
Reshaping each parameter into long-format ARD rows (one row per statistic)
Adding grouping columns (visit, parameter_type, lsm_type)
Optionally enriching with MI diagnostic statistics when analysis_obj
is provided
Applying cards::as_card() and cards::tidy_ard_column_order() for
standard ARD structure
When analysis_obj is provided:
For Rubin's rules pooling: diagnostic statistics (FMI, lambda, RIV, Barnard-Rubin adjusted df, relative efficiency) are computed per parameter using the per-imputation estimates, standard errors, and degrees of freedom.
For non-Rubin pooling methods: an informative message is emitted and the base ARD is returned without diagnostic rows.
The resulting ARD passes cards::check_ard_structure() validation and is
suitable for downstream use with gtsummary.
A data frame of class "card" (ARD format) with grouping columns
for visit (group1), parameter_type (group2), and lsm_type (group3).
Each parameter produces rows for five statistics: estimate, std.error,
conf.low, conf.high, and p.value, plus a method row. When analysis_obj
is provided and the pooling method is Rubin's rules, additional diagnostic
stat rows are included: fmi, lambda, riv, df.adjusted, df.complete, re,
and m.imputations.
rbmi::pool() for creating pool objects
analyse_mi_data() for creating analysis objects
tidy_pool_obj() for the underlying data transformation
cards::as_card() and cards::check_ard_structure() for ARD validation
if (requireNamespace("cards", quietly = TRUE)) { library(rbmi) data("ADMI", package = "rbmiUtils") ADMI$TRT <- factor(ADMI$TRT, levels = c("Placebo", "Drug A")) ADMI$USUBJID <- factor(ADMI$USUBJID) ADMI$AVISIT <- factor(ADMI$AVISIT) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") ) method <- method_bayes( n_samples = 20, control = control_bayes(warmup = 20, thin = 1) ) ana_obj <- analyse_mi_data(ADMI, vars, method, fun = ancova) pool_obj <- pool(ana_obj) # Base ARD ard <- pool_to_ard(pool_obj) # Enriched ARD with MI diagnostics (FMI, lambda, RIV, df) ard_diag <- pool_to_ard(pool_obj, analysis_obj = ana_obj) }if (requireNamespace("cards", quietly = TRUE)) { library(rbmi) data("ADMI", package = "rbmiUtils") ADMI$TRT <- factor(ADMI$TRT, levels = c("Placebo", "Drug A")) ADMI$USUBJID <- factor(ADMI$USUBJID) ADMI$AVISIT <- factor(ADMI$AVISIT) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") ) method <- method_bayes( n_samples = 20, control = control_bayes(warmup = 20, thin = 1) ) ana_obj <- analyse_mi_data(ADMI, vars, method, fun = ancova) pool_obj <- pool(ana_obj) # Base ARD ard <- pool_to_ard(pool_obj) # Enriched ARD with MI diagnostics (FMI, lambda, RIV, df) ard_diag <- pool_to_ard(pool_obj, analysis_obj = ana_obj) }
Builds a data_ice data.frame from a column in the dataset that flags
intercurrent events. For each subject, the first visit (by factor level
order) where the flag is TRUE is used as the ICE visit.
prepare_data_ice(data, vars, ice_col, strategy)prepare_data_ice(data, vars, ice_col, strategy)
data |
A data.frame containing the analysis dataset. |
vars |
A |
ice_col |
Character string naming the column in |
strategy |
Character string specifying the imputation strategy to assign.
Must be one of |
A data.frame with columns corresponding to vars$subjid,
vars$visit, and vars$strategy, suitable for passing to
rbmi::draws().
rbmi::draws() which accepts the data_ice output from this function
validate_data() to check data before imputation
summarise_missingness() to understand missing data patterns
library(rbmi) dat <- data.frame( USUBJID = factor(rep(c("S1", "S2", "S3"), each = 3)), AVISIT = factor(rep(c("Week 4", "Week 8", "Week 12"), 3), levels = c("Week 4", "Week 8", "Week 12")), TRT = factor(rep(c("Placebo", "Drug A", "Drug A"), each = 3)), CHG = rnorm(9), DISCFL = c("N","N","N", "N","Y","Y", "N","N","Y") ) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG" ) ice <- prepare_data_ice(dat, vars, ice_col = "DISCFL", strategy = "JR") print(ice)library(rbmi) dat <- data.frame( USUBJID = factor(rep(c("S1", "S2", "S3"), each = 3)), AVISIT = factor(rep(c("Week 4", "Week 8", "Week 12"), 3), levels = c("Week 4", "Week 8", "Week 12")), TRT = factor(rep(c("Placebo", "Drug A", "Drug A"), each = 3)), CHG = rnorm(9), DISCFL = c("N","N","N", "N","Y","Y", "N","N","Y") ) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG" ) ice <- prepare_data_ice(dat, vars, ice_col = "DISCFL", strategy = "JR") print(ice)
Prints a summary of an analysis object from analyse_mi_data().
## S3 method for class 'analysis' print(x, ...)## S3 method for class 'analysis' print(x, ...)
x |
An object of class |
... |
Additional arguments (currently unused). |
Invisibly returns the input object.
library(rbmi) library(rbmiUtils) data("ADMI") # Create analysis object vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA") ) method <- method_bayes(n_samples = 10, control = control_bayes(warmup = 10)) ana_obj <- analyse_mi_data(ADMI, vars, method, fun = function(d, v, ...) 1) print(ana_obj)library(rbmi) library(rbmiUtils) data("ADMI") # Create analysis object vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA") ) method <- method_bayes(n_samples = 10, control = control_bayes(warmup = 10)) ana_obj <- analyse_mi_data(ADMI, vars, method, fun = function(d, v, ...) 1) print(ana_obj)
Displays a formatted summary of a draws description using cli formatting.
## S3 method for class 'describe_draws' print(x, ...)## S3 method for class 'describe_draws' print(x, ...)
x |
A |
... |
Additional arguments (currently unused). |
Invisibly returns x (for pipe chaining).
## Not run: # After creating draws_obj via the rbmi pipeline (see describe_draws): desc <- describe_draws(draws_obj) print(desc) # Formatted cli output with method, formula, samples, convergence ## End(Not run)## Not run: # After creating draws_obj via the rbmi pipeline (see describe_draws): desc <- describe_draws(draws_obj) print(desc) # Formatted cli output with method, formula, samples, convergence ## End(Not run)
Displays a formatted summary of an imputation description using cli formatting, including method, number of imputations, reference arm mappings, and a missingness breakdown by visit and treatment arm.
## S3 method for class 'describe_imputation' print(x, ...)## S3 method for class 'describe_imputation' print(x, ...)
x |
A |
... |
Additional arguments (currently unused). |
Invisibly returns x (for pipe chaining).
## Not run: # After creating impute_obj via the rbmi pipeline (see describe_imputation): desc <- describe_imputation(impute_obj) print(desc) # Formatted cli output with method, M, subjects, references, missingness ## End(Not run)## Not run: # After creating impute_obj via the rbmi pipeline (see describe_imputation): desc <- describe_imputation(impute_obj) print(desc) # Formatted cli output with method, M, subjects, references, missingness ## End(Not run)
Displays a formatted summary of a pooled analysis object from rbmi::pool().
Uses cli formatting to show rounded estimates, confidence intervals, parameter
labels, method information, number of imputations, and confidence level.
## S3 method for class 'pool' print(x, digits = 2, ...)## S3 method for class 'pool' print(x, digits = 2, ...)
x |
An object of class |
digits |
Integer. Number of decimal places for rounding estimates, standard errors, and confidence interval bounds. Default is 2. |
... |
Additional arguments (currently unused). |
This method overrides rbmi::print.pool() to provide enhanced, formatted
console output using the cli package. The override produces a
"Registered S3 method overwritten" message at package load time, which is
expected and harmless (same pattern as print.analysis()).
The output includes:
A header with parameter and visit counts
Metadata: pooling method, number of imputations, confidence level
A compact results table with key columns: parameter, visit, est, lci, uci, pval
Invisibly returns the original pool object x (for pipe chaining).
tidy_pool_obj() for full tidy tibble output
summary.pool() for visit-level breakdown with significance flags
rbmi::pool() to create pool objects
library(rbmi) library(rbmiUtils) data("ADMI") ADMI$TRT <- factor(ADMI$TRT, levels = c("Placebo", "Drug A")) ADMI$USUBJID <- factor(ADMI$USUBJID) ADMI$AVISIT <- factor(ADMI$AVISIT) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") ) method <- method_bayes(n_samples = 20, control = control_bayes(warmup = 20)) ana_obj <- analyse_mi_data(ADMI, vars, method, fun = ancova) pool_obj <- pool(ana_obj) print(pool_obj)library(rbmi) library(rbmiUtils) data("ADMI") ADMI$TRT <- factor(ADMI$TRT, levels = c("Placebo", "Drug A")) ADMI$USUBJID <- factor(ADMI$USUBJID) ADMI$AVISIT <- factor(ADMI$AVISIT) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") ) method <- method_bayes(n_samples = 20, control = control_bayes(warmup = 20)) ana_obj <- analyse_mi_data(ADMI, vars, method, fun = ancova) pool_obj <- pool(ana_obj) print(pool_obj)
Extracts only the imputed records (those that were originally missing) from a full imputed dataset. This significantly reduces storage requirements when working with many imputations, as observed values are identical across all imputations and only need to be stored once in the original data.
reduce_imputed_data(imputed_data, original_data, vars)reduce_imputed_data(imputed_data, original_data, vars)
imputed_data |
A data.frame containing the full imputed dataset with an
|
original_data |
A data.frame containing the original dataset before imputation, with missing values in the outcome column. |
vars |
A |
Storage savings depend on the proportion of missing data. For example:
Original: 1000 rows, 44 missing values
Full imputed (1000 imputations): 1,000,000 rows
Reduced (1000 imputations): 44,000 rows (4.4\
Use expand_imputed_data() to reconstruct the full imputed dataset when
needed for analysis.
A data.frame containing only the rows from imputed_data that
correspond to originally missing outcome values. All columns from
imputed_data are preserved.
rbmi::impute() which creates the imputed datasets this function operates on
expand_imputed_data() to reconstruct the full dataset
get_imputed_data() to extract imputed data from an rbmi imputation object
library(rbmi) library(dplyr) # Example with package data data("ADMI", package = "rbmiUtils") data("ADEFF", package = "rbmiUtils") # Prepare original data to match ADMI structure original <- ADEFF |> mutate( TRT = TRT01P, USUBJID = as.character(USUBJID) ) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG" ) # Reduce to only imputed values reduced <- reduce_imputed_data(ADMI, original, vars) # Compare sizes cat("Full imputed rows:", nrow(ADMI), "\n") cat("Reduced rows:", nrow(reduced), "\n") cat("Compression:", round(100 * nrow(reduced) / nrow(ADMI), 1), "%\n")library(rbmi) library(dplyr) # Example with package data data("ADMI", package = "rbmiUtils") data("ADEFF", package = "rbmiUtils") # Prepare original data to match ADMI structure original <- ADEFF |> mutate( TRT = TRT01P, USUBJID = as.character(USUBJID) ) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG" ) # Reduce to only imputed values reduced <- reduce_imputed_data(ADMI, original, vars) # Compare sizes cat("Full imputed rows:", nrow(ADMI), "\n") cat("Reduced rows:", nrow(reduced), "\n") cat("Compression:", round(100 * nrow(reduced) / nrow(ADMI), 1), "%\n")
Tabulates missing outcome data by visit and treatment group, and classifies each subject's missing data pattern as complete, monotone, or intermittent.
summarise_missingness(data, vars)summarise_missingness(data, vars)
data |
A data.frame containing the analysis dataset with one row per subject-visit combination. |
vars |
A |
A list with three components:
A tibble with columns: visit, group, n, n_miss, pct_miss
A tibble with columns: subjid, group, pattern ("complete", "monotone", or "intermittent"), dropout_visit (NA if not monotone)
A tibble with columns: group, n_subjects, n_complete, n_monotone, n_intermittent
rbmi::draws() for imputation after reviewing missingness patterns
validate_data() to check data before imputation
prepare_data_ice() to create intercurrent event data from flags
library(rbmi) dat <- data.frame( USUBJID = factor(rep(c("S1", "S2", "S3", "S4"), each = 3)), AVISIT = factor(rep(c("Week 4", "Week 8", "Week 12"), 4), levels = c("Week 4", "Week 8", "Week 12")), TRT = factor(rep(c("Placebo", "Drug A"), each = 6)), CHG = c(1, 2, 3, 1, NA, NA, 1, 2, NA, 1, NA, 2) ) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG" ) result <- summarise_missingness(dat, vars) print(result$by_visit) print(result$patterns) print(result$summary)library(rbmi) dat <- data.frame( USUBJID = factor(rep(c("S1", "S2", "S3", "S4"), each = 3)), AVISIT = factor(rep(c("Week 4", "Week 8", "Week 12"), 4), levels = c("Week 4", "Week 8", "Week 12")), TRT = factor(rep(c("Placebo", "Drug A"), each = 6)), CHG = c(1, 2, 3, 1, NA, NA, 1, 2, NA, 1, NA, 2) ) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG" ) result <- summarise_missingness(dat, vars) print(result$by_visit) print(result$patterns) print(result$summary)
Provides a detailed summary of an analysis object from analyse_mi_data().
## S3 method for class 'analysis' summary(object, n_preview = 5, ...)## S3 method for class 'analysis' summary(object, n_preview = 5, ...)
object |
An object of class |
n_preview |
Maximum number of parameters to show in the preview table. Defaults to 5. |
... |
Additional arguments (currently unused). |
A list containing summary information (invisibly).
library(rbmi) library(rbmiUtils) data("ADMI") # Create analysis object vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA") ) method <- method_bayes(n_samples = 10, control = control_bayes(warmup = 10)) ana_obj <- analyse_mi_data(ADMI, vars, method, fun = function(d, v, ...) 1) summary(ana_obj)library(rbmi) library(rbmiUtils) data("ADMI") # Create analysis object vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA") ) method <- method_bayes(n_samples = 10, control = control_bayes(warmup = 10)) ana_obj <- analyse_mi_data(ADMI, vars, method, fun = function(d, v, ...) 1) summary(ana_obj)
Provides a detailed visit-level breakdown of pooled analysis results with significance flags. Shows treatment comparisons and least squares means grouped by visit.
## S3 method for class 'pool' summary(object, alpha = 0.05, ...)## S3 method for class 'pool' summary(object, alpha = 0.05, ...)
object |
An object of class |
alpha |
Numeric. Significance threshold for flagging p-values.
Default is 0.05. Flags are: |
... |
Additional arguments (currently unused). |
The summary output groups results by visit, showing treatment comparisons with significance flags and least squares means. This provides a quick overview of which visits have statistically significant treatment effects.
Significance flags:
* p < alpha (default 0.05)
** p < 0.01
*** p < 0.001
Invisibly returns a list with:
Number of parameters in the pool object
Character vector of unique visit names
Pooling method used
Number of imputations combined
Confidence level
The full tidy tibble from tidy_pool_obj()
print.pool() for compact tabular output
tidy_pool_obj() for full tidy tibble output
rbmi::pool() to create pool objects
library(rbmi) library(rbmiUtils) data("ADMI") ADMI$TRT <- factor(ADMI$TRT, levels = c("Placebo", "Drug A")) ADMI$USUBJID <- factor(ADMI$USUBJID) ADMI$AVISIT <- factor(ADMI$AVISIT) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") ) method <- method_bayes(n_samples = 20, control = control_bayes(warmup = 20)) ana_obj <- analyse_mi_data(ADMI, vars, method, fun = ancova) pool_obj <- pool(ana_obj) summary(pool_obj)library(rbmi) library(rbmiUtils) data("ADMI") ADMI$TRT <- factor(ADMI$TRT, levels = c("Placebo", "Drug A")) ADMI$USUBJID <- factor(ADMI$USUBJID) ADMI$AVISIT <- factor(ADMI$AVISIT) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") ) method <- method_bayes(n_samples = 20, control = control_bayes(warmup = 20)) ana_obj <- analyse_mi_data(ADMI, vars, method, fun = ancova) pool_obj <- pool(ana_obj) summary(pool_obj)
This function processes a pooled analysis object of class pool into a tidy tibble format.
It adds contextual information, such as whether a parameter is a treatment comparison or a least squares mean,
dynamically identifies visit names from the parameter column, and provides additional columns for parameter type,
least squares mean type, and visit.
tidy_pool_obj(pool_obj)tidy_pool_obj(pool_obj)
pool_obj |
A pooled analysis object of class |
The function dynamically processes the parameter column by separating it into
components (e.g., type of estimate, reference vs. alternative arm, and visit), and provides
informative descriptions in the output.
Workflow:
Prepare data and run imputation with rbmi
Analyse with analyse_mi_data()
Pool with rbmi::pool()
Tidy with tidy_pool_obj() for publication-ready output
A tibble containing the processed pooled analysis results with the following columns:
Original parameter name from the pooled object
Human-readable description of the parameter
Visit name extracted from parameter (if applicable)
Either "trt" (treatment comparison) or "lsm" (least squares mean)
For LSM parameters: "ref" (reference) or "alt" (alternative)
Point estimate
Standard error
Lower confidence interval
Upper confidence interval
P-value
rbmi::pool() which creates the pool objects this function tidies
analyse_mi_data() to analyse imputed datasets
format_results() for additional formatting options
# Example usage: library(dplyr) library(rbmi) data("ADMI") N_IMPUTATIONS <- 100 BURN_IN <- 200 BURN_BETWEEN <- 5 # Convert key columns to factors ADMI$TRT <- factor(ADMI$TRT, levels = c("Placebo", "Drug A")) ADMI$USUBJID <- factor(ADMI$USUBJID) ADMI$AVISIT <- factor(ADMI$AVISIT) # Define key variables for ANCOVA analysis vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") # Covariates for adjustment ) # Specify the imputation method (Bayesian) - need for pool step method <- rbmi::method_bayes( n_samples = N_IMPUTATIONS, control = rbmi::control_bayes( warmup = BURN_IN, thin = BURN_BETWEEN ) ) # Perform ANCOVA Analysis on Each Imputed Dataset ana_obj_ancova <- analyse_mi_data( data = ADMI, vars = vars, method = method, fun = ancova, # Apply ANCOVA delta = NULL # No sensitivity analysis adjustment ) pool_obj_ancova <- pool(ana_obj_ancova) tidy_df <- tidy_pool_obj(pool_obj_ancova) # Print tidy data frames print(tidy_df)# Example usage: library(dplyr) library(rbmi) data("ADMI") N_IMPUTATIONS <- 100 BURN_IN <- 200 BURN_BETWEEN <- 5 # Convert key columns to factors ADMI$TRT <- factor(ADMI$TRT, levels = c("Placebo", "Drug A")) ADMI$USUBJID <- factor(ADMI$USUBJID) ADMI$AVISIT <- factor(ADMI$AVISIT) # Define key variables for ANCOVA analysis vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA", "REGION") # Covariates for adjustment ) # Specify the imputation method (Bayesian) - need for pool step method <- rbmi::method_bayes( n_samples = N_IMPUTATIONS, control = rbmi::control_bayes( warmup = BURN_IN, thin = BURN_BETWEEN ) ) # Perform ANCOVA Analysis on Each Imputed Dataset ana_obj_ancova <- analyse_mi_data( data = ADMI, vars = vars, method = method, fun = ancova, # Apply ANCOVA delta = NULL # No sensitivity analysis adjustment ) pool_obj_ancova <- pool(ana_obj_ancova) tidy_df <- tidy_pool_obj(pool_obj_ancova) # Print tidy data frames print(tidy_df)
Pre-flight validation of data, variable specification, and intercurrent event
data before calling rbmi::draws(). Collects all issues and reports them
together in a single error message.
validate_data(data, vars, data_ice = NULL)validate_data(data, vars, data_ice = NULL)
data |
A data.frame containing the analysis dataset. |
vars |
A |
data_ice |
An optional data.frame of intercurrent events. If provided,
must contain columns corresponding to |
The following checks are performed:
data is a data.frame
All columns named in vars exist in data
subjid, visit, and group columns are factors
outcome column is numeric
Covariate columns have no missing values
Data has one row per subject-visit combination
If data_ice is provided: correct columns, valid subjects, valid
visits, recognised strategies, and at most one row per subject
Recommended Workflow:
Call validate_data() to check your data
Use prepare_data_ice() to create ICE data if needed
Review missingness with summarise_missingness()
Proceed with rbmi::draws() for imputation
Invisibly returns TRUE if all checks pass. Throws an error with
collected messages if any issues are found.
rbmi::draws() which requires validated input data
prepare_data_ice() to create intercurrent event data from flags
summarise_missingness() to understand missing data patterns
library(rbmi) dat <- data.frame( USUBJID = factor(rep(c("S1", "S2", "S3"), each = 3)), AVISIT = factor(rep(c("Week 4", "Week 8", "Week 12"), 3), levels = c("Week 4", "Week 8", "Week 12")), TRT = factor(rep(c("Placebo", "Drug A", "Drug A"), each = 3)), CHG = c(1.1, 2.2, 3.3, 0.5, NA, NA, 1.0, 2.0, NA), BASE = rep(c(10, 12, 11), each = 3), STRATA = factor(rep(c("A", "B", "A"), each = 3)) ) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA") ) validate_data(dat, vars)library(rbmi) dat <- data.frame( USUBJID = factor(rep(c("S1", "S2", "S3"), each = 3)), AVISIT = factor(rep(c("Week 4", "Week 8", "Week 12"), 3), levels = c("Week 4", "Week 8", "Week 12")), TRT = factor(rep(c("Placebo", "Drug A", "Drug A"), each = 3)), CHG = c(1.1, 2.2, 3.3, 0.5, NA, NA, 1.0, 2.0, NA), BASE = rep(c(10, 12, 11), each = 3), STRATA = factor(rep(c("A", "B", "A"), each = 3)) ) vars <- set_vars( subjid = "USUBJID", visit = "AVISIT", group = "TRT", outcome = "CHG", covariates = c("BASE", "STRATA") ) validate_data(dat, vars)