Learn R Programming

marginaleffects (version 0.3.4)

predictions: Adjusted Predictions

Description

Calculate adjusted predictions for each row of the dataset. The datagrid() function and the newdata argument can be used to calculate Average Adjusted Predictions (AAP), Average Predictions at the Mean (APM), or Predictions at User-Specified Values of the regressors (aka Adjusted Predictions at Representative values, APR). See the Details and Examples sections below.

Usage

predictions(
  model,
  variables = NULL,
  newdata = NULL,
  conf.level = 0.95,
  type = "response",
  ...
)

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 or highest density interval (for bayesian models)

  • conf.high: upper bound of the confidence or highest density interval (for bayesian models)

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
random.only lme4::predict.merMod

Details

An "adjusted prediction" is the outcome predicted by a model for some combination of the regressors' values, such as their observed values, their means, or factor levels (a.k.a. <U+201C>reference grid<U+201D>). When possible, this function uses the delta method to compute the standard error associated with the adjusted predictions.

A detailed vignette on adjusted predictions is published on the package website:

https://vincentarelbundock.github.io/marginaleffects/ Compute model-adjusted predictions (fitted values) for a "grid" of regressor values.

Examples

Run this code
# NOT RUN {
# Adjusted Prediction for every row of the original dataset
mod <- lm(mpg ~ hp + factor(cyl), data = mtcars)
pred <- predictions(mod)
head(pred)

# Adjusted Predictions at User-Specified Values of the Regressors
predictions(mod, newdata = datagrid(hp = c(100, 120), cyl = 4))

# Average Adjusted Predictions (AAP)
library(dplyr)
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))

# Conditional Adjusted Predictions
plot_cap(mod, condition = "hp")
# }

Run the code above in your browser using DataLab