contrast | R Documentation |
These methods provide for follow-up analyses of emmGrid
objects:
Contrasts, pairwise comparisons, tests, and confidence intervals. They may
also be used to compute arbitrary linear functions of predictions or EMMs.
contrast(object, ...)
## S3 method for class 'emmGrid'
contrast(object, method = "eff", interaction = FALSE, by,
offset = NULL, scale = NULL, name = "contrast",
options = get_emm_option("contrast"), type, adjust, simple,
combine = FALSE, ratios = TRUE, parens, enhance.levels = TRUE, wts,
...)
## S3 method for class 'emmGrid'
pairs(x, reverse = FALSE, ...)
## S3 method for class 'emmGrid'
coef(object, ...)
## S3 method for class 'emmGrid'
weights(object, ...)
object |
An object of class |
... |
Additional arguments passed to other methods |
method |
Character value giving the root name of a contrast method (e.g.
|
interaction |
Character vector, logical value, or list. If this is specified,
|
by |
Character names of variable(s) to be used for “by” groups. The
contrasts or joint tests will be evaluated separately for each combination
of these variables. If |
offset , scale |
Numeric vectors of the same length as each |
name |
Character name to use to override the default label for contrasts used in table headings or subsequent contrasts of the returned object. |
options |
If non- |
type |
Character: prediction type (e.g., |
adjust |
Character: adjustment method (e.g., |
simple |
Character vector or list: Specify the factor(s) not in
|
combine |
Logical value that determines what is returned when
|
ratios |
Logical value determining how log and logit transforms are
handled. These transformations are exceptional cases in that there is a
valid way to back-transform contrasts: differences of logs are logs of
ratios, and differences of logits are odds ratios. If |
parens |
character or |
enhance.levels |
character or logical.
If character, the levels of the named factors that are contrasted are enhanced
by appending the name of the factor; e.g., if a factor named |
wts |
The |
x |
An |
reverse |
Logical value - determines whether to use |
contrast
and pairs
return an object of class
emmGrid
. Its grid will correspond to the levels of the contrasts and
any by
variables. The exception is that an emm_list
object is returned if simple
is a list and combine
is
FALSE
.
coef
returns a data.frame
containing the "parent" object's grid,
along with columns named c.1, c.2, ...
containing the contrast coefficients
used to produce the linear functions embodied in the object. coef()
only
returns coefficients if object
is the result of a call to contrast()
,
and the parent object is the object that was handed to contrast
. This
is most useful for understanding interaction contrasts.
weights
returns the weights stored for each row of object
,
or a vector of 1s if no weights are saved.
The call pairs(object)
is equivalent to
contrast(object, method = "pairwise")
; and pairs(object,
reverse = TRUE)
is the same as contrast(object, method =
"revpairwise")
.
When interaction
is specified,
interaction contrasts are computed. Specifically contrasts are generated
for each factor separately, one at a time; and these contrasts are applied
to the object (the first time around) or to the previous result
(subsequently). (Any factors specified in by
are skipped.) The final
result comprises contrasts of contrasts, or, equivalently, products of
contrasts for the factors involved. Any named elements of interaction
are assigned to contrast methods; others are assigned in order of
appearance in object@levels
. The contrast factors in the resulting
emmGrid
object are ordered the same as in interaction
.
interaction
may be a character vector or list of valid contrast
methods (as documented for the method
argument). If the vector or
list is shorter than the number needed, it is recycled. Alternatively, if
the user specifies contrast = TRUE
, the contrast specified in
method
is used for all factors involved.
simple
is essentially the complement of by
: When
simple
is a character vector, by
is set to all the factors in
the grid except those in simple
. If simple
is a list,
each element is used in turn as simple
, and assembled in an
"emm_list"
. To generate all simple main effects, use
simple = "each"
(this works unless there actually is a factor named
"each"
). Note that a non-missing simple
will cause by
to be ignored.
Ordinarily, when simple
is a list or "each"
, the return value
is an emm_list
object with each entry in correspondence with
the entries of simple
. However, with combine = TRUE
, the
elements are all combined into one family of contrasts in a single
emmGrid
object using
rbind.emmGrid
.. In that case, the adjust
argument sets
the adjustment method for the combined set of contrasts.
When object
has a nesting structure (this can be seen via
str(object)
), then any grouping factors involved are forced into
service as by
variables, and the contrasts are thus computed
separately in each nest. This in turn may lead to an irregular grid in the
returned emmGrid
object, which may not be valid for subsequent
emmeans
calls.
warp.lm <- lm(breaks ~ wool*tension, data = warpbreaks)
(warp.emm <- emmeans(warp.lm, ~ tension | wool))
contrast(warp.emm, "poly") # inherits 'by = "wool"' from warp.emm
### Custom contrast coefs (we already have wool as 'by' thus 3 means to contrast)
contrast(warp.emm, list(mid.vs.ends = c(-1,2,-1)/2, lo.vs.hi = c(1,0,-1)))
pairs(warp.emm)
# Effects (dev from mean) of the 6 factor combs, with enhanced levels:
contrast(warp.emm, "eff", by = NULL,
enhance.levels = c("wool", "tension"))
pairs(warp.emm, simple = "wool") # same as pairs(warp.emm, by = "tension")
# Do all "simple" comparisons, combined into one family
pairs(warp.emm, simple = "each", combine = TRUE)
## Not run:
## Note that the following are NOT the same:
contrast(warp.emm, simple = c("wool", "tension"))
contrast(warp.emm, simple = list("wool", "tension"))
## The first generates contrasts for combinations of wool and tension
## (same as by = NULL)
## The second generates contrasts for wool by tension, and for
## tension by wool, respectively.
## End(Not run)
# An interaction contrast for tension:wool
tw.emm <- contrast(warp.emm, interaction = c(tension = "poly", wool = "consec"),
by = NULL)
tw.emm # see the estimates
coef(tw.emm) # see the contrast coefficients
# Use of scale and offset
# an unusual use of the famous stack-loss data...
mod <- lm(Water.Temp ~ poly(stack.loss, degree = 2), data = stackloss)
(emm <- emmeans(mod, "stack.loss", at = list(stack.loss = 10 * (1:4))))
# Convert results from Celsius to Fahrenheit:
confint(contrast(emm, "identity", scale = 9/5, offset = 32))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.