Learn R Programming

caret (version 3.16)

extractPrediction: Extract predictions and class probabilities from train objects

Description

This function loops through a number of train objects and calculates the training and test data predictions and class probabilities

Usage

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

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

Arguments

object
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

Value

  • For extractPrediction, a data frame with columns:
  • obsthe observed training and test data
  • predpredicted values
  • modelthe type of model used to predict
  • 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)

Details

The optimal tuning values given in the tuneValue slot of the finalModel object are used to predict.

Examples

Run this code
data(Satellite)
numSamples <- dim(Satellite)[1]
set.seed(716)

varIndex <- 1:numSamples

trainSamples <- sample(varIndex, 150)

varIndex <- (1:numSamples)[-trainSamples]
testSamples <- sample(varIndex, 100)

varIndex <- (1:numSamples)[-c(testSamples, trainSamples)]
unkSamples <- sample(varIndex, 50)

trainX <- Satellite[trainSamples, -37]
trainY <- Satellite[trainSamples, 37]

testX <- Satellite[testSamples, -37]
testY <- Satellite[testSamples, 37]

unkX <- Satellite[unkSamples, -37]

knnFit <- train(trainX, trainY, "knn")
rpartFit <- train(trainX, trainY, "rpart")

predTargets <- extractPrediction(list(knnFit, rpartFit), testX = testX, testY = testY, unkX = unkX)

Run the code above in your browser using DataLab