predict_model: Generate predictions for different model classes

Description Usage Arguments Details Value Author(s) Examples

View source: R/models.R

Description

Performs prediction of response lm, glm, ranger, mgcv::gam and xgboost::xgb.train with binary or continuous response. See details for more information.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
predict_model(x, newdata)

## Default S3 method:
predict_model(x, newdata)

## S3 method for class 'lm'
predict_model(x, newdata)

## S3 method for class 'glm'
predict_model(x, newdata)

## S3 method for class 'ranger'
predict_model(x, newdata)

## S3 method for class 'xgb.Booster'
predict_model(x, newdata)

## S3 method for class 'gam'
predict_model(x, newdata)

Arguments

x

Model object for the model to be explained.

newdata

A data frame (or matrix) in which to look for variables with which to predict.

Details

The following models are currently supported:

The returned object p always satisfies the following properties:

If you have a binary classification model we'll always return the probability prediction for a single class.

For more details on how to explain other types of models (i.e. custom models), see the Advanced usage section of the vignette:
From R: vignette("understanding_shapr", package = "shapr")
Web: https://norskregnesentral.github.io/shapr/articles/understanding_shapr.html#explain-custom-models

Value

Numeric

Author(s)

Martin Jullum

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
if (requireNamespace("MASS", quietly = TRUE)) {
  # Load example data
  data("Boston", package = "MASS")
  # Split data into test- and training data
  x_train <- head(Boston, -3)
  x_test <- tail(Boston, 3)
  # Fit a linear model
  model <- lm(medv ~ lstat + rm + dis + indus, data = x_train)

  # Predicting for a model with a standardized format
  predict_model(x = model, newdata = x_test)
}

shapr documentation built on Jan. 28, 2021, 5:06 p.m.