R: Get marginal effects from model terms

ggeffect {ggeffects} | R Documentation

## Get marginal effects from model terms

### Description

`ggeffect()` computes marginal effects of model terms. It internally calls `Effect` and puts the result into tidy data frames.

### Usage

    ggeffect(model, terms, ci.lvl = 0.95, x.as.factor = FALSE, ...)

### Arguments

`model` | A fitted model object, or a list of model objects. Any model that is supported by the effects-package should work.
`terms` | Character vector with the names of those terms from `model`, for which marginal effects should be displayed. At least one term is required to calculate effects for certain terms, maximum length is three terms, where the second and third term indicate the groups, i.e. predictions of first term are grouped by the levels of the second (and third) term. If `terms` is missing or `NULL`, marginal effects for each model term are calculated. It is also possible to define specific values for terms, at which marginal effects should be calculated (see 'Details'). All remaining covariates that are not specified in `terms` are held constant (if `full.data = FALSE`, the default) or are set to the values from the observations (i.e. are kept as they happen to be; see 'Details'). See also argument `condition` and `typical`.
`ci.lvl` | Numeric, the level of the confidence intervals. For `ggpredict()`, use `ci.lvl = NA`, if confidence intervals should not be calculated (for instance, due to computation time).
`x.as.factor` | Logical, if `TRUE`, preserves factor-class as `x`-column in the returned data frame. By default, the `x`-column is always numeric.
`...` | Further arguments passed down to `Effect`.

### Details

The results of `ggeffect()` and `ggpredict()` are usually (almost) identical. It's just that `ggpredict()` calls `predict()`, while `ggeffect()` calls `Effect` to compute marginal effects at the mean. However, results may differ when using factors inside the formula: in such cases, `Effect()` takes the "mean" value of factors (i.e. computes a kind of "average" value, which represents the proportions of each factor's category), while `ggpredict()` uses the base (reference) level when holding these predictors at a constant value.

### Value

A tibble (with `ggeffects` class attribute) with consistent data columns:

`x`

  the values of the model predictor to which the effect pertains, used as x-position in plots.

`predicted`

  the predicted values, used as y-position in plots.

`conf.low`

  the lower bound of the confidence interval for the predicted values.

`conf.high`

  the upper bound of the confidence interval for the predicted values.

`group`

  the grouping level from the second term in `terms`, used as grouping-aesthetics in plots.

`facet`

  the grouping level from the third term in `terms`, used to indicate facets in plots.

### Examples

    data(efc)
    fit <- lm(barthtot ~ c12hour + neg_c_7 + c161sex + c172code, data = efc)
    ggeffect(fit, terms = "c12hour")

    mydf <- ggeffect(fit, terms = c("c12hour", "c161sex"))
    plot(mydf)

[Package ggeffects version 0.4.0 Index]