Easy 'emmeans' and 'emtrends'
Source:R/get_emcontrasts.R
, R/get_emmeans.R
, R/get_emtrends.R
get_emmeans.Rd
The get_emmeans()
function is a wrapper to facilitate the usage of
emmeans::emmeans()
and emmeans::emtrends()
, providing a somewhat simpler
and intuitive API to find the specifications and variables of interest. It is
meanly made to for the developers to facilitate the organization and
debugging, and end-users should rather use the estimate_*()
series of
functions.
Usage
get_emcontrasts(
model,
contrast = NULL,
by = NULL,
transform = "none",
method = "pairwise",
...
)
model_emcontrasts(
model,
contrast = NULL,
by = NULL,
transform = "none",
method = "pairwise",
...
)
get_emmeans(model, by = "auto", transform = "response", ...)
model_emmeans(model, by = "auto", transform = "response", ...)
get_emtrends(model, trend = NULL, by = NULL, ...)
model_emtrends(model, trend = NULL, by = NULL, ...)
Arguments
- model
A statistical model.
- contrast
A character vector indicating the name of the variable(s) for which to compute the contrasts.
- by
The predictor variable(s) at which to evaluate the desired effect / mean / contrasts. Other predictors of the model that are not included here will be collapsed and "averaged" over (the effect will be estimated across them).
- transform
Is passed to the
type
argument inemmeans::emmeans()
. See this vignette. Can be"none"
(default for contrasts),"response"
(default for means),"mu"
,"unlink"
,"log"
."none"
will leave the values on scale of the linear predictors."response"
will transform them on scale of the response variable. Thus for a logistic model,"none"
will give estimations expressed in log-odds (probabilities on logit scale) and"response"
in terms of probabilities.- method
Contrast method. See same argument in emmeans::contrast.
- ...
Other arguments passed for instance to
insight::get_datagrid()
.- trend
A character indicating the name of the variable for which to compute the slopes.
Examples
# Basic usage
model <- lm(Sepal.Width ~ Species, data = iris)
get_emcontrasts(model)
#> No variable was specified for contrast estimation. Selecting `contrast =
#> "Species"`.
#> contrast estimate SE df t.ratio p.value
#> setosa - versicolor 0.658 0.0679 147 9.685 <.0001
#> setosa - virginica 0.454 0.0679 147 6.683 <.0001
#> versicolor - virginica -0.204 0.0679 147 -3.003 0.0088
#>
#> P value adjustment: tukey method for comparing a family of 3 estimates
# Dealing with interactions
model <- lm(Sepal.Width ~ Species * Petal.Width, data = iris)
# By default: selects first factor
get_emcontrasts(model)
#> No variable was specified for contrast estimation. Selecting `contrast =
#> "Species"`.
#> NOTE: Results may be misleading due to involvement in interactions
#> contrast estimate SE df t.ratio p.value
#> setosa - versicolor 1.590 0.394 144 4.039 0.0003
#> setosa - virginica 1.774 0.413 144 4.293 0.0001
#> versicolor - virginica 0.184 0.145 144 1.272 0.4131
#>
#> P value adjustment: tukey method for comparing a family of 3 estimates
# Can also run contrasts between points of numeric
get_emcontrasts(model, contrast = "Petal.Width", length = 3)
#> NOTE: Results may be misleading due to involvement in interactions
#> contrast estimate SE df t.ratio p.value
#> Petal.Width0.1 - Petal.Width1.3 -1.01 0.195 144 -5.180 <.0001
#> Petal.Width0.1 - Petal.Width2.5 -2.02 0.390 144 -5.180 <.0001
#> Petal.Width1.3 - Petal.Width2.5 -1.01 0.195 144 -5.180 <.0001
#>
#> Results are averaged over the levels of: Species
#> P value adjustment: tukey method for comparing a family of 3 estimates
# Or both
get_emcontrasts(model, contrast = c("Species", "Petal.Width"), length = 2)
#> contrast estimate SE df
#> setosa Petal.Width0.1 - versicolor Petal.Width0.1 1.8275 0.279 144
#> setosa Petal.Width0.1 - virginica Petal.Width0.1 1.5479 0.312 144
#> setosa Petal.Width0.1 - setosa Petal.Width2.5 -2.0093 0.977 144
#> setosa Petal.Width0.1 - versicolor Petal.Width2.5 -0.7012 0.268 144
#> setosa Petal.Width0.1 - virginica Petal.Width2.5 0.0325 0.112 144
#> versicolor Petal.Width0.1 - virginica Petal.Width0.1 -0.2797 0.406 144
#> versicolor Petal.Width0.1 - setosa Petal.Width2.5 -3.8368 0.957 144
#> versicolor Petal.Width0.1 - versicolor Petal.Width2.5 -2.5288 0.521 144
#> versicolor Petal.Width0.1 - virginica Petal.Width2.5 -1.7951 0.282 144
#> virginica Petal.Width0.1 - setosa Petal.Width2.5 -3.5571 0.967 144
#> virginica Petal.Width0.1 - versicolor Petal.Width2.5 -2.2491 0.399 144
#> virginica Petal.Width0.1 - virginica Petal.Width2.5 -1.5154 0.375 144
#> setosa Petal.Width2.5 - versicolor Petal.Width2.5 1.3080 0.954 144
#> setosa Petal.Width2.5 - virginica Petal.Width2.5 2.0417 0.922 144
#> versicolor Petal.Width2.5 - virginica Petal.Width2.5 0.7337 0.272 144
#> t.ratio p.value
#> 6.550 <.0001
#> 4.955 <.0001
#> -2.057 0.3158
#> -2.614 0.1005
#> 0.289 0.9997
#> -0.689 0.9829
#> -4.009 0.0013
#> -4.858 <.0001
#> -6.355 <.0001
#> -3.678 0.0044
#> -5.642 <.0001
#> -4.043 0.0012
#> 1.371 0.7441
#> 2.214 0.2379
#> 2.699 0.0817
#>
#> P value adjustment: tukey method for comparing a family of 6 estimates
# Or with custom specifications
estimate_contrasts(model, contrast = c("Species", "Petal.Width=c(1, 2)"))
#> Marginal Contrasts Analysis
#>
#> Level1 | Level2 | Difference | 95% CI
#> -------------------------------------------------------------------------------
#> setosa Petal.Width1 | setosa Petal.Width2 | -0.84 | [-2.05, 0.38]
#> setosa Petal.Width1 | versicolor Petal.Width1 | 1.63 | [ 0.68, 2.59]
#> setosa Petal.Width1 | versicolor Petal.Width2 | 0.58 | [-0.45, 1.61]
#> setosa Petal.Width1 | virginica Petal.Width1 | 1.73 | [ 0.68, 2.78]
#> setosa Petal.Width1 | virginica Petal.Width2 | 1.10 | [ 0.17, 2.04]
#> setosa Petal.Width2 | versicolor Petal.Width2 | 1.42 | [-0.77, 3.60]
#> setosa Petal.Width2 | virginica Petal.Width2 | 1.94 | [-0.20, 4.08]
#> versicolor Petal.Width1 | setosa Petal.Width2 | -2.47 | [-4.62, -0.32]
#> versicolor Petal.Width1 | versicolor Petal.Width2 | -1.05 | [-1.70, -0.41]
#> versicolor Petal.Width1 | virginica Petal.Width1 | 0.10 | [-0.45, 0.65]
#> versicolor Petal.Width1 | virginica Petal.Width2 | -0.53 | [-0.81, -0.25]
#> versicolor Petal.Width2 | virginica Petal.Width2 | 0.52 | [ 0.05, 0.99]
#> virginica Petal.Width1 | setosa Petal.Width2 | -2.57 | [-4.76, -0.38]
#> virginica Petal.Width1 | versicolor Petal.Width2 | -1.15 | [-1.83, -0.48]
#> virginica Petal.Width1 | virginica Petal.Width2 | -0.63 | [-1.10, -0.17]
#>
#> Level1 | SE | t(144) | p
#> ------------------------------------------------
#> setosa Petal.Width1 | 0.41 | -2.06 | 0.166
#> setosa Petal.Width1 | 0.32 | 5.09 | < .001
#> setosa Petal.Width1 | 0.35 | 1.68 | 0.191
#> setosa Petal.Width1 | 0.35 | 4.93 | < .001
#> setosa Petal.Width1 | 0.31 | 3.52 | 0.005
#> setosa Petal.Width2 | 0.73 | 1.94 | 0.166
#> setosa Petal.Width2 | 0.72 | 2.71 | 0.038
#> versicolor Petal.Width1 | 0.72 | -3.43 | 0.005
#> versicolor Petal.Width1 | 0.22 | -4.86 | < .001
#> versicolor Petal.Width1 | 0.19 | 0.54 | 0.589
#> versicolor Petal.Width1 | 0.09 | -5.72 | < .001
#> versicolor Petal.Width2 | 0.16 | 3.31 | 0.007
#> virginica Petal.Width1 | 0.73 | -3.50 | 0.005
#> virginica Petal.Width1 | 0.23 | -5.13 | < .001
#> virginica Petal.Width1 | 0.16 | -4.04 | < .001
#>
#> Marginal contrasts estimated at Species, Petal.Width
#> p-value adjustment method: Holm (1979)
# Or modulate it
get_emcontrasts(model, by = "Petal.Width", length = 4)
#> No variable was specified for contrast estimation. Selecting `contrast =
#> "Species"`.
#> Petal.Width = 0.1:
#> contrast estimate SE df t.ratio p.value
#> setosa - versicolor 1.8275 0.279 144 6.550 <.0001
#> setosa - virginica 1.5479 0.312 144 4.955 <.0001
#> versicolor - virginica -0.2797 0.406 144 -0.689 0.7703
#>
#> Petal.Width = 0.9:
#> contrast estimate SE df t.ratio p.value
#> setosa - versicolor 1.6544 0.288 144 5.743 <.0001
#> setosa - virginica 1.7125 0.325 144 5.276 <.0001
#> versicolor - virginica 0.0581 0.208 144 0.280 0.9577
#>
#> Petal.Width = 1.7:
#> contrast estimate SE df t.ratio p.value
#> setosa - versicolor 1.4812 0.600 144 2.467 0.0390
#> setosa - virginica 1.8771 0.597 144 3.144 0.0057
#> versicolor - virginica 0.3959 0.113 144 3.502 0.0018
#>
#> Petal.Width = 2.5:
#> contrast estimate SE df t.ratio p.value
#> setosa - versicolor 1.3080 0.954 144 1.371 0.3587
#> setosa - virginica 2.0417 0.922 144 2.214 0.0722
#> versicolor - virginica 0.7337 0.272 144 2.699 0.0212
#>
#> P value adjustment: tukey method for comparing a family of 3 estimates
model <- lm(Sepal.Length ~ Species + Petal.Width, data = iris)
if (require("emmeans", quietly = TRUE)) {
# By default, 'by' is set to "Species"
get_emmeans(model)
# Overall mean (close to 'mean(iris$Sepal.Length)')
get_emmeans(model, by = NULL)
# One can estimate marginal means at several values of a 'modulate' variable
get_emmeans(model, by = "Petal.Width", length = 3)
# Interactions
model <- lm(Sepal.Width ~ Species * Petal.Length, data = iris)
get_emmeans(model)
get_emmeans(model, by = c("Species", "Petal.Length"), length = 2)
get_emmeans(model, by = c("Species", "Petal.Length = c(1, 3, 5)"), length = 2)
}
#> Welcome to emmeans.
#> Caution: You lose important information if you filter this package's results.
#> See '? untidy'
#> We selected `by = c("Species")`.
#> We selected `by = c("Species")`.
#> NOTE: Results may be misleading due to involvement in interactions
#> Species Petal.Length emmean SE df lower.CL upper.CL
#> setosa 1 3.25 0.1280 144 2.995 3.50
#> versicolor 1 1.55 0.3170 144 0.924 2.18
#> virginica 1 1.91 0.3750 144 1.165 2.65
#> setosa 3 4.02 0.4030 144 3.229 4.82
#> versicolor 3 2.30 0.1290 144 2.043 2.55
#> virginica 3 2.38 0.2140 144 1.954 2.80
#> setosa 5 4.80 0.9220 144 2.979 6.62
#> versicolor 5 3.05 0.0840 144 2.881 3.21
#> virginica 5 2.84 0.0636 144 2.719 2.97
#>
#> Confidence level used: 0.95
if (require("emmeans")) {
model <- lm(Sepal.Width ~ Species * Petal.Length, data = iris)
get_emtrends(model)
get_emtrends(model, by = "Species")
get_emtrends(model, by = "Petal.Length")
get_emtrends(model, by = c("Species", "Petal.Length"))
model <- lm(Petal.Length ~ poly(Sepal.Width, 4), data = iris)
get_emtrends(model)
get_emtrends(model, by = "Sepal.Width")
}
#> No numeric variable was specified for slope estimation. Selecting `trend
#> = "Petal.Length"`.
#> No numeric variable was specified for slope estimation. Selecting `trend
#> = "Petal.Length"`.
#> No numeric variable was specified for slope estimation. Selecting `trend
#> = "Petal.Length"`.
#> NOTE: Results may be misleading due to involvement in interactions
#> No numeric variable was specified for slope estimation. Selecting `trend
#> = "Petal.Length"`.
#> No numeric variable was specified for slope estimation. Selecting `trend
#> = "Sepal.Width"`.
#> No numeric variable was specified for slope estimation. Selecting `trend
#> = "Sepal.Width"`.
#> Sepal.Width Sepal.Width.trend SE df lower.CL upper.CL
#> 2.00 7.484 5.420 145 -3.225 18.192
#> 2.27 3.779 2.090 145 -0.358 7.916
#> 2.53 0.831 0.765 145 -0.681 2.342
#> 2.80 -1.337 0.706 145 -2.732 0.058
#> 3.07 -2.699 0.543 145 -3.772 -1.626
#> 3.33 -3.231 0.607 145 -4.430 -2.032
#> 3.60 -2.909 0.838 145 -4.564 -1.254
#> 3.87 -1.708 1.010 145 -3.702 0.287
#> 4.13 0.398 2.390 145 -4.330 5.125
#> 4.40 3.431 5.800 145 -8.028 14.890
#>
#> Confidence level used: 0.95