predict_model: Generate predictions for different model classes

View source: R/models.R

predict_modelR Documentation

Generate predictions for different model classes

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

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:

  • stats::lm

  • stats::glm

  • ranger::ranger

  • mgcv::gam

  • xgboost::xgb.train

The returned object p always satisfies the following properties:

  • is.atomic(p) equals TRUE

  • is.double(p) equals TRUE

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

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 May 4, 2023, 5:10 p.m.