Title: | Tables and Graphs for Mixed Models for Repeated Measures (MMRM) |
Version: | 0.3.3 |
Date: | 2025-07-03 |
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. |
License: | Apache License 2.0 |
URL: | https://github.com/insightsengineering/tern.mmrm, https://insightsengineering.github.io/tern.mmrm/ |
BugReports: | https://github.com/insightsengineering/tern.mmrm/issues |
Depends: | R (≥ 3.6), tern (≥ 0.9.9) |
Imports: | checkmate (≥ 2.1.0), cowplot, dplyr (≥ 1.1.0), emmeans (≥ 1.10.4), formatters (≥ 0.5.11), generics, ggplot2, lifecycle (≥ 0.2.0), magrittr, mmrm (≥ 0.3.5), parallelly (≥ 1.25.0), rlang (≥ 1.0.1), rtables (≥ 0.6.13), stats, tidyr (≥ 0.8.3) |
Suggests: | broom (≥ 0.7.10), grid, knitr (≥ 1.42), maditr (≥ 0.8.1), Matrix, rmarkdown (≥ 2.23), testthat (≥ 3.1), TMB, vdiffr (≥ 1.0.7), withr (≥ 2.0.0) |
VignetteBuilder: | knitr, rmarkdown |
Config/Needs/verdepcheck: | insightsengineering/tern, mllg/checkmate, wilkelab/cowplot, tidyverse/dplyr, rvlenth/emmeans, insightsengineering/formatters, r-lib/generics, tidyverse/ggplot2, r-lib/lifecycle, tidyverse/magrittr, openpharma/mmrm, HenrikBengtsson/parallelly, r-lib/rlang, insightsengineering/rtables, tidyverse/tidyr, tidymodels/broom, yihui/knitr, gdemin/maditr, cran/Matrix, rstudio/rmarkdown, r-lib/testthat, r-lib/vdiffr, r-lib/withr |
Config/testthat/edition: | 3 |
Encoding: | UTF-8 |
Language: | en-US |
LazyData: | true |
RoxygenNote: | 7.3.2 |
Collate: | 'utils.R' 'assert_data.R' 'covariance_plot.R' 'fit_mmrm.R' 'formula.R' 'g_mmrm.R' 'labels.R' 'lsmeans.R' 'subgroups.R' 'tabulate_mmrm.R' 'tern.mmrm-package.R' |
NeedsCompilation: | no |
Packaged: | 2025-07-03 23:39:04 UTC; delaruae |
Author: | Daniel Sabanés Bové [aut],
Joe Zhu |
Maintainer: | Joe Zhu <joe.zhu@roche.com> |
Repository: | CRAN |
Date/Publication: | 2025-07-04 05:10:02 UTC |
tern.mmrm
Package
Description
tern.mmrm
is an analysis package to create tables, listings and graphs to analyze clinical trials data.
Author(s)
Maintainer: Joe Zhu joe.zhu@roche.com (ORCID)
Authors:
Daniel Sabanés Bové daniel@rconis.com
Godwin Yung yungg@gene.com
Francois Collin
Julia Dedic julia.dedic@roche.com
Jana Stoilova jana.stoilova@roche.com
Other contributors:
F. Hoffmann-La Roche AG [copyright holder, funder]
See Also
Useful links:
Report bugs at https://github.com/insightsengineering/tern.mmrm/issues
Formatted Analysis Function for MMRM Subgroups
Description
Formatted Analysis Function for MMRM Subgroups
Usage
a_mmrm_subgroups(.formats)
Arguments
.formats |
(named |
Value
List of formatted analysis functions to be used in
tabulate_mmrm_subgroups()
.
Building Model Formula
Description
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.
Usage
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")
)
Arguments
vars |
( |
cor_struct |
( |
Value
Formula to use in mmrm::mmrm()
.
Examples
vars <- list(
response = "AVAL", covariates = c("RACE", "SEX"),
id = "USUBJID", arm = "ARMCD", visit = "AVISIT"
)
build_formula(vars, "auto-regressive")
build_formula(vars)
Extraction of MMRM Subgroup Results based on Population Model Definition
Description
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.
Usage
extract_mmrm_subgroups(
fit,
visit,
subgroups = NULL,
groups_lists = list(),
treatment_arm = fit$treatment_levels[1L],
label_all = "All Patients"
)
Arguments
fit |
( |
visit |
( |
subgroups |
( |
groups_lists |
(named |
treatment_arm |
( |
label_all |
( |
Details
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.
Value
A list with two elements:
-
estimates
:data.frame
with columnsarm
,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 columnsn_tot
,diff
,lcl
,ucl
,pval
,subgroup
,var
,var_label
,row_type
. Note that this has half the number of rows asestimates
.
Note
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.
Examples
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
Analysis
Description
Does the MMRM
analysis. Multiple other functions can be called on the result to produce
tables and graphs.
Usage
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,
...
)
Arguments
vars |
(named
Note that the main effects and interaction of |
data |
( |
conf_level |
( |
cor_struct |
( |
weights_emmeans |
( |
averages_emmeans |
( |
parallel |
( |
... |
additional arguments for |
Details
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 areT
visits, thenT * (T+1) / 2
variance parameters are used. -
toeplitz
: Homogeneous Toeplitz covariance matrix, which usesT
variance parameters. -
heterogeneous toeplitz
: Heterogeneous Toeplitz covariance matrix, which uses2 * T - 1
variance parameters. -
ante-dependence
: Homogeneous Ante-Dependence covariance matrix, which usesT
variance parameters. -
heterogeneous ante-dependence
: Heterogeneous Ante-Dependence covariance matrix, which uses2 * 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 usesT + 1
variance parameters. -
compound symmetry
: Homogeneous Compound Symmetry covariance matrix, which uses 2 variance parameters. -
heterogeneous compound symmetry
: Heterogeneous Compound Symmetry covariance matrix, which usesT + 1
variance parameters.
Value
A tern_mmrm
object which is a list with MMRM results:
-
fit
: Themmrm
object which was fitted to the data. Note that viammrm::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 framesestimates
andcontrasts
. The attributesaverages
andweights
save the settings used (averages_emmeans
andweights_emmeans
). -
vars
: The variable list. -
labels
: Corresponding list with variable labels extracted fromdata
. -
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 thelsmeans
confidence intervals. -
additional
: List with any additional inputs passed via...
Examples
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")
)
)
Visualization of Covariance Matrix
Description
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.
Usage
g_covariance(
vcov_matrix,
time_prefix = NULL,
x_var = c("lag", "time_diff"),
xlab = NULL,
ylab = ""
)
Arguments
vcov_matrix |
( |
time_prefix |
( |
x_var |
( |
xlab |
( |
ylab |
( |
Details
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
.
Value
The ggplot
object.
Examples
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")
Diagnostic Plots for MMRM
Description
This function produces diagnostic plots.
Usage
g_mmrm_diagnostic(
object,
type = c("fit-residual", "q-q-residual"),
z_threshold = NULL
)
Arguments
object |
( |
type |
(
|
z_threshold |
( |
Details
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.
Value
A ggplot2
plot.
See Also
g_mmrm_lsmeans()
for plotting the least-squares means and contrasts.
Examples
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")
Plot LS means for MMRM
Description
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.
Usage
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
)
Arguments
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 |
( |
Details
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.
Value
A ggplot2
plot.
Examples
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")
)
Extract Least Square Means from MMRM
Description
Extracts the least square means from an MMRM
fit.
Usage
get_mmrm_lsmeans(fit, vars, conf_level, weights, averages = list())
Arguments
fit |
( |
vars |
(named
Note that the main effects and interaction of |
conf_level |
( |
weights |
( |
averages |
( |
Value
A list with data frames estimates
and contrasts
.
The attributes averages
and weights
save the settings used.
Assertions for Datasets
Description
We provide assertion functions to check the input dataset.
Usage
h_assert_one_rec_pt_visit(vars, data)
h_assert_rsp_var(vars, data)
h_assert_visit_var(vars, data)
h_assert_id_var(vars, data)
h_assert_weights_var(vars, data)
h_assert_data(vars, data)
Arguments
vars |
( |
data |
( |
Value
Nothing, only fails with an error if assertion does not hold.
Functions
-
h_assert_one_rec_pt_visit()
: asserts single record per patient and visit. -
h_assert_rsp_var()
: assert numeric response variable. -
h_assert_visit_var()
: assert factor visit variable. -
h_assert_id_var()
: assert subject ID variable. -
h_assert_weights_var()
: assert numeric weights variable. -
h_assert_data()
: high-level assertion function to check the dataset.
Extraction of Covariate Parts from Character Vector
Description
Extraction of Covariate Parts from Character Vector
Usage
h_get_covariate_parts(covariates)
Arguments
covariates |
( |
Value
Character vector of the covariates involved in covariates
specification.
Linear Mixed Model Diagnostics
Description
Compute the model diagnostic statistics for a linear mixed model fit.
Usage
h_get_diagnostics(fit)
Arguments
fit |
( |
Value
A list with the REML
criterion, the AIC
, AICc
and BIC
.
Time points helper for Covariance Plot
Description
Get the inputted symmetric matrices row and column labels as numeric time points.
Usage
h_get_timepoint_vars(vcov_matrix, time_prefix = NULL)
Arguments
vcov_matrix |
( |
time_prefix |
( |
Value
This function returns a list with of two sets of numbers:
row_time
and col_time
, identifying the timepoints of the upper triangular
part of vcov_matrix
.
Helper for Extraction and Formatting of MMRM Subgroups
Description
This is an internal helper to extract the correct LS mean estimates and contrast for a specific visit and treatment arm relative to the reference arm for one MMRM subgroup.
Usage
h_mmrm_subgroup_df(
lsmeans,
overall_fit,
is_in_subset,
visit,
treatment_arm,
subgroup,
var,
label
)
Arguments
lsmeans |
(named |
overall_fit |
( |
is_in_subset |
( |
visit |
( |
treatment_arm |
( |
subgroup |
( |
var |
( |
label |
( |
Value
List with estimates
(with 2 rows) and contrasts
(with 1 row) in the
format needed in extract_mmrm_subgroups()
.
Vectorization helper for Covariance Plot
Description
This function vectorizes the upper-diagonal elements of a symmetric matrix (e.g. the covariance matrix) and obtains the lag and time distance between pairs of observations if the time values are part of the names or are part of the matrix column and row names.
Usage
h_vectorization(vcov_matrix, time_prefix = NULL)
Arguments
vcov_matrix |
( |
time_prefix |
( |
Value
A data.frame
with the upper-diagonal elements of vcov_matrix
.
In the context of repeated measures, this matrix contains the association between pairs
of measurements taken at different time points. It contains the following columns:
-
Vect
: the upper-diagonal elements ofvcov_matrix
. -
time_diff
: the difference between column and row times. -
lag
: the lag, defined as the difference between column and row ranks. -
rank_row
: the row rank. -
rank_col
: the column rank.
Adding Labels To Variables For Model
Description
Adding Labels To Variables For Model
Usage
h_is_specified(x, vars)
h_is_specified_and_in_data(x, vars, data)
h_check_and_get_label(x, vars, data)
h_labels(vars, data)
Arguments
x |
( |
vars |
( |
data |
( |
Functions
-
h_is_specified()
: checks if element invars
is notNULL
and not empty. -
h_is_specified_and_in_data()
: checks if element in vars is not NULL and exists in dataset. -
h_check_and_get_label()
: gets label for each element in vars. -
h_labels()
: returns the list of variables with labels.
Helpers for Processing Least Square Means
Description
Helpers for Processing Least Square Means
Usage
h_get_emmeans_res(fit, vars, weights)
h_get_average_visit_specs(emmeans_res, vars, averages, fit)
h_get_spec_visit_estimates(emmeans_res, specs, conf_level, tests = FALSE)
h_get_single_visit_estimates(emmeans_res, conf_level)
h_get_relative_reduc_df(estimates, vars)
h_single_visit_contrast_specs(emmeans_res, vars)
h_average_visit_contrast_specs(specs, averages)
Arguments
fit |
result of |
vars |
(named
Note that the main effects and interaction of |
weights |
( |
emmeans_res |
( |
averages |
( |
specs |
( |
conf_level |
( |
tests |
( |
estimates |
( |
Functions
-
h_get_emmeans_res()
: returns a list withobject
(emmGrid
object containingemmeans
results) andgrid
(data.frame
containing the potential arm and the visit variables together with the sample sizen
for each combination). -
h_get_average_visit_specs()
: constructs average of visits specifications. -
h_get_spec_visit_estimates()
: estimates least square means as adata.frame
given specifications. -
h_get_single_visit_estimates()
: estimates least square means for single visits. -
h_get_relative_reduc_df()
: constructsdata.frame
with relative reduction vs. reference arm based on single visit estimates. -
h_single_visit_contrast_specs()
: constructs single visit contrast specifications. -
h_average_visit_contrast_specs()
: constructs average visits contrast specifications, given thespecs
for single visit contrasts and the averages required.
Example dataset for tern.mmrm
package.
Description
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
).
Usage
mmrm_test_data
Format
A tibble
with 800 rows and 7 variables:
-
USUBJID
: unique subject identifier. -
AVISIT
: visit number. -
ARMCD
: treatment,TRT
orPBO
. -
RACE
: 3-category race. -
SEX
: sex. -
FEV1_BL
:FEV1
at baseline (%). -
FEV1
:FEV1
at study visits.
Objects exported from other packages
Description
These objects are imported from other packages. Follow the links below to see their documentation.
Tabulation of MMRM
Results
Description
These functions can be used to produce tables from a fitted MMRM
produced with
fit_mmrm()
.
Usage
## 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
)
Arguments
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 |
Value
as.rtable.tern_mmrm()
returns the fixed effects, covariance estimate or
diagnostic statistics tables.
Functions
-
as.rtable(tern_mmrm)
: Produce simpleMMRM
tables via the genericas.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 (forbroom::tidy()
) to prepare adata.frame
from antern_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 callingrtables::make_afun()
on it. It is used asafun
inrtables::analyze()
. -
s_mmrm_lsmeans_single()
: Statistics function which is extracting estimates from a tidied least-squares means data frame whenARM
is not considered in the model. -
a_mmrm_lsmeans_single()
: Formatted Analysis function (whenARM
is not considered in the model) which can be further customized by callingrtables::make_afun()
on it. It is used asafun
inrtables::analyze()
. -
summarize_lsmeans()
: Analyze function for tabulating least-squares means estimates from tidiedmmrm
results.
Examples
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
)
Tabulation of MMRM Subgroups Results
Description
This function tabulates the results from extract_mmrm_subgroups()
.
Usage
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")
)
Arguments
lyt |
( |
df |
( |
vars |
( |
.formats |
(named |
.labels |
(named |
Value
The rtables
object.
Examples
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)