| Title: | Tables and Graphs for Mixed Models for Repeated Measures (MMRM) |
|---|---|
| Description: | Mixed models for repeated measures (MMRM) are a popular choice for analyzing longitudinal continuous outcomes in randomized clinical trials and beyond; see for example Cnaan, Laird and Slasor (1997) <doi:10.1002/(SICI)1097-0258(19971030)16:20%3C2349::AID-SIM667%3E3.0.CO;2-E>. This package provides an interface for fitting MMRM within the 'tern' <https://cran.r-project.org/package=tern> framework by Zhu et al. (2023) and tabulate results easily using 'rtables' <https://cran.r-project.org/package=rtables> by Becker et al. (2023). It builds on 'mmrm' <https://cran.r-project.org/package=mmrm> by Sabanés Bové et al. (2023) for the actual MMRM computations. |
| Authors: | Daniel Sabanés Bové [aut], Joe Zhu [aut, cre] (ORCID: <https://orcid.org/0000-0001-7566-2787>), Godwin Yung [aut], Francois Collin [aut], Julia Dedic [aut], Jana Stoilova [aut], F. Hoffmann-La Roche AG [cph, fnd] |
| Maintainer: | Joe Zhu <[email protected]> |
| License: | Apache License 2.0 |
| Version: | 0.3.3 |
| Built: | 2026-05-21 03:18:30 UTC |
| Source: | https://github.com/insightsengineering/tern.mmrm |
This builds the model formula which is used inside fit_mmrm() and provided
to mmrm::mmrm() internally. It can be instructive to look at the resulting
formula directly sometimes.
build_formula( vars, cor_struct = c("unstructured", "toeplitz", "heterogeneous toeplitz", "ante-dependence", "heterogeneous ante-dependence", "auto-regressive", "heterogeneous auto-regressive", "compound symmetry", "heterogeneous compound symmetry") )build_formula( vars, cor_struct = c("unstructured", "toeplitz", "heterogeneous toeplitz", "ante-dependence", "heterogeneous ante-dependence", "auto-regressive", "heterogeneous auto-regressive", "compound symmetry", "heterogeneous compound symmetry") )
vars |
( |
cor_struct |
( |
Formula to use in mmrm::mmrm().
vars <- list( response = "AVAL", covariates = c("RACE", "SEX"), id = "USUBJID", arm = "ARMCD", visit = "AVISIT" ) build_formula(vars, "auto-regressive") build_formula(vars)vars <- list( response = "AVAL", covariates = c("RACE", "SEX"), id = "USUBJID", arm = "ARMCD", visit = "AVISIT" ) build_formula(vars, "auto-regressive") build_formula(vars)
This prepares LS mean estimates and contrasts for a specific visit and treatment arm relative to the reference arm, along with a list of subgroup variables and corresponding (grouped) factor levels.
extract_mmrm_subgroups( fit, visit, subgroups = NULL, groups_lists = list(), treatment_arm = fit$treatment_levels[1L], label_all = "All Patients" )extract_mmrm_subgroups( fit, visit, subgroups = NULL, groups_lists = list(), treatment_arm = fit$treatment_levels[1L], label_all = "All Patients" )
fit |
( |
visit |
( |
subgroups |
( |
groups_lists |
(named |
treatment_arm |
( |
label_all |
( |
The groups_lists argument is handy when you don't want to have
subgroups identical to the original levels of the factor variable. This might
be the case when you want to merge levels into a single subgroup, define
overlapping subgroups or omit levels completely. Then you insert an element into
groups_lists with the name of the subgroups variable and containing
as a named list the subgroup definitions. See the example below.
A list with two elements:
estimates: data.frame with columns arm, n, lsmean, subgroup,
var, var_label, row_type, containing the LS means results for
the overall population and the specified subgroups.
contrasts: data.frame with columns n_tot, diff, lcl, ucl,
pval, subgroup, var, var_label, row_type. Note
that this has half the number of rows as estimates.
If the original model vars include covariates which are used here in
subgroups then these are dropped from covariates before the corresponding
model is fitted.
mmrm_results <- fit_mmrm( vars = list( response = "FEV1", covariates = "RACE", id = "USUBJID", arm = "ARMCD", visit = "AVISIT" ), data = mmrm_test_data, cor_struct = "compound symmetry", weights_emmeans = "equal", averages_emmeans = list( "VIS1+2" = c("VIS1", "VIS2") ) ) extract_mmrm_subgroups( fit = mmrm_results, visit = "VIS3", subgroups = c("RACE", "SEX"), groups_lists = list( RACE = list( A = c("Asian", "White"), B = c("Black or African American", "White") ) ) )mmrm_results <- fit_mmrm( vars = list( response = "FEV1", covariates = "RACE", id = "USUBJID", arm = "ARMCD", visit = "AVISIT" ), data = mmrm_test_data, cor_struct = "compound symmetry", weights_emmeans = "equal", averages_emmeans = list( "VIS1+2" = c("VIS1", "VIS2") ) ) extract_mmrm_subgroups( fit = mmrm_results, visit = "VIS3", subgroups = c("RACE", "SEX"), groups_lists = list( RACE = list( A = c("Asian", "White"), B = c("Black or African American", "White") ) ) )
MMRM AnalysisDoes the MMRM analysis. Multiple other functions can be called on the result to produce
tables and graphs.
fit_mmrm( vars = list(response = "AVAL", covariates = c(), id = "USUBJID", arm = "ARM", visit = "AVISIT"), data, conf_level = 0.95, cor_struct = "unstructured", weights_emmeans = "proportional", averages_emmeans = list(), parallel = FALSE, ... )fit_mmrm( vars = list(response = "AVAL", covariates = c(), id = "USUBJID", arm = "ARM", visit = "AVISIT"), data, conf_level = 0.95, cor_struct = "unstructured", weights_emmeans = "proportional", averages_emmeans = list(), parallel = FALSE, ... )
vars |
(named
Note that the main effects and interaction of |
data |
( |
conf_level |
( |
cor_struct |
( |
weights_emmeans |
( |
averages_emmeans |
( |
parallel |
( |
... |
additional arguments for |
Multiple different degree of freedom adjustments are available via the method argument
for mmrm::mmrm(). In addition, covariance matrix adjustments are available via vcov.
Please see mmrm::mmrm_control() for details and additional useful options.
For the covariance structure (cor_struct), the user can choose among the following options.
unstructured: Unstructured covariance matrix. This is the most flexible choice and default.
If there are T visits, then T * (T+1) / 2 variance parameters are used.
toeplitz: Homogeneous Toeplitz covariance matrix, which uses T variance parameters.
heterogeneous toeplitz: Heterogeneous Toeplitz covariance matrix,
which uses 2 * T - 1 variance parameters.
ante-dependence: Homogeneous Ante-Dependence covariance matrix, which uses T variance parameters.
heterogeneous ante-dependence: Heterogeneous Ante-Dependence covariance matrix,
which uses 2 * T - 1 variance parameters.
auto-regressive: Homogeneous Auto-Regressive (order 1) covariance matrix,
which uses 2 variance parameters.
heterogeneous auto-regressive: Heterogeneous Auto-Regressive (order 1) covariance matrix,
which uses T + 1 variance parameters.
compound symmetry: Homogeneous Compound Symmetry covariance matrix, which uses 2
variance parameters.
heterogeneous compound symmetry: Heterogeneous Compound Symmetry covariance matrix, which uses
T + 1 variance parameters.
A tern_mmrm object which is a list with MMRM results:
fit: The mmrm object which was fitted to the data. Note that via mmrm::component(fit, "optimizer")
the finally used optimization algorithm can be obtained, which can be useful for refitting the model
later on.
cov_estimate: The matrix with the covariance matrix estimate.
diagnostics: A list with model diagnostic statistics (REML criterion, AIC, corrected AIC, BIC).
lsmeans: This is a list with data frames estimates and contrasts.
The attributes averages and weights save the settings used
(averages_emmeans and weights_emmeans).
vars: The variable list.
labels: Corresponding list with variable labels extracted from data.
cor_struct: input.
parallel: input.
ref_level: The reference level for the arm variable, which is always the first level.
treatment_levels: The treatment levels for the arm variable.
conf_level: The confidence level which was used to construct the lsmeans confidence intervals.
additional: List with any additional inputs passed via ...
library(dplyr) library(rtables) mmrm_results <- fit_mmrm( vars = list( response = "FEV1", covariates = c("RACE", "SEX"), id = "USUBJID", arm = "ARMCD", visit = "AVISIT" ), data = mmrm_test_data, cor_struct = "unstructured", weights_emmeans = "equal", averages_emmeans = list( "VIS1+2" = c("VIS1", "VIS2") ) )library(dplyr) library(rtables) mmrm_results <- fit_mmrm( vars = list( response = "FEV1", covariates = c("RACE", "SEX"), id = "USUBJID", arm = "ARMCD", visit = "AVISIT" ), data = mmrm_test_data, cor_struct = "unstructured", weights_emmeans = "equal", averages_emmeans = list( "VIS1+2" = c("VIS1", "VIS2") ) )
Plot of covariance (or correlation) matrix as a function of lag or time. The covariance structure is vectorized internally and lag or time distances are computed and can be used for visualization.
g_covariance( vcov_matrix, time_prefix = NULL, x_var = c("lag", "time_diff"), xlab = NULL, ylab = "" )g_covariance( vcov_matrix, time_prefix = NULL, x_var = c("lag", "time_diff"), xlab = NULL, ylab = "" )
vcov_matrix |
( |
time_prefix |
( |
x_var |
( |
xlab |
( |
ylab |
( |
The default time_prefix value is NULL, which assumes that the names
of the input matrix don't have any character string other than time point
value. If a time_prefix is specified, this string should appear in front of
all the names in vcov_matrix.
The ggplot object.
vcov_matrix <- matrix( c(49, 12, 12, 23), nrow = 2, ncol = 2, dimnames = list( c(1, 2), c(1, 2) ) ) g_covariance(vcov_matrix, x_var = "time_diff")vcov_matrix <- matrix( c(49, 12, 12, 23), nrow = 2, ncol = 2, dimnames = list( c(1, 2), c(1, 2) ) ) g_covariance(vcov_matrix, x_var = "time_diff")
MMRM
This function produces diagnostic plots.
g_mmrm_diagnostic( object, type = c("fit-residual", "q-q-residual"), z_threshold = NULL )g_mmrm_diagnostic( object, type = c("fit-residual", "q-q-residual"), z_threshold = NULL )
object |
( |
type |
(
|
z_threshold |
( |
Here we use marginal fitted values and residuals. That is, we estimate fitted values, and the difference of those fitted values vs. the observed data are the residuals.
A ggplot2 plot.
g_mmrm_lsmeans() for plotting the least-squares means and contrasts.
mmrm_results <- fit_mmrm( vars = list( response = "FEV1", covariates = c("RACE", "SEX"), id = "USUBJID", arm = "ARMCD", visit = "AVISIT" ), data = mmrm_test_data, cor_struct = "unstructured", weights_emmeans = "equal" ) g_mmrm_diagnostic(mmrm_results) g_mmrm_diagnostic(mmrm_results, type = "q-q-residual")mmrm_results <- fit_mmrm( vars = list( response = "FEV1", covariates = c("RACE", "SEX"), id = "USUBJID", arm = "ARMCD", visit = "AVISIT" ), data = mmrm_test_data, cor_struct = "unstructured", weights_emmeans = "equal" ) g_mmrm_diagnostic(mmrm_results) g_mmrm_diagnostic(mmrm_results, type = "q-q-residual")
This function summarizes adjusted lsmeans and standard error, as well as conducts
comparisons between groups' adjusted lsmeans, where the first level of the group
is the reference level.
g_mmrm_lsmeans( object, select = c("estimates", "contrasts"), titles = c(estimates = paste("Adjusted mean of", object$labels$response, "by treatment at visits"), contrasts = paste0("Differences of ", object$labels$response, " adjusted means vs. control ('", object$ref_level, "')")), xlab = object$labels$visit, ylab = paste0("Estimates with ", round(object$conf_level * 100), "% CIs"), xlimits = NULL, ylimits = NULL, width = 0.6, show_pval = TRUE, show_lines = FALSE, constant_baseline = NULL, n_baseline = NA_integer_, table_stats = character(), table_formats = c(n = "xx.", estimate = "xx.x", se = "xx.x", ci = "(xx.xx, xx.xx)"), table_labels = c(n = "n", estimate = "LS mean", se = "Std. Error", ci = paste0(round(object$conf_level * 100), "% CI")), table_font_size = 3, table_rel_height = 0.5 )g_mmrm_lsmeans( object, select = c("estimates", "contrasts"), titles = c(estimates = paste("Adjusted mean of", object$labels$response, "by treatment at visits"), contrasts = paste0("Differences of ", object$labels$response, " adjusted means vs. control ('", object$ref_level, "')")), xlab = object$labels$visit, ylab = paste0("Estimates with ", round(object$conf_level * 100), "% CIs"), xlimits = NULL, ylimits = NULL, width = 0.6, show_pval = TRUE, show_lines = FALSE, constant_baseline = NULL, n_baseline = NA_integer_, table_stats = character(), table_formats = c(n = "xx.", estimate = "xx.x", se = "xx.x", ci = "(xx.xx, xx.xx)"), table_labels = c(n = "n", estimate = "LS mean", se = "Std. Error", ci = paste0(round(object$conf_level * 100), "% CI")), table_font_size = 3, table_rel_height = 0.5 )
object |
( |
select |
( |
titles |
( |
xlab |
( |
ylab |
( |
xlimits |
( |
ylimits |
( |
width |
( |
show_pval |
( |
show_lines |
( |
constant_baseline |
(named |
n_baseline |
( |
table_stats |
( |
table_formats |
(named |
table_labels |
(named |
table_font_size |
( |
table_rel_height |
( |
If variable labels are available in the original data set, then these are used. Otherwise the variable names themselves are used for annotating the plot.
The contrast plot is not going to be returned if treatment is not
considered in the tern_mmrm object input,
no matter if select argument contains the contrasts value.
A ggplot2 plot.
library(dplyr) mmrm_results <- fit_mmrm( vars = list( response = "FEV1", covariates = c("RACE", "SEX"), id = "USUBJID", arm = "ARMCD", visit = "AVISIT" ), data = mmrm_test_data, cor_struct = "unstructured", weights_emmeans = "equal" ) g_mmrm_lsmeans(mmrm_results, constant_baseline = c(BSL = 0)) g_mmrm_lsmeans( mmrm_results, select = "estimates", show_lines = TRUE, xlab = "Visit" ) g_mmrm_lsmeans( mmrm_results, select = "contrasts", titles = c(contrasts = "Contrasts of FKSI-FWB means"), show_pval = TRUE, show_lines = TRUE, width = 0.8 ) mmrm_test_data2 <- mmrm_test_data %>% filter(ARMCD == "TRT") mmrm_results_no_arm <- fit_mmrm( vars = list( response = "FEV1", covariates = c("RACE", "SEX"), id = "USUBJID", visit = "AVISIT" ), data = mmrm_test_data2, cor_struct = "unstructured", weights_emmeans = "equal" ) g_mmrm_lsmeans(mmrm_results_no_arm, select = "estimates") g_mmrm_lsmeans( mmrm_results_no_arm, select = c("estimates", "contrasts"), titles = c( estimates = "Adjusted mean of FKSI-FWB", contrasts = "it will not be created" ), show_pval = TRUE, width = 0.8 ) g_mmrm_lsmeans( mmrm_results_no_arm, select = "estimates", titles = c(estimates = "Adjusted mean of FKSI-FWB"), show_pval = TRUE, width = 0.8, show_lines = TRUE ) g_mmrm_lsmeans( mmrm_results, select = "estimates", titles = c(estimates = "Adjusted mean of FKSI-FWB"), table_stats = c("n", "ci") )library(dplyr) mmrm_results <- fit_mmrm( vars = list( response = "FEV1", covariates = c("RACE", "SEX"), id = "USUBJID", arm = "ARMCD", visit = "AVISIT" ), data = mmrm_test_data, cor_struct = "unstructured", weights_emmeans = "equal" ) g_mmrm_lsmeans(mmrm_results, constant_baseline = c(BSL = 0)) g_mmrm_lsmeans( mmrm_results, select = "estimates", show_lines = TRUE, xlab = "Visit" ) g_mmrm_lsmeans( mmrm_results, select = "contrasts", titles = c(contrasts = "Contrasts of FKSI-FWB means"), show_pval = TRUE, show_lines = TRUE, width = 0.8 ) mmrm_test_data2 <- mmrm_test_data %>% filter(ARMCD == "TRT") mmrm_results_no_arm <- fit_mmrm( vars = list( response = "FEV1", covariates = c("RACE", "SEX"), id = "USUBJID", visit = "AVISIT" ), data = mmrm_test_data2, cor_struct = "unstructured", weights_emmeans = "equal" ) g_mmrm_lsmeans(mmrm_results_no_arm, select = "estimates") g_mmrm_lsmeans( mmrm_results_no_arm, select = c("estimates", "contrasts"), titles = c( estimates = "Adjusted mean of FKSI-FWB", contrasts = "it will not be created" ), show_pval = TRUE, width = 0.8 ) g_mmrm_lsmeans( mmrm_results_no_arm, select = "estimates", titles = c(estimates = "Adjusted mean of FKSI-FWB"), show_pval = TRUE, width = 0.8, show_lines = TRUE ) g_mmrm_lsmeans( mmrm_results, select = "estimates", titles = c(estimates = "Adjusted mean of FKSI-FWB"), table_stats = c("n", "ci") )
MMRM
Extracts the least square means from an MMRM fit.
get_mmrm_lsmeans(fit, vars, conf_level, weights, averages = list())get_mmrm_lsmeans(fit, vars, conf_level, weights, averages = list())
fit |
( |
vars |
(named
Note that the main effects and interaction of |
conf_level |
( |
weights |
( |
averages |
( |
A list with data frames estimates and contrasts.
The attributes averages and weights save the settings used.
tern.mmrm package.Measurements of FEV1 (forced expired volume in one second) is a measure of how quickly the lungs can be emptied.
Low levels of FEV1 may indicate chronic obstructive pulmonary disease (COPD).
mmrm_test_datammrm_test_data
A tibble with 800 rows and 7 variables:
USUBJID: unique subject identifier.
AVISIT: visit number.
ARMCD: treatment, TRT or PBO.
RACE: 3-category race.
SEX: sex.
FEV1_BL: FEV1 at baseline (%).
FEV1: FEV1 at study visits.
MMRM ResultsThese functions can be used to produce tables from a fitted MMRM produced with
fit_mmrm().
## S3 method for class 'tern_mmrm' as.rtable(x, type = c("fixed", "cov", "diagnostic"), ...) h_mmrm_fixed(x, format = "xx.xxxx") h_mmrm_cov(x, format = "xx.xxxx") h_mmrm_diagnostic(x, format = "xx.xxxx") ## S3 method for class 'tern_mmrm' tidy(x, ...) s_mmrm_lsmeans(df, .in_ref_col, show_relative = c("reduction", "increase")) a_mmrm_lsmeans(df, .in_ref_col, show_relative = c("reduction", "increase")) s_mmrm_lsmeans_single(df) a_mmrm_lsmeans_single(df) summarize_lsmeans( lyt, arms = TRUE, ..., table_names = "lsmeans_summary", .stats = NULL, .formats = NULL, .indent_mods = NULL, .labels = NULL )## S3 method for class 'tern_mmrm' as.rtable(x, type = c("fixed", "cov", "diagnostic"), ...) h_mmrm_fixed(x, format = "xx.xxxx") h_mmrm_cov(x, format = "xx.xxxx") h_mmrm_diagnostic(x, format = "xx.xxxx") ## S3 method for class 'tern_mmrm' tidy(x, ...) s_mmrm_lsmeans(df, .in_ref_col, show_relative = c("reduction", "increase")) a_mmrm_lsmeans(df, .in_ref_col, show_relative = c("reduction", "increase")) s_mmrm_lsmeans_single(df) a_mmrm_lsmeans_single(df) summarize_lsmeans( lyt, arms = TRUE, ..., table_names = "lsmeans_summary", .stats = NULL, .formats = NULL, .indent_mods = NULL, .labels = NULL )
x |
( |
type |
( |
... |
additional argument |
format |
( |
df |
( |
.in_ref_col |
( |
show_relative |
should the "reduction" ( |
lyt |
( |
arms |
( |
table_names |
( |
.stats |
( |
.formats |
(named |
.indent_mods |
(named |
.labels |
(named |
as.rtable.tern_mmrm() returns the fixed effects, covariance estimate or
diagnostic statistics tables.
as.rtable(tern_mmrm): Produce simple MMRM tables via the generic as.rtable().
h_mmrm_fixed(): Helper function to produce fixed effects table.
h_mmrm_cov(): Helper function to produce a covariance matrix table.
h_mmrm_diagnostic(): Helper function to produce a diagnostic statistics table.
tidy(tern_mmrm): Helper method (for broom::tidy()) to prepare a data.frame from an
tern_mmrm object containing the least-squares means and contrasts.
s_mmrm_lsmeans(): Statistics function which is extracting estimates from a tidied least-squares means
data frame.
a_mmrm_lsmeans(): Formatted Analysis function which can be further customized by calling
rtables::make_afun() on it. It is used as afun in rtables::analyze().
s_mmrm_lsmeans_single(): Statistics function which is extracting estimates from a tidied least-squares means
data frame when ARM is not considered in the model.
a_mmrm_lsmeans_single(): Formatted Analysis function (when ARM is not considered in the model)
which can be further customized by calling
rtables::make_afun() on it. It is used as afun in rtables::analyze().
summarize_lsmeans(): Analyze function for tabulating least-squares means estimates from tidied mmrm results.
result <- fit_mmrm( vars = list( response = "FEV1", covariates = c("RACE", "SEX"), id = "USUBJID", arm = "ARMCD", visit = "AVISIT" ), data = mmrm_test_data, cor_struct = "unstructured", weights_emmeans = "equal" ) as.rtable(result, type = "cov", format = "xx.x") result_no_arm <- fit_mmrm( vars = list( response = "FEV1", covariates = c("RACE", "SEX"), id = "USUBJID", visit = "AVISIT" ), data = mmrm_test_data, cor_struct = "unstructured", weights_emmeans = "equal" ) as.rtable(result_no_arm, type = "cov", format = "xx.x") df <- broom::tidy(result) df_no_arm <- broom::tidy(result_no_arm) s_mmrm_lsmeans(df[8, ], .in_ref_col = FALSE) s_mmrm_lsmeans_single(df_no_arm[4, ]) library(dplyr) dat_adsl <- mmrm_test_data %>% select(USUBJID, ARMCD) %>% unique() basic_table() %>% split_cols_by("ARMCD", ref_group = result$ref_level) %>% add_colcounts() %>% split_rows_by("AVISIT") %>% summarize_lsmeans( .stats = c("n", "adj_mean_se", "adj_mean_ci", "diff_mean_se", "diff_mean_ci"), .labels = c(adj_mean_se = "Adj. LS Mean (Std. Error)"), .formats = c(adj_mean_se = sprintf_format("%.1f (%.2f)")) ) %>% build_table( df = broom::tidy(result), alt_counts_df = dat_adsl ) basic_table() %>% split_rows_by("AVISIT") %>% summarize_lsmeans(arms = FALSE) %>% build_table( df = broom::tidy(result_no_arm), alt_counts_df = dat_adsl )result <- fit_mmrm( vars = list( response = "FEV1", covariates = c("RACE", "SEX"), id = "USUBJID", arm = "ARMCD", visit = "AVISIT" ), data = mmrm_test_data, cor_struct = "unstructured", weights_emmeans = "equal" ) as.rtable(result, type = "cov", format = "xx.x") result_no_arm <- fit_mmrm( vars = list( response = "FEV1", covariates = c("RACE", "SEX"), id = "USUBJID", visit = "AVISIT" ), data = mmrm_test_data, cor_struct = "unstructured", weights_emmeans = "equal" ) as.rtable(result_no_arm, type = "cov", format = "xx.x") df <- broom::tidy(result) df_no_arm <- broom::tidy(result_no_arm) s_mmrm_lsmeans(df[8, ], .in_ref_col = FALSE) s_mmrm_lsmeans_single(df_no_arm[4, ]) library(dplyr) dat_adsl <- mmrm_test_data %>% select(USUBJID, ARMCD) %>% unique() basic_table() %>% split_cols_by("ARMCD", ref_group = result$ref_level) %>% add_colcounts() %>% split_rows_by("AVISIT") %>% summarize_lsmeans( .stats = c("n", "adj_mean_se", "adj_mean_ci", "diff_mean_se", "diff_mean_ci"), .labels = c(adj_mean_se = "Adj. LS Mean (Std. Error)"), .formats = c(adj_mean_se = sprintf_format("%.1f (%.2f)")) ) %>% build_table( df = broom::tidy(result), alt_counts_df = dat_adsl ) basic_table() %>% split_rows_by("AVISIT") %>% summarize_lsmeans(arms = FALSE) %>% build_table( df = broom::tidy(result_no_arm), alt_counts_df = dat_adsl )
This function tabulates the results from extract_mmrm_subgroups().
tabulate_mmrm_subgroups( lyt, df, vars = c("n_tot", "n", "lsmean", "diff", "ci"), .formats = list(n = "xx", n_tot = "xx", lsmean = "xx.x", diff = "xx.x", ci = "(xx.x, xx.x)", pval = "x.xxxx | (<0.0001)"), .labels = list(n = "n", n_tot = "Total n", lsmean = "Mean", diff = "Mean Difference", ci = paste0(round(100 * df$contrasts$conf_level[1]), "% CI"), pval = "p-value") )tabulate_mmrm_subgroups( lyt, df, vars = c("n_tot", "n", "lsmean", "diff", "ci"), .formats = list(n = "xx", n_tot = "xx", lsmean = "xx.x", diff = "xx.x", ci = "(xx.x, xx.x)", pval = "x.xxxx | (<0.0001)"), .labels = list(n = "n", n_tot = "Total n", lsmean = "Mean", diff = "Mean Difference", ci = paste0(round(100 * df$contrasts$conf_level[1]), "% CI"), pval = "p-value") )
lyt |
( |
df |
( |
vars |
( |
.formats |
(named |
.labels |
(named |
The rtables object.
mmrm_results <- fit_mmrm( vars = list( response = "FEV1", covariates = "RACE", id = "USUBJID", arm = "ARMCD", visit = "AVISIT" ), data = mmrm_test_data, cor_struct = "compound symmetry", weights_emmeans = "equal", averages_emmeans = list( "VIS1+2" = c("VIS1", "VIS2") ) ) df <- extract_mmrm_subgroups( fit = mmrm_results, visit = "VIS3", subgroups = c("RACE", "SEX") ) ## Table with default columns. basic_table() %>% tabulate_mmrm_subgroups(df) ## Table with selected columns. tab <- basic_table() %>% tabulate_mmrm_subgroups( df = df, vars = c("n_tot", "diff", "ci", "pval") ) tab ## Forest plot can be produced based on this very easily. g_forest(tab, logx = FALSE, xlim = c(-10, 10), x_at = c(-10, -5, 0, 5, 10), vline = 0)mmrm_results <- fit_mmrm( vars = list( response = "FEV1", covariates = "RACE", id = "USUBJID", arm = "ARMCD", visit = "AVISIT" ), data = mmrm_test_data, cor_struct = "compound symmetry", weights_emmeans = "equal", averages_emmeans = list( "VIS1+2" = c("VIS1", "VIS2") ) ) df <- extract_mmrm_subgroups( fit = mmrm_results, visit = "VIS3", subgroups = c("RACE", "SEX") ) ## Table with default columns. basic_table() %>% tabulate_mmrm_subgroups(df) ## Table with selected columns. tab <- basic_table() %>% tabulate_mmrm_subgroups( df = df, vars = c("n_tot", "diff", "ci", "pval") ) tab ## Forest plot can be produced based on this very easily. g_forest(tab, logx = FALSE, xlim = c(-10, 10), x_at = c(-10, -5, 0, 5, 10), vline = 0)