Calculate the Change from Baseline or Treatment Effects from Estimated Marginal Means
Source:R/contrasts.R
treatment_effect.RdPass emmeans::emmeans() objects (probably obtained via
ncs_emmeans()) to emmeans::contrast() using specially constructed
contrast matrices so that change from baseline and treatment effects can be
calculated.
change_from_baselinecalculate the change from baseline for each of the different study arms/subgroups.treatment_effect()calculate the treatment effect for each study arm when there is no subgroup. When there is a subgroup, calculate the treatment effect between subgroups (examining the differences between the subgroups within each study arm) or within subgroups (examining the differences between the study arms within each subgroup).
Usage
change_from_baseline(
emmeans,
time_observed_continuous = emmeans@roles$predictors[2],
time_scheduled_baseline = 0,
arm = emmeans@roles$predictors[1],
subgroup = if (length(emmeans@roles$predictors) == 3) emmeans@roles$predictors[3],
contrast_args = list(adjust = "none"),
...,
as_tibble = FALSE,
confint_args = list(level = 0.95)
)
treatment_effect(
emmeans,
time_observed_continuous = emmeans@roles$predictors[2],
time_scheduled_baseline,
arm = emmeans@roles$predictors[1],
subgroup = if (length(emmeans@roles$predictors) == 3) emmeans@roles$predictors[3],
ref_value,
subgroup_type = c("between", "within"),
contrast_args = list(adjust = "none"),
...,
as_tibble = FALSE,
confint_args = list(level = 0.95)
)Arguments
- emmeans
(
emmGrid)
an object of classemmGrid, ideally obtained viancs_emmeans(), which wrapsemmeans::emmeans().- time_scheduled_baseline
(
scalar numeric)
the continuous time point when baseline was scheduled to occur. Defaults to 0.- arm, time_observed_continuous, subgroup
(
string)
strings identifying the study arm variable, observed continuous time variable, and (optionally) subgroup variable supplied toemmeans::emmeans(), probably viancs_emmeans()). Ifncs_emmeans()was indeed used, these strings should be contained in the character vectoremmeans@roles$predictors(see the default arguments).- contrast_args, ...
(named
list)
arguments to be passed toemmeans::contrast(). Any arguments with the namesobjectormethodwill be overwritten. Arguments incontrast_argsoverride identically named arguments in....- as_tibble
(
flag)TRUEorFALSEindicating whether or not the results ofemmeans::contrast()should be processed and returned as a tibble.- confint_args
(named
list)
arguments to be passed tostats::confint()when calculating confidence intervals. Ignored ifas_tibble = FALSE. IfNULL, confidence intervals will not be calculated. Defaults tolist(level = 0.95).- ref_value
(
string)
the value inarm(ifsubgroup = NULLor ifsubgroup_type = "within") or the value insubgroup(ifsubgroupis notNULLandsubgroup_type = "between") denoting the control group.- subgroup_type
(
string)
either"between"or"within", denoting whether to calculate the treatment effect between subgroups (examining the differences between the subgroups within each study arm) and once within subgroups (examining the differences between the study arms within each subgroup).
Value
When as_tibble = FALSE, the value returned by
emmeans::contrast(). If as_tibble = TRUE, a tibble:
{column name will be the value of the
armargument}: the study arm.{column name will be the value of the
time_observed_continuousargument}: the observed continuous time variable.{column name will be the value of the
subgroupargument}: the subgroup. Only present ifsubgroupis notNULL.estimate: estimate for change from baseline or treatment effect.SE: standard error ofestimate.df: degrees of freedom for calculating the confidence interval for and estimating the significance ofestimate.lower.CL: lower bound of confidence interval forestimate. Only present ifconfint_argsis notNULL.upper.CL: upper bound of confidence interval forestimate. Only present ifconfint_argsis notNULL.t.ratio: test statistic measuring the significance ofestimate.p.value: p-value for the significance ofestimate.
Examples
if (FALSE) { # interactive()
# Create a usable data set out of mmrm::fev_data
fev_mod <- mmrm::fev_data
fev_mod$VISITN <- fev_mod$VISITN * 10
fev_mod$time_cont <- fev_mod$VISITN + rnorm(nrow(fev_mod))
fev_mod$obs_visit_index <- round(fev_mod$time_cont)
fit <-
ncs_mmrm_fit(
data = fev_mod,
type = "subgroup_full",
response = FEV1,
subject = USUBJID,
cov_structs = c("ar1", "us"),
time_observed_continuous = time_cont,
df = 2,
time_observed_index = obs_visit_index,
time_scheduled_continuous = VISITN,
arm = ARMCD,
control_group = "PBO",
subgroup = SEX,
subgroup_comparator = "Male",
covariates = ~ FEV1_BL + RACE
)
marginal_means <-
ncs_emmeans(
fit = fit,
observed_time = "time_cont",
scheduled_time = "VISITN",
arm = "ARMCD",
subgroup = "SEX"
)
change_from_baseline(
emmeans = marginal_means,
time_observed_continuous = "time_cont",
time_scheduled_baseline = 10,
arm = "ARMCD",
subgroup = "SEX"
)
# Same thing as a tibble:
change_from_baseline(
emmeans = marginal_means,
time_observed_continuous = "time_cont",
time_scheduled_baseline = 10,
arm = "ARMCD",
subgroup = "SEX",
as_tibble = TRUE
)
treatment_effect(
emmeans = marginal_means,
time_observed_continuous = "time_cont",
time_scheduled_baseline = 10,
arm = "ARMCD",
subgroup = "SEX",
ref_value = "Male",
as_tibble = TRUE
)
}