Estimate average value of response variable at each factor levels. For plotting, check the examples in visualisation_recipe(). See also other related functions such as estimate_contrasts() and estimate_slopes().

## Usage

estimate_means(
model,
at = "auto",
fixed = NULL,
transform = "response",
ci = 0.95,
backend = "emmeans",
...
)

## Arguments

model

A statistical model.

at

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).

fixed

A character vector indicating the names of the predictors to be "fixed" (i.e., maintained), so that the estimation is made at these values.

transform

Is passed to the type argument in emmeans::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.

ci

Confidence Interval (CI) level. Default to 0.95 (95%).

backend

Whether to use 'emmeans' or 'marginaleffects' as a backend. The latter is experimental and some features might not work.

...

Other arguments passed for instance to insight::get_datagrid().

## Value

A data frame of estimated marginal means.

## Details

See the Details section below, and don't forget to also check out the Vignettes and README examples for various examples, tutorials and use cases.

The estimate_slopes(), estimate_means() and estimate_contrasts() functions are forming a group, as they are all based on marginal estimations (estimations based on a model). All three are also built on the emmeans package, so reading its documentation (for instance for emmeans::emmeans() and emmeans::emtrends()) is recommended to understand the idea behind these types of procedures.

• Model-based predictions is the basis for all that follows. Indeed, the first thing to understand is how models can be used to make predictions (see estimate_link()). This corresponds to the predicted response (or "outcome variable") given specific predictor values of the predictors (i.e., given a specific data configuration). This is why the concept of reference grid() is so important for direct predictions.

• Marginal "means", obtained via estimate_means(), are an extension of such predictions, allowing to "average" (collapse) some of the predictors, to obtain the average response value at a specific predictors configuration. This is typically used when some of the predictors of interest are factors. Indeed, the parameters of the model will usually give you the intercept value and then the "effect" of each factor level (how different it is from the intercept). Marginal means can be used to directly give you the mean value of the response variable at all the levels of a factor. Moreover, it can also be used to control, or average over predictors, which is useful in the case of multiple predictors with or without interactions.

• Marginal contrasts, obtained via estimate_contrasts(), are themselves at extension of marginal means, in that they allow to investigate the difference (i.e., the contrast) between the marginal means. This is, again, often used to get all pairwise differences between all levels of a factor. It works also for continuous predictors, for instance one could also be interested in whether the difference at two extremes of a continuous predictor is significant.

• Finally, marginal effects, obtained via estimate_slopes(), are different in that their focus is not values on the response variable, but the model's parameters. The idea is to assess the effect of a predictor at a specific configuration of the other predictors. This is relevant in the case of interactions or non-linear relationships, when the effect of a predictor variable changes depending on the other predictors. Moreover, these effects can also be "averaged" over other predictors, to get for instance the "general trend" of a predictor over different factor levels.

Example: Let's imagine the following model lm(y ~ condition * x) where condition is a factor with 3 levels A, B and C and x a continuous variable (like age for example). One idea is to see how this model performs, and compare the actual response y to the one predicted by the model (using estimate_response()). Another idea is evaluate the average mean at each of the condition's levels (using estimate_means()), which can be useful to visualize them. Another possibility is to evaluate the difference between these levels (using estimate_contrasts()). Finally, one could also estimate the effect of x averaged over all conditions, or instead within each condition (using [estimate_slopes]).

## Examples

library(modelbased)

# Frequentist models
# -------------------
model <- lm(Petal.Length ~ Sepal.Width * Species, data = iris)

estimate_means(model)
#> We selected at = c("Species").
#> NOTE: Results may be misleading due to involvement in interactions
#> Estimated Marginal Means
#>
#> Species    | Mean |   SE |       95% CI
#> ---------------------------------------
#> setosa     | 1.43 | 0.08 | [1.28, 1.58]
#> versicolor | 4.50 | 0.07 | [4.35, 4.65]
#> virginica  | 5.61 | 0.06 | [5.50, 5.72]
#>
#> Marginal means estimated at Species
estimate_means(model, fixed = "Sepal.Width")
#> We selected at = c("Species").
#> Estimated Marginal Means
#>
#> Species    | Sepal.Width | Mean |   SE |       95% CI
#> -----------------------------------------------------
#> setosa     |        3.06 | 1.43 | 0.08 | [1.28, 1.58]
#> versicolor |        3.06 | 4.50 | 0.07 | [4.35, 4.65]
#> virginica  |        3.06 | 5.61 | 0.06 | [5.50, 5.72]
#>
#> Marginal means estimated at Species
estimate_means(model, at = c("Species", "Sepal.Width"), length = 2)
#> Estimated Marginal Means
#>
#> Species    | Sepal.Width | Mean |   SE |       95% CI
#> -----------------------------------------------------
#> setosa     |        2.00 | 1.35 | 0.21 | [0.92, 1.77]
#> versicolor |        2.00 | 3.61 | 0.15 | [3.33, 3.90]
#> virginica  |        2.00 | 4.88 | 0.17 | [4.54, 5.23]
#> setosa     |        4.40 | 1.54 | 0.15 | [1.24, 1.84]
#> versicolor |        4.40 | 5.63 | 0.29 | [5.05, 6.20]
#> virginica  |        4.40 | 6.53 | 0.25 | [6.04, 7.02]
#>
#> Marginal means estimated at Species, Sepal.Width
estimate_means(model, at = "Species=c('versicolor', 'setosa')")
#> NOTE: Results may be misleading due to involvement in interactions
#> Estimated Marginal Means
#>
#> Species    | Mean |   SE |       95% CI
#> ---------------------------------------
#> versicolor | 4.50 | 0.07 | [4.35, 4.65]
#> setosa     | 1.43 | 0.08 | [1.28, 1.58]
#>
#> Marginal means estimated at Species
estimate_means(model, at = "Sepal.Width=c(2, 4)")
#> NOTE: Results may be misleading due to involvement in interactions
#> Estimated Marginal Means
#>
#> Sepal.Width | Mean |   SE |       95% CI
#> ----------------------------------------
#> 2.00        | 3.28 | 0.10 | [3.07, 3.49]
#> 4.00        | 4.35 | 0.10 | [4.15, 4.55]
#>
#> Marginal means estimated at Sepal.Width
estimate_means(model, at = c("Species", "Sepal.Width=0"))
#> Estimated Marginal Means
#>
#> Species    | Sepal.Width | Mean |   SE |       95% CI
#> -----------------------------------------------------
#> setosa     |        0.00 | 1.18 | 0.50 | [0.19, 2.17]
#> versicolor |        0.00 | 1.93 | 0.49 | [0.97, 2.90]
#> virginica  |        0.00 | 3.51 | 0.51 | [2.50, 4.52]
#>
#> Marginal means estimated at Species, Sepal.Width
estimate_means(model, at = "Sepal.Width", length = 5)
#> NOTE: Results may be misleading due to involvement in interactions
#> Estimated Marginal Means
#>
#> Sepal.Width | Mean |   SE |       95% CI
#> ----------------------------------------
#> 2.00        | 3.28 | 0.10 | [3.07, 3.49]
#> 2.60        | 3.60 | 0.06 | [3.49, 3.71]
#> 3.20        | 3.92 | 0.04 | [3.84, 4.01]
#> 3.80        | 4.25 | 0.08 | [4.08, 4.41]
#> 4.40        | 4.57 | 0.14 | [4.30, 4.84]
#>
#> Marginal means estimated at Sepal.Width
estimate_means(model, at = "Sepal.Width=c(2, 4)")
#> NOTE: Results may be misleading due to involvement in interactions
#> Estimated Marginal Means
#>
#> Sepal.Width | Mean |   SE |       95% CI
#> ----------------------------------------
#> 2.00        | 3.28 | 0.10 | [3.07, 3.49]
#> 4.00        | 4.35 | 0.10 | [4.15, 4.55]
#>
#> Marginal means estimated at Sepal.Width

# Methods that can be applied to it:
means <- estimate_means(model, fixed = "Sepal.Width")
#> We selected at = c("Species").
plot(means) # which runs visualisation_recipe()

standardize(means)
#> Estimated Marginal Means (standardized)
#>
#> Species    | Sepal.Width |  Mean |   SE |         95% CI
#> --------------------------------------------------------
#> setosa     |        0.00 | -1.32 | 0.04 | [-1.40, -1.23]
#> versicolor |        0.00 |  0.42 | 0.04 | [ 0.34,  0.50]
#> virginica  |        0.00 |  1.05 | 0.03 | [ 0.99,  1.11]
#>
#> Marginal means estimated at Species
# \donttest{
data <- iris
data$Petal.Length_factor <- ifelse(data$Petal.Length < 4.2, "A", "B")

model <- lmer(Petal.Length ~ Sepal.Width + Species + (1 | Petal.Length_factor), data = data)
estimate_means(model)
#> We selected at = c("Species").
#> Cannot use mode = "kenward-roger" because *pbkrtest* package is not installed
#> Cannot use mode = "satterthwaite" because *lmerTest* package is not installed
#> Estimated Marginal Means
#>
#> Species    | Mean |   SE |       95% CI
#> ---------------------------------------
#> setosa     | 1.67 | 0.34 | [1.00, 2.34]
#> versicolor | 4.27 | 0.34 | [3.61, 4.93]
#> virginica  | 5.25 | 0.34 | [4.59, 5.92]
#>
#> Marginal means estimated at Species
estimate_means(model, at = "Sepal.Width", length = 3)
#> Cannot use mode = "kenward-roger" because *pbkrtest* package is not installed
#> Cannot use mode = "satterthwaite" because *lmerTest* package is not installed
#> Estimated Marginal Means
#>
#> Sepal.Width | Mean |   SE |       95% CI
#> ----------------------------------------
#> 2.00        | 3.40 | 0.35 | [2.72, 4.08]
#> 3.20        | 3.78 | 0.33 | [3.12, 4.43]
#> 4.40        | 4.15 | 0.35 | [3.46, 4.85]
#>
#> Marginal means estimated at Sepal.Width
# }