Learn R Programming

marginaleffects (version 0.3.3)

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

Arguments

model

Model object

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.

newdata

A dataset over which to compute adjusted predictions. NULL uses the original data used to fit the model.

conf.level

The confidence level to use for the confidence interval. No interval is computed if conf.int=NULL. Must be strictly greater than 0 and less than 1. Defaults to 0.95, which corresponds to a 95 percent confidence interval.

type

Type(s) of prediction as string or vector This can differ based on the model type, but will typically be a string such as: "response", "link", "probs", or "zero".

...

Additional arguments are pushed forward to predict().

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)

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