Learn R Programming

midr (version 0.5.2)

get.yhat: Wrapper Prediction Function

Description

get.yhat() is a generic function that provides a unified interface for obtaining predictions from various fitted model objects.

Usage

get.yhat(object, newdata, ...)

# S3 method for default get.yhat(object, newdata, target = -1L, ...)

# S3 method for mid get.yhat(object, newdata, ...)

# S3 method for lm get.yhat(object, newdata, ...)

# S3 method for glm get.yhat(object, newdata, ...)

# S3 method for rpart get.yhat(object, newdata, target = -1L, ...)

# S3 method for randomForest get.yhat(object, newdata, target = -1L, ...)

# S3 method for ranger get.yhat(object, newdata, target = -1L, ...)

# S3 method for svm get.yhat(object, newdata, target = -1L, ...)

# S3 method for ksvm get.yhat(object, newdata, target = -1L, ...)

# S3 method for AccurateGLM get.yhat(object, newdata, ...)

# S3 method for glmnet get.yhat(object, newdata, ...)

# S3 method for model_fit get.yhat(object, newdata, target = -1L, ...)

# S3 method for rpf get.yhat(object, newdata, target = -1L, ...)

Value

get.yhat() returns a numeric vector of model predictions for newdata.

Arguments

object

a fitted model object.

newdata

a data.frame or matrix.

...

optional arguments passed on to the underlying predict() method for the object's class.

target

an integer or character vector specifying the target levels used for the classification models that return a matrix or data frame of class probabilities. The default, -1, represents the probability of not being the base level.

Details

While many predictive models have a stats::predict() method, the structure and type of their outputs are not uniform. For example, some return a numeric vector, others a matrix of class probabilities, and some a list. This function, get.yhat(), abstracts away this complexity.

For regression models, it returns the numeric prediction in the original scale of the response variable. For classification models, it returns the sum of class probabilities for the classes specified by the target argument.

Furthermore, get.yhat() provides more consistent handling of missing values. While some stats::predict() methods may return a shorter vector by omitting NAs, get.yhat() is designed to return a vector of the same length as newdata, preserving NAs in their original positions.

The design of get.yhat() is strongly influenced by DALEX::yhat().

See Also

predict.mid

Examples

Run this code
data(trees, package = "datasets")
model <- glm(Volume ~ ., trees, family = Gamma(log))

# The output of stats::predict() might not be in the scale of the response variable
predict(model, trees[1:5, ])

# get.yhat() returns a numeric vector in the original scale of the response variable
get.yhat(model, trees[1:5, ])
predict(model, trees[1:5, ], type = "response")

Run the code above in your browser using DataLab