Outcome predicted by a fitted model on a specified scale for a given combination of values of the predictor variables, such as their observed values, their means, or factor levels (a.k.a. "reference grid"). The tidy() and summary() functions can be used to aggregate the output of predictions(). To learn more, read the predictions vignette, visit the package website, or scroll down this page for a full list of vignettes:

predictions(
model,
newdata = NULL,
variables = NULL,
vcov = TRUE,
conf_level = 0.95,
type = "response",
wts = NULL,
transform_post = NULL,
hypothesis = NULL,
...
)

## Arguments

model

Model object

newdata

A data frame over which to compute quantities of interest.

• NULL: adjusted predictions for each observed value in the original dataset.

• The datagrid() function can be used to specify a custom grid of regressors. For example:

• newdata = datagrid(): contrast at the mean

• newdata = datagrid(cyl = c(4, 6)): cyl variable equal to 4 and 6 and other regressors fixed at their means or modes.

• See the Examples section and the datagrid() documentation for more.

variables

Character vector. Compute Adjusted Predictions for combinations of each of these variables. Factor levels are considered at each of their levels. Numeric variables variables are considered at Tukey's Five-Number Summaries. NULL uses the original data used to fit the model.

vcov

Type of uncertainty estimates to report (e.g., for robust standard errors). Acceptable values:

• FALSE: Do not compute standard errors. This can speed up computation considerably.

• TRUE: Unit-level standard errors using the default vcov(model) variance-covariance matrix.

• String which indicates the kind of uncertainty estimates to return.

• Heteroskedasticity-consistent: "HC", "HC0", "HC1", "HC2", "HC3", "HC4", "HC4m", "HC5". See ?sandwich::vcovHC

• Heteroskedasticity and autocorrelation consistent: "HAC"

• Other: "NeweyWest", "KernHAC", "OPG". See the sandwich package documentation.

• One-sided formula which indicates the name of cluster variables (e.g., ~unit_id). This formula is passed to the cluster argument of the sandwich::vcovCL function.

• Square covariance matrix

• Function which returns a covariance matrix (e.g., stats::vcov(model))

conf_level

numeric value between 0 and 1. Confidence level to use to build a confidence interval.

type

string indicates the type (scale) of the predictions used to compute marginal effects or contrasts. This can differ based on the model type, but will typically be a string such as: "response", "link", "probs", or "zero". When an unsupported string is entered, the model-specific list of acceptable values is returned in an error message.

wts

string or numeric: weights to use when computing average contrasts or marginaleffects. These weights only affect the averaging in tidy() or summary(), and not the unit-level estimates themselves.

• string: column name of the weights variable in newdata. When supplying a column name to wts, it is recommended to supply the original data (including the weights variable) explicitly to newdata.

• numeric: vector of length equal to the number of rows in the original data or in newdata (if supplied).

transform_post

(experimental) A function applied to unit-level adjusted predictions and confidence intervals just before the function returns results. For bayesian models, this function is applied to individual draws from the posterior distribution, before computing summaries.

hypothesis

specify a hypothesis test or custom contrast using a vector, matrix, string, or string formula.

• String:

• "pairwise": pairwise differences between estimates in each row.

• "reference": differences between the estimates in each row and the estimate in the first row.

• String formula to specify linear or non-linear hypothesis tests. If the term column uniquely identifies rows, terms can be used in the formula. Otherwise, use b1, b2, etc. to identify the position of each parameter. Examples:

• hp = drat

• hp + drat = 12

• b1 + b2 + b3 = 0

• Numeric vector: Weights to compute a linear combination of (custom contrast between) estimates. Length equal to the number of rows generated by the same function call, but without the hypothesis argument.

• Numeric matrix: Each column is a vector of weights, as describe above, used to compute a distinct linear combination of (contrast between) estimates.

• See the Examples section below and the vignette: https://vincentarelbundock.github.io/marginaleffects/articles/hypothesis.html

...

Additional arguments are passed to the predict() method supplied by the modeling package.These arguments are particularly useful for mixed-effects or bayesian models (see the online vignettes on the marginaleffects website). Available arguments can vary from model to model, depending on the range of supported arguments by each modeling package. See the "Model-Specific Arguments" section of the ?marginaleffects documentation for a non-exhaustive list of available arguments.

## Value

A data.frame with one row per observation and several columns:

• rowid: row number of the newdata data frame

• type: prediction type, as defined by the type argument

• group: (optional) value of the grouped outcome (e.g., categorical outcome models)

• predicted: predicted outcome

• std.error: standard errors computed by the insight::get_predicted function or, if unavailable, via marginaleffects delta method functionality.

• conf.low: lower bound of the confidence interval (or equal-tailed interval for bayesian models)

• conf.high: upper bound of the confidence interval (or equal-tailed interval for bayesian models)

## Details

The newdata argument, the tidy() function, and datagrid() function can be used to control the kind of predictions to report:

• Average Predictions

• Predictions at the Mean

• Predictions at User-Specified values (aka Predictions at Representative values).

