View source: R/apply_contrast.R
apply_contrast | R Documentation |
Calculates the marginal estimate of treatment effect and its corresponding standard error based on a fitted GLM object using specified contrast (summary measure) methods
apply_contrast(
object,
contrast = c("diff", "rr", "or", "logrr", "logor"),
reference
)
object |
a fitted |
contrast |
a string specifying the type of contrast to apply.
Accepted values are "diff" (risk difference), "rr" (risk ratio),
"or" (odds ratio), "logrr" (log risk ratio), "logor" (log odds ratio).
Note: log-transformed ratios (logrr and logor) work better compared to rr
and or when computing confidence intervals using normal approximation.
The choice of contrast affects how treatment effects are calculated and
interpreted. Default is |
reference |
a string or list of strings indicating which treatment
group(s) to use as reference level for pairwise comparisons. Accepted values
must be a subset of the levels in the treatment variable. Default to the
first n-1 treatment levels used in the This parameter influences the calculation of treatment effects relative to the chosen reference group. |
The apply_constrast()
functions computes the summary measure between two arms
based on the estimated marginal effect and its variance-covariance matrix using
the Delta method.
Note: Ensure that the glm
object has been adequately prepared with
average_predictions()
and estimate_varcov()
before applying apply_contrast()
. Failure to do so may result in
errors indicating missing components.
An updated glm
object with two additional components
appended: marginal_est
(marginal estimate of the treatment effect)
and marginal_se
(standard error of the marginal estimate).
These appended component provide crucial information for interpreting
the treatment effect using the specified contrast method.
get_marginal_effect()
for estimating marginal effects directly
from an original glm
object
trial01$trtp <- factor(trial01$trtp)
fit1 <- glm(aval ~ trtp + bl_cov, family = "binomial", data = trial01) |>
predict_counterfactuals(trt = "trtp") |>
average_predictions() |>
estimate_varcov(method = "Ye") |>
apply_contrast("diff", reference = "0")
# Assuming `trial01` is a dataset with treatment (`trtp`)
# and baseline covariate (`bl_cov`)
trial01$trtp <- factor(trial01$trtp)
fit1 <- glm(aval ~ trtp + bl_cov, family = "binomial", data = trial01)
# Preprocess fit1 as required by apply_contrast
fit2 <- fit1 |>
predict_counterfactuals(trt = "trtp") |>
average_predictions() |>
estimate_varcov(method = "Ye")
# Apply contrast to calculate marginal estimates
fit3 <- apply_contrast(fit2, contrast = "diff", reference = "0")
fit3$marginal_est
fit3$marginal_se
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.