Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

predict.train

Extract predictions and class probabilities from train objects


Description

These functions can be used for a single train object or to loop through a number of train objects to calculate the training and test data predictions and class probabilities.

Usage

extractPrediction(
  models,
  testX = NULL,
  testY = NULL,
  unkX = NULL,
  unkOnly = !is.null(unkX) & is.null(testX),
  verbose = FALSE
)

extractProb(
  models,
  testX = NULL,
  testY = NULL,
  unkX = NULL,
  unkOnly = !is.null(unkX) & is.null(testX),
  verbose = FALSE
)

## S3 method for class 'train'
predict(object, newdata = NULL, type = "raw", na.action = na.omit, ...)

Arguments

models

a list of objects of the class train. The objects must have been generated with fitBest = FALSE and returnData = TRUE.

testX

an optional set of data to predict

testY

an optional outcome corresponding to the data given in testX

unkX

another optional set of data to predict without known outcomes

unkOnly

a logical to bypass training and test set predictions. This is useful if speed is needed for unknown samples.

verbose

a logical for printing messages

object

For predict.train, an object of class train. For predict.list, a list of objects of class train.

newdata

an optional set of data to predict on. If NULL, then the original training data are used but, if the train model used a recipe, an error will occur.

type

either "raw" or "prob", for the number/class predictions or class probabilities, respectively. Class probabilities are not available for all classification models

na.action

the method for handling missing data

...

only used for sort and modelCor and captures arguments to pass to sort or FUN.

Details

These functions are wrappers for the specific prediction functions in each modeling package. In each case, the optimal tuning values given in the tuneValue slot of the finalModel object are used to predict.

To get simple predictions for a new data set, the predict function can be used. Limits can be imposed on the range of predictions. See trainControl for more information.

To get predictions for a series of models at once, a list of train objects can be passes to the predict function and a list of model predictions will be returned.

The two extraction functions can be used to get the predictions and observed outcomes at once for the training, test and/or unknown samples at once in a single data frame (instead of a list of just the predictions). These objects can then be passes to plotObsVsPred or plotClassProbs.

Value

For predict.train, a vector of predictions if type = "raw" or a data frame of class probabilities for type = "prob". In the latter case, there are columns for each class.

For predict.list, a list results. Each element is produced by predict.train.

For extractPrediction, a data frame with columns:

obs

the observed training and test data

pred

predicted values

model

the type of model used to predict

object

the names of the objects within models. If models is an un-named list, the values of object will be "Object1", "Object2" and so on

dataType

"Training", "Test" or "Unknown" depending on what was specified

For extractProb, a data frame. There is a column for each class containing the probabilities. The remaining columns are the same as above (although the pred column is the predicted class)

Author(s)

Max Kuhn

References

Kuhn (2008), “Building Predictive Models in R Using the caret” (http://www.jstatsoft.org/article/view/v028i05/v28i05.pdf)

See Also

Examples

## Not run: 

knnFit <- train(Species ~ ., data = iris, method = "knn",
                trControl = trainControl(method = "cv"))

rdaFit <- train(Species ~ ., data = iris, method = "rda",
                trControl = trainControl(method = "cv"))

predict(knnFit)
predict(knnFit, type = "prob")

bothModels <- list(knn = knnFit,
                   tree = rdaFit)

predict(bothModels)

extractPrediction(bothModels, testX = iris[1:10, -5])
extractProb(bothModels, testX = iris[1:10, -5])
  
## End(Not run)

caret

Classification and Regression Training

v6.0-86
GPL (>= 2)
Authors
Max Kuhn [aut, cre], Jed Wing [ctb], Steve Weston [ctb], Andre Williams [ctb], Chris Keefer [ctb], Allan Engelhardt [ctb], Tony Cooper [ctb], Zachary Mayer [ctb], Brenton Kenkel [ctb], R Core Team [ctb], Michael Benesty [ctb], Reynald Lescarbeau [ctb], Andrew Ziem [ctb], Luca Scrucca [ctb], Yuan Tang [ctb], Can Candan [ctb], Tyler Hunt [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.