When possible, predictions() delegates the computation of confidence intervals to the insight::get_predicted() function, which uses back transformation to produce adequate confidence intervals on the scale specified by the type argument. When this is not possible, predictions() uses the Delta Method to compute standard errors around adjusted predictions.

## Model-Specific Arguments

Some model types allow model-specific arguments to modify the nature of marginal effects, predictions, marginal means, and contrasts.

 Package Class Argument Documentation brms brmsfit ndraws brms::posterior_predict re_formula lme4 merMod include_random insight::get_predicted re.form lme4::predict.merMod allow.new.levels lme4::predict.merMod glmmTMB glmmTMB re.form glmmTMB::predict.glmmTMB allow.new.levels glmmTMB::predict.glmmTMB zitype glmmTMB::predict.glmmTMB mgcv bam exclude mgcv::predict.bam robustlmm rlmerMod re.form robustlmm::predict.rlmerMod allow.new.levels robustlmm::predict.rlmerMod

## Examples

# Adjusted Prediction for every row of the original dataset
mod <- lm(mpg ~ hp + factor(cyl), data = mtcars)
pred <- predictions(mod)
#>   rowid     type predicted std.error statistic       p.value conf.low conf.high
#> 1     1 response  20.03819 1.2041405  16.64107  3.512623e-62 17.67812  22.39826
#> 2     2 response  20.03819 1.2041405  16.64107  3.512623e-62 17.67812  22.39826
#> 3     3 response  26.41451 0.9619738  27.45866 5.476301e-166 24.52907  28.29994
#> 4     4 response  20.03819 1.2041405  16.64107  3.512623e-62 17.67812  22.39826
#> 5     5 response  15.92247 0.9924560  16.04350  6.347069e-58 13.97729  17.86765
#> 6     6 response  20.15839 1.2186288  16.54186  1.832792e-61 17.76992  22.54685
#>    mpg  hp cyl
#> 1 21.0 110   6
#> 2 21.0 110   6
#> 3 22.8  93   4
#> 4 21.4 110   6
#> 5 18.7 175   8
#> 6 18.1 105   6

# Adjusted Predictions at User-Specified Values of the Regressors
predictions(mod, newdata = datagrid(hp = c(100, 120), cyl = 4))
#>   rowid     type predicted std.error statistic       p.value conf.low conf.high
#> 1     1 response  26.24623 0.9856325  26.62883 3.148430e-156 24.31443  28.17804
#> 2     2 response  25.76546 1.1096486  23.21947 2.895018e-119 23.59059  27.94033
#>    hp cyl mpg
#> 1 100   4   0
#> 2 120   4   0

library(dplyr)
#>
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:marginaleffects’:
#>
#>     between, first, last
#> The following objects are masked from ‘package:stats’:
#>
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#>
#>     intersect, setdiff, setequal, union
mod <- lm(mpg ~ hp * am * vs, mtcars)

pred <- predictions(mod, newdata = datagrid(am = 0, grid_type = "counterfactual")) %>%
summarize(across(c(predicted, std.error), mean))

predictions(mod, newdata = datagrid(am = 0:1, grid_type = "counterfactual")) %>%
group_by(am) %>%
summarize(across(c(predicted, std.error), mean))
#> # A tibble: 2 × 3
#>      am predicted std.error
#>   <int>     <dbl>     <dbl>
#> 1     0      18.1      1.54
#> 2     1      22.8      1.41

plot_cap(mod, condition = "hp")

# hypothesis test: is the prediction in the 1st row equal to the prediction in the 2nd row
mod <- lm(mpg ~ wt + drat, data = mtcars)

predictions(
mod,
newdata = datagrid(wt = 2:3),
hypothesis = "b1 = b2")
#>       type  term predicted std.error statistic      p.value conf.low conf.high
#> 1 response b1=b2   4.78289 0.7970353  6.000851 1.962855e-09  3.22073  6.345051

# same hypothesis test using row indices
predictions(
mod,
newdata = datagrid(wt = 2:3),
hypothesis = "b1 - b2 = 0")
#>       type    term predicted std.error statistic      p.value conf.low
#> 1 response b1-b2=0   4.78289 0.7970353  6.000851 1.962855e-09  3.22073
#>   conf.high
#> 1  6.345051

# same hypothesis test using numeric vector of weights
predictions(
mod,
newdata = datagrid(wt = 2:3),
hypothesis = c(1, -1))
#>       type   term predicted std.error statistic      p.value conf.low conf.high
#> 1 response custom   4.78289 0.7970353  6.000851 1.962855e-09  3.22073  6.345051

# two custom contrasts using a matrix of weights
lc <- matrix(c(
1, -1,
2, 3),
ncol = 2)
predictions(
mod,
newdata = datagrid(wt = 2:3),
hypothesis = lc)
#>   rowid     type   term predicted std.error statistic       p.value  conf.low
#> 1     1 response custom   4.78289 0.7970353  6.000851  1.962855e-09   3.22073
#> 2     2 response custom 115.21432 3.6474827 31.587352 5.507900e-219 108.06539
#>    conf.high     drat wt mpg
#> 1   6.345051 3.596563  2   0
#> 2 122.363255 3.596563  3   0