R/tidy_add_pairwise_contrasts.R
tidy_add_pairwise_contrasts.RdComputes pairwise contrasts with emmeans::emmeans() and add them to the
results tibble. Works only with models supported by emmeans, see
vignette("models", package = "emmeans").
tidy_add_pairwise_contrasts(
x,
variables = all_categorical(),
keep_model_terms = FALSE,
pairwise_reverse = TRUE,
contrasts_adjust = NULL,
conf.level = attr(x, "conf.level"),
emmeans_args = list(),
model = tidy_get_model(x),
quiet = FALSE
)(data.frame)
A tidy tibble as produced by tidy_*() functions.
include (tidy-select)
Variables for those pairwise contrasts should be added.
Default is all_categorical().
(logical)
Keep terms from the model?
(logical)
Determines whether to use "pairwise" (if TRUE)
or "revpairwise" (if FALSE), see emmeans::contrast().
(string)
Optional adjustment method when computing contrasts,
see emmeans::contrast() (if NULL, use emmeans default).
(numeric)
Confidence level, by default use the value indicated
previously in tidy_and_attach().
(list)
List of additional parameter to pass to
emmeans::emmeans() when computing pairwise contrasts.
(a model object, e.g. glm)
The corresponding model, if not attached to x.
(logical)
Whether broom.helpers should not return a message when requested output
cannot be generated. Default is FALSE.
If the contrasts column is not yet available in x,
tidy_add_contrasts() will be automatically applied.
For multi-components models, such as zero-inflated Poisson or beta regression, support of pairwise contrasts is still experimental.
Other tidy_helpers:
tidy_add_coefficients_type(),
tidy_add_contrasts(),
tidy_add_estimate_to_reference_rows(),
tidy_add_header_rows(),
tidy_add_n(),
tidy_add_reference_rows(),
tidy_add_term_labels(),
tidy_add_variable_labels(),
tidy_attach_model(),
tidy_disambiguate_terms(),
tidy_group_by(),
tidy_identify_variables(),
tidy_plus_plus(),
tidy_remove_intercept(),
tidy_select_variables()
# \donttest{
mod1 <- lm(Sepal.Length ~ Species, data = iris)
mod1 |>
tidy_and_attach() |>
tidy_add_pairwise_contrasts()
#> # A tibble: 4 × 13
#> term variable var_class var_type var_nlevels contrasts contrasts_type
#> <chr> <chr> <chr> <chr> <int> <chr> <chr>
#> 1 (Intercept) (Interc… NA interce… NA NA NA
#> 2 versicolor -… Species factor categor… 3 pairwise pairwise
#> 3 virginica - … Species factor categor… 3 pairwise pairwise
#> 4 virginica - … Species factor categor… 3 pairwise pairwise
#> # ℹ 6 more variables: estimate <dbl>, std.error <dbl>, statistic <dbl>,
#> # p.value <dbl>, conf.low <dbl>, conf.high <dbl>
mod1 |>
tidy_and_attach() |>
tidy_add_pairwise_contrasts(pairwise_reverse = FALSE)
#> # A tibble: 4 × 13
#> term variable var_class var_type var_nlevels contrasts contrasts_type
#> <chr> <chr> <chr> <chr> <int> <chr> <chr>
#> 1 (Intercept) (Interc… NA interce… NA NA NA
#> 2 setosa - ver… Species factor categor… 3 revpairw… pairwise
#> 3 setosa - vir… Species factor categor… 3 revpairw… pairwise
#> 4 versicolor -… Species factor categor… 3 revpairw… pairwise
#> # ℹ 6 more variables: estimate <dbl>, std.error <dbl>, statistic <dbl>,
#> # p.value <dbl>, conf.low <dbl>, conf.high <dbl>
mod1 |>
tidy_and_attach() |>
tidy_add_pairwise_contrasts(keep_model_terms = TRUE)
#> # A tibble: 6 × 13
#> term variable var_class var_type var_nlevels contrasts contrasts_type
#> <chr> <chr> <chr> <chr> <int> <chr> <chr>
#> 1 (Intercept) (Interc… NA interce… NA NA NA
#> 2 Speciesversi… Species factor categor… 3 contr.tr… treatment
#> 3 Speciesvirgi… Species factor categor… 3 contr.tr… treatment
#> 4 versicolor -… Species factor categor… 3 pairwise pairwise
#> 5 virginica - … Species factor categor… 3 pairwise pairwise
#> 6 virginica - … Species factor categor… 3 pairwise pairwise
#> # ℹ 6 more variables: estimate <dbl>, std.error <dbl>, statistic <dbl>,
#> # p.value <dbl>, conf.low <dbl>, conf.high <dbl>
mod1 |>
tidy_and_attach() |>
tidy_add_pairwise_contrasts(contrasts_adjust = "none")
#> # A tibble: 4 × 13
#> term variable var_class var_type var_nlevels contrasts contrasts_type
#> <chr> <chr> <chr> <chr> <int> <chr> <chr>
#> 1 (Intercept) (Interc… NA interce… NA NA NA
#> 2 versicolor -… Species factor categor… 3 pairwise pairwise
#> 3 virginica - … Species factor categor… 3 pairwise pairwise
#> 4 virginica - … Species factor categor… 3 pairwise pairwise
#> # ℹ 6 more variables: estimate <dbl>, std.error <dbl>, statistic <dbl>,
#> # p.value <dbl>, conf.low <dbl>, conf.high <dbl>
if (.assert_package("gtsummary", boolean = TRUE)) {
mod2 <- glm(
response ~ age + trt + grade,
data = gtsummary::trial,
family = binomial
)
mod2 |>
tidy_and_attach(exponentiate = TRUE) |>
tidy_add_pairwise_contrasts()
}
#> # A tibble: 6 × 13
#> term variable var_class var_type var_nlevels contrasts contrasts_type
#> <chr> <chr> <chr> <chr> <int> <chr> <chr>
#> 1 (Intercept) (Interc… NA interce… NA NA NA
#> 2 age age numeric continu… NA NA NA
#> 3 Drug B / Dru… trt character dichoto… 2 pairwise pairwise
#> 4 II / I grade factor categor… 3 pairwise pairwise
#> 5 III / I grade factor categor… 3 pairwise pairwise
#> 6 III / II grade factor categor… 3 pairwise pairwise
#> # ℹ 6 more variables: estimate <dbl>, std.error <dbl>, statistic <dbl>,
#> # p.value <dbl>, conf.low <dbl>, conf.high <dbl>
# }