View source: R/emmeans_comparison.R
emmeans_comparison | R Documentation |
Convert emmeans contrast methods into comparison functions
suitable for use with compare_levels()
.
emmeans_comparison(method, ...)
method |
An emmeans-style contrast method. One of: (1) a string specifying
the name of an emmeans contrast method, like
|
... |
Arguments passed on to the contrast method. |
Given an emmeans contrast method name as a string
(e.g., "pairwise"
, "trt.vs.ctrl"
, etc) or an emmeans-style contrast function
(e.g., emmeans::pairwise.emmc, emmeans::trt.vs.ctrl.emmc, etc), emmeans_comparison()
returns a new function that can be used in the comparison
argument to compare_levels()
to compute those contrasts.
A function that takes a single argument, var
, containing a variable
to generate contrasts for (e.g., a factor or a character vector) and returns
a function that generates a list of named unevaluated expressions representing
different contrasts of that variable. This function is suitable to be used
as the comparison
argument in compare_levels()
.
Matthew Kay
compare_levels()
, emmeans::contrast-methods.
See gather_emmeans_draws()
for a different approach to using emmeans
with
tidybayes
.
library(dplyr)
library(ggplot2)
data(RankCorr, package = "ggdist")
# emmeans contrast methods return matrices. E.g. the "eff" comparison
# compares each level to the average of all levels:
emmeans:::eff.emmc(c("a","b","c","d"))
# tidybayes::compare_levels() can't use a contrast matrix like this
# directly; it takes arbitrary expressions of factor levels. But
# we can use `emmeans_comparison` to generate the equivalent expressions:
emmeans_comparison("eff")(c("a","b","c","d"))
# We can use the "eff" comparison type with `compare_levels()` as follows:
RankCorr %>%
spread_draws(b[i,j]) %>%
filter(j == 1) %>%
compare_levels(b, by = i, comparison = emmeans_comparison("eff")) %>%
median_qi()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.