Caret PDF
Caret PDF
Caret PDF
URL https://github.com/topepo/caret/
BugReports https://github.com/topepo/caret/issues
Imports foreach, methods, plyr, ModelMetrics (>= 1.2.2.2), nlme,
reshape2, stats, stats4, utils, grDevices, recipes (>= 0.1.10),
withr (>= 2.0.0), pROC,
Suggests BradleyTerry2, e1071, earth (>= 2.2-3), fastICA, gam (>=
1.15), ipred, kernlab, knitr, klaR, MASS, ellipse, mda, mgcv,
mlbench, MLmetrics, nnet, party (>= 0.9-99992), pls, proxy,
randomForest, RANN, spls, subselect, pamr, superpc, Cubist,
testthat (>= 0.9.1), rpart, dplyr, covr
License GPL (>= 2)
RoxygenNote 7.0.2.9000
VignetteBuilder knitr
Encoding UTF-8
NeedsCompilation yes
Author 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],
1
2 R topics documented:
R topics documented:
as.matrix.confusionMatrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
avNNet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
bag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
bagEarth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
bagFDA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
BloodBrain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
BoxCoxTrans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
caretSBF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
cars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
classDist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
confusionMatrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
confusionMatrix.train . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
cox2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
createDataPartition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
defaultSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
densityplot.rfe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
dhfr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
diff.resamples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
dotPlot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
dotplot.diff.resamples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
downSample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
dummyVars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
extractPrediction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
featurePlot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
filterVarImp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
findCorrelation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
findLinearCombos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
format.bagEarth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
gafs.default . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
gafsControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
gafs_initial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
GermanCredit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
getSamplingInfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
ggplot.rfe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
R topics documented: 3
ggplot.train . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
histogram.train . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
icr.formula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
index2vec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
knn3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
knnreg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
learning_curve_dat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
lift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
maxDissim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
mdrr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
modelLookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
nearZeroVar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
negPredValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
nullModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
oil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
oneSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
panel.lift2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
panel.needle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
pcaNNet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
pickSizeBest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
plot.gafs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
plot.varImp.train . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
plotClassProbs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
plotObsVsPred . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
plsda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
pottery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
prcomp.resamples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
predict.bagEarth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
predict.gafs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
predict.knn3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
predict.knnreg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
predictors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
preProcess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
print.confusionMatrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
print.train . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
recall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
resampleHist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
resamples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
resampleSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
rfe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
rfeControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Sacramento . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
safs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
safs_initial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
sbf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
sbfControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
scat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
segmentationData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
4 as.matrix.confusionMatrix
SLC14_1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
spatialSign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
summary.bagEarth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
tecator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
thresholder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
train . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
trainControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
train_model_list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
update.safs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
update.train . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
varImp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
varImp.gafs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
var_seq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
xyplot.resamples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Index 218
as.matrix.confusionMatrix
Confusion matrix as a table
Description
Conversion functions for class confusionMatrix
Usage
## S3 method for class 'confusionMatrix'
as.matrix(x, what = "xtabs", ...)
Arguments
x an object of class confusionMatrix
what data to convert to matrix. Either "xtabs", "overall" or "classes"
... not currently used
Details
For as.table, the cross-tabulations are saved. For as.matrix, the three object types are saved in
matrix format.
Value
A matrix or table
Author(s)
Max Kuhn
avNNet 5
Examples
###################
## 2 class example
###################
## 3 class example
Description
Aggregate several neural network models
Usage
avNNet(x, ...)
subset,
na.action,
contrasts = NULL
)
## Default S3 method:
avNNet(
x,
y,
repeats = 5,
bag = FALSE,
allowParallel = TRUE,
seeds = sample.int(1e+05, repeats),
...
)
Arguments
x matrix or data frame of x values for examples.
... arguments passed to nnet
formula A formula of the form class ~ x1 + x2 + ...
data Data frame from which variables specified in formula are preferentially to be
taken.
weights (case) weights for each example - if missing defaults to 1.
repeats the number of neural networks with different random number seeds
bag a logical for bagging for each repeat
allowParallel if a parallel backend is loaded and available, should the function use it?
seeds random number seeds that can be set prior to bagging (if done) and network
creation. This helps maintain reproducibility when models are run in parallel.
subset An index vector specifying the cases to be used in the training sample. (NOTE:
If given, this argument must be named.)
na.action A function to specify the action to be taken if NAs are found. The default action
is for the procedure to fail. An alternative is na.omit, which leads to rejection
of cases with missing values on any required variable. (NOTE: If given, this
argument must be named.)
contrasts a list of contrasts to be used for some or all of the factors appearing as variables
in the model formula.
y matrix or data frame of target values for examples.
object an object of class avNNet as returned by avNNet.
avNNet 7
newdata matrix or data frame of test examples. A vector is considered to be a row vector
comprising a single case.
type Type of output, either: raw for the raw outputs, code for the predicted class or
prob for the class probabilities.
Details
Following Ripley (1996), the same neural network model is fit using different random number seeds.
All the resulting models are used for prediction. For regression, the output from each network are
averaged. For classification, the model scores are first averaged, then translated to predicted classes.
Bagging can also be used to create the models.
If a parallel backend is registered, the foreach package is used to train the networks in parallel.
Value
For avNNet, an object of "avNNet" or "avNNet.formula". Items of interest in #’ the output are:
Author(s)
These are heavily based on the nnet code from Brian Ripley.
References
See Also
nnet, preProcess
Examples
data(BloodBrain)
## Not run:
modelFit <- avNNet(bbbDescr, logBBB, size = 5, linout = TRUE, trace = FALSE)
modelFit
predict(modelFit, bbbDescr)
## End(Not run)
8 bag
Description
bag provides a framework for bagging classification or regression models. The user can provide
their own functions for model building, prediction and aggregation of predictions (see Details be-
low).
Usage
bag(x, ...)
bagControl(
fit = NULL,
predict = NULL,
aggregate = NULL,
downSample = FALSE,
oob = TRUE,
allowParallel = TRUE
)
## Default S3 method:
bag(x, y, B = 10, vars = ncol(x), bagControl = NULL, ...)
ldaBag
plsBag
nbBag
ctreeBag
svmBag
bag 9
nnetBag
Arguments
x a matrix or data frame of predictors
... arguments to pass to the model function
fit a function that has arguments x, y and ... and produces a model object #’
that can later be used for prediction. Example functions are found in ldaBag,
plsBag, #’ nbBag, svmBag and nnetBag.
predict a function that generates predictions for each sub-model. The function should
have #’ arguments object and x. The output of the function can be any type
of object (see the #’ example below where posterior probabilities are gener-
ated. Example functions are found in ldaBag#’ , plsBag, nbBag, svmBag and
nnetBag.)
aggregate a function with arguments x and type. The function that takes the output #’ of
the predict function and reduces the bagged predictions to a single prediction
per sample. #’ the type argument can be used to switch between predicting
classes or class probabilities for #’ classification models. Example functions are
found in ldaBag, plsBag, nbBag, #’ svmBag and nnetBag.
downSample logical: for classification, should the data set be randomly sampled so that each
#’ class has the same number of samples as the smallest class?
oob logical: should out-of-bag statistics be computed and the predictions retained?
allowParallel a parallel backend is loaded and available, should the function use it?
y a vector of outcomes
B the number of bootstrap samples to train over.
vars an integer. If this argument is not NULL, a random sample of size vars is taken
of the predictors in each bagging iteration. If NULL, all predictors are used.
bagControl a list of options.
object an object of class bag.
newdata a matrix or data frame of samples for prediction. Note that this argument must
have a non-null value
digits minimal number of significant digits.
Format
An object of class list of length 3.
Details
The function is basically a framework where users can plug in any model in to assess the effect of
bagging. Examples functions can be found in ldaBag, plsBag , nbBag, svmBag and nnetBag. Each
has elements fit, pred and aggregate.
One note: when vars is not NULL, the sub-setting occurs prior to the fit and #’ predict functions
are called. In this way, the user probably does not need to account for the #’ change in predictors in
their functions.
10 bag
When using bag with train, classification models should use type = "prob" #’ inside of the
predict function so that predict.train(object,newdata,type = "prob") will #’ work.
If a parallel backend is registered, the foreach package is used to train the models in parallel.
Value
bag produces an object of class bag with elements
fits a list with two sub-objects: the fit object has the actual model fit for that #’
bagged samples and the vars object is either NULL or a vector of integers corre-
sponding to which predictors were sampled for that model
control a mirror of the arguments passed into bagControl
call the call
B the number of bagging iterations
dims the dimensions of the training set
Author(s)
Max Kuhn
Examples
## A simple example of bagging conditional inference regression trees:
data(BloodBrain)
Description
A bagging wrapper for multivariate adaptive regression splines (MARS) via the earth function
Usage
bagEarth(x, ...)
## Default S3 method:
bagEarth(x, y, weights = NULL, B = 50, summary = mean, keepX = TRUE, ...)
Arguments
x matrix or data frame of ’x’ values for examples.
... arguments passed to the earth function
y matrix or data frame of numeric values outcomes.
weights (case) weights for each example - if missing defaults to 1.
B the number of bootstrap samples
summary a function with a single argument specifying how the bagged predictions should
be summarized
keepX a logical: should the original training data be kept?
formula A formula of the form y ~ x1 + x2 + ...
data Data frame from which variables specified in ’formula’ are preferentially to be
taken.
subset An index vector specifying the cases to be used in the training sample. (NOTE:
If given, this argument must be named.)
12 bagEarth
na.action A function to specify the action to be taken if ’NA’s are found. The default action
is for the procedure to fail. An alternative is na.omit, which leads to rejection
of cases with missing values on any required variable. (NOTE: If given, this
argument must be named.)
Details
Value
Author(s)
References
J. Friedman, “Multivariate Adaptive Regression Splines” (with discussion) (1991). Annals of Statis-
tics, 19/1, 1-141.
See Also
earth, predict.bagEarth
Examples
## Not run:
library(mda)
library(earth)
data(trees)
fit1 <- earth(x = trees[,-3], y = trees[,3])
set.seed(2189)
fit2 <- bagEarth(x = trees[,-3], y = trees[,3], B = 10)
## End(Not run)
bagFDA 13
Description
A bagging wrapper for flexible discriminant analysis (FDA) using multivariate adaptive regression
splines (MARS) basis functions
Usage
bagFDA(x, ...)
## Default S3 method:
bagFDA(x, y, weights = NULL, B = 50, keepX = TRUE, ...)
Arguments
x matrix or data frame of ’x’ values for examples.
... arguments passed to the mars function
y matrix or data frame of numeric values outcomes.
weights (case) weights for each example - if missing defaults to 1.
B the number of bootstrap samples
keepX a logical: should the original training data be kept?
formula A formula of the form y ~ x1 + x2 + ...
data Data frame from which variables specified in ’formula’ are preferentially to be
taken.
subset An index vector specifying the cases to be used in the training sample. (NOTE:
If given, this argument must be named.)
14 bagFDA
na.action A function to specify the action to be taken if ’NA’s are found. The default action
is for the procedure to fail. An alternative is na.omit, which leads to rejection
of cases with missing values on any required variable. (NOTE: If given, this
argument must be named.)
Details
The function computes a FDA model for each bootstap sample.
Value
A list with elements
fit a list of B FDA fits
B the number of bootstrap samples
call the function call
x either NULL or the value of x, depending on the value of keepX
oob a matrix of performance estimates for each bootstrap sample
Author(s)
Max Kuhn (bagFDA.formula is based on Ripley’s nnet.formula)
References
J. Friedman, “Multivariate Adaptive Regression Splines” (with discussion) (1991). Annals of Statis-
tics, 19/1, 1-141.
See Also
fda, predict.bagFDA
Examples
library(mlbench)
library(earth)
data(Glass)
set.seed(36)
inTrain <- sample(1:dim(Glass)[1], 150)
set.seed(3577)
baggedFit <- bagFDA(Type ~ ., trainData)
confusionMatrix(data = predict(baggedFit, testData[, -10]),
reference = testData[, 10])
BloodBrain 15
Description
Mente and Lombardo (2005) develop models to predict the log of the ratio of the concentration of
a compound in the brain and the concentration in blood. For each compound, they computed three
sets of molecular descriptors: MOE 2D, rule-of-five and Charge Polar Surface Area (CPSA). In all,
134 descriptors were calculated. Included in this package are 208 non-proprietary literature com-
pounds. The vector logBBB contains the concentration ratio and the data fame bbbDescr contains
the descriptor values.
Value
bbbDescr data frame of chemical descriptors
logBBB vector of assay results
Source
Mente, S.R. and Lombardo, F. (2005). A recursive-partitioning model for blood-brain barrier per-
meation, Journal of Computer-Aided Molecular Design, Vol. 19, pg. 465-481.
Description
These classes can be used to estimate transformations and apply them to existing and future data
Usage
BoxCoxTrans(y, ...)
## Default S3 method:
BoxCoxTrans(
y,
x = rep(1, length(y)),
fudge = 0.2,
numUnique = 3,
na.rm = FALSE,
...
)
Arguments
y a numeric vector of data to be transformed. For BoxCoxTrans, the data must be
strictly positive.
... for BoxCoxTrans: options to pass to boxcox. plotit should not be passed
through. For predict.BoxCoxTrans, additional arguments are ignored.
x an optional dependent variable to be used in a linear model.
fudge a tolerance value: lambda values within +/-fudge will be coerced to 0 and within
1+/-fudge will be coerced to 1.
numUnique how many unique values should y have to estimate the transformation?
na.rm a logical value indicating whether NA values should be stripped from y and x
before the computation proceeds.
newdata a numeric vector of values to transform.
digits minimal number of significant digits.
object an object of class BoxCoxTrans or expoTrans.
Details
BoxCoxTrans function is basically a wrapper for the boxcox function in the MASS library. It can
be used to estimate the transformation and apply it to new data.
expoTrans estimates the exponential transformation of Manly (1976) but assumes a common mean
for the data. The transformation parameter is estimated by directly maximizing the likelihood.
If any(y <= 0) or if length(unique(y)) < numUnique, lambda is not estimated and no transfor-
mation is applied.
Value
Both functions returns a list of class of either BoxCoxTrans or expoTrans with elements
Author(s)
Max Author
References
Box, G. E. P. and Cox, D. R. (1964) An analysis of transformations (with discussion). Journal of
the Royal Statistical Society B, 26, 211-252. Manly, B. L. (1976) Exponential data transformations.
The Statistician, 25, 37 - 42.
See Also
boxcox, preProcess, optim
Examples
data(BloodBrain)
predict(bc, ratio[1:5])
ratio[5] <- NA
bc2 <- BoxCoxTrans(ratio, bbbDescr$tpsa, na.rm = TRUE)
bc2
Description
For classification models, this function creates a ’calibration plot’ that describes how consistent
model probabilities are with observed event rates.
Usage
calibration(x, ...)
## Default S3 method:
calibration(x, ...)
x,
data = NULL,
class = NULL,
cuts = 11,
subset = TRUE,
lattice.options = NULL,
...
)
Arguments
x a lattice formula (see xyplot for syntax) where the left -hand side of the
formula is a factor class variable of the observed outcome and the right-hand side
specifies one or model columns corresponding to a numeric ranking variable for
a model (e.g. class probabilities). The classification variable should have two
levels.
... options to pass through to xyplot or the panel function (not used in calibration.formula).
data For calibration.formula, a data frame (or more precisely, anything that is a
valid envir argument in eval, e.g., a list or an environment) containing val-
ues for any variables in the formula, as well as groups and subset if ap-
plicable. If not found in data, or if data is unspecified, the variables are
looked for in the environment of the formula. This argument is not used for
xyplot.calibration. For ggplot.calibration, data should be an object of class
"calibration"."
class a character string for the class of interest
cuts If a single number this indicates the number of splits of the data are used to
create the plot. By default, it uses as many cuts as there are rows in data. If a
vector, these are the actual cuts that will be used.
subset An expression that evaluates to a logical or integer indexing vector. It is evalu-
ated in data. Only the resulting rows of data are used for the plot.
lattice.options
A list that could be supplied to lattice.options
bwidth, dwidth a numeric value for the confidence interval bar width and dodge width, respec-
tively. In the latter case, a dodge is only used when multiple models are specified
in the formula.
calibration 19
Details
calibration.formula is used to process the data and xyplot.calibration is used to create the
plot.
To construct the calibration plot, the following steps are used for each model:
1. The data are split into cuts -1 roughly equal groups by their class probabilities
2. the number of samples with true results equal to class are determined
3. the event rate is determined for each bin
xyplot.calibration produces a plot of the observed event rate by the mid-point of the bins.
This implementation uses the lattice function xyplot, so plot elements can be changed via panel
functions, trellis.par.set or other means. calibration uses the panel function panel.calibration
by default, but it can be changed by passing that argument into xyplot.calibration.
The following elements are set by default in the plot but can be changed by passing new values into
xyplot.calibration: xlab = "Bin Midpoint", ylab = "Observed Event Percentage", type =
"o", ylim = extendrange(c(0,100)),xlim = extendrange(c(0,100)) and panel = panel.calibration
For the ggplot method, confidence intervals on the estimated proportions (from binom.test) are
also shown.
Value
calibration.formula returns a list with elements:
data the data used for plotting
cuts the number of cuts
class the event class
probNames the names of the model probabilities
xyplot.calibration returns a lattice object
Author(s)
Max Kuhn, some lattice code and documentation by Deepayan Sarkar
See Also
xyplot, trellis.par.set
Examples
## Not run:
data(mdrr)
mdrrDescr <- mdrrDescr[, -nearZeroVar(mdrrDescr)]
mdrrDescr <- mdrrDescr[, -findCorrelation(cor(mdrrDescr), .5)]
library(MASS)
## End(Not run)
Description
Usage
caretSBF
anovaScores(x, y)
gamScores(x, y)
Arguments
Format
Details
Author(s)
Max Kuhn
See Also
cars Kelly Blue Book resale data for 2005 model year GM cars
Description
Kuiper (2008) collected data on Kelly Blue Book resale data for 804 GM cars (2005 model year).
Value
cars data frame of the suggested retail price (column Price) and various characteris-
tics of each car (columns Mileage, Cylinder, Doors, Cruise, Sound, Leather,
Buick, Cadillac, Chevy, Pontiac, Saab, Saturn, convertible, coupe, hatchback,
sedan and wagon)
Source
Kuiper, S. (2008). Introduction to Multiple Regression: How Much Is Your Car Worth?, Journal of
Statistics Education, Vol. 16 http://jse.amstat.org/jse_archive.htm#2008.
22 classDist
Description
This function computes the class centroids and covariance matrix for a training set for determining
Mahalanobis distances of samples to each class centroid.
Usage
classDist(x, ...)
## Default S3 method:
classDist(x, y, groups = 5, pca = FALSE, keep = NULL, ...)
Arguments
Details
For factor outcomes, the data are split into groups for each class and the mean and covariance matrix
are calculated. These are then used to compute Mahalanobis distances to the class centers (using
predict.classDist The function will check for non-singular matrices.
For numeric outcomes, the data are split into roughly equal sized bins based on groups. Percentiles
are used to split the data.
classDist 23
Value
values a list with elements for each class. Each element contains a mean vector for the
class centroid and the inverse of the class covariance matrix
classes a character vector of class labels
pca the results of prcomp when pca = TRUE
call the function call
p the number of variables
n a vector of samples sizes per class
For predict.classDist, a matrix with columns for each class. The columns names are the names
of the class with the prefix dist.. In the case of numeric y, the class labels are the percentiles. For
example, of groups = 9, the variable names would be dist.11.11, dist.22.22, etc.
Author(s)
Max Kuhn
References
Forina et al. CAIMAN brothers: A family of powerful classification and class modeling techniques.
Chemometrics and Intelligent Laboratory Systems (2009) vol. 96 (2) pp. 239-245
See Also
mahalanobis
Examples
Description
Calculates a cross-tabulation of observed and predicted classes with associated statistics.
Usage
confusionMatrix(data, ...)
## Default S3 method:
confusionMatrix(
data,
reference,
positive = NULL,
dnn = c("Prediction", "Reference"),
prevalence = NULL,
mode = "sens_spec",
...
)
Arguments
data a factor of predicted classes (for the default method) or an object of class table.
... options to be passed to table. NOTE: do not include dnn here
reference a factor of classes to be used as the true results
positive an optional character string for the factor level that corresponds to a "positive"
result (if that makes sense for your data). If there are only two factor levels, the
first level will be used as the "positive" result. When mode = "prec_recall",
positive is the same value used for relevant for functions precision, recall,
and F_meas.table.
dnn a character vector of dimnames for the table
prevalence a numeric value or matrix for the rate of the "positive" class of the data. When
data has two levels, prevalence should be a single numeric value. Otherwise,
it should be a vector of numeric values with elements for each class. The vector
should have names corresponding to the classes.
mode a single character string either "sens_spec", "prec_recall", or "everything"
confusionMatrix 25
Details
The functions requires that the factors have exactly the same levels.
For two class problems, the sensitivity, specificity, positive predictive value and negative predictive
value is calculated using the positive argument. Also, the prevalence of the "event" is computed
from the data (unless passed in as an argument), the detection rate (the rate of true events also
predicted to be events) and the detection prevalence (the prevalence of predicted events).
Suppose a 2x2 table with notation
Reference
Predicted Event No Event
Event A B
No Event C D
P revalence = (A + C)/(A + B + C + D)
P P V = (sensitivity∗prevalence)/((sensitivity∗prevalence)+((1−specif icity)∗(1−prevalence)))
DetectionRate = A/(A + B + C + D)
P recision = A/(A + B)
Recall = A/(A + C)
Value
a list with elements
Note
If the reference and data factors have the same levels, but in the incorrect order, the function will
reorder them to the order of the data and issue a warning.
Author(s)
Max Kuhn
References
Kuhn, M. (2008), “Building predictive models in R using the caret package, ” Journal of Statistical
Software, (http://www.jstatsoft.org/article/view/v028i05/v28i05.pdf).
Altman, D.G., Bland, J.M. (1994) “Diagnostic tests 1: sensitivity and specificity,” British Medical
Journal, vol 308, 1552.
Altman, D.G., Bland, J.M. (1994) “Diagnostic tests 2: predictive values,” British Medical Journal,
vol 309, 102.
Velez, D.R., et. al. (2008) “A balanced accuracy function for epistasis modeling in imbalanced
datasets using multifactor dimensionality reduction.,” Genetic Epidemiology, vol 4, 306.
See Also
as.table.confusionMatrix, as.matrix.confusionMatrix, sensitivity, specificity, posPredValue,
negPredValue, print.confusionMatrix, binom.test
Examples
###################
## 2 class example
confusionMatrix(xtab)
confusionMatrix(pred, truth)
confusionMatrix(xtab, prevalence = 0.25)
###################
## 3 class example
confusionMatrix(iris$Species, sample(iris$Species))
confusionMatrix(iris$Species, sample(iris$Species))
Description
Using a train, rfe, sbf object, determine a confusion matrix based on the resampling procedure
Usage
## S3 method for class 'train'
confusionMatrix(
data,
norm = "overall",
dnn = c("Prediction", "Reference"),
...
)
Arguments
data An object of class train, rfe, sbf that did not use out-of-bag resampling or
leave-one-out cross-validation.
norm A character string indicating how the table entries should be normalized. Valid
values are "none", "overall" or "average".
dnn A character vector of dimnames for the table
... not used here
28 confusionMatrix.train
Details
When train is used for tuning a model, it tracks the confusion matrix cell entries for the hold-
out samples. These can be aggregated and used for diagnostic purposes. For train, the matrix
is estimated for the final model tuning parameters determined by train. For rfe, the matrix is
associated with the optimal number of variables.
There are several ways to show the table entries. Using norm = "none" will show the aggregated
counts of samples on each of the cells (across all resamples). For norm = "average", the average
number of cell counts across resamples is computed (this can help evaluate how many holdout sam-
ples there were on average). The default is norm = "overall", which is equivalento to "average"
but in percentages.
Value
a list of class confusionMatrix.train, confusionMatrix.rfe or confusionMatrix.sbf with
elements
Author(s)
Max Kuhn
See Also
confusionMatrix, train, rfe, sbf, trainControl
Examples
data(iris)
TrainData <- iris[,1:4]
TrainClasses <- iris[,5]
Description
From Sutherland, O’Brien, and Weaver (2003): "A set of 467 cyclooxygenase-2 (COX-2) inhibitors
has been assembled from the published work of a single research group, with in vitro activities
against human recombinant enzyme expressed as IC50 values ranging from 1 nM to >100 uM (53
compounds have indeterminate IC50 values)."
Details
The data are in the Supplemental Data file for the article.
A set of 255 descriptors (MOE2D and QikProp) were generated. To classify the data, we used a
cutoff of $2^2.5$ to determine activity
Value
cox2Descr the descriptors
cox2IC50 the IC50 data used to determine activity
cox2Class the categorical outcome ("Active" or "Inactive") based on the $2^2.5$ cutoff
Source
Sutherland, J. J., O’Brien, L. A. and Weaver, D. F. (2003). Spline-Fitting with a Genetic Algorithm:
A Method for Developing Classification Structure-Activity Relationships, Journal of Chemical In-
formation and Computer Sciences, Vol. 43, pg. 1906-1915.
Description
A series of test/training partitions are created using createDataPartition while createResample
creates one or more bootstrap samples. createFolds splits the data into k groups while createTimeSlices
creates cross-validation split for series data. groupKFold splits the data based on a grouping factor.
Usage
createDataPartition(
y,
times = 1,
p = 0.5,
list = TRUE,
groups = min(5, length(y))
30 createDataPartition
groupKFold(group, k = length(unique(group)))
Arguments
y a vector of outcomes. For createTimeSlices, these should be in chronological
order.
times the number of partitions to create
p the percentage of data that goes to training
list logical - should the results be in a list (TRUE) or a matrix with the number of
rows equal to floor(p * length(y)) and times columns.
groups for numeric y, the number of breaks in the quantiles (see below)
k an integer for the number of folds.
returnTrain a logical. When true, the values returned are the sample positions corresponding
to the data used during training. This argument only works in conjunction with
list = TRUE
initialWindow The initial number of consecutive values in each training set sample
horizon the number of consecutive values in test set sample
fixedWindow logical, if FALSE, all training samples start at 1
skip integer, how many (if any) resamples to skip to thin the total amount
group a vector of groups whose length matches the number of rows in the overall data
set.
Details
For bootstrap samples, simple random sampling is used.
For other data splitting, the random sampling is done within the levels of y when y is a factor in an
attempt to balance the class distributions within the splits.
For numeric y, the sample is split into groups sections based on percentiles and sampling is done
within these subgroups. For createDataPartition, the number of percentiles is set via the groups
argument. For createFolds and createMultiFolds, the number of groups is set dynamically
based on the sample size and k. For smaller samples sizes, these two functions may not do stratified
splitting and, at most, will split the data into quartiles.
Also, for createDataPartition, very small class sizes (<= 3) the classes may not show up in both
the training and test data
createDataPartition 31
For multiple k-fold cross-validation, completely independent folds are created. The names of the
list objects will denote the fold membership using the pattern "Foldi.Repj" meaning the ith section
(of k) of the jth cross-validation set (of times). Note that this function calls createFolds with
list = TRUE and returnTrain = TRUE.
Hyndman and Athanasopoulos (2013)) discuss rolling forecasting origin techniques that move the
training and test sets in time. createTimeSlices can create the indices for this type of splitting.
For Group k-fold cross-validation, the data are split such that no group is contained in both the
modeling and holdout sets. One or more group could be left out, depending on the value of k.
Value
A list or matrix of row position integers corresponding to the training data. For createTimeSlices
subsamples are named by the end index of each training subsample.
Author(s)
Max Kuhn, createTimeSlices by Tony Cooper
References
http://topepo.github.io/caret/data-splitting.html
Hyndman and Athanasopoulos (2013), Forecasting: principles and practice. https://www.otexts.
org/fpp
Examples
data(oil)
createDataPartition(oilType, 2)
plot(density(x[inA]))
rug(x[inA])
createResample(oilType, 2)
createFolds(oilType, 10)
createFolds(oilType, 5, FALSE)
createFolds(rnorm(21))
createTimeSlices(1:15, 5, 3)
createTimeSlices(1:15, 5, 3, skip = 2)
createTimeSlices(1:15, 5, 3, skip = 3)
set.seed(131)
groups <- sort(sample(letters[1:4], size = 20, replace = TRUE))
table(groups)
folds <- groupKFold(groups)
lapply(folds, function(x, y) table(y[x]), y = groups)
Description
Given two numeric vectors of data, the mean squared error and R-squared are calculated. For two
factors, the overall agreement rate and Kappa are determined.
Usage
defaultSummary(data, lev = NULL, model = NULL)
postResample(pred, obs)
Arguments
data a data frame with columns obs and pred for the observed and predicted out-
comes. For metrics that rely on class probabilities, such as twoClassSummary,
columns should also include predicted probabilities for each class. See the
classProbs argument to trainControl.
lev a character vector of factors levels for the response. In regression cases, this
would be NULL.
model a character string for the model name (as taken from the method argument of
train.
pred A vector of numeric data (could be a factor)
obs A vector of numeric data (could be a factor)
defaultSummary 33
Details
postResample is meant to be used with apply across a matrix. For numeric data the code checks
to see if the standard deviation of either vector is zero. If so, the correlation between those samples
is assigned a value of zero. NA values are ignored everywhere.
Note that many models have more predictors (or parameters) than data points, so the typical mean
squared error denominator (n - p) does not apply. Root mean squared error is calculated using
sqrt(mean((pred -obs)^2. Also, R2 is calculated wither using as the square of the correlation
between the observed and predicted outcomes when form = "corr". when form = "traditional",
(yi − ŷi )2
P
2
R =1− P
(yi − ȳi )2
. Mean absolute error is calculated using mean(abs(pred-obs)).
defaultSummary is the default function to compute performance metrics in train. It is a wrapper
around postResample. The first argument is data, which is data.frame with columns named
obs and pred for the observed and predicted outcome values (either numeric data for regression
or character values for classification). The second argument is lev, a character string that has
the outcome factor levels or NULL for a regression model. The third parameter is model, which
can be used if a summary metric is specific to a model function. If other columns from the data
are required to compute the summary statistics, but should not be used in the model, the recipe
method for train can be used.
twoClassSummary computes sensitivity, specificity and the area under the ROC curve. mnLogLoss
computes the minus log-likelihood of the multinomial distribution (without the constant term):
n C
−1 X X
−logLoss = yij log(pij )
n i=1 j=1
where the y values are binary indicators for the classes and p are the predicted class probabilities.
prSummary (for precision and recall) computes values for the default 0.50 probability cutoff as well
as the area under the precision-recall curve across all cutoffs and is labelled as "AUC" in the output.
If assumes that the first level of the factor variables corresponds to a relevant result but the lev
argument can be used to change this.
multiClassSummary computes some overall measures of for performance (e.g. overall accuracy
and the Kappa statistic) and several averages of statistics calculated from "one-versus-all" configu-
rations. For example, if there are three classes, three sets of sensitivity values are determined and the
average is reported with the name ("Mean_Sensitivity"). The same is true for a number of statistics
generated by confusionMatrix. With two classes, the basic sensitivity is reported with the name
"Sensitivity".
To use twoClassSummary and/or mnLogLoss, the classProbs argument of trainControl should
be TRUE. multiClassSummary can be used without class probabilities but some statistics (e.g. over-
all log loss and the average of per-class area under the ROC curves) will not be in the result set.
Other functions can be used via the summaryFunction argument of trainControl. Custom func-
tions must have the same arguments asdefaultSummary.
The function getTrainPerf returns a one row data frame with the resampling results for the chosen
model. The statistics will have the prefix "Train" (i.e. "TrainROC"). There is also a column called
"method" that echoes the argument of the call to trainControl of the same name.
34 densityplot.rfe
Value
A vector of performance estimates.
Author(s)
Max Kuhn, Zachary Mayer
References
Kvalseth. Cautionary note about R2 . American Statistician (1985) vol. 39 (4) pp. 279-285
See Also
trainControl
Examples
Description
A set of lattice functions are provided to plot the resampled performance estimates (e.g. classifica-
tion accuracy, RMSE) over different subset sizes.
Usage
## S3 method for class 'rfe'
densityplot(x, data = NULL, metric = x$metric, ...)
densityplot.rfe 35
Arguments
x An object produced by rfe
data This argument is not used
metric A character string specifying the single performance metric that will be plotted
... arguments to pass to either histogram, densityplot, xyplot or stripplot
Details
By default, only the resampling results for the optimal model are saved in the rfe object. The
function rfeControl can be used to save all the results using the returnResamp argument.
If leave-one-out or out-of-bag resampling was specified, plots cannot be produced (see the method
argument of rfeControl)
Value
A lattice plot object
Author(s)
Max Kuhn
See Also
rfe, rfeControl, histogram, densityplot, xyplot, stripplot
Examples
## Not run:
library(mlbench)
n <- 100
p <- 40
sigma <- 1
set.seed(1)
sim <- mlbench.friedman1(n, sd = sigma)
x <- cbind(sim$x, matrix(rnorm(n * p), nrow = n))
y <- sim$y
colnames(x) <- paste("var", 1:ncol(x), sep = "")
histogram(lmProfile)
densityplot(lmProfile)
## End(Not run)
Description
Sutherland and Weaver (2004) discuss QSAR models for dihydrofolate reductase (DHFR) inhi-
bition. This data set contains values for 325 compounds. For each compound, 228 molecular
descriptors have been calculated. Additionally, each samples is designated as "active" or "inactive".
Details
The data frame dhfr contains a column called Y with the outcome classification. The remainder of
the columns are molecular descriptor values.
Value
Source
Sutherland, J.J. and Weaver, D.F. (2004). Three-dimensional quantitative structure-activity and
structure-selectivity relationships of dihydrofolate reductase inhibitors, Journal of Computer-Aided
Molecular Design, Vol. 18, pg. 309-331.
Description
Usage
Arguments
Details
The ideas and methods here are based on Hothorn et al. (2005) and Eugster et al. (2008).
For each metric, all pair-wise differences are computed and tested to assess if the difference is equal
to zero.
When a Bonferroni correction is used, the confidence level is changed from confLevel to 1-((1-confLevel)/p)
here p is the number of pair-wise comparisons are being made. For other correction methods, no
such change is used.
compare_models is a shorthand function to compare two models using a single metric. It returns
the results of t.test on the differences.
38 diff.resamples
Value
An object of class "diff.resamples" with elements:
call the call
difs a list for each metric being compared. Each list contains a matrix with differ-
ences in columns and resamples in rows
statistics a list of results generated by test
adjustment the p-value adjustment used
models a character string for which models were compared.
metrics a character string of performance metrics that were used
or...
An object of class "summary.diff.resamples" with elements:
call the call
table a list of tables that show the differences and p-values
...or (for compare_models) an object of class htest resulting from t.test.
Author(s)
Max Kuhn
References
Hothorn et al. The design and analysis of benchmark experiments. Journal of Computational and
Graphical Statistics (2005) vol. 14 (3) pp. 675-699
Eugster et al. Exploratory and inferential analysis of benchmark experiments. Ludwigs-Maximilians-
Universitat Munchen, Department of Statistics, Tech. Rep (2008) vol. 30
See Also
resamples, dotplot.diff.resamples, densityplot.diff.resamples, bwplot.diff.resamples,
levelplot.diff.resamples
Examples
## Not run:
#load(url("http://topepo.github.io/caret/exampleModels.RData"))
difs
dotPlot 39
summary(difs)
compare_models(rpartFit, ctreeFit)
## End(Not run)
Description
A lattice dotplot is created from an object of class varImp.train.
Usage
dotPlot(x, top = min(20, dim(x$importance)[1]), ...)
Arguments
x an object of class varImp.train
top the number of predictors to plot
... options passed to dotplot
Value
an object of class trellis.
Author(s)
Max Kuhn
See Also
varImp, dotplot
Examples
data(iris)
TrainData <- iris[,1:4]
TrainClasses <- iris[,5]
dotPlot(knnImp)
40 dotplot.diff.resamples
dotplot.diff.resamples
Lattice Functions for Visualizing Resampling Differences
Description
Usage
Arguments
Details
densityplot and bwplot display univariate visualizations of the resampling distributions. levelplot
displays the matrix of pair-wide comparisons. dotplot shows the differences along with their as-
sociated confidence intervals.
Value
a lattice object
Author(s)
Max Kuhn
See Also
Examples
## Not run:
#load(url("http://topepo.github.io/caret/exampleModels.RData"))
dotplot(difs)
densityplot(difs,
metric = "RMSE",
auto.key = TRUE,
pch = "|")
bwplot(difs,
metric = "RMSE")
## End(Not run)
Description
downSample will randomly sample a data set so that all classes have the same frequency as the
minority class. upSample samples with replacement to make the class distributions equal
Usage
downSample(x, y, list = FALSE, yname = "Class")
Arguments
x a matrix or data frame of predictor variables
y a factor variable with the class memberships
list should the function return list(x,y) or bind x and y together? If FALSE, the
output will be coerced to a data frame.
yname if list = FALSE, a label for the class column
42 dummyVars
Details
Simple random sampling is used to down-sample for the majority class(es). Note that the minority
class data are left intact and that the samples will be re-ordered in the down-sampled version.
For up-sampling, all the original data are left intact and additional samples are added to the minority
classes with replacement.
Value
Either a data frame or a list with elements x and y.
Author(s)
Max Kuhn
Examples
## A ridiculous example...
data(oil)
table(oilType)
downSample(fattyAcids, oilType)
upSample(fattyAcids, oilType)
Description
dummyVars creates a full set of dummy variables (i.e. less than full rank parameterization)
Usage
dummyVars(formula, ...)
## Default S3 method:
dummyVars(formula, data, sep = ".", levelsOnly = FALSE, fullRank = FALSE, ...)
Arguments
formula An appropriate R model formula, see References
... additional arguments to be passed to other methods
data A data frame with the predictors of interest
sep An optional separator between factor variable names and their levels. Use sep
= NULL for no separator (i.e. normal behavior of model.matrix as shown in the
Details section)
levelsOnly A logical; TRUE means to completely remove the variable names from the col-
umn names
fullRank A logical; should a full rank or less than full rank parameterization be used? If
TRUE, factors are encoded to be consistent with model.matrix and the resulting
there are no linear dependencies induced between the columns.
x A factor vector.
object An object of class dummyVars
newdata A data frame with the required columns
na.action A function determining what should be done with missing values in newdata.
The default is to predict NA.
n A vector of levels for a factor, or the number of levels.
contrasts A logical indicating whether contrasts should be computed.
sparse A logical indicating if the result should be sparse.
drop2nd A logical: if the factor has two levels, should a single binary vector be returned?
Details
Most of the contrasts functions in R produce full rank parameterizations of the predictor data. For
example, contr.treatment creates a reference cell in the data and defines dummy variables for all
factor levels except those in the reference cell. For example, if a factor with 5 levels is used in a
model formula alone, contr.treatment creates columns for the intercept and all the factor levels
except the first level of the factor. For the data in the Example section below, this would produce:
In some situations, there may be a need for dummy variables for all the levels of the factor. For the
same example:
44 dummyVars
Given a formula and initial data set, the class dummyVars gathers all the information needed to
produce a full set of dummy variables for any data set. It uses contr.ltfr as the base function to
do this.
class2ind is most useful for converting a factor outcome vector to a matrix (or vector) of dummy
variables.
Value
The output of dummyVars is a list of class ’dummyVars’ with elements
Author(s)
contr.ltfr is a small modification of contr.treatment by Max Kuhn
References
https://cran.r-project.org/doc/manuals/R-intro.html#Formulae-for-statistical-models
See Also
model.matrix, contrasts, formula
extractPrediction 45
Examples
when <- data.frame(time = c("afternoon", "night", "afternoon",
"morning", "morning", "morning",
"morning", "afternoon", "afternoon"),
day = c("Mon", "Mon", "Mon",
"Wed", "Wed", "Fri",
"Sat", "Sat", "Fri"),
stringsAsFactors = TRUE)
## Default behavior:
model.matrix(~day, when)
head(class2ind(iris$Species))
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.
46 extractPrediction
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
)
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.
extractPrediction 47
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:
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
plotObsVsPred, plotClassProbs, trainControl
Examples
## Not run:
predict(knnFit)
predict(knnFit, type = "prob")
predict(bothModels)
## End(Not run)
Description
A shortcut to produce lattice graphs
Usage
featurePlot(
x,
y,
plot = if (is.factor(y)) "strip" else "scatter",
labels = c("Feature", ""),
...
)
Arguments
x a matrix or data frame of continuous feature/probe/spectra data.
y a factor indicating class membership.
plot the type of plot. For classification: box, strip, density, pairs or ellipse.
For regression, pairs or scatter
labels a bad attempt at pre-defined axis labels
... options passed to lattice calls.
Details
This function “stacks” data to get it into a form compatible with lattice and creates the plots
Value
An object of class “trellis”. The ‘update’ method can be used to update components of the object
and the ‘print’ method (usually called by default) will plot it on an appropriate plotting device.
filterVarImp 49
Author(s)
Max Kuhn
Examples
x <- matrix(rnorm(50*5),ncol=5)
y <- factor(rep(c("A", "B"), 25))
Description
Specific engines for variable importance on a model by model basis.
Usage
filterVarImp(x, y, nonpara = FALSE, ...)
Arguments
x A matrix or data frame of predictor data
y A vector (numeric or factor) of outcomes)
nonpara should nonparametric methods be used to assess the relationship between the
features and response
... options to pass to either lm or loess
Details
The importance of each predictor is evaluated individually using a “filter” approach.
For classification, ROC curve analysis is conducted on each predictor. For two class problems, a
series of cutoffs is applied to the predictor data to predict the class. The sensitivity and specificity
are computed for each cutoff and the ROC curve is computed. The trapezoidal rule is used to
compute the area under the ROC curve. This area is used as the measure of variable importance.
For multi-class outcomes, the problem is decomposed into all pair-wise problems and the area under
the curve is calculated for each class pair (i.e class 1 vs. class 2, class 2 vs. class 3 etc.). For a
specific class, the maximum area under the curve across the relevant pair-wise AUC’s is used as the
variable importance measure.
50 findCorrelation
For regression, the relationship between each predictor and the outcome is evaluated. An argument,
nonpara, is used to pick the model fitting technique. When nonpara = FALSE, a linear model is fit
and the absolute value of the $t$-value for the slope of the predictor is used. Otherwise, a loess
smoother is fit between the outcome and the predictor. The $R^2$ statistic is calculated for this
model against the intercept only null model.
Value
A data frame with variable importances. Column names depend on the problem type. For regres-
sion, the data frame contains one column: "Overall" for the importance values.
Author(s)
Max Kuhn
Examples
data(mdrr)
filterVarImp(mdrrDescr[, 1:5], mdrrClass)
data(BloodBrain)
Description
This function searches through a correlation matrix and returns a vector of integers corresponding
to columns to remove to reduce pair-wise correlations.
Usage
findCorrelation(
x,
cutoff = 0.9,
verbose = FALSE,
names = FALSE,
exact = ncol(x) < 100
)
findCorrelation 51
Arguments
x A correlation matrix
cutoff A numeric value for the pair-wise absolute correlation cutoff
verbose A boolean for printing the details
names a logical; should the column names be returned (TRUE) or the column index
(FALSE)?
exact a logical; should the average correlations be recomputed at each step? See De-
tails below.
Details
The absolute values of pair-wise correlations are considered. If two variables have a high correla-
tion, the function looks at the mean absolute correlation of each variable and removes the variable
with the largest mean absolute correlation.
Using exact = TRUE will cause the function to re-evaluate the average correlations at each step
while exact = FALSE uses all the correlations regardless of whether they have been eliminated or
not. The exact calculations will remove a smaller number of predictors but can be much slower
when the problem dimensions are "big".
There are several function in the subselect package (leaps, genetic, anneal) that can also be used
to accomplish the same goal but tend to retain more predictors.
Value
A vector of indices denoting the columns to remove (when names = TRUE) otherwise a vector of
column names. If no correlations meet the criteria, integer(0) is returned.
Author(s)
Original R code by Dong Li, modified by Max Kuhn
See Also
leaps, genetic, anneal, findLinearCombos
Examples
R1 <- structure(c(1, 0.86, 0.56, 0.32, 0.85, 0.86, 1, 0.01, 0.74, 0.32,
0.56, 0.01, 1, 0.65, 0.91, 0.32, 0.74, 0.65, 1, 0.36,
0.85, 0.32, 0.91, 0.36, 1),
.Dim = c(5L, 5L))
colnames(R1) <- rownames(R1) <- paste0("x", 1:ncol(R1))
R1
Description
Enumerate and resolve the linear combinations in a numeric matrix
Usage
findLinearCombos(x)
Arguments
x a numeric matrix
Details
The QR decomposition is used to determine if the matrix is full rank and then identify the sets of
columns that are involved in the dependencies.
To "resolve" them, columns are iteratively removed and the matrix rank is rechecked.
The trim.matrix function in the subselect package can also be used to accomplish the same goal.
Value
a list with elements:
linearCombos If there are linear combinations, this will be a list with elements for each depen-
dency that contains vectors of column numbers.
remove a list of column numbers that can be removed to counter the linear combinations
Author(s)
Kirk Mettler and Jed Wing (enumLC) and Max Kuhn (findLinearCombos)
format.bagEarth 53
See Also
trim.matrix
Examples
findLinearCombos(testData1)
findLinearCombos(testData2)
Description
Return a string representing the ‘bagEarth’ expression.
Usage
## S3 method for class 'bagEarth'
format(x, file = "", cat = TRUE, ...)
Arguments
x An bagEarth object. This is the only required argument.
file A connection, or a character string naming the file to print to. If "" (the default),
the output prints to the standard output connection. See cat.
cat a logical; should the equation be printed?
... Arguments to format.earth.
54 gafs.default
Value
A character representation of the bagged earth object.
See Also
earth
Examples
# yields:
# (
# 31.61075
# + 6.587273 * pmax(0, Girth - 14.2)
# - 3.229363 * pmax(0, 14.2 - Girth)
# - 0.3167140 * pmax(0, 79 - Height)
# +
# 22.80225
# + 5.309866 * pmax(0, Girth - 12)
# - 2.378658 * pmax(0, 12 - Girth)
# + 0.793045 * pmax(0, Height - 80)
# - 0.3411915 * pmax(0, 80 - Height)
# +
# 31.39772
# + 6.18193 * pmax(0, Girth - 14.2)
# - 3.660456 * pmax(0, 14.2 - Girth)
# + 0.6489774 * pmax(0, Height - 80)
# )/3
Description
Supervised feature selection using genetic algorithms
Usage
## Default S3 method:
gafs(
x,
y,
iters = 10,
popSize = 50,
pcrossover = 0.8,
gafs.default 55
pmutation = 0.1,
elite = 0,
suggestions = NULL,
differences = TRUE,
gafsControl = gafsControl(),
...
)
Arguments
x An object where samples are in rows and features are in columns. This could be
a simple matrix, data frame or other type (e.g. sparse matrix). For the recipes
method, x is a recipe object. See Details below
y a numeric or factor vector containing the outcome for each sample
iters number of search iterations
popSize number of subsets evaluated at each iteration
pcrossover the crossover probability
pmutation the mutation probability
elite the number of best subsets to survive at each generation
suggestions a binary matrix of subsets strings to be included in the initial population. If
provided the number of columns must match the number of columns in x
differences a logical: should the difference in fitness values with and without each predictor
be calculated?
gafsControl a list of values that define how this function acts. See gafsControl and URL.
... additional arguments to be passed to other methods
data Data frame from which variables specified in formula or recipe are preferen-
tially to be taken.
56 gafs.default
Details
gafs conducts a supervised binary search of the predictor space using a genetic algorithm. See
Mitchell (1996) and Scrucca (2013) for more details on genetic algorithms.
This function conducts the search of the feature space repeatedly within resampling iterations. First,
the training data are split be whatever resampling method was specified in the control function. For
example, if 10-fold cross-validation is selected, the entire genetic algorithm is conducted 10 separate
times. For the first fold, nine tenths of the data are used in the search while the remaining tenth is
used to estimate the external performance since these data points were not used in the search.
During the genetic algorithm, a measure of fitness is needed to guide the search. This is the internal
measure of performance. During the search, the data that are available are the instances selected by
the top-level resampling (e.g. the nine tenths mentioned above). A common approach is to conduct
another resampling procedure. Another option is to use a holdout set of samples to determine the
internal estimate of performance (see the holdout argument of the control function). While this is
faster, it is more likely to cause overfitting of the features and should only be used when a large
amount of training data are available. Yet another idea is to use a penalized metric (such as the AIC
statistic) but this may not exist for some metrics (e.g. the area under the ROC curve).
The internal estimates of performance will eventually overfit the subsets to the data. However, since
the external estimate is not used by the search, it is able to make better assessments of overfitting.
After resampling, this function determines the optimal number of generations for the GA.
Finally, the entire data set is used in the last execution of the genetic algorithm search and the final
model is built on the predictor subset that is associated with the optimal number of generations
determined by resampling (although the update function can be used to manually set the number of
generations).
This is an example of the output produced when gafsControl(verbose = TRUE) is used:
For the second resample (e.g. fold 2), the best subset across all individuals tested in the first genera-
tion contained 13 predictors and was associated with a fitness value of 0.715. The second generation
produced a better subset containing 17 samples with an associated fitness values of 0.737 (and im-
provement is symbolized by the *. The percentage listed is the Jaccard similarity between the
previous best individual (with 13 predictors) and the new best. The third generation did not produce
a better fitness value but the fourth generation did.
The search algorithm can be parallelized in several places:
It is probably best to pick one of these areas for parallelization and the first is likely to produces
the largest decrease in run-time since it is the least likely to incur multiple re-starting of the worker
processes. Keep in mind that if multiple levels of parallelization occur, this can effect the number
of workers and the amount of memory required exponentially.
Value
an object of class gafs
Author(s)
Max Kuhn, Luca Scrucca (for GA internals)
References
Kuhn M and Johnson K (2013), Applied Predictive Modeling, Springer, Chapter 19 http://
appliedpredictivemodeling.com
Scrucca L (2013). GA: A Package for Genetic Algorithms in R. Journal of Statistical Software,
53(4), 1-37. https://www.jstatsoft.org/article/view/v053i04
Mitchell M (1996), An Introduction to Genetic Algorithms, MIT Press.
http://en.wikipedia.org/wiki/Jaccard_index
See Also
gafsControl, predict.gafs, caretGA, rfGA treebagGA
Examples
## Not run:
set.seed(1)
train_data <- twoClassSim(100, noiseVars = 10)
test_data <- twoClassSim(10, noiseVars = 10)
## A short example
ctrl <- gafsControl(functions = rfGA,
method = "cv",
number = 3)
rf_search
## End(Not run)
58 gafsControl
Description
Control the computational nuances of the gafs and safs functions
Many of these options are the same as those described for trainControl. More extensive docu-
mentation and examples can be found on the caret website at http://topepo.github.io/caret/
feature-selection-using-genetic-algorithms.html#syntax and http://topepo.github.
io/caret/feature-selection-using-simulated-annealing.html#syntax.
The functions component contains the information about how the model should be fit and sum-
marized. It also contains the elements needed for the GA and SA modules (e.g. cross-over, etc).
The elements of functions that are the same for GAs and SAs are:
• fit, with arguments x, y, lev, last, and ..., is used to fit the classification or regression
model
• pred, with arguments object and x, predicts new samples
• fitness_intern, with arguments object, x, y, maximize, and p, summarizes performance
for the internal estimates of fitness
• fitness_extern, with arguments data, lev, and model, summarizes performance using the
externally held-out samples
• selectIter, with arguments x, metric, and maximize, determines the best search iteration
for feature selection.
The elements of functions specific to genetic algorithms are:
• initial, with arguments vars, popSize and ..., creates an initial population.
• selection, with arguments population, fitness, r, q, and ..., conducts selection of indi-
viduals.
• crossover, with arguments population, fitness, parents and ..., control genetic repro-
duction.
• mutation, with arguments population, parent and ..., adds mutations.
The elements of functions specific to simulated annealing are:
• initial, with arguments vars, prob, and ..., creates the initial subset.
• perturb, with arguments x, vars, and number, makes incremental changes to the subsets.
• prob, with arguments old, new, and iteration, computes the acceptance probabilities
The pages http://topepo.github.io/caret/feature-selection-using-genetic-algorithms.
html and http://topepo.github.io/caret/feature-selection-using-simulated-annealing.
html have more details about each of these functions.
holdout can be used to hold out samples for computing the internal fitness value. Note that this is
independent of the external resampling step. Suppose 10-fold CV is being used. Within a resam-
pling iteration, holdout can be used to sample an additional proportion of the 90% resampled data
gafsControl 59
to use for estimating fitness. This may not be a good idea unless you have a very large training set
and want to avoid an internal resampling procedure to estimate fitness.
The search algorithms can be parallelized in several places:
It is probably best to pick one of these areas for parallelization and the first is likely to produces
the largest decrease in run-time since it is the least likely to incur multiple re-starting of the worker
processes. Keep in mind that if multiple levels of parallelization occur, this can effect the number
of workers and the amount of memory required exponentially.
Usage
gafsControl(
functions = NULL,
method = "repeatedcv",
metric = NULL,
maximize = NULL,
number = ifelse(grepl("cv", method), 10, 25),
repeats = ifelse(grepl("cv", method), 1, 5),
verbose = FALSE,
returnResamp = "final",
p = 0.75,
index = NULL,
indexOut = NULL,
seeds = NULL,
holdout = 0,
genParallel = FALSE,
allowParallel = TRUE
)
safsControl(
functions = NULL,
method = "repeatedcv",
metric = NULL,
maximize = NULL,
number = ifelse(grepl("cv", method), 10, 25),
repeats = ifelse(grepl("cv", method), 1, 5),
verbose = FALSE,
returnResamp = "final",
p = 0.75,
index = NULL,
60 gafsControl
indexOut = NULL,
seeds = NULL,
holdout = 0,
improve = Inf,
allowParallel = TRUE
)
Arguments
functions a list of functions for model fitting, prediction etc (see Details below)
method The resampling method: boot, boot632, cv, repeatedcv, LOOCV, LGOCV (for
repeated training/test splits)
metric a two-element string that specifies what summary metric will be used to select
the optimal number of iterations from the external fitness value and which met-
ric should guide subset selection. If specified, this vector should have names
"internal" and "external". See gafs and/or safs for explanations of the
difference.
maximize a two-element logical: should the metrics be maximized or minimized? Like the
metric argument, this this vector should have names "internal" and "external".
number Either the number of folds or number of resampling iterations
repeats For repeated k-fold cross-validation only: the number of complete sets of folds
to compute
verbose a logical for printing results
returnResamp A character string indicating how much of the resampled summary metrics
should be saved. Values can be “all” or “none”
p For leave-group out cross-validation: the training percentage
index a list with elements for each resampling iteration. Each list element is the sample
rows used for training at that iteration.
indexOut a list (the same length as index) that dictates which sample are held-out for each
resample. If NULL, then the unique set of samples not contained in index is used.
seeds a vector or integers that can be used to set the seed during each search. The
number of seeds must be equal to the number of resamples plus one.
holdout the proportion of data in [0, 1) to be held-back from x and y to calculate the
internal fitness values
genParallel if a parallel backend is loaded and available, should gafs use it tp parallelize the
fitness calculations within a generation within a resample?
allowParallel if a parallel backend is loaded and available, should the function use it?
improve the number of iterations without improvement before safs reverts back to the
previous optimal subset
Value
An echo of the parameters specified
gafs_initial 61
Author(s)
Max Kuhn
References
http://topepo.github.io/caret/feature-selection-using-genetic-algorithms.html, http:
//topepo.github.io/caret/feature-selection-using-simulated-annealing.html
See Also
safs, safs, , caretGA, rfGA, treebagGA, caretSA, rfSA, treebagSA
Description
Built-in functions related to genetic algorithms
These functions are used with the functions argument of the gafsControl function. More infor-
mation on the details of these functions are at http://topepo.github.io/caret/feature-selection-using-genetic-a
html.
Most of the gafs_* functions are based on those from the GA package by Luca Scrucca. These
functions here are small re-writes to work outside of the GA package.
The objects caretGA, rfGA and treebagGA are example lists that can be used with the functions
argument of gafsControl.
In the case of caretGA, the ... structure of gafs passes through to the model fitting routine. As a
consequence, the train function can easily be accessed by passing important arguments belonging
to train to gafs. See the examples below. By default, using caretGA will used the resampled
performance estimates produced by train as the internal estimate of fitness.
For rfGA and treebagGA, the randomForest and bagging functions are used directly (i.e. train
is not used). Arguments to either of these functions can also be passed to them though the gafs call
(see examples below). For these two functions, the internal fitness is estimated using the out-of-bag
estimates naturally produced by those functions. While faster, this limits the user to accuracy or
Kappa (for classification) and RMSE and R-squared (for regression).
Usage
gafs_initial(vars, popSize, ...)
Arguments
vars number of possible predictors
popSize the population size passed into gafs
... not currently used
population a binary matrix of the current subsets with predictors in columns and individuals
in rows
fitness a vector of fitness values
r, q, k tuning parameters for the specific selection operator
parent, parents
integer(s) for which chromosomes are altered
Value
The return value depends on the function.
Author(s)
Luca Scrucca, gafs_initial, caretGA, rfGA and treebagGA by Max Kuhn
References
Scrucca L (2013). GA: A Package for Genetic Algorithms in R. Journal of Statistical Software,
53(4), 1-37.
https://cran.r-project.org/package=GA
http://topepo.github.io/caret/feature-selection-using-genetic-algorithms.html
See Also
gafs, gafsControl
Examples
## Not run:
## Hypothetical examples
lda_ga <- gafs(x = predictors,
y = classes,
gafsControl = gafsControl(functions = caretGA),
## now pass arguments to `train`
method = "lda",
metric = "Accuracy"
trControl = trainControl(method = "cv", classProbs = TRUE))
## End(Not run)
Description
Details
These data have two classes for the credit worthiness: good or bad. There are predictors related to
attributes, such as: checking account status, duration, credit history, purpose of the loan, amount
of the loan, savings accounts or bonds, employment duration, Installment rate in percentage of
disposable income, personal information, other debtors/guarantors, residence duration, property,
age, other installment plans, housing, number of existing credits, job information, Number of people
being liable to provide maintenance for, telephone, and foreign worker status.
Many of these predictors are discrete and have been expanded into several 0/1 indicator variables
Source
Description
Placeholder.
Usage
getSamplingInfo(method = NULL, regex = TRUE, ...)
Arguments
method Modeling method.
regex Whether to use regex matching.
... additional arguments to passed to grepl.
Details
Placeholder.
Value
A list
Description
These functions plot the resampling results for the candidate subset sizes evaluated during the re-
cursive feature elimination (RFE) process
Usage
## S3 method for class 'rfe'
ggplot(
data = NULL,
mapping = NULL,
metric = data$metric[1],
output = "layered",
...,
environment = NULL
)
Arguments
data an object of class rfe.
mapping, environment
unused arguments to make consistent with ggplot2 generic method
metric What measure of performance to plot. Examples of possible values are "RMSE",
"Rsquared", "Accuracy" or "Kappa". Other values can be used depending on
what metrics have been calculated.
output either "data", "ggplot" or "layered". The first returns a data frame while the
second returns a simple ggplot object with no layers. The third value returns a
plot with a set of layers.
... plot only: specifications to be passed to xyplot. The function automatically
sets some arguments (e.g. axis labels) but passing in values here will over-ride
the defaults.
x an object of class rfe.
Details
These plots show the average performance versus the subset sizes.
Value
a lattice or ggplot object
Note
We using a recipe as an input, there may be some subset sizes that are not well-replicated over
resamples. The ‘ggplot‘ method will only show subset sizes where at least half of the resamples
have associated results.
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
rfe, xyplot, ggplot
Examples
## Not run:
data(BloodBrain)
66 ggplot.train
x <- scale(bbbDescr[,-nearZeroVar(bbbDescr)])
x <- x[, -findCorrelation(cor(x), .8)]
x <- as.data.frame(x, stringsAsFactors = TRUE)
set.seed(1)
lmProfile <- rfe(x, logBBB,
sizes = c(2:25, 30, 35, 40, 45, 50, 55, 60, 65),
rfeControl = rfeControl(functions = lmFuncs,
number = 200))
plot(lmProfile)
plot(lmProfile, metric = "Rsquared")
ggplot(lmProfile)
## End(Not run)
Description
This function takes the output of a train object and creates a line or level plot using the lattice or
ggplot2 libraries.
Usage
## S3 method for class 'train'
ggplot(
data = NULL,
mapping = NULL,
metric = data$metric[1],
plotType = "scatter",
output = "layered",
nameInStrip = FALSE,
highlight = FALSE,
...,
environment = NULL
)
Arguments
data an object of class train.
mapping, environment
unused arguments to make consistent with ggplot2 generic method
metric What measure of performance to plot. Examples of possible values are "RMSE",
"Rsquared", "Accuracy" or "Kappa". Other values can be used depending on
what metrics have been calculated.
plotType a string describing the type of plot ("scatter", "level" or "line" (plot only))
output either "data", "ggplot" or "layered". The first returns a data frame while the
second returns a simple ggplot object with no layers. The third value returns a
plot with a set of layers.
nameInStrip a logical: if there are more than 2 tuning parameters, should the name and value
be included in the panel title?
highlight a logical: if TRUE, a diamond is placed around the optimal parameter setting for
models using grid search.
... plot only: specifications to be passed to levelplot, xyplot, stripplot (for
line plots). The function automatically sets some arguments (e.g. axis labels)
but passing in values here will over-ride the defaults
x an object of class train.
digits an integer specifying the number of significant digits used to label the parameter
value.
xTrans a function that will be used to scale the x-axis in scatter plots.
Details
If there are no tuning parameters, or none were varied, an error is produced.
If the model has one tuning parameter with multiple candidate values, a plot is produced showing
the profile of the results over the parameter. Also, a plot can be produced if there are multiple tuning
parameters but only one is varied.
If there are two tuning parameters with different values, a plot can be produced where a different
line is shown for each value of of the other parameter. For three parameters, the same line plot is
created within conditioning panels/facets of the other parameter.
Also, with two tuning parameters (with different values), a levelplot (i.e. un-clustered heatmap) can
be created. For more than two parameters, this plot is created inside conditioning panels/facets.
Author(s)
Max Kuhn
References
Kuhn (2008), “Building Predictive Models in R Using the caret” (http://www.jstatsoft.org/
article/view/v028i05/v28i05.pdf)
68 histogram.train
See Also
Examples
## Not run:
library(klaR)
rdaFit <- train(Species ~ .,
data = iris,
method = "rda",
control = trainControl(method = "cv"))
plot(rdaFit)
plot(rdaFit, plotType = "level")
ggplot(rdaFit) + theme_bw()
## End(Not run)
Description
A set of lattice functions are provided to plot the resampled performance estimates (e.g. classifica-
tion accuracy, RMSE) over tuning parameters (if any).
Usage
Arguments
Details
By default, only the resampling results for the optimal model are saved in the train object. The
function trainControl can be used to save all the results (see the example below).
If leave-one-out or out-of-bag resampling was specified, plots cannot be produced (see the method
argument of trainControl)
For xyplot and stripplot, the tuning parameter with the most unique values will be plotted on the
x-axis. The remaining parameters (if any) will be used as conditioning variables. For densityplot
and histogram, all tuning parameters are used for conditioning.
Using horizontal = FALSE in stripplot works.
Value
A lattice plot object
Author(s)
Max Kuhn
See Also
train, trainControl, histogram, densityplot, xyplot, stripplot
Examples
## Not run:
library(mlbench)
data(BostonHousing)
library(rpart)
rpartFit <- train(medv ~ .,
data = BostonHousing,
"rpart",
tuneLength = 9,
trControl = trainControl(
method = "boot",
returnResamp = "all"))
densityplot(rpartFit,
adjust = 1.25)
xyplot(rpartFit,
metric = "Rsquared",
type = c("p", "a"))
stripplot(rpartFit,
horizontal = FALSE,
jitter = TRUE)
70 icr.formula
## End(Not run)
Description
Usage
## Default S3 method:
icr(x, y, ...)
Arguments
Details
This produces a model analogous to Principal Components Regression (PCR) but uses Independent
Component Analysis (ICA) to produce the scores. The user must specify a value of n.comp to pass
to fastICA.
The function preProcess to produce the ICA scores for the original data and for newdata.
Value
For icr, a list with elements
Author(s)
Max Kuhn
See Also
fastICA, preProcess, lm
Examples
data(BloodBrain)
icrFit
predict(icrFit, bbbDescr[1:5,])
Description
The function performs the opposite of which converting a set of integers to a binary vector
Usage
index2vec(x, vars, sign = FALSE)
72 knn3
Arguments
x a vector of integers
vars the number of possible locations
sign a lgical; when true the data are encoded as -1/+1, and 0/1 otherwise
Value
a numeric vector
Author(s)
Max Kuhn
Examples
Description
$k$-nearest neighbour classification that can return class votes for all classes.
Usage
knn3(x, ...)
Arguments
x a matrix of training set predictors
... additional parameters to pass to knn3Train. However, passing prob = FALSE
will be over-ridden.
formula a formula of the form lhs ~ rhs where lhs is the response variable and rhs a
set of predictors.
data optional data frame containing the variables in the model formula.
subset optional vector specifying a subset of observations to be used.
na.action function which indicates what should happen when the data contain NAs.
k number of neighbours considered.
y a factor vector of training set classes
train matrix or data frame of training set cases.
test matrix or data frame of test set cases. A vector will be interpreted as a row
vector for a single case.
cl factor of true classifications of training set
l minimum vote for definite decision, otherwise doubt. (More precisely, less than
k-l dissenting votes are allowed, even if k is increased by ties.)
prob If this is true, the proportion of the votes for each class are returned as attribute
prob.
use.all controls handling of ties. If true, all distances equal to the kth largest are in-
cluded. If false, a random selection of distances equal to the kth is chosen to use
exactly k neighbours.
Details
knn3 is essentially the same code as ipredknn and knn3Train is a copy of knn. The underlying
C code from the class package has been modified to return the vote percentages for each class
(previously the percentage for the winning class was returned).
Value
An object of class knn3. See predict.knn3.
Author(s)
knn by W. N. Venables and B. D. Ripley and ipredknn by Torsten.Hothorn <[email protected]
erlangen.de>, modifications by Max Kuhn and Andre Williams
Examples
data(iris3)
train <- rbind(iris3[1:25,,1], iris3[1:25,,2], iris3[1:25,,3])
test <- rbind(iris3[26:50,,1], iris3[26:50,,2], iris3[26:50,,3])
cl <- factor(c(rep("s",25), rep("c",25), rep("v",25)))
knn3Train(train, test, cl, k = 5, prob = TRUE)
Description
$k$-nearest neighbour regression that can return the average value for the neighbours.
Usage
knnreg(x, ...)
## Default S3 method:
knnreg(x, ...)
Arguments
x a matrix or data frame of training set predictors.
... additional parameters to pass to knnregTrain.
formula a formula of the form lhs ~ rhs where lhs is the response variable and rhs a
set of predictors.
data optional data frame containing the variables in the model formula.
subset optional vector specifying a subset of observations to be used.
na.action function which indicates what should happen when the data contain NAs.
k number of neighbours considered.
learning_curve_dat 75
Details
knnreg is similar to ipredknn and knnregTrain is a modification of knn. The underlying C code
from the class package has been modified to return average outcome.
Value
An object of class knnreg. See predict.knnreg.
Author(s)
knn by W. N. Venables and B. D. Ripley and ipredknn by Torsten.Hothorn <[email protected]
erlangen.de>, modifications by Max Kuhn and Chris Keefer
Examples
data(BloodBrain)
Description
For a given model, this function fits several versions on different sizes of the total training set and
returns the results
76 learning_curve_dat
Usage
learning_curve_dat(
dat,
outcome = NULL,
proportion = (1:10)/10,
test_prop = 0,
verbose = TRUE,
...
)
Arguments
dat the training data
outcome a character string identifying the outcome column name
proportion the incremental proportions of the training set that are used to fit the model
test_prop an optional proportion of the data to be used to measure performance.
verbose a logical to print logs to the screen as models are fit
... options to pass to train to specify the model. These should not include x, y,
formula, or data. If trainControl is used here, do not use method = "none".
Details
This function creates a data set that can be used to plot how well the model performs over different
sized versions of the training set. For each data set size, the performance metrics are determined
and saved. If test_prop == 0, the apparent measure of performance (i.e. re-predicting the training
set) and the resampled estimate of performance are available. Otherwise, the test set results are also
added.
If the model being fit has tuning parameters, the results are based on the optimal settings determined
by train.
Value
a data frame with columns for each performance metric calculated by train as well as columns:
Training_Size the number of data points used in the current model fit
Data which data were used to calculate performance. Values are "Resampling", "Train-
ing", and (optionally) "Testing"
In the results, each data set size will have one row for the apparent error rate, one row for the test
set results (if used) and as many rows as resamples (e.g. 10 rows if 10-fold CV is used).
Author(s)
Max Kuhn
See Also
train
lift 77
Examples
## Not run:
set.seed(1412)
class_dat <- twoClassSim(1000)
set.seed(29510)
lda_data <-
learning_curve_dat(dat = class_dat,
outcome = "Class",
test_prop = 1/4,
## `train` arguments:
method = "lda",
metric = "ROC",
trControl = ctrl)
## End(Not run)
Description
For classification models, this function creates a ’lift plot’ that describes how well a model ranks
samples for one class
Usage
lift(x, ...)
## Default S3 method:
lift(x, ...)
lattice.options = NULL,
cuts = NULL,
labels = NULL,
...
)
Arguments
x a lattice formula (see xyplot for syntax) where the left-hand side of the for-
mula is a factor class variable of the observed outcome and the right-hand side
specifies one or model columns corresponding to a numeric ranking variable for
a model (e.g. class probabilities). The classification variable should have two
levels.
... options to pass through to xyplot or the panel function (not used in lift.formula).
data For lift.formula, a data frame (or more precisely, anything that is a valid
envir argument in eval, e.g., a list or an environment) containing values for
any variables in the formula, as well as groups and subset if applicable. If
not found in data, or if data is unspecified, the variables are looked for in the
environment of the formula. This argument is not used for xyplot.lift or
ggplot.lift.
class a character string for the class of interest
subset An expression that evaluates to a logical or integer indexing vector. It is evalu-
ated in data. Only the resulting rows of data are used for the plot.
lattice.options
A list that could be supplied to lattice.options
cuts If a single value is given, a sequence of values between 0 and 1 are created with
length cuts. If a vector, these values are used as the cuts. If NULL, each unique
value of the model prediction is used. This is helpful when the data set is large.
labels A named list of labels for keys. The list should have an element for each term
on the right-hand side of the formula and the names should match the names of
the models.
lift 79
plot Either "gain" (the default) or "lift". The former plots the number of samples
called events versus the event rate while the latter shows the event cut-off versus
the lift statistic.
values A vector of numbers between 0 and 100 specifying reference values for the
percentage of samples found (i.e. the y-axis). Corresponding points on the x-
axis are found via interpolation and line segments are shown to indicate how
many samples must be tested before these percentages are found. The lines
use either the plot.line or superpose.line component of the current lattice
theme to draw the lines (depending on whether groups were used. These values
are only used when type = "gain".
mapping, environment
Not used (required for ggplot consistency).
Details
lift.formula is used to process the data and xyplot.lift is used to create the plot.
To construct data for the the lift and gain plots, the following steps are used for each model:
1. The data are ordered by the numeric model prediction used on the right-hand side of the model
formula
2. Each unique value of the score is treated as a cut point
3. The number of samples with true results equal to class are determined
4. The lift is calculated as the ratio of the percentage of samples in each split corresponding to
class over the same percentage in the entire data set
lift with plot = "gain" produces a plot of the cumulative lift values by the percentage of samples
evaluated while plot = "lift" shows the cut point value versus the lift statistic.
This implementation uses the lattice function xyplot, so plot elements can be changed via panel
functions, trellis.par.set or other means. lift uses the panel function panel.lift2 by default,
but it can be changes using update.trellis (see the examples in panel.lift2).
The following elements are set by default in the plot but can be changed by passing new values
into xyplot.lift: xlab = "% Samples Tested", ylab = "% Samples Found", type = "S", ylim =
extendrange(c(0,100)) and xlim = extendrange(c(0,100)).
Value
lift.formula returns a list with elements:
Author(s)
Max Kuhn, some lattice code and documentation by Deepayan Sarkar
See Also
xyplot, trellis.par.set
Examples
set.seed(1)
simulated <- data.frame(obs = factor(rep(letters[1:2], each = 100)),
perfect = sort(runif(200), decreasing = TRUE),
random = runif(200))
Description
Functions to create a sub-sample by maximizing the dissimilarity between new samples and the
existing subset.
Usage
maxDissim(
a,
b,
n = 2,
obj = minDiss,
useNames = FALSE,
randomFrac = 1,
verbose = FALSE,
...
)
maxDissim 81
minDiss(u)
sumDiss(u)
Arguments
a a matrix or data frame of samples to start
b a matrix or data frame of samples to sample from
n the size of the sub-sample
obj an objective function to measure overall dissimilarity
useNames a logical: should the function return the row names (as opposed ot the row index)
randomFrac a number in (0, 1] that can be used to sub-sample from the remaining candidate
values
verbose a logical; should each step be printed?
... optional arguments to pass to dist
u a vector of dissimilarities
Details
Given an initial set of m samples and a larger pool of n samples, this function iteratively adds points
to the smaller set by finding with of the n samples is most dissimilar to the initial set. The argument
obj measures the overall dissimilarity between the initial set and a candidate point. For example,
maximizing the minimum or the sum of the m dissimilarities are two common approaches.
This algorithm tends to select points on the edge of the data mainstream and will reliably select
outliers. To select more samples towards the interior of the data set, set randomFrac to be small
(see the examples below).
Value
a vector of integers or row names (depending on useNames) corresponding to the rows of b that
comprise the sub-sample.
Author(s)
Max Kuhn <[email protected]>
References
Willett, P. (1999), "Dissimilarity-Based Algorithms for Selecting Structurally Diverse Sets of Com-
pounds," Journal of Computational Biology, 6, 447-457.
See Also
dist
82 maxDissim
Examples
plot(
tmp[-newSamp,],
xlim = extendrange(tmp[,1]), ylim = extendrange(tmp[,2]),
col = "darkgrey",
xlab = "variable 1", ylab = "variable 2")
points(base, pch = 16, cex = .7)
par(mfrow=c(2,2))
set.seed(414)
example(1, minDiss)
title("No Random Sampling, Min Score")
set.seed(414)
example(.1, minDiss)
title("10 Pct Random Sampling, Min Score")
set.seed(414)
example(1, sumDiss)
title("No Random Sampling, Sum Score")
set.seed(414)
example(.1, sumDiss)
title("10 Pct Random Sampling, Sum Score")
mdrr 83
Description
Svetnik et al. (2003) describe these data: "Bakken and Jurs studied a set of compounds originally
discussed by Klopman et al., who were interested in multidrug resistance reversal (MDRR) agents.
The original response variable is a ratio measuring the ability of a compound to reverse a leukemia
cell’s resistance to adriamycin. However, the problem was treated as a classification problem, and
compounds with the ratio >4.2 were considered active, and those with the ratio <= 2.0 were con-
sidered inactive. Compounds with the ratio between these two cutoffs were called moderate and
removed from the data for twoclass classification, leaving a set of 528 compounds (298 actives
and 230 inactives). (Various other arrangements of these data were examined by Bakken and Jurs,
but we will focus on this particular one.) We did not have access to the original descriptors, but
we generated a set of 342 descriptors of three different types that should be similar to the original
descriptors, using the DRAGON software."
Details
The data and R code are in the Supplemental Data file for the article.
Value
mdrrDescr the descriptors
mdrrClass the categorical outcome ("Active" or "Inactive")
Source
Svetnik, V., Liaw, A., Tong, C., Culberson, J. C., Sheridan, R. P. Feuston, B. P (2003). Random
Forest: A Classification and Regression Tool for Compound Classification and QSAR Modeling,
Journal of Chemical Information and Computer Sciences, Vol. 43, pg. 1947-1958.
Description
These function show information about models and packages that are accessible via train
Usage
modelLookup(model = NULL)
checkInstall(pkg)
Arguments
model a character string associated with the method argument of train. If no value is
passed, all models are returned. For getModelInfo, regular expressions can be
used.
pkg a character string of package names.
regex a logical: should a regular expressions be used? If FALSE, a simple match is
conducted against the whole name of the model.
... options to pass to grepl
Details
modelLookup is good for getting information related to the tuning parameters for a model. getModelInfo
will return all the functions and metadata associated with a model. Both of these functions will only
search within the models bundled in this package.
checkInstall will check to see if packages are installed. If they are not and the session is interac-
tive, an option is given to install the packages using install.packages using that functions default
arguments (the missing packages are listed if you would like to install them with other options). If
the session is not interactive, an error is thrown.
Value
modelLookup produces a data frame with columns
getModelInfo returns a list containing one or more lists of the standard model information.
checkInstall returns not value.
Note
The column seq is no longer included in the output of modelLookup.
Author(s)
Max Kuhn
See Also
train, install.packages, grepl
nearZeroVar 85
Examples
## Not run:
modelLookup()
modelLookup("gbm")
getModelInfo("pls")
getModelInfo("^pls")
getModelInfo("pls", regex = FALSE)
checkInstall(getModelInfo("pls")$library)
## End(Not run)
Description
nearZeroVar diagnoses predictors that have one unique value (i.e. are zero variance predictors)
or predictors that are have both of the following characteristics: they have very few unique values
relative to the number of samples and the ratio of the frequency of the most common value to the
frequency of the second most common value is large. checkConditionalX looks at the distribution
of the columns of x conditioned on the levels of y and identifies columns of x that are sparse within
groups of y.
Usage
nearZeroVar(
x,
freqCut = 95/5,
uniqueCut = 10,
saveMetrics = FALSE,
names = FALSE,
foreach = FALSE,
allowParallel = TRUE
)
checkConditionalX(x, y)
checkResamples(index, x, y)
Arguments
x a numeric vector or matrix, or a data frame with all numeric data
freqCut the cutoff for the ratio of the most common value to the second most common
value
86 nearZeroVar
uniqueCut the cutoff for the percentage of distinct values out of the number of total samples
saveMetrics a logical. If false, the positions of the zero- or near-zero predictors is returned.
If true, a data frame with predictor information is returned.
names a logical. If false, column indexes are returned. If true, column names are
returned.
foreach should the foreach package be used for the computations? If TRUE, less memory
should be used.
allowParallel should the parallel processing via the foreach package be used for the computa-
tions? If TRUE, more memory will be used but execution time should be shorter.
y a factor vector with at least two levels
index a list. Each element corresponds to the training set samples in x for a given
resample
Details
For example, an example of near zero variance predictor is one that, for 1000 samples, has two
distinct values and 999 of them are a single value.
To be flagged, first the frequency of the most prevalent value over the second most frequent value
(called the “frequency ratio”) must be above freqCut. Secondly, the “percent of unique values,” the
number of unique values divided by the total number of samples (times 100), must also be below
uniqueCut.
In the above example, the frequency ratio is 999 and the unique value percentage is 0.0001.
Checking the conditional distribution of x may be needed for some models, such as naive Bayes
where the conditional distributions should have at least one data point within a class.
nzv is the original version of the function.
Value
For nearZeroVar: if saveMetrics = FALSE, a vector of integers corresponding to the column po-
sitions of the problematic predictors. If saveMetrics = TRUE, a data frame with columns:
freqRatio the ratio of frequencies for the most common value over the second most com-
mon value
percentUnique the percentage of unique data points out of the total number of data points
zeroVar a vector of logicals for whether the predictor has only one distinct value
nzv a vector of logicals for whether the predictor is a near zero variance predictor
Author(s)
Max Kuhn, with speed improvements to nearZeroVar by Allan Engelhardt
negPredValue 87
Examples
data(BloodBrain)
nearZeroVar(bbbDescr)
nearZeroVar(bbbDescr, names = TRUE)
set.seed(1)
classes <- factor(rep(letters[1:3], each = 30))
x <- data.frame(x1 = rep(c(0, 1), 45),
x2 = c(rep(0, 10), rep(1, 80)))
checkResamples(folds, x, classes)
Description
These functions calculate the sensitivity, specificity or predictive values of a measurement system
compared to a reference results (the truth or a gold standard). The measurement and "truth" data
must have the same two possible outcomes and one of the outcomes must be thought of as a "posi-
tive" results.
Usage
negPredValue(data, ...)
## Default S3 method:
negPredValue(
data,
reference,
negative = levels(reference)[2],
prevalence = NULL,
...
)
88 negPredValue
posPredValue(data, ...)
## Default S3 method:
posPredValue(
data,
reference,
positive = levels(reference)[1],
prevalence = NULL,
...
)
sensitivity(data, ...)
## Default S3 method:
sensitivity(
data,
reference,
positive = levels(reference)[1],
na.rm = TRUE,
...
)
Arguments
data for the default functions, a factor containing the discrete measurements. For the
table or matrix functions, a table or matric object, respectively.
... not currently used
reference a factor containing the reference values
negPredValue 89
negative a character string that defines the factor level corresponding to the "negative"
results
prevalence a numeric value for the rate of the "positive" class of the data
positive a character string that defines the factor level corresponding to the "positive"
results
na.rm a logical value indicating whether NA values should be stripped before the com-
putation proceeds
Details
The sensitivity is defined as the proportion of positive results out of the number of samples which
were actually positive. When there are no positive results, sensitivity is not defined and a value of
NA is returned. Similarly, when there are no negative results, specificity is not defined and a value
of NA is returned. Similar statements are true for predictive values.
The positive predictive value is defined as the percent of predicted positives that are actually positive
while the negative predictive value is defined as the percent of negative positives that are actually
negative.
Suppose a 2x2 table with notation
Reference
Predicted Event No Event
Event A B
No Event C D
P revalence = (A + C)/(A + B + C + D)
Value
Author(s)
Max Kuhn
90 negPredValue
References
Kuhn, M. (2008), “Building predictive models in R using the caret package, ” Journal of Statistical
Software, (http://www.jstatsoft.org/article/view/v028i05/v28i05.pdf).
Altman, D.G., Bland, J.M. (1994) “Diagnostic tests 1: sensitivity and specificity,” British Medical
Journal, vol 308, 1552.
Altman, D.G., Bland, J.M. (1994) “Diagnostic tests 2: predictive values,” British Medical Journal,
vol 309, 102.
See Also
confusionMatrix
Examples
## Not run:
###################
## 2 class example
sensitivity(pred, truth)
sensitivity(xtab)
posPredValue(pred, truth)
posPredValue(pred, truth, prevalence = 0.25)
specificity(pred, truth)
negPredValue(pred, truth)
negPredValue(xtab)
negPredValue(pred, truth, prevalence = 0.25)
plot(prev, ppvVals,
ylim = c(0, 1),
nullModel 91
type = "l",
ylab = "",
xlab = "Prevalence (i.e. prior)")
points(prev, npvVals, type = "l", col = "red")
abline(h=sensitivity(pred, truth), lty = 2)
abline(h=specificity(pred, truth), lty = 2, col = "red")
legend(.5, .5,
c("ppv", "npv", "sens", "spec"),
col = c("black", "red", "black", "red"),
lty = c(1, 1, 2, 2))
###################
## 3 class example
library(MASS)
## End(Not run)
Description
Fit a single mean or largest class model
Usage
nullModel(x, ...)
## Default S3 method:
nullModel(x = NULL, y, ...)
Arguments
x An optional matrix or data frame of predictors. These values are not used in the
model fit
... Optional arguments (not yet used)
y A numeric vector (for regression) or factor (for classification) of outcomes
object An object of class nullModel
newdata A matrix or data frame of predictors (only used to determine the number of
predictions to return)
type Either "raw" (for regression), "class" or "prob" (for classification)
Details
nullModel emulates other model building functions, but returns the simplest model possible given a
training set: a single mean for numeric outcomes and the most prevalent class for factor outcomes.
When class probabilities are requested, the percentage of the training set samples with the most
prevalent class is returned.
Value
The output of nullModel is a list of class nullModel with elements
predict.nullModel returns a either a factor or numeric vector depending on the class of y. All
predictions are always the same.
Examples
Description
Fatty acid concentrations of commercial oils were measured using gas chromatography. The data is
used to predict the type of oil. Note that only the known oils are in the data set. Also, the authors
state that there are 95 samples of known oils. However, we count 96 in Table 1 (pgs. 33-35).
Value
fattyAcids data frame of fatty acid compositions: Palmitic, Stearic, Oleic, Linoleic, Linolenic,
Eicosanoic and Eicosenoic. When values fell below the lower limit of the assay
(denoted as <X in the paper), the limit was used.
oilType factor of oil types: pumpkin (A), sunflower (B), peanut (C), olive (D), soybean
(E), rapeseed (F) and corn (G).
Source
Brodnjak-Voncina et al. (2005). Multivariate data analysis in classification of vegetable oils char-
acterized by the content of fatty acids, Chemometrics and Intelligent Laboratory Systems, Vol.
75:31-45.
Description
Various functions for setting tuning parameters
Usage
oneSE(x, metric, num, maximize)
Arguments
x a data frame of tuning parameters and model results, sorted from least complex
models to the mst complex
metric a string that specifies what summary metric will be used to select the optimal
model. By default, possible values are "RMSE" and "Rsquared" for regression
and "Accuracy" and "Kappa" for classification. If custom performance metrics
are used (via the summaryFunction argument in trainControl, the value of
metric should match one of the arguments. If it does not, a warning is issued
and the first metric given by the summaryFunction is used.
94 oneSE
Details
These functions can be used by train to select the "optimal" model from a series of models. Each
requires the user to select a metric that will be used to judge performance. For regression models,
values of "RMSE" and "Rsquared" are applicable. Classification models use either "Accuracy" or
"Kappa" (for unbalanced class distributions.
More details on these functions can be found at http://topepo.github.io/caret/model-training-and-tuning.
html#custom.
By default, train uses best.
best simply chooses the tuning parameter associated with the largest (or lowest for "RMSE") per-
formance.
oneSE is a rule in the spirit of the "one standard error" rule of Breiman et al. (1984), who suggest
that the tuning parameter associated with the best performance may over fit. They suggest that
the simplest model within one standard error of the empirically optimal model is the better choice.
This assumes that the models can be easily ordered from simplest to most complex (see the Details
section below).
tolerance takes the simplest model that is within a percent tolerance of the empirically optimal
model. For example, if the largest Kappa value is 0.5 and a simpler model within 3 percent is
acceptable, we score the other models using (x -0.5)/0.5 * 100. The simplest model whose score
is not less than 3 is chosen (in this case, a model with a Kappa value of 0.35 is acceptable).
User-defined functions can also be used. The argument selectionFunction in trainControl can
be used to pass the function directly or to pass the function by name.
Value
a row index
Note
In many cases, it is not very clear how to order the models on simplicity. For simple trees and other
models (such as PLS), this is straightforward. However, for others it is not.
For example, many of the boosting models used by caret have parameters for the number of boost-
ing iterations and the tree complexity (others may also have a learning rate parameter). In this
implementation, we order models on number of iterations, then tree depth. Clearly, this is arguable
(please email the author for suggestions though).
For MARS models, they are orders on the degree of the features, then the number of retained terms.
RBF SVM models are ordered first by the cost parameter, then by the kernel parameter while poly-
nomial models are ordered first on polynomial degree, then cost and scale.
Neural networks are ordered by the number of hidden units and then the amount of weight decay.
k-nearest neighbor models are ordered from most neighbors to least (i.e. smoothest to model jagged
decision boundaries).
Elastic net models are ordered first on the L1 penalty, then by the L2 penalty.
panel.lift2 95
Author(s)
Max Kuhn
References
Breiman, Friedman, Olshen, and Stone. (1984) Classification and Regression Trees. Wadsworth.
See Also
train, trainControl
Examples
## Not run:
# simulate a PLS regression model
test <- data.frame(ncomp = 1:5,
RMSE = c(3, 1.1, 1.02, 1, 2),
RMSESD = .4)
data(BloodBrain)
set.seed(1)
marsFit <- train(bbbDescr, logBBB,
method = "earth",
tuneGrid = marsGrid,
trControl = trainControl(method = "cv",
number = 10,
selectionFunction = "tolerance"))
## End(Not run)
Description
Two panel functions that be used in conjunction with lift.
96 panel.lift2
Usage
panel.lift2(x, y, pct = 0, values = NULL, ...)
Arguments
x the percentage of searched to be plotted in the scatterplot
y the percentage of events found to be plotted in the scatterplot
pct the baseline percentage of true events in the data
values A vector of numbers between 0 and 100 specifying reference values for the
percentage of samples found (i.e. the y-axis). Corresponding points on the x-
axis are found via interpolation and line segments are shown to indicate how
many samples must be tested before these percentages are found. The lines
use either the plot.line or superpose.line component of the current lattice
theme to draw the lines (depending on whether groups were used
... options to pass to panel.xyplot
Details
panel.lift plots the data with a simple (black) 45 degree reference line.
panel.lift2 is the default for lift and plots the data points with a shaded region encompassing
the space between to the random model and perfect model trajectories. The color of the region is
determined by the lattice reference.line information (see example below).
Author(s)
Max Kuhn
See Also
lift, panel.xyplot, xyplot, trellis.par.set
Examples
set.seed(1)
simulated <- data.frame(obs = factor(rep(letters[1:2], each = 100)),
perfect = sort(runif(200), decreasing = TRUE),
random = runif(200))
Description
A variation of panel.dotplot that plots horizontal lines from zero to the data point.
Usage
panel.needle(
x,
y,
horizontal = TRUE,
pch = if (is.null(groups)) dot.symbol$pch else sup.symbol$pch,
col = if (is.null(groups)) dot.symbol$col else sup.symbol$col,
lty = dot.line$lty,
lwd = dot.line$lwd,
col.line = dot.line$col,
levels.fos = NULL,
groups = NULL,
...
)
Arguments
x, y variables to be plotted in the panel. Typically y is the ’factor’
horizontal logical. If FALSE, the plot is ‘transposed’ in the sense that the behaviours of
x and y are switched. x is now the ‘factor’. Interpretation of other arguments
change accordingly. See documentation of bwplot for a fuller explanation.
pch, col, lty, lwd, col.line
graphical parameters
levels.fos locations where reference lines will be drawn
groups grouping variable (affects graphical parameters)
... extra parameters, passed to panel.xyplot which is responsible for drawing the
foreground points (panel.dotplot only draws the background reference lines).
Details
Creates (possibly grouped) needleplot of x against y or vice versa
Author(s)
Max Kuhn, based on panel.dotplot by Deepayan Sarkar
See Also
dotplot
98 pcaNNet
Description
Run PCA on a dataset, then use it in a neural network model
Usage
pcaNNet(x, ...)
## Default S3 method:
pcaNNet(x, y, thresh = 0.99, ...)
Arguments
x matrix or data frame of x values for examples.
... arguments passed to nnet, such as size, decay, etc.
formula A formula of the form class ~ x1 + x2 + ...{}
data Data frame from which variables specified in formula are preferentially to be
taken.
weights (case) weights for each example - if missing defaults to 1.
thresh a threshold for the cumulative proportion of variance to capture from the PCA
analysis. For example, to retain enough PCA components to capture 95 percent
of variation, set thresh = .95
subset An index vector specifying the cases to be used in the training sample. (NOTE:
If given, this argument must be named.)
pcaNNet 99
na.action A function to specify the action to be taken if NAs are found. The default action
is for the procedure to fail. An alternative is na.omit, which leads to rejection
of cases with missing values on any required variable. (NOTE: If given, this
argument must be named.)
contrasts a list of contrasts to be used for some or all of the factors appearing as variables
in the model formula.
y matrix or data frame of target values for examples.
object an object of class pcaNNet as returned by pcaNNet.
newdata matrix or data frame of test examples. A vector is considered to be a row vector
comprising a single case.
type Type of output
Details
The function first will run principal component analysis on the data. The cumulative percentage
of variance is computed for each principal component. The function uses the thresh argument
to determine how many components must be retained to capture this amount of variance in the
predictors.
The principal components are then used in a neural network model.
When predicting samples, the new data are similarly transformed using the information from the
PCA analysis on the training data and then predicted.
Because the variance of each predictor is used in the PCA analysis, the code does a quick check to
make sure that each predictor has at least two distinct values. If a predictor has one unique value, it
is removed prior to the analysis.
Value
For pcaNNet, an object of "pcaNNet" or "pcaNNet.formula". Items of interest in the output are:
Author(s)
These are heavily based on the nnet code from Brian Ripley.
References
Ripley, B. D. (1996) Pattern Recognition and Neural Networks. Cambridge.
See Also
nnet, preProcess
100 pickSizeBest
Examples
data(BloodBrain)
modelFit <- pcaNNet(bbbDescr[, 1:10], logBBB, size = 5, linout = TRUE, trace = FALSE)
modelFit
Description
Ancillary functions for backwards selection
Usage
pickSizeBest(x, metric, maximize)
pickVars(y, size)
caretFuncs
ldaFuncs
treebagFuncs
gamFuncs
rfFuncs
lmFuncs
nbFuncs
lrFuncs
Arguments
x a matrix or data frame with the performance metric of interest
metric a character string with the name of the performance metric that should be used
to choose the appropriate number of variables
maximize a logical; should the metric be maximized?
pickSizeBest 101
tol a scalar to denote the acceptable difference in optimal performance (see Details
below)
y a list of data frames with variables Overall and var
size an integer for the number of variables to retain
Format
An object of class list of length 6.
Details
This page describes the functions that are used in backwards selection (aka recursive feature elim-
ination). The functions described here are passed to the algorithm via the functions argument of
rfeControl.
See rfeControl for details on how these functions should be defined.
The ’pick’ functions are used to find the appropriate subset size for different situations. pickBest
will find the position associated with the numerically best value (see the maximize argument to help
define this).
pickSizeTolerance picks the lowest position (i.e. the smallest subset size) that has no more of
an X percent loss in performances. When maximizing, it calculates (O-X)/O*100, where X is the
set of performance values and O is max(X). This is the percent loss. When X is to be minimized,
it uses (X-O)/O*100 (so that values greater than X have a positive "loss"). The function finds the
smallest subset size that has a percent loss less than tol.
Both of the ’pick’ functions assume that the data are sorted from smallest subset size to largest.
Author(s)
Max Kuhn
See Also
rfeControl, rfe
Examples
cat("numerically optimal:",
example$RMSE[absoluteBest],
"RMSE in position",
absoluteBest, "\n")
cat("Accepting a 1.5 pct loss:",
example$RMSE[within5Pct],
"RMSE in position",
within5Pct, "\n")
cat("numerically optimal:",
example2$Rsquared[absoluteBest2],
"R^2 in position",
absoluteBest2, "\n")
cat("Accepting a 1.5 pct loss:",
example2$Rsquared[within5Pct2],
"R^2 in position",
within5Pct2, "\n")
Description
Plot the performance values versus search iteration
Usage
## S3 method for class 'gafs'
plot(
x,
metric = x$control$metric["external"],
estimate = c("internal", "external"),
output = "ggplot",
...
)
plot.gafs 103
Arguments
x an object of class gafs or safs
metric the measure of performance to plot (e.g. RMSE, accuracy, etc)
estimate the type of estimate: either "internal" or "external"
output either "data", "ggplot" or "lattice"
... For plot methods, these are options passed to xyplot. For ggplot methods,
they are not used.
data, mapping, environment
kept for consistency with ggplot and are not used here.
Details
The mean (averaged over the resamples) is plotted against the search iteration using a scatter plot.
When output = "data", the unaveraged data are returned with columns for all the performance
metrics and the resample indicator.
Value
Either a data frame, ggplot object or lattice object
Author(s)
Max Kuhn
See Also
gafs, safs, ggplot, xyplot
Examples
## Not run:
set.seed(1)
train_data <- twoClassSim(100, noiseVars = 10)
test_data <- twoClassSim(10, noiseVars = 10)
## A short example
ctrl <- safsControl(functions = rfSA,
method = "cv",
number = 3)
y = train_data$Class,
iters = 50,
safsControl = ctrl)
plot(rf_search)
plot(rf_search,
output = "lattice",
auto.key = list(columns = 2))
## End(Not run)
Description
This function produces lattice and ggplot plots of objects with class "varImp.train". More info will
be forthcoming.
Usage
## S3 method for class 'varImp.train'
plot(x, top = dim(x$importance)[1], ...)
Arguments
x, data an object with class varImp.
top a scalar numeric that specifies the number of variables to be displayed (in order
of importance)
... arguments to pass to the lattice plot function (dotplot and panel.needle)
mapping, environment
unused arguments to make consistent with ggplot2 generic method
plotClassProbs 105
Details
For models where there is only one importance value, such a regression models, a "Pareto-type"
plot is produced where the variables are ranked by their importance and a needle-plot is used to
show the top variables. Horizontal bar charts are used for ggplot.
When there is more than one importance value per predictor, the same plot is produced within
conditioning panels for each class. The top predictors are sorted by their average importance.
Value
a lattice plot object
Author(s)
Max Kuhn
Description
This function takes an object (preferably from the function extractProb) and creates a lattice plot.
Usage
plotClassProbs(object, plotType = "histogram", useObjects = FALSE, ...)
Arguments
object an object (preferably from the function extractProb. There should be columns
for each level of the class factor and columns named obs, pred, model (e.g.
"rpart", "nnet" etc), dataType (e.g. "Training", "Test" etc) and optionally objects
(for giving names to objects with the same model type).
plotType either "histogram" or "densityplot"
useObjects a logical; should the object name (if any) be used as a conditioning variable?
... parameters to pass to histogram or densityplot
Details
If the call to extractProb included test data, these data are shown, but if unknowns were also
included, these are not plotted
Value
A lattice object. Note that the plot has to be printed to be displayed (especially in a loop).
106 plotObsVsPred
Author(s)
Max Kuhn
Examples
## Not run:
data(mdrr)
set.seed(90)
inTrain <- createDataPartition(mdrrClass, p = .5)[[1]]
## End(Not run)
Description
This function takes an object (preferably from the function extractPrediction) and creates a
lattice plot. For numeric outcomes, the observed and predicted data are plotted with a 45 degree
reference line and a smoothed fit. For factor outcomes, a dotplot plot is produced with the accuracies
for the different models.
Usage
plotObsVsPred(object, equalRanges = TRUE, ...)
Arguments
object an object (preferably from the function extractPrediction. There should be
columns named obs, pred, model (e.g. "rpart", "nnet" etc.) and dataType (e.g.
"Training", "Test" etc)
equalRanges a logical; should the x- and y-axis ranges be the same?
... parameters to pass to xyplot or dotplot, such as auto.key
Details
If the call to extractPrediction included test data, these data are shown, but if unknowns were
also included, they are not plotted
Value
A lattice object. Note that the plot has to be printed to be displayed (especially in a loop).
Author(s)
Max Kuhn
Examples
## Not run:
# regression example
data(BostonHousing)
rpartFit <- train(BostonHousing[1:100, -c(4, 14)],
BostonHousing$medv[1:100],
"rpart", tuneLength = 9)
plsFit <- train(BostonHousing[1:100, -c(4, 14)],
BostonHousing$medv[1:100],
"pls")
plotObsVsPred(predVals)
108 plsda
#classification example
data(Satellite)
numSamples <- dim(Satellite)[1]
set.seed(716)
plotObsVsPred(predTargets)
## End(Not run)
plsda Partial Least Squares and Sparse Partial Least Squares Discriminant
Analysis
Description
plsda is used to fit standard PLS models for classification while splsda performs sparse PLS that
embeds feature selection and regularization for the same purpose.
Usage
plsda(x, ...)
plsda 109
## Default S3 method:
plsda(x, y, ncomp = 2, probMethod = "softmax", prior = NULL, ...)
Arguments
x a matrix or data frame of predictors
... arguments to pass to plsr or spls. For splsda, this is the method for passing
tuning parameters specifications (e.g. K, eta or kappa)
object an object produced by plsda
newdata a matrix or data frame of predictors
ncomp the number of components to include in the model. Predictions can be made for
models with values less than ncomp.
type either "class", "prob" or "raw" to produce the predicted class, class probabil-
ities or the raw model scores, respectively.
y a factor or indicator matrix for the discrete outcome. If a matrix, the entries must
be either 0 or 1 and rows must sum to one
probMethod either "softmax" or "Bayes" (see Details)
prior a vector or prior probabilities for the classes (only used for probeMethod =
"Bayes")
Details
If a factor is supplied, the appropriate indicator matrix is created.
A multivariate PLS model is fit to the indicator matrix using the plsr or spls function.
Two prediction methods can be used.
The softmax function transforms the model predictions to "probability-like" values (e.g. on [0, 1]
and sum to 1). The class with the largest class probability is the predicted class.
Also, Bayes rule can be applied to the model predictions to form posterior probabilities. Here,
the model predictions for the training set are used along with the training set outcomes to create
conditional distributions for each class. When new samples are predicted, the raw model predictions
are run through these conditional distributions to produce a posterior probability for each class
(along with the prior). This process is repeated ncomp times for every possible PLS model. The
NaiveBayes function is used with usekernel = TRUE for the posterior probability calculations.
Value
For plsda, an object of class "plsda" and "mvr". For splsda, an object of class splsda.
The predict methods produce either a vector, matrix or three-dimensional array, depending on the
values of type of ncomp. For example, specifying more than one value of ncomp with type =
"class" with produce a three dimensional array but the default specification would produce a
factor vector.
110 plsda
See Also
plsr, spls
Examples
## Not run:
data(mdrr)
set.seed(1)
inTrain <- sample(seq(along = mdrrClass), 450)
confusionMatrix(predict(useBayes, testDescr),
testMDRR)
confusionMatrix(predict(useSoftmax, testDescr),
testMDRR)
## Using spls:
## (As of 11/09, the spls package now has a similar function with
## the same mane. To avoid conflicts, use caret:::splsda to
## get this version)
K = 5, eta = .9,
probMethod = "Bayes")
confusionMatrix(caret:::predict.splsda(splsFit, testDescr),
testMDRR)
## End(Not run)
Description
Value
Source
R. G. Brereton (2003). Chemometrics: Data Analysis for the Laboratory and Chemical Plant, pg.
261.
Description
Performs a principal components analysis on an object of class resamples and returns the results
as an object with classes prcomp.resamples and prcomp.
Usage
Arguments
x For prcomp, an object of class resamples and for plot.prcomp.resamples, an
object of class plot.prcomp.resamples
metric a performance metric that was estimated for every resample
... For prcomp.resamples, options to pass to prcomp, for plot.prcomp.resamples,
options to pass to Lattice objects (see Details below) and, for cluster.resamples,
options to pass to hclust.
what the type of plot: "scree" produces a bar chart of standard deviations, "cumulative"
produces a bar chart of the cumulative percent of variance, "loadings" pro-
duces a scatterplot matrix of the loading values and "components" produces a
scatterplot matrix of the PCA components
dims The number of dimensions to plot when what = "loadings" or what = "components"
Details
The principal components analysis treats the models as variables and the resamples are realizations
of the variables. In this way, we can use PCA to "cluster" the assays and look for similarities. Most
of the methods for prcomp can be used, although custom print and plot methods are used.
The plot method uses lattice graphics. When what = "scree" or what = "cumulative", barchart
is used. When what = "loadings" or what = "components", either xyplot or splom are used (the
latter when dims > 2). Options can be passed to these methods using ....
When what = "loadings" or what = "components", the plots are put on a common scale so that
later components are less likely to be over-interpreted. See Geladi et al. (2003) for examples of
why this can be important.
For clustering, hclust is used to determine clusters of models based on the resampled performance
values.
Value
For prcomp.resamples, an object with classes prcomp.resamples and prcomp. This object is the
same as the object produced by prcomp, but with additional elements:
Author(s)
Max Kuhn
References
Geladi, P.; Manley, M.; and Lestander, T. (2003), "Scatter plotting in multivariate data analysis," J.
Chemometrics, 17: 503-511
predict.bagEarth 113
See Also
resamples, barchart, xyplot, splom, hclust
Examples
## Not run:
#load(url("http://topepo.github.io/caret/exampleModels.RData"))
resampPCA
## End(Not run)
Description
Predicted values based on bagged Earth and FDA models
Usage
## S3 method for class 'bagEarth'
predict(object, newdata = NULL, type = NULL, ...)
Arguments
object Object of class inheriting from bagEarth
newdata An optional data frame or matrix in which to look for variables with which to
predict. If omitted, the fitted values are used (see note below).
114 predict.bagEarth
type The type of prediction. For bagged earth regression model, type = "response"
will produce a numeric vector of the usual model predictions. earth also al-
lows the user to fit generalized linear models. In this case, type = "response"
produces the inverse link results as a vector. In the case of a binomial general-
ized linear model, type = "response" produces a vector of probabilities, type
= "class" generates a factor vector and type = "prob" produces a two-column
matrix with probabilities for both classes (averaged across the individual mod-
els). Similarly, for bagged fda models, type = "class" generates a factor vec-
tor and type = "probs" outputs a matrix of class probabilities.
... not used
Value
A vector of predictions (for regression or type = "class") or a data frame of class probabilities.
By default, when the model predicts a number, a vector of numeric predictions is returned. When a
classification model is used, the default prediction is a factor vector of classes.
Note
If the predictions for the original training set are needed, there are two ways to calculate them. First,
the original data set can be predicted by each bagged earth model. Secondly, the predictions from
each bootstrap sample could be used (but are more likely to overfit). If the original call to bagEarth
or bagFDA had keepX = TRUE, the first method is used, otherwise the values are calculated via the
second method.
Author(s)
Max Kuhn
See Also
bagEarth
Examples
## Not run:
data(trees)
## out of bag predictions vs just re-predicting the training set
set.seed(655)
fit1 <- bagEarth(Volume ~ ., data = trees, keepX = TRUE)
set.seed(655)
fit2 <- bagEarth(Volume ~ ., data = trees, keepX = FALSE)
hist(predict(fit1) - predict(fit2))
## End(Not run)
predict.gafs 115
Description
Predict new samples using safs and gafs objects.
Usage
## S3 method for class 'gafs'
predict(object, newdata, ...)
Arguments
object an object of class safs or gafs
newdata a data frame or matrix of predictors.
... not currently used
Details
Only the predictors listed in object$optVariables are required.
Value
The type of result depends on what was specified in object$control$functions$predict.
Author(s)
Max Kuhn
See Also
safs, gafs
Examples
## Not run:
set.seed(1)
train_data <- twoClassSim(100, noiseVars = 10)
test_data <- twoClassSim(10, noiseVars = 10)
## A short example
ctrl <- safsControl(functions = rfSA,
method = "cv",
number = 3)
116 predict.knn3
rf_search
predict(rf_search, train_data)
## End(Not run)
Description
Predict the class of a new observation based on k-NN.
Usage
## S3 method for class 'knn3'
predict(object, newdata, type = c("prob", "class"), ...)
Arguments
object object of class knn3.
newdata a data frame of new observations.
type return either the predicted class or the proportion of the votes for the winning
class.
... additional arguments.
Details
This function is a method for the generic function predict for class knn3. For the details see knn3.
This is essentially a copy of predict.ipredknn.
Value
Either the predicted class or the proportion of the votes for each class.
Author(s)
predict.ipredknn by Torsten.Hothorn <[email protected]>
predict.knnreg 117
Description
Predict the outcome of a new observation based on k-NN.
Usage
## S3 method for class 'knnreg'
predict(object, newdata, ...)
Arguments
object object of class knnreg.
newdata a data frame or matrix of new observations.
... additional arguments.
Details
This function is a method for the generic function predict for class knnreg. For the details see
knnreg. This is essentially a copy of predict.ipredknn.
Value
a numeric vector
Author(s)
Max Kuhn, Chris Keefer, adapted from knn and predict.ipredknn
Description
This class uses a model fit to determine which predictors were used in the final model.
Usage
predictors(x, ...)
Arguments
x a model object, list or terms
... not currently used
118 preProcess
Details
For randomForest, cforest, ctree, rpart, ipredbagg, bagging, earth, fda, pamr.train, superpc.train,
bagEarth and bagFDA, an attempt was made to report the predictors that were actually used in the
final model.
The predictors function can be called on the model object (as opposed to the train) object) and
the package will try to find the appropriate coed (if it exists).
In cases where the predictors cannot be determined, NA is returned. For example, nnet may return
missing values from predictors.
Value
a character string of predictors or NA.
Description
Pre-processing transformation (centering, scaling etc.) can be estimated from the training data and
applied to any data set with the same variables.
Usage
preProcess(x, ...)
## Default S3 method:
preProcess(
x,
method = c("center", "scale"),
thresh = 0.95,
pcaComp = NULL,
na.remove = TRUE,
k = 5,
knnSummary = mean,
outcome = NULL,
fudge = 0.2,
numUnique = 3,
verbose = FALSE,
freqCut = 95/5,
uniqueCut = 10,
cutoff = 0.9,
rangeBounds = c(0, 1),
...
)
Arguments
x a matrix or data frame. Non-numeric predictors are allowed but will be ignored.
... additional arguments to pass to fastICA, such as n.comp
method a character vector specifying the type of processing. Possible values are "Box-
Cox", "YeoJohnson", "expoTrans", "center", "scale", "range", "knnImpute", "bag-
Impute", "medianImpute", "pca", "ica", "spatialSign", "corr", "zv", "nzv", and
"conditionalX" (see Details below)
thresh a cutoff for the cumulative percent of variance to be retained by PCA
pcaComp the specific number of PCA components to keep. If specified, this over-rides
thresh
na.remove a logical; should missing values be removed from the calculations?
k the number of nearest neighbors from the training set to use for imputation
knnSummary function to average the neighbor values per column during imputation
outcome a numeric or factor vector for the training set outcomes. This can be used to
help estimate the Box-Cox transformation of the predictor variables (see Details
below)
fudge a tolerance value: Box-Cox transformation lambda values within +/-fudge will
be coerced to 0 and within 1+/-fudge will be coerced to 1.
numUnique how many unique values should y have to estimate the Box-Cox transformation?
verbose a logical: prints a log as the computations proceed
freqCut the cutoff for the ratio of the most common value to the second most common
value. See nearZeroVar.
uniqueCut the cutoff for the percentage of distinct values out of the number of total samples.
See nearZeroVar.
cutoff a numeric value for the pair-wise absolute correlation cutoff. See findCorrelation.
rangeBounds a two-element numeric vector specifying closed interval for range transforma-
tion
object an object of class preProcess
newdata a matrix or data frame of new data to be pre-processed
Details
In all cases, transformations and operations are estimated using the data in x and these operations
are applied to new data using these values; nothing is recomputed when using the predict function.
The Box-Cox (method = "BoxCox"), Yeo-Johnson (method = "YeoJohnson"), and exponential trans-
formations (method = "expoTrans") have been "repurposed" here: they are being used to transform
the predictor variables. The Box-Cox transformation was developed for transforming the response
variable while another method, the Box-Tidwell transformation, was created to estimate transfor-
mations of predictor data. However, the Box-Cox method is simpler, more computationally efficient
and is equally effective for estimating power transformations. The Yeo-Johnson transformation is
similar to the Box-Cox model but can accommodate predictors with zero and/or negative values
(while the predictors values for the Box-Cox transformation must be strictly positive). The expo-
nential transformation of Manly (1976) can also be used for positive or negative data.
120 preProcess
method = "center" subtracts the mean of the predictor’s data (again from the data in x) from the
predictor values while method = "scale" divides by the standard deviation.
The "range" transformation scales the data to be within rangeBounds. If new samples have values
larger or smaller than those in the training set, values will be outside of this range.
Predictors that are not numeric are ignored in the calculations.
method = "zv" identifies numeric predictor columns with a single value (i.e. having zero variance)
and excludes them from further calculations. Similarly, method = "nzv" does the same by applying
nearZeroVar exclude "near zero-variance" predictors. The options freqCut and uniqueCut can
be used to modify the filter.
method = "corr" seeks to filter out highly correlated predictors. See findCorrelation.
For classification, method = "conditionalX" examines the distribution of each predictor condi-
tional on the outcome. If there is only one unique value within any class, the predictor is excluded
from further calculations (see checkConditionalX for an example). When outcome is not a factor,
this calculation is not executed. This operation can be time consuming when used within resampling
via train.
The operations are applied in this order: zero-variance filter, near-zero variance filter, correlation fil-
ter, Box-Cox/Yeo-Johnson/exponential transformation, centering, scaling, range, imputation, PCA,
ICA then spatial sign. This is a departure from versions of caret prior to version 4.76 (where
imputation was done first) and is not backwards compatible if bagging was used for imputation.
If PCA is requested but centering and scaling are not, the values will still be centered and scaled.
Similarly, when ICA is requested, the data are automatically centered and scaled.
k-nearest neighbor imputation is carried out by finding the k closest samples (Euclidian distance) in
the training set. Imputation via bagging fits a bagged tree model for each predictor (as a function of
all the others). This method is simple, accurate and accepts missing values, but it has much higher
computational cost. Imputation via medians takes the median of each predictor in the training set,
and uses them to fill missing values. This method is simple, fast, and accepts missing values, but
treats each predictor independently, and may be inaccurate.
A warning is thrown if both PCA and ICA are requested. ICA, as implemented by the fastICA
package automatically does a PCA decomposition prior to finding the ICA scores.
The function will throw an error of any numeric variables in x has less than two unique values unless
either method = "zv" or method = "nzv" are invoked.
Non-numeric data will not be pre-processed and their values will be in the data frame produced by
the predict function. Note that when PCA or ICA is used, the non-numeric columns may be in
different positions when predicted.
Value
preProcess results in a list with elements
Author(s)
Max Kuhn, median imputation by Zachary Mayer
References
http://topepo.github.io/caret/pre-processing.html
Kuhn and Johnson (2013), Applied Predictive Modeling, Springer, New York (chapter 4)
Kuhn (2008), Building predictive models in R using the caret (http://www.jstatsoft.org/
article/view/v028i05/v28i05.pdf)
Box, G. E. P. and Cox, D. R. (1964) An analysis of transformations (with discussion). Journal of
the Royal Statistical Society B, 26, 211-252.
Box, G. E. P. and Tidwell, P. W. (1962) Transformation of the independent variables. Technometrics
4, 531-550.
Manly, B. L. (1976) Exponential data transformations. The Statistician, 25, 37 - 42.
Yeo, I-K. and Johnson, R. (2000). A new family of power transformations to improve normality or
symmetry. Biometrika, 87, 954-959.
See Also
BoxCoxTrans, expoTrans boxcox, prcomp, fastICA, spatialSign
Examples
data(BloodBrain)
# one variable has one unique value
## Not run:
preProc <- preProcess(bbbDescr)
## End(Not run)
Description
a print method for confusionMatrix
Usage
## S3 method for class 'confusionMatrix'
print(
x,
mode = x$mode,
digits = max(3, getOption("digits") - 3),
printStats = TRUE,
...
)
Arguments
x an object of class confusionMatrix
mode a single character string either "sens_spec", "prec_recall", or "everything"
digits number of significant digits when printed
printStats a logical: if TRUE then table statistics are also printed
... optional arguments to pass to print.table
Value
x is invisibly returned
Author(s)
Max Kuhn
See Also
confusionMatrix
print.train 123
Description
Print the results of a train object.
Usage
## S3 method for class 'train'
print(
x,
printCall = FALSE,
details = FALSE,
selectCol = FALSE,
showSD = FALSE,
...
)
Arguments
x an object of class train.
printCall a logical to print the call at the top of the output
details a logical to show print or summary methods for the final model. In some cases
(such as gbm, knn, lvq, naive Bayes and bagged tree models), no information
will be printed even if details = TRUE
selectCol a logical whether to add a column with a star next to the selected parameters
showSD a logical whether to show the standard deviation of the resampling results within
parentheses (e.g. "4.24 (0.493)")
... options passed to format
Details
The table of complexity parameters used, their resampled performance and a flag for which rows
are optimal.
Value
A matrix with the complexity parameters and performance (invisibly).
Author(s)
Max Kuhn
See Also
train
124 recall
Examples
## Not run:
data(iris)
TrainData <- iris[,1:4]
TrainClasses <- iris[,5]
options(digits = 3)
library(klaR)
rdaFit <- train(TrainData, TrainClasses, method = "rda",
control = trainControl(method = "cv"))
rdaFit
print(rdaFit, showSD = TRUE)
## End(Not run)
Description
These functions calculate the recall, precision or F values of a measurement system for find-
ing/retrieving relevant documents compared to reference results (the truth regarding relevance). The
measurement and "truth" data must have the same two possible outcomes and one of the outcomes
must be thought of as a "relevant" results.
Usage
recall(data, ...)
## Default S3 method:
recall(data, reference, relevant = levels(reference)[1], na.rm = TRUE, ...)
precision(data, ...)
## Default S3 method:
precision(data, reference, relevant = levels(reference)[1], na.rm = TRUE, ...)
F_meas(data, ...)
recall 125
## Default S3 method:
F_meas(
data,
reference,
relevant = levels(reference)[1],
beta = 1,
na.rm = TRUE,
...
)
Arguments
data for the default functions, a factor containing the discrete measurements. For the
table function, a table.
... not currently used
relevant a character string that defines the factor level corresponding to the "relevant"
results
reference a factor containing the reference values (i.e. truth)
na.rm a logical value indicating whether NA values should be stripped before the com-
putation proceeds
beta a numeric value used to weight precision and recall. A value of 1 is traditionally
used and corresponds to the harmonic mean of the two values but other values
weight recall beta times more important than precision.
Details
The recall (aka sensitivity) is defined as the proportion of relevant results out of the number of
samples which were actually relevant. When there are no relevant results, recall is not defined and
a value of NA is returned.
The precision is percentage of predicted truly relevant results of the total number of predicted rele-
vant results and characterizes the "purity in retrieval performance" (Buckland and Gey, 1994)
The measure "F" is a combination of precision and recall (see below).
Suppose a 2x2 table with notation
Reference
Predicted relevant Irrelevant
relevant A B
Irrelevant C D
Value
A number between 0 and 1 (or NA).
Author(s)
Max Kuhn
References
Kuhn, M. (2008), “Building predictive models in R using the caret package, ” Journal of Statistical
Software, (http://www.jstatsoft.org/article/view/v028i05/v28i05.pdf).
Buckland, M., & Gey, F. (1994). The relationship between Recall and Precision. Journal of the
American Society for Information Science, 45(1), 12-19.
Powers, D. (2007). Evaluation: From Precision, Recall and F Factor to ROC, Informedness,
Markedness and Correlation. Technical Report SIE-07-001, Flinders University
See Also
confusionMatrix
Examples
###################
## Data in Table 2 of Powers (2007)
precision(tbl_2_1)
precision(data = tbl_2_1_pred, reference = tbl_2_1_truth, relevant = "Relevant")
recall(tbl_2_1)
recall(data = tbl_2_1_pred, reference = tbl_2_1_truth, relevant = "Relevant")
precision(tbl_2_2)
resampleHist 127
Description
Create a lattice histogram or densityplot from the resampled outcomes from a train object.
Usage
Arguments
Details
All the metrics from the object are plotted, but only for the final model. For more comprehensive
plots functions, see histogram.train, densityplot.train, xyplot.train, stripplot.train.
For the plot to be made, the returnResamp argument in trainControl should be either "final" or
"all".
Value
Author(s)
Max Kuhn
See Also
Examples
## Not run:
data(iris)
TrainData <- iris[,1:4]
TrainClasses <- iris[,5]
resampleHist(knnFit)
## End(Not run)
Description
These functions provide methods for collection, analyzing and visualizing a set of resampling re-
sults from a common data set.
Usage
resamples(x, ...)
## Default S3 method:
resamples(x, modelNames = names(x), ...)
Arguments
x a list of two or more objects of class train, sbf or rfe with a common set of
resampling indices in the control object. For sort.resamples, it is an object
generated by resamples.
... only used for sort and modelCor and captures arguments to pass to sort or
FUN.
modelNames an optional set of names to give to the resampling results
decreasing logical. Should the sort be increasing or decreasing?
metric a character string for the performance measure used to sort or computing the
between-model correlations
FUN a function whose first argument is a vector and returns a scalar, to be applied to
each model’s performance measure.
object an object generated by resamples
row.names, optional
not currently used but included for consistency with as.data.frame
Details
The ideas and methods here are based on Hothorn et al. (2005) and Eugster et al. (2008).
The results from train can have more than one performance metric per resample. Each metric in
the input object is saved.
resamples checks that the resampling results match; that is, the indices in the object trainObject$control$index
are the same. Also, the argument trainControl returnResamp should have a value of "final"
for each model.
The summary function computes summary statistics across each model/metric combination.
Value
For resamples: an object with class "resamples" with elements
For sort.resamples a character string in the sorted order is generated. modelCor returns a corre-
lation matrix.
Author(s)
Max Kuhn
130 resamples
References
Hothorn et al. The design and analysis of benchmark experiments. Journal of Computational and
Graphical Statistics (2005) vol. 14 (3) pp. 675-699
Eugster et al. Exploratory and inferential analysis of benchmark experiments. Ludwigs-Maximilians-
Universitat Munchen, Department of Statistics, Tech. Rep (2008) vol. 30
See Also
train, trainControl, diff.resamples, xyplot.resamples, densityplot.resamples, bwplot.resamples,
splom.resamples
Examples
data(BloodBrain)
set.seed(1)
## resamps
## summary(resamps)
resampleSummary 131
Description
This function uses the out-of-bag predictions to calculate overall performance metrics and returns
the observed and predicted data.
Usage
resampleSummary(obs, resampled, index = NULL, keepData = TRUE)
Arguments
obs A vector (numeric or factor) of the outcome data
resampled For bootstrapping, this is either a matrix (for numeric outcomes) or a data frame
(for factors). For cross-validation, a vector is produced.
index The list to index of samples in each cross-validation fold (only used for cross-
validation).
keepData A logical for returning the observed and predicted data.
Details
The mean and standard deviation of the values produced by postResample are calculated.
Value
A list with:
Author(s)
Max Kuhn
See Also
postResample
Examples
Description
A simple backwards selection, a.k.a. recursive feature elimination (RFE), algorithm
Usage
rfe(x, ...)
## Default S3 method:
rfe(
x,
y,
sizes = 2^(2:4),
metric = ifelse(is.factor(y), "Accuracy", "RMSE"),
maximize = ifelse(metric == "RMSE", FALSE, TRUE),
rfeControl = rfeControl(),
...
)
rfeIter(
x,
y,
testX,
testY,
sizes,
rfeControl = rfeControl(),
label = "",
seeds = NA,
...
)
rfeControl = rfeControl(),
...
)
Arguments
x A matrix or data frame of predictors for model training. This object must have
unique column names. For the recipes method, x is a recipe object.
... options to pass to the model fitting function (ignored in predict.rfe)
y a vector of training set outcomes (either numeric or factor)
sizes a numeric vector of integers corresponding to the number of features that should
be retained
metric a string that specifies what summary metric will be used to select the optimal
model. By default, possible values are "RMSE" and "Rsquared" for regression
and "Accuracy" and "Kappa" for classification. If custom performance met-
rics are used (via the functions argument in rfeControl, the value of metric
should match one of the arguments.
maximize a logical: should the metric be maximized or minimized?
rfeControl a list of options, including functions for fitting and prediction. The web page
http://topepo.github.io/caret/recursive-feature-elimination.html#
rfe has more details and examples related to this function.
form A formula of the form y ~ x1 + x2 + ...
data Data frame from which variables specified in formula or recipe are preferen-
tially to be taken.
subset An index vector specifying the cases to be used in the training sample. (NOTE:
If given, this argument must be named.)
na.action A function to specify the action to be taken if NAs are found. The default action
is for the procedure to fail. An alternative is na.omit, which leads to rejection
of cases with missing values on any required variable. (NOTE: If given, this
argument must be named.)
contrasts A list of contrasts to be used for some or all the factors appearing as variables in
the model formula.
testX a matrix or data frame of test set predictors. This must have the same column
names as x
testY a vector of test set outcomes
label an optional character string to be printed when in verbose mode.
seeds an optional vector of integers for the size. The vector should have length of
length(sizes)+1
object an object of class rfe
size a single integers corresponding to the number of features that should be retained
in the updated model
134 rfe
Details
More details on this function can be found at http://topepo.github.io/caret/recursive-feature-elimination.
html.
This function implements backwards selection of predictors based on predictor importance rank-
ing. The predictors are ranked and the less important ones are sequentially eliminated prior to
modeling. The goal is to find a subset of predictors that can be used to produce an accurate
model. The web page http://topepo.github.io/caret/recursive-feature-elimination.
html#rfe has more details and examples related to this function.
rfe can be used with "explicit parallelism", where different resamples (e.g. cross-validation group)
can be split up and run on multiple machines or processors. By default, rfe will use a single
processor on the host machine. As of version 4.99 of this package, the framework used for parallel
processing uses the foreach package. To run the resamples in parallel, the code for rfe does not
change; prior to the call to rfe, a parallel backend is registered with foreach (see the examples
below).
rfeIter is the basic algorithm while rfe wraps these operations inside of resampling. To avoid
selection bias, it is better to use the function rfe than rfeIter.
When updating a model, if the entire set of resamples were not saved using rfeControl(returnResamp
= "final"), the existing resamples are removed with a warning.
Value
A list with elements
finalVariables a list of size length(sizes) + 1 containing the column names of the “surviv-
ing” predictors at each stage of selection. The first element corresponds to all
the predictors (i.e. size = ncol(x))
pred a data frame with columns for the test set outcome, the predicted outcome and
the subset size.
Note
We using a recipe as an input, there may be some subset sizes that are not well-replicated over
resamples. ‘rfe‘ method will only consider subset sizes where at least half of the resamples have
associated results in the search for an optimal subset size.
Author(s)
Max Kuhn
See Also
rfeControl
Examples
## Not run:
data(BloodBrain)
rfe 135
x <- scale(bbbDescr[,-nearZeroVar(bbbDescr)])
x <- x[, -findCorrelation(cor(x), .8)]
x <- as.data.frame(x, stringsAsFactors = TRUE)
set.seed(1)
lmProfile <- rfe(x, logBBB,
sizes = c(2:25, 30, 35, 40, 45, 50, 55, 60, 65),
rfeControl = rfeControl(functions = lmFuncs,
number = 200))
set.seed(1)
lmProfile2 <- rfe(x, logBBB,
sizes = c(2:25, 30, 35, 40, 45, 50, 55, 60, 65),
rfeControl = rfeControl(functions = lmFuncs,
rerank = TRUE,
number = 200))
xyplot(lmProfile$results$RMSE + lmProfile2$results$RMSE ~
lmProfile$results$Variables,
type = c("g", "p", "l"),
auto.key = TRUE)
set.seed(1)
svmProfile <- rfe(x, logBBB,
sizes = c(2, 5, 10, 20),
rfeControl = rfeControl(functions = caretFuncs,
number = 200),
## pass options to train()
method = "svmRadial")
## classification
data(mdrr)
mdrrDescr <- mdrrDescr[,-nearZeroVar(mdrrDescr)]
mdrrDescr <- mdrrDescr[, -findCorrelation(cor(mdrrDescr), .8)]
set.seed(1)
inTrain <- createDataPartition(mdrrClass, p = .75, list = FALSE)[,1]
set.seed(2)
136 rfeControl
## End(Not run)
#######################################
## Parallel Processing Example via multicore
## Not run:
library(doMC)
set.seed(1)
lmProfile <- rfe(x, logBBB,
sizes = c(2:25, 30, 35, 40, 45, 50, 55, 60, 65),
rfeControl = rfeControl(functions = lmFuncs,
number = 200))
## End(Not run)
Description
This function generates a control object that can be used to specify the details of the feature selection
algorithms used in this package.
Usage
rfeControl(
functions = NULL,
rerank = FALSE,
method = "boot",
saveDetails = FALSE,
number = ifelse(method %in% c("cv", "repeatedcv"), 10, 25),
repeats = ifelse(method %in% c("cv", "repeatedcv"), 1, number),
rfeControl 137
verbose = FALSE,
returnResamp = "final",
p = 0.75,
index = NULL,
indexOut = NULL,
timingSamps = 0,
seeds = NA,
allowParallel = TRUE
)
Arguments
functions a list of functions for model fitting, prediction and variable importance (see De-
tails below)
rerank a logical: should variable importance be re-calculated each time features are
removed?
method The external resampling method: boot, cv, LOOCV or LGOCV (for repeated train-
ing/test splits
saveDetails a logical to save the predictions and variable importances from the selection
process
number Either the number of folds or number of resampling iterations
repeats For repeated k-fold cross-validation only: the number of complete sets of folds
to compute
verbose a logical to print a log for each external resampling iteration
returnResamp A character string indicating how much of the resampled summary metrics
should be saved. Values can be “final”, “all” or “none”
p For leave-group out cross-validation: the training percentage
index a list with elements for each external resampling iteration. Each list element is
the sample rows used for training at that iteration.
indexOut a list (the same length as index) that dictates which sample are held-out for each
resample. If NULL, then the unique set of samples not contained in index is used.
timingSamps the number of training set samples that will be used to measure the time for pre-
dicting samples (zero indicates that the prediction time should not be estimated).
seeds an optional set of integers that will be used to set the seed at each resampling
iteration. This is useful when the models are run in parallel. A value of NA will
stop the seed from being set within the worker processes while a value of NULL
will set the seeds using a random set of integers. Alternatively, a list can be used.
The list should have B+1 elements where B is the number of resamples. The first
B elements of the list should be vectors of integers of length P where P is the
number of subsets being evaluated (including the full set). The last element of
the list only needs to be a single integer (for the final model). See the Examples
section below.
allowParallel if a parallel backend is loaded and available, should the function use it?
138 rfeControl
Details
More details on this function can be found at http://topepo.github.io/caret/recursive-feature-elimination.
html#rfe.
Backwards selection requires function to be specified for some operations.
The fit function builds the model based on the current data set. The arguments for the function
must be:
• x the current training set of predictor data with the appropriate subset of variables
• y the current outcome data (either a numeric or factor vector)
• first a single logical value for whether the current predictor set has all possible variables
• last similar to first, but TRUE when the last model is fit with the final subset size and
predictors.
• ...optional arguments to pass to the fit function in the call to rfe
The function should return a model object that can be used to generate predictions.
The pred function returns a vector of predictions (numeric or factors) from the current model. The
arguments are:
The rank function is used to return the predictors in the order of the most important to the least
important. Inputs are:
The function should return a data frame with a column called var that has the current variable
names. The first row should be the most important predictor etc. Other columns can be included in
the output and will be returned in the final rfe object.
The selectSize function determines the optimal number of predictors based on the resampling
output. Inputs for the function are:
• xa matrix with columns for the performance metrics and the number of variables, called
"Variables"
• metrica character string of the performance measure to optimize (e.g. "RMSE", "Rsquared",
"Accuracy" or "Kappa")
• maximizea single logical for whether the metric should be maximized
This function should return an integer corresponding to the optimal subset size. caret comes with
two examples functions for this purpose: pickSizeBest and pickSizeTolerance.
After the optimal subset size is determined, the selectVar function will be used to calculate the
best rankings for each variable across all the resampling iterations. Inputs for the function are:
rfeControl 139
• y a list of variables importance for each resampling iteration and each subset size (generated
by the user-defined rank function). In the example, each each of the cross-validation groups
the output of the rank function is saved for each of the subset sizes (including the original
subset). If the rankings are not recomputed at each iteration, the values will be the same
within each cross-validation iteration.
• size the integer returned by the selectSize function
This function should return a character string of predictor names (of length size) in the order of
most important to least important
Examples of these functions are included in the package: lmFuncs, rfFuncs, treebagFuncs and
nbFuncs.
Model details about these functions, including examples, are at http://topepo.github.io/caret/
recursive-feature-elimination.html. .
Value
A list
Author(s)
Max Kuhn
See Also
Examples
## Not run:
subsetSizes <- c(2, 4, 6, 8)
set.seed(123)
seeds <- vector(mode = "list", length = 51)
for(i in 1:50) seeds[[i]] <- sample.int(1000, length(subsetSizes) + 1)
seeds[[51]] <- sample.int(1000, 1)
set.seed(1)
rfMod <- rfe(bbbDescr, logBBB,
sizes = subsetSizes,
rfeControl = rfeControl(functions = rfFuncs,
seeds = seeds,
number = 50))
## End(Not run)
140 safs
Description
This data frame contains house and sale price data for 932 homes in Sacramento CA. The orig-
inal data were obtained from the website for the SpatialKey software. From their website: "The
Sacramento real estate transactions file is a list of 985 real estate transactions in the Sacramento
area reported over a five-day period, as reported by the Sacramento Bee." Google was used to fill in
missing/incorrect data.
Value
Sacramento a data frame with columns ’city’, ’zip’, ’beds’, ’baths’, ’sqft’, ’type’, ’price’,
’latitude’, and ’longitude’
Source
SpatialKey website: https://support.spatialkey.com/spatialkey-sample-csv-data
Examples
data(Sacramento)
set.seed(955)
in_train <- createDataPartition(log10(Sacramento$price), p = .8, list = FALSE)
Description
Supervised feature selection using simulated annealing
safs conducts a supervised binary search of the predictor space using simulated annealing (SA).
See Kirkpatrick et al (1983) for more information on this search algorithm.
This function conducts the search of the feature space repeatedly within resampling iterations. First,
the training data are split be whatever resampling method was specified in the control function. For
example, if 10-fold cross-validation is selected, the entire simulated annealing search is conducted
10 separate times. For the first fold, nine tenths of the data are used in the search while the remaining
tenth is used to estimate the external performance since these data points were not used in the search.
safs 141
During the search, a measure of fitness (i.e. SA energy value) is needed to guide the search. This is
the internal measure of performance. During the search, the data that are available are the instances
selected by the top-level resampling (e.g. the nine tenths mentioned above). A common approach
is to conduct another resampling procedure. Another option is to use a holdout set of samples to
determine the internal estimate of performance (see the holdout argument of the control function).
While this is faster, it is more likely to cause overfitting of the features and should only be used
when a large amount of training data are available. Yet another idea is to use a penalized metric
(such as the AIC statistic) but this may not exist for some metrics (e.g. the area under the ROC
curve).
The internal estimates of performance will eventually overfit the subsets to the data. However, since
the external estimate is not used by the search, it is able to make better assessments of overfitting.
After resampling, this function determines the optimal number of iterations for the SA.
Finally, the entire data set is used in the last execution of the simulated annealing algorithm search
and the final model is built on the predictor subset that is associated with the optimal number of
iterations determined by resampling (although the update function can be used to manually set the
number of iterations).
This is an example of the output produced when safsControl(verbose = TRUE) is used:
The text "Fold03" indicates that this search is for the third cross-validation fold. The initial subset of
11 predictors had a fitness value of 0.401. The next iteration added a single feature the the existing
best subset of 11 (as indicated by "11+1") that increased the fitness value to 0.410. This new
solution, which has a Jaccard similarity value of 91.7% to the current best solution, is automatically
accepted. The third iteration adds another feature to the current set of 12 but does not improve the
fitness. The acceptance probability for this difference is shown to be 95.6% and the "A" indicates
that this new sub-optimal subset is accepted. The fourth iteration does not show an increase and is
not accepted. Note that the Jaccard similarity value of 85.7% is the similarity to the current best
solution (from iteration 2) and the "12+2" indicates that there are two additional features added
from the current best that contains 12 predictors.
The search algorithm can be parallelized in several places:
1. each externally resampled SA can be run independently (controlled by the allowParallel
option of safsControl)
2. if inner resampling is used, these can be run in parallel (controls depend on the function used.
See, for example, trainControl)
3. any parallelization of the individual model fits. This is also specific to the modeling function.
It is probably best to pick one of these areas for parallelization and the first is likely to produces
the largest decrease in run-time since it is the least likely to incur multiple re-starting of the worker
processes. Keep in mind that if multiple levels of parallelization occur, this can effect the number
of workers and the amount of memory required exponentially.
142 safs
Usage
safs(x, ...)
## Default S3 method:
safs(x, y, iters = 10, differences = TRUE, safsControl = safsControl(), ...)
Arguments
x An object where samples are in rows and features are in columns. This could be
a simple matrix, data frame or other type (e.g. sparse matrix). For the recipes
method, x is a recipe object. See Details below.
... arguments passed to the classification or regression routine specified in the func-
tion safsControl$functions$fit
y a numeric or factor vector containing the outcome for each sample.
iters number of search iterations
differences a logical: should the difference in fitness values with and without each predictor
be calculated
safsControl a list of values that define how this function acts. See safsControl and URL.
data an object of class rfe.
Value
Author(s)
Max Kuhn
References
http://topepo.github.io/caret/feature-selection-using-genetic-algorithms.html
http://topepo.github.io/caret/feature-selection-using-simulated-annealing.html
Kuhn and Johnson (2013), Applied Predictive Modeling, Springer
Kirkpatrick, S., Gelatt, C. D., and Vecchi, M. P. (1983). Optimization by simulated annealing.
Science, 220(4598), 671.
See Also
safsControl, predict.safs
safs_initial 143
Examples
## Not run:
set.seed(1)
train_data <- twoClassSim(100, noiseVars = 10)
test_data <- twoClassSim(10, noiseVars = 10)
## A short example
ctrl <- safsControl(functions = rfSA,
method = "cv",
number = 3)
rf_search
## End(Not run)
Description
prob
= exp[(current-new)/current*iteration]
144 safs_initial
Usage
safs_initial(vars, prob = 0.2, ...)
caretSA
treebagSA
rfSA
Arguments
vars the total number of possible predictor variables
prob The probability that an individual predictor is included in the initial predictor set
... not currently used
x the integer index vector for the current subset
number the number of predictor variables to perturb
old, new fitness values associated with the current and new subset
iteration the number of iterations overall or the number of iterations since restart (if
improve is used in safsControl)
Format
An object of class list of length 8.
Value
The return value depends on the function. Note that the SA code encodes the subsets as a vector of
integers that are included in the subset (which is different than the encoding used for GAs).
The objects caretSA, rfSA and treebagSA are example lists that can be used with the functions
argument of safsControl.
In the case of caretSA, the ... structure of safs passes through to the model fitting routine. As a
consequence, the train function can easily be accessed by passing important arguments belonging
to train to safs. See the examples below. By default, using caretSA will used the resampled
performance estimates produced by train as the internal estimate of fitness.
For rfSA and treebagSA, the randomForest and bagging functions are used directly (i.e. train
is not used). Arguments to either of these functions can also be passed to them though the safs call
(see examples below). For these two functions, the internal fitness is estimated using the out-of-bag
estimates naturally produced by those functions. While faster, this limits the user to accuracy or
Kappa (for classification) and RMSE and R-squared (for regression).
Author(s)
Max Kuhn
safs_initial 145
References
http://topepo.github.io/caret/feature-selection-using-simulated-annealing.html
See Also
safs, safsControl
Examples
###
###
## Not run:
###
## Hypothetical examples
lda_sa <- safs(x = predictors,
y = classes,
safsControl = safsControl(functions = caretSA),
## now pass arguments to `train`
method = "lda",
metric = "Accuracy"
trControl = trainControl(method = "cv", classProbs = TRUE))
y = classes,
safsControl = safsControl(functions = rfSA),
## these are arguments to `randomForest`
ntree = 1000,
importance = TRUE)
## End(Not run)
Description
Model fitting after applying univariate filters
Usage
sbf(x, ...)
## Default S3 method:
sbf(x, y, sbfControl = sbfControl(), ...)
Arguments
x a data frame containing training data where samples are in rows and features are
in columns. For the recipes method, x is a recipe object.
... for sbf: arguments passed to the classification or regression routine (such as
randomForest). For predict.sbf: augments cannot be passed to the predic-
tion function using predict.sbf as it uses the function originally specified for
prediction.
y a numeric or factor vector containing the outcome for each sample.
sbfControl a list of values that define how this function acts. See sbfControl. (NOTE: If
given, this argument must be named.)
form A formula of the form y ~ x1 + x2 + ...
sbf 147
data Data frame from which variables specified in formula are preferentially to be
taken.
subset An index vector specifying the cases to be used in the training sample. (NOTE:
If given, this argument must be named.)
na.action A function to specify the action to be taken if NAs are found. The default action
is for the procedure to fail. An alternative is na.omit, which leads to rejection
of cases with missing values on any required variable. (NOTE: If given, this
argument must be named.)
contrasts a list of contrasts to be used for some or all the factors appearing as variables in
the model formula.
object an object of class sbf
newdata a matrix or data frame of predictors. The object must have non-null column
names
Details
More details on this function can be found at http://topepo.github.io/caret/feature-selection-using-univariate
html.
This function can be used to get resampling estimates for models when simple, filter-based feature
selection is applied to the training data.
For each iteration of resampling, the predictor variables are univariately filtered prior to modeling.
Performance of this approach is estimated using resampling. The same filter and model are then
applied to the entire training set and the final model (and final features) are saved.
sbf can be used with "explicit parallelism", where different resamples (e.g. cross-validation group)
can be split up and run on multiple machines or processors. By default, sbf will use a single
processor on the host machine. As of version 4.99 of this package, the framework used for parallel
processing uses the foreach package. To run the resamples in parallel, the code for sbf does not
change; prior to the call to sbf, a parallel backend is registered with foreach (see the examples
below).
The modeling and filtering techniques are specified in sbfControl. Example functions are given in
lmSBF.
Value
for sbf, an object of class sbf with elements:
Author(s)
Max Kuhn
See Also
sbfControl
Examples
## Not run:
data(BloodBrain)
predict(RFwithGAM, bbbDescr[1:10,])
## library(doMC)
data(mdrr)
mdrrDescr <- mdrrDescr[,-nearZeroVar(mdrrDescr)]
mdrrDescr <- mdrrDescr[, -findCorrelation(cor(mdrrDescr), .8)]
set.seed(1)
filteredNB <- sbf(mdrrDescr, mdrrClass,
sbfControl = sbfControl(functions = nbSBF,
verbose = FALSE,
method = "repeatedcv",
repeats = 5))
confusionMatrix(filteredNB)
## End(Not run)
sbfControl 149
Description
Controls the execution of models with simple filters for feature selection
Usage
sbfControl(
functions = NULL,
method = "boot",
saveDetails = FALSE,
number = ifelse(method %in% c("cv", "repeatedcv"), 10, 25),
repeats = ifelse(method %in% c("cv", "repeatedcv"), 1, number),
verbose = FALSE,
returnResamp = "final",
p = 0.75,
index = NULL,
indexOut = NULL,
timingSamps = 0,
seeds = NA,
allowParallel = TRUE,
multivariate = FALSE
)
Arguments
functions a list of functions for model fitting, prediction and variable filtering (see Details
below)
method The external resampling method: boot, cv, LOOCV or LGOCV (for repeated train-
ing/test splits
saveDetails a logical to save the predictions and variable importances from the selection
process
number Either the number of folds or number of resampling iterations
repeats For repeated k-fold cross-validation only: the number of complete sets of folds
to compute
verbose a logical to print a log for each external resampling iteration
returnResamp A character string indicating how much of the resampled summary metrics
should be saved. Values can be “final” or “none”
p For leave-group out cross-validation: the training percentage
150 sbfControl
index a list with elements for each external resampling iteration. Each list element is
the sample rows used for training at that iteration.
indexOut a list (the same length as index) that dictates which sample are held-out for each
resample. If NULL, then the unique set of samples not contained in index is used.
timingSamps the number of training set samples that will be used to measure the time for pre-
dicting samples (zero indicates that the prediction time should not be estimated).
seeds an optional set of integers that will be used to set the seed at each resampling
iteration. This is useful when the models are run in parallel. A value of NA
will stop the seed from being set within the worker processes while a value of
NULL will set the seeds using a random set of integers. Alternatively, a vector
of integers can be used. The vector should have B+1 elements where B is the
number of resamples. See the Examples section below.
allowParallel if a parallel backend is loaded and available, should the function use it?
multivariate a logical; should all the columns of x be exposed to the score function at once?
Details
More details on this function can be found at http://topepo.github.io/caret/feature-selection-using-univariate
html.
Simple filter-based feature selection requires function to be specified for some operations.
The fit function builds the model based on the current data set. The arguments for the function
must be:
• x the current training set of predictor data with the appropriate subset of variables (i.e. after
filtering)
• y the current outcome data (either a numeric or factor vector)
• ... optional arguments to pass to the fit function in the call to sbf
The function should return a model object that can be used to generate predictions.
The pred function returns a vector of predictions (numeric or factors) from the current model. The
arguments are:
• object the model generated by the fit function
• x the current set of predictor set for the held-back samples
The score function is used to return scores with names for each predictor (such as a p-value). Inputs
are:
• x the predictors for the training samples. If sbfControl()$multivariate is TRUE, this will
be the full predictor matrix. Otherwise it is a vector for a specific predictor.
• y the current training outcomes
When sbfControl()$multivariate is TRUE, the score function should return a named vector
where length(scores) == ncol(x). Otherwise, the function’s output should be a single value.
Univariate examples are give by anovaScores for classification and gamScores for regression and
the example below.
The filter function is used to return a logical vector with names for each predictor (TRUE indicates
that the prediction should be retained). Inputs are:
sbfControl 151
Value
a list that echos the specified arguments
Author(s)
Max Kuhn
See Also
sbf, caretSBF, lmSBF, rfSBF, treebagSBF, ldaSBF and nbSBF
Examples
## Not run:
data(BloodBrain)
set.seed(1)
RFwithGAM2 <- sbf(bbbDescr, logBBB,
sbfControl = sbfControl(functions = rfSBF2,
verbose = FALSE,
seeds = sample.int(100000, 11),
method = "cv",
multivariate = TRUE))
RFwithGAM2
152 segmentationData
## End(Not run)
Description
Reid (2015) collected data on animal feses in coastal California. The data consist of DNA verified
species designations as well as fields related to the time and place of the collection and the scat
itself. The data frame scat_orig contains while scat contains data on the three main species.
Value
scat_orig the entire data set in the Supplemental Materials
scat data on the three main species
Source
Reid, R. E. B. (2015). A morphometric modeling approach to distinguishing among bobcat, coyote
and gray fox scats. Wildlife Biology, 21(5), 254-262
Description
Hill, LaPan, Li and Haney (2007) develop models to predict which cells in a high content screen
were well segmented. The data consists of 119 imaging measurements on 2019. The original
analysis used 1009 for training and 1010 as a test set (see the column called Case).
Details
The outcome class is contained in a factor variable called Class with levels "PS" for poorly seg-
mented and "WS" for well segmented.
The raw data used in the paper can be found at the Biomedcentral website. Versions of caret < 4.98
contained the original data. The version now contained in segmentationData is modified. First,
several discrete versions of some of the predictors (with the suffix "Status") were removed. Second,
there are several skewed predictors with minimum values of zero (that would benefit from some
transformation, such as the log). A constant value of 1 was added to these fields: AvgIntenCh2,
FiberAlign2Ch3, FiberAlign2Ch4, SpotFiberCountCh4 and TotalIntenCh2.
A binary version of the original data is at http://topepo.github.io/caret/segmentationOriginal.
RData.
SLC14_1 153
Value
segmentationData
data frame of cells
Source
Hill, LaPan, Li and Haney (2007). Impact of image segmentation on high-content screening data
quality for SK-BR-3 cells, BMC Bioinformatics, Vol. 8, pg. 340, http://www.biomedcentral.
com/1471-2105/8/340.
Description
This function simulates regression and classification data with truly important predictors and irrel-
evant predictions.
Usage
SLC14_1(n = 100, noiseVars = 0, corrVars = 0, corrType = "AR1", corrValue = 0)
LPH07_1(
n = 100,
noiseVars = 0,
corrVars = 0,
corrType = "AR1",
corrValue = 0,
factors = FALSE,
class = FALSE
)
twoClassSim(
n = 100,
intercept = -5,
linearVars = 10,
noiseVars = 0,
corrVars = 0,
corrType = "AR1",
corrValue = 0,
mislabel = 0,
ordinal = FALSE
)
154 SLC14_1
Arguments
Details
The first function (twoClassSim) generates two class data. The data are simulated in different sets.
First, two multivariate normal predictors (denoted here as A and B) are created with a correlation
our about 0.65. They change the log-odds using main effects and an interaction:
intercept - 4A + 4B + 2AB
The intercept is a parameter for the simulation and can be used to control the amount of class
imbalance.
The second set of effects are linear with coefficients that alternate signs and have values between
2.5 and 0.025. For example, if there were six predictors in this set, their contribution to the log-odds
would be
The third set is a nonlinear function of a single predictor ranging between [0, 1] called J here:
(J^3) + 2exp(-6(J-0.3)^2)
The fourth set of informative predictors are copied from one of Friedman’s systems and use two
more predictors (K and L):
2sin(KL)
SLC14_1 155
The simulated error term is a standard normal (i.e. Gaussian). The noise variables are simulated
in the same manner as described above but are made binary based on whether the normal random
variable is above or below 0. If factors = TRUE, each of the predictors is coerced into a factor.
This simulation can also be adapted for classification using the option class = TRUE. In this case,
the outcome is converted to be a factor by first computing the logit transformation of the equation
above and using uniform random numbers to assign the observed class.
A second function (LPH07_2) uses 20 independent Gaussians with mean zero and variance 16. The
functional form here is:
The error term is also Gaussian with mean zero and variance 16.
The function SLC14_1 simulates a system from Sapp et al. (2014). All informative predictors
are independent Gaussian random variables with mean zero and a variance of 9. The prediction
equation is:
The random error here is also Gaussian with mean zero and a variance of 9.
SLC14_2 is also from Sapp et al. (2014). Two hundred independent Gaussian variables are gener-
ated, each having mean zero and variance 16. The functional form is
and the error term is Gaussian with mean zero and a variance of 25.
For each simulation, the user can also add non-informative predictors to the data. These are random
standard normal predictors and can be optionally added to the data in two ways: a specified number
of independent predictors or a set number of predictors that follow a particular correlation structure.
The only two correlation structure that have been implemented are
156 SLC14_1
• compound-symmetry (aka exchangeable) where there is a constant correlation between all the
predictors
• auto-regressive 1 [AR(1)]. While there is no time component to these data, this structure
can be used to add predictors of varying levels of correlation. For example, if there were 4
predictors and r was the correlation parameter, the between predictor correlation matrix would
be
Value
a data frame with columns:
Author(s)
Max Kuhn
References
van der Laan, M. J., & Polley Eric, C. (2007). Super learner. Statistical Applications in Genetics
and Molecular Biology, 6(1), 1-23.
Sapp, S., van der Laan, M. J., & Canny, J. (2014). Subsemble: an ensemble method for combining
subset-specific algorithm fits. Journal of Applied Statistics, 41(6), 1247-1259.
Examples
Description
Compute the spatial sign (a projection of a data vector to a unit length circle). The spatial sign of a
vector w is w /norm(w).
Usage
spatialSign(x, ...)
## Default S3 method:
spatialSign(x, na.rm = TRUE, ...)
Arguments
x an object full of numeric data (which should probably be scaled). Factors are
not allowed. This could be a vector, matrix or data frame.
... Not currently used.
na.rm A logical; should missing data be removed when computing the norm of the
vector?
Value
A vector, matrix or data frame with the same dim names of the original data.
Author(s)
Max Kuhn
References
Serneels et al. Spatial sign preprocessing: a simple way to impart moderate robustness to multivari-
ate estimators. J. Chem. Inf. Model (2006) vol. 46 (3) pp. 1402-1409
158 summary.bagEarth
Examples
spatialSign(rnorm(5))
spatialSign(iris[,-5])
trellis.par.set(caretTheme())
featurePlot(iris[,-5], iris[,5], "pairs")
featurePlot(spatialSign(scale(iris[,-5])), iris[,5], "pairs")
Description
The function shows a summary of the results from a bagged earth model
Usage
## S3 method for class 'bagEarth'
summary(object, ...)
Arguments
object an object of class "bagEarth" or "bagFDA"
... optional arguments (not used)
Details
The out-of-bag statistics are summarized, as well as the distribution of the number of model terms
and number of variables used across all the bootstrap samples.
Value
a list with elements
modelInfo a matrix with the number of model terms and variables used
oobStat a summary of the out-of-bag statistics
bmarsCall the original call to bagEarth
tecator 159
Author(s)
Max Kuhn
Examples
## Not run:
data(trees)
set.seed(9655)
fit <- bagEarth(trees[,-3], trees[3])
summary(fit)
## End(Not run)
Description
"These data are recorded on a Tecator Infratec Food and Feed Analyzer working in the wavelength
range 850 - 1050 nm by the Near Infrared Transmission (NIT) principle. Each sample contains
finely chopped pure meat with different moisture, fat and protein contents.
Details
If results from these data are used in a publication we want you to mention the instrument and
company name (Tecator) in the publication. In addition, please send a preprint of your article to
Karin Thente, Tecator AB, Box 70, S-263 21 Hoganas, Sweden
The data are available in the public domain with no responsibility from the original data source.
The data can be redistributed as long as this permission note is attached."
"For each meat sample the data consists of a 100 channel spectrum of absorbances and the contents
of moisture (water), fat and protein. The absorbance is -log10 of the transmittance measured by the
spectrometer. The three contents, measured in percent, are determined by analytic chemistry."
Included here are the traning, monitoring and test sets.
Value
absorp absorbance data for 215 samples. The first 129 were originally used as a training
set
endpoints the percentages of water, fat and protein
160 thresholder
Examples
data(tecator)
splom(~endpoints)
plot(absorpSubset[1,],
type = "n",
ylim = range(absorpSubset),
xlim = c(0, 105),
xlab = "Wavelength Index",
ylab = "Absorption")
for(i in 1:10)
{
points(absorpSubset[i,], type = "l", col = plotColors[i], lwd = 2)
text(105, absorpSubset[i,100], endpointSubset[i], col = plotColors[i])
}
title("Predictor Profiles for 10 Random Samples")
Description
This function uses the resampling results from a train object to generate performance statistics
over a set of probability thresholds for two-class problems.
Usage
Arguments
x A train object where the values of savePredictions was either TRUE, "all",
or "final" in trainControl. Also, the control argument clasProbs should
have been TRUE.
threshold A numeric vector of candidate probability thresholds between [0,1]. If the class
probability corresponding to the first level of the outcome is greater than the
threshold, the data point is classified as that level.
final A logical: should only the final tuning parameters chosen by train be used
when savePredictions = 'all'?
statistics A character vector indicating which statistics to calculate. See details below for
possible choices; the default value "all" computes all of these.
Details
The argument statistics designates the statistics to compute for each probability threshold. One
or more of the following statistics can be selected:
• Sensitivity
• Specificity
• Pos Pred Value
• Neg Pred Value
• Precision
• Recall
• F1
• Prevalence
• Detection Rate
• Detection Prevalence
• Balanced Accuracy
• Accuracy
• Kappa
• J
• Dist
For a description of these statistics (except the last two), see the documentation of confusionMatrix.
The last two statistics are Youden’s J statistic and the distance to the best possible cutoff (i.e. perfect
sensitivity and specificity.
Value
A data frame with columns for each of the tuning parameters from the model along with an ad-
ditional column called prob_threshold for the probability threshold. There are also columns for
summary statistics averaged over resamples with column names corresponding to the input argu-
ment statistics.
162 train
Examples
## Not run:
set.seed(2444)
dat <- twoClassSim(500, intercept = -10)
table(dat$Class)
set.seed(2863)
mod <- train(Class ~ ., data = dat,
method = "rda",
tuneLength = 4,
metric = "ROC",
trControl = ctrl)
## End(Not run)
Description
This function sets up a grid of tuning parameters for a number of classification and regression
routines, fits each model and calculates a resampling based performance measure.
Usage
train(x, ...)
## Default S3 method:
train(
x,
y,
method = "rf",
train 163
preProcess = NULL,
...,
weights = NULL,
metric = ifelse(is.factor(y), "Accuracy", "RMSE"),
maximize = ifelse(metric %in% c("RMSE", "logLoss", "MAE"), FALSE, TRUE),
trControl = trainControl(),
tuneGrid = NULL,
tuneLength = ifelse(trControl$method == "none", 1, 3)
)
Arguments
x For the default method, x is an object where samples are in rows and features
are in columns. This could be a simple matrix, data frame or other type (e.g.
sparse matrix) but must have column names (see Details below). Preprocessing
using the preProcess argument only supports matrices or data frames. When
using the recipe method, x should be an unprepared recipe object that describes
the model terms (i.e. outcome, predictors, etc.) as well as any pre-processing
that should be done to the data. This is an alternative approach to specifying
the model. Note that, when using the recipe method, any arguments passed to
preProcess will be ignored. See the links and example below for more details
using recipes.
... Arguments passed to the classification or regression routine (such as randomForest).
Errors will occur if values for tuning parameters are passed here.
y A numeric or factor vector containing the outcome for each sample.
method A string specifying which classification or regression model to use. Possible val-
ues are found using names(getModelInfo()). See http://topepo.github.
io/caret/train-models-by-tag.html. A list of functions can also be passed
for a custom model function. See http://topepo.github.io/caret/using-your-own-model-in-tra
html for details.
preProcess A string vector that defines a pre-processing of the predictor data. Current pos-
sibilities are "BoxCox", "YeoJohnson", "expoTrans", "center", "scale", "range",
164 train
Details
train can be used to tune models by picking the complexity parameters that are associated with
the optimal resampling statistics. For particular model, a grid of parameters (if any) is created and
the model is trained on slightly different data for each candidate combination of tuning parameters.
train 165
Across each data set, the performance of held-out samples is calculated and the mean and stan-
dard deviation is summarized for each combination. The combination with the optimal resampling
statistic is chosen as the final model and the entire training set is used to fit a final model.
The predictors in x can be most any object as long as the underlying model fit function can deal with
the object class. The function was designed to work with simple matrices and data frame inputs,
so some functionality may not work (e.g. pre-processing). When using string kernels, the vector of
character strings should be converted to a matrix with a single column.
More details on this function can be found at http://topepo.github.io/caret/model-training-and-tuning.
html.
A variety of models are currently available and are enumerated by tag (i.e. their model characteris-
tics) at http://topepo.github.io/caret/train-models-by-tag.html.
More details on using recipes can be found at http://topepo.github.io/caret/using-recipes-with-train.
html. Note that case weights can be passed into train using a role of "case weight" for a single
variable. Also, if there are non-predictor columns that should be used when determining the model’s
performance metrics, the role of "performance var" can be used with multiple columns and these
will be made available during resampling to the summaryFunction function.
Value
A list is returned of class train containing:
method The chosen model.
modelType An identifier of the model type.
results A data frame the training error rate and values of the tuning parameters.
bestTune A data frame with the final parameters.
call The (matched) function call with dots expanded
dots A list containing any ... values passed to the original call
metric A string that specifies what summary metric will be used to select the optimal
model.
control The list of control parameters.
preProcess Either NULL or an object of class preProcess
finalModel A fit object using the best parameters
trainingData A data frame
resample A data frame with columns for each performance metric. Each row corre-
sponds to each resample. If leave-one-out cross-validation or out-of-bag esti-
mation methods are requested, this will be NULL. The returnResamp argument
of trainControl controls how much of the resampled results are saved.
perfNames A character vector of performance metrics that are produced by the summary
function
maximize A logical recycled from the function arguments.
yLimits The range of the training set outcomes.
times A list of execution times: everything is for the entire call to train, final
for the final model fit and, optionally, prediction for the time to predict new
samples (see trainControl)
166 train
Author(s)
Max Kuhn (the guts of train.formula were based on Ripley’s nnet.formula)
References
http://topepo.github.io/caret/
Kuhn (2008), “Building Predictive Models in R Using the caret” (http://www.jstatsoft.org/
article/view/v028i05/v28i05.pdf)
https://topepo.github.io/recipes/
See Also
models, trainControl, update.train, modelLookup, createFolds, recipe
Examples
## Not run:
#######################################
## Classification Example
data(iris)
TrainData <- iris[,1:4]
TrainClasses <- iris[,5]
library(MASS)
nnetFit <- train(TrainData, TrainClasses,
method = "nnet",
preProcess = "range",
tuneLength = 2,
trace = FALSE,
maxit = 100)
#######################################
## Regression Example
library(mlbench)
train 167
data(BostonHousing)
library(rpart)
rpartFit <- train(medv ~ .,
data = BostonHousing,
method = "rpart",
tuneLength = 9)
#######################################
## Example with a custom metric
#######################################
## Example with a recipe
data(cox2)
library(recipes)
set.seed(888)
cox2_lm <- train(cox2_recipe,
data = cox2,
method = "lm",
trControl = trainControl(method = "cv"))
#######################################
## Parallel Processing Example via multicore package
## library(doMC)
## registerDoMC(2)
## or use:
## library(doMPI) or
## library(doParallel) or
## library(doSMP) and so on
## End(Not run)
Description
Control the computational nuances of the train function
Usage
trainControl(
trainControl 169
method = "boot",
number = ifelse(grepl("cv", method), 10, 25),
repeats = ifelse(grepl("[d_]cv$", method), 1, NA),
p = 0.75,
search = "grid",
initialWindow = NULL,
horizon = 1,
fixedWindow = TRUE,
skip = 0,
verboseIter = FALSE,
returnData = TRUE,
returnResamp = "final",
savePredictions = FALSE,
classProbs = FALSE,
summaryFunction = defaultSummary,
selectionFunction = "best",
preProcOptions = list(thresh = 0.95, ICAcomp = 3, k = 5, freqCut = 95/5, uniqueCut =
10, cutoff = 0.9),
sampling = NULL,
index = NULL,
indexOut = NULL,
indexFinal = NULL,
timingSamps = 0,
predictionBounds = rep(FALSE, 2),
seeds = NA,
adaptive = list(min = 5, alpha = 0.05, method = "gls", complete = TRUE),
trim = FALSE,
allowParallel = TRUE
)
Arguments
method The resampling method: "boot", "boot632", "optimism_boot", "boot_all",
"cv", "repeatedcv", "LOOCV", "LGOCV" (for repeated training/test splits), "none"
(only fits one model to the entire training set), "oob" (only for random for-
est, bagged trees, bagged earth, bagged flexible discriminant analysis, or con-
ditional tree forest models), timeslice, "adaptive_cv", "adaptive_boot" or
"adaptive_LGOCV"
number Either the number of folds or number of resampling iterations
repeats For repeated k-fold cross-validation only: the number of complete sets of folds
to compute
p For leave-group out cross-validation: the training percentage
search Either "grid" or "random", describing how the tuning parameter grid is deter-
mined. See details below.
initialWindow, horizon, fixedWindow, skip
possible arguments to createTimeSlices when method is timeslice.
verboseIter A logical for printing a training log.
170 trainControl
Details
When setting the seeds manually, the number of models being evaluated is required. This may not
be obvious as train does some optimizations for certain models. For example, when tuning over
PLS model, the only model that is fit is the one with the largest number of components. So if the
model is being tuned over comp in 1:10, the only model fit is ncomp = 10. However, if the vector
of integers used in the seeds arguments is longer than actually needed, no error is thrown.
Using method = "none" and specifying more than one model in train’s tuneGrid or tuneLength
arguments will result in an error.
Using adaptive resampling when method is either "adaptive_cv", "adaptive_boot" or "adaptive_LGOCV",
the full set of resamples is not run for each model. As resampling continues, a futility analysis is
conducted and models with a low probability of being optimal are removed. These features are
experimental. See Kuhn (2014) for more details. The options for this procedure are:
• min: the minimum number of resamples used before models are removed
• alpha: the confidence level of the one-sided intervals used to measure futility
• method: either generalized least squares (method = "gls") or a Bradley-Terry model (method
= "BT")
• complete: if a single parameter value is found before the end of resampling, should the full
set of resamples be computed for that parameter. )
The option search = "grid" uses the default grid search routine. When search = "random", a
random search procedure is used (Bergstra and Bengio, 2012). See http://topepo.github.io/
caret/random-hyperparameter-search.html for details and an example.
The supported bootstrap methods are:
The "boot632" method should not to be confused with the 0.632+ estimator proposed later by the
same author.
Note that if index or indexOut are specified, the label shown by train may not be accurate since
these arguments supersede the method argument.
Value
An echo of the parameters specified
Author(s)
Max Kuhn
References
Efron (1983). “Estimating the error rate of a prediction rule: improvement on cross-validation”.
Journal of the American Statistical Association, 78(382):316-331
Efron, B., & Tibshirani, R. J. (1994). “An introduction to the bootstrap”, pages 249-252. CRC
press.
Bergstra and Bengio (2012), “Random Search for Hyper-Parameter Optimization”, Journal of Ma-
chine Learning Research, 13(Feb):281-305
Kuhn (2014), “Futility Analysis in the Cross-Validation of Machine Learning Models” http://
arxiv.org/abs/1405.6974,
Package website for subsampling: https://topepo.github.io/caret/subsampling-for-class-imbalances.
html
Examples
## Not run:
set.seed(123)
seeds <- vector(mode = "list", length = 51)
for(i in 1:50) seeds[[i]] <- sample.int(1000, 22)
set.seed(1)
mod <- train(Species ~ ., data = iris,
method = "knn",
tuneLength = 12,
train_model_list 173
trControl = ctrl)
set.seed(1)
mod2 <- train(Species ~ ., data = iris,
method = "knn",
tuneLength = 12,
trControl = ctrl2)
## End(Not run)
Description
These models are included in the package via wrappers for train. Custom models can also be
created. See the URL below.
AdaBoost Classification Trees (method = 'adaboost')
For classification using package fastAdaboost with tuning parameters:
• Number of Trees (nIter, numeric)
• Method (method, character)
AdaBoost.M1 (method = 'AdaBoost.M1')
For classification using packages adabag and plyr with tuning parameters:
• Number of Trees (mfinal, numeric)
• Max Tree Depth (maxdepth, numeric)
• Coefficient Type (coeflearn, character)
Adaptive Mixture Discriminant Analysis (method = 'amdai')
For classification using package adaptDA with tuning parameters:
• Model Type (model, character)
Adaptive-Network-Based Fuzzy Inference System (method = 'ANFIS')
For regression using package frbs with tuning parameters:
• Number of Fuzzy Terms (num.labels, numeric)
• Max. Iterations (max.iter, numeric)
174 train_model_list
Note: The prune option for this model enables the number of iterations to be determined by the
optimal AIC value across all iterations. See the examples in ?mboost::mstop. If pruning is not
used, the ensemble makes predictions using the exact value of the mstop tuning parameter value.
Boosted Generalized Linear Model (method = 'glmboost')
For classification and regression using packages plyr and mboost with tuning parameters:
Note: The prune option for this model enables the number of iterations to be determined by the
optimal AIC value across all iterations. See the examples in ?mboost::mstop. If pruning is not
used, the ensemble makes predictions using the exact value of the mstop tuning parameter value.
Boosted Linear Model (method = 'BstLm')
For classification and regression using packages bst and plyr with tuning parameters:
Note: Which terms enter the model in a nonlinear manner is determined by the number of unique
values for the predictor. For example, if a predictor only has four unique values, most basis expan-
sion method will fail because there are not enough granularity in the data. By default, a predictor
must have at least 10 unique values to be used in a nonlinear basis expansion. Unlike other packages
used by train, the gam package is fully loaded when this model is used.
Generalized Additive Model using Splines (method = 'bam')
For classification and regression using package mgcv with tuning parameters:
Note: Which terms enter the model in a nonlinear manner is determined by the number of unique
values for the predictor. For example, if a predictor only has four unique values, most basis expan-
sion method will fail because there are not enough granularity in the data. By default, a predictor
must have at least 10 unique values to be used in a nonlinear basis expansion. Unlike other packages
used by train, the mgcv package is fully loaded when this model is used.
Generalized Additive Model using Splines (method = 'gam')
For classification and regression using package mgcv with tuning parameters:
Note: Which terms enter the model in a nonlinear manner is determined by the number of unique
values for the predictor. For example, if a predictor only has four unique values, most basis expan-
sion method will fail because there are not enough granularity in the data. By default, a predictor
must have at least 10 unique values to be used in a nonlinear basis expansion. Unlike other packages
used by train, the mgcv package is fully loaded when this model is used.
Generalized Additive Model using Splines (method = 'gamSpline')
For classification and regression using package gam with tuning parameters:
Note: Which terms enter the model in a nonlinear manner is determined by the number of unique
values for the predictor. For example, if a predictor only has four unique values, most basis expan-
sion method will fail because there are not enough granularity in the data. By default, a predictor
must have at least 10 unique values to be used in a nonlinear basis expansion. Unlike other packages
used by train, the gam package is fully loaded when this model is used.
Generalized Linear Model (method = 'glm')
For classification and regression with no tuning parameters.
Generalized Linear Model with Stepwise Feature Selection (method = 'glmStepAIC')
For classification and regression using package MASS with no tuning parameters.
Generalized Partial Least Squares (method = 'gpls')
For classification using package gpls with tuning parameters:
Genetic Lateral Tuning and Rule Selection of Linguistic Fuzzy Systems (method = 'GFS.LT.RS')
For regression using package frbs with tuning parameters:
Note: After train completes, the keras model object is serialized so that it can be used between R
session. When predicting, the code will temporarily unsearalize the object. To make the predictions
more efficient, the user might want to use keras::unsearlize_model(object$finalModel$object)
in the current R session so that that operation is only done once. Also, this model cannot be run in
parallel due to the nature of how tensorflow does the computations. Unlike other packages used by
train, the dplyr package is fully loaded when this model is used.
Multilayer Perceptron Network with Dropout (method = 'mlpKerasDropoutCost')
For classification using package keras with tuning parameters:
Note: After train completes, the keras model object is serialized so that it can be used between R
session. When predicting, the code will temporarily unsearalize the object. To make the predictions
more efficient, the user might want to use keras::unsearlize_model(object$finalModel$object)
in the current R session so that that operation is only done once. Also, this model cannot be run
in parallel due to the nature of how tensorflow does the computations. Finally, the cost parame-
ter weights the first class in the outcome vector. Unlike other packages used by train, the dplyr
package is fully loaded when this model is used.
Multilayer Perceptron Network with Weight Decay (method = 'mlpKerasDecay')
For classification and regression using package keras with tuning parameters:
Note: After train completes, the keras model object is serialized so that it can be used between R
session. When predicting, the code will temporarily unsearalize the object. To make the predictions
more efficient, the user might want to use keras::unsearlize_model(object$finalModel$object)
in the current R session so that that operation is only done once. Also, this model cannot be run in
parallel due to the nature of how tensorflow does the computations. Unlike other packages used by
train, the dplyr package is fully loaded when this model is used.
Multilayer Perceptron Network with Weight Decay (method = 'mlpKerasDecayCost')
For classification using package keras with tuning parameters:
Note: After train completes, the keras model object is serialized so that it can be used between R
session. When predicting, the code will temporarily unsearalize the object. To make the predictions
more efficient, the user might want to use keras::unsearlize_model(object$finalModel$object)
in the current R session so that that operation is only done once. Also, this model cannot be run
in parallel due to the nature of how tensorflow does the computations. Finally, the cost parame-
ter weights the first class in the outcome vector. Unlike other packages used by train, the dplyr
package is fully loaded when this model is used.
Multivariate Adaptive Regression Spline (method = 'earth')
For classification and regression using package earth with tuning parameters:
Note: Unlike other packages used by train, the earth package is fully loaded when this model is
used.
Multivariate Adaptive Regression Splines (method = 'gcvEarth')
For classification and regression using package earth with tuning parameters:
Note: Unlike other packages used by train, the earth package is fully loaded when this model is
used.
Naive Bayes (method = 'naive_bayes')
For classification using package naivebayes with tuning parameters:
Note: Unlike other packages used by train, the obliqueRF package is fully loaded when this
model is used.
Oblique Random Forest (method = 'ORFridge')
For classification using package obliqueRF with tuning parameters:
Note: Unlike other packages used by train, the obliqueRF package is fully loaded when this
model is used.
Oblique Random Forest (method = 'ORFsvm')
For classification using package obliqueRF with tuning parameters:
Note: Unlike other packages used by train, the obliqueRF package is fully loaded when this
model is used.
Optimal Weighted Nearest Neighbor Classifier (method = 'ownn')
For classification using package snn with tuning parameters:
Note: Unlike other packages used by train, the plsRglm package is fully loaded when this model
is used.
Patient Rule Induction Method (method = 'PRIM')
For classification using package supervisedPRIM with tuning parameters:
Relevance Vector Machines with Radial Basis Function Kernel (method = 'rvmRadial')
For regression using package kernlab with tuning parameters:
Note: Unlike other packages used by train, the rrlda package is fully loaded when this model is
used.
Robust SIMCA (method = 'RSimca')
For classification using package rrcovHD with no tuning parameters.
Note: Unlike other packages used by train, the rrcovHD package is fully loaded when this model
is used.
ROC-Based Classifier (method = 'rocc')
For classification using package rocc with tuning parameters:
Note: As of version 3.0.0 of the kohonen package, the argument user.weights replaces the old
alpha parameter. user.weights is usually a vector of relative weights such as c(1,3) but is pa-
rameterized here as a proportion such as c(1-.75,.75) where the .75 is the value of the tuning
parameter passed to train and indicates that the outcome layer has 3 times the weight as the pre-
dictor layer.
Semi-Naive Structure Learner Wrapper (method = 'nbSearch')
For classification using package bnclassify with tuning parameters:
• Number of Folds (k, numeric)
• Minimum Absolute Improvement (epsilon, numeric)
• Smoothing Parameter (smooth, numeric)
• Final Smoothing Parameter (final_smooth, numeric)
• Search Direction (direction, character)
Shrinkage Discriminant Analysis (method = 'sda')
For classification using package sda with tuning parameters:
• Diagonalize (diagonal, logical)
• shrinkage (lambda, numeric)
SIMCA (method = 'CSimca')
For classification using packages rrcov and rrcovHD with no tuning parameters.
Simplified TSK Fuzzy Rules (method = 'FS.HGD')
For regression using package frbs with tuning parameters:
• Number of Fuzzy Terms (num.labels, numeric)
• Max. Iterations (max.iter, numeric)
Single C5.0 Ruleset (method = 'C5.0Rules')
For classification using package C50 with no tuning parameters.
Single C5.0 Tree (method = 'C5.0Tree')
For classification using package C50 with no tuning parameters.
Single Rule Classification (method = 'OneR')
For classification using package RWeka with no tuning parameters.
Sparse Distance Weighted Discrimination (method = 'sdwd')
For classification using package sdwd with tuning parameters:
• L1 Penalty (lambda, numeric)
• L2 Penalty (lambda2, numeric)
Sparse Linear Discriminant Analysis (method = 'sparseLDA')
For classification using package sparseLDA with tuning parameters:
• Number of Predictors (NumVars, numeric)
• Lambda (lambda, numeric)
train_model_list 201
Support Vector Machines with Radial Basis Function Kernel (method = 'svmRadial')
For classification and regression using package kernlab with tuning parameters:
train_model_list 203
Support Vector Machines with Radial Basis Function Kernel (method = 'svmRadialCost')
For classification and regression using package kernlab with tuning parameters:
Support Vector Machines with Radial Basis Function Kernel (method = 'svmRadialSigma')
For classification and regression using package kernlab with tuning parameters:
Note: This SVM model tunes over the cost parameter and the RBF kernel parameter sigma. In
the latter case, using tuneLength will, at most, evaluate six values of the kernel parameter. This
enables a broad search over the cost parameter and a relatively narrow search over sigma
Support Vector Machines with Spectrum String Kernel (method = 'svmSpectrumString')
For classification and regression using package kernlab with tuning parameters:
Note: This model creates predictions using the mean of the posterior distributions but sets some
parameters specifically to zero based on the tuning parameter sparsity. For example, when
sparsity = .5, only coefficients where at least half the posterior estimates are nonzero are used.
The lasso (method = 'lasso')
For regression using package elasticnet with tuning parameters:
Tree Augmented Naive Bayes Classifier Structure Learner Wrapper (method = 'tanSearch')
For classification using package bnclassify with tuning parameters:
Tree Augmented Naive Bayes Classifier with Attribute Weighting (method = 'awtan')
For classification using package bnclassify with tuning parameters:
References
Description
update allows a user to over-ride the search iteration selection process.
Based on the results of plotting a gafs or safs object, these functions can be used to supersede the
number of iterations determined analytically from the resamples.
Any values of ... originally passed to gafs or safs are automatically passed on to the updated
model (i.e. they do not need to be supplied again to update.
Usage
## S3 method for class 'safs'
update(object, iter, x, y, ...)
Arguments
object An object produced by gafs or safs
iter a single numeric integer
x, y the original training data used in the call to gafs or safs. Only required for
non-recipe methods.
... not currently used
Value
an object of class gafs or safs
Author(s)
Max Kuhn
See Also
gafs, safs
Examples
## Not run:
set.seed(1)
train_data <- twoClassSim(100, noiseVars = 10)
test_data <- twoClassSim(10, noiseVars = 10)
## A short example
ctrl <- safsControl(functions = rfSA,
method = "cv",
206 update.train
number = 3)
## End(Not run)
Description
update allows a user to over-ride the tuning parameter selection process by specifying a set of
tuning parameters or to update the model object to the latest version of this package.
Usage
## S3 method for class 'train'
update(object, param = NULL, ...)
Arguments
object an object of class train
param a data frame or named list of all tuning parameters
... not currently used
Details
If the model object was created with version 5.17-7 or earlier, the underlying package structure was
different. To make old train objects consistent with the new structure, use param = NULL to get the
same object back with updates.
To update the model parameters, the training data must be stored in the model object (see the option
returnData in trainControl. Also, all tuning parameters must be specified in the param slot.
All other options are held constant, including the original pre-processing (if any), options passed in
using code... and so on. When printing, the verbiage "The tuning parameter was set manually." is
used to describe how the tuning parameters were created.
Value
a new train object
varImp 207
Author(s)
Max Kuhn
See Also
train, trainControl
Examples
## Not run:
data(iris)
TrainData <- iris[,1:4]
TrainClasses <- iris[,5]
## End(Not run)
Description
A generic method for calculating variable importance for objects produced by train and method
specific methods
Usage
varImp(object, ...)
varImp(object, ...)
Arguments
object an object corresponding to a fitted model
... parameters to pass to the specific varImp methods
weights a numeric vector of length two that weighs the usage of variables in the rule
conditions and the usage in the linear models (see details below).
cuts the number of rule sets to use in the model (for partDSA only)
lambda a single value of the penalty parameter
surrogates should surrogate splits contribute to the importance calculation?
competes should competing splits contribute to the importance calculation?
numTrees the number of iterations (trees) to use in a boosted tree model
threshold the shrinkage threshold (pamr models only)
data the training set predictors (pamr models only)
estimate which estimate of performance should be used? See mvrVal
value the statistic that will be used to calculate importance: either gcv, nsubsets, or
rss
useModel use a model based technique for measuring variable importance? This is only
used for some models (lm, pls, rf, rpart, gbm, pam and mars)
210 varImp
nonpara should nonparametric methods be used to assess the relationship between the
features and response (only used with useModel = FALSE and only passed to
filterVarImp).
scale should the importance values be scaled to 0 and 100?
Details
For models that do not have corresponding varImp methods, see filterVarImp.
Otherwise:
Linear Models: the absolute value of the t-statistic for each model parameter is used.
glmboost and glmnet: the absolute value of the coefficients corresponding the the tuned model are
used.
Random Forest: varImp.randomForest and varImp.RandomForest are wrappers around the im-
portance functions from the randomForest and party packages, respectively.
Partial Least Squares: the variable importance measure here is based on weighted sums of the
absolute regression coefficients. The weights are a function of the reduction of the sums of squares
across the number of PLS components and are computed separately for each outcome. Therefore,
the contribution of the coefficients are weighted proportionally to the reduction in the sums of
squares.
Recursive Partitioning: The reduction in the loss function (e.g. mean squared error) attributed to
each variable at each split is tabulated and the sum is returned. Also, since there may be candidate
variables that are important but are not used in a split, the top competing variables are also tabulated
at each split. This can be turned off using the maxcompete argument in rpart.control. This
method does not currently provide class-specific measures of importance when the response is a
factor.
Bagged Trees: The same methodology as a single tree is applied to all bootstrapped trees and the
total importance is returned
Boosted Trees: varImp.gbm is a wrapper around the function from that package (see the gbm
package vignette)
Multivariate Adaptive Regression Splines: MARS models include a backwards elimination fea-
ture selection routine that looks at reductions in the generalized cross-validation (GCV) estimate
of error. The varImp function tracks the changes in model statistics, such as the GCV, for each
predictor and accumulates the reduction in the statistic when each predictor’s feature is added to the
model. This total reduction is used as the variable importance measure. If a predictor was never used
in any of the MARS basis functions in the final model (after pruning), it has an importance value of
zero. Prior to June 2008, the package used an internal function for these calculations. Currently, the
varImp is a wrapper to the evimp function in the earth package. There are three statistics that can
be used to estimate variable importance in MARS models. Using varImp(object,value = "gcv")
tracks the reduction in the generalized cross-validation statistic as terms are added. However, there
are some cases when terms are retained in the model that result in an increase in GCV. Negative
variable importance values for MARS are set to zero. Alternatively, using varImp(object,value
= "rss") monitors the change in the residual sums of squares (RSS) as terms are added, which
will never be negative. Also, the option varImp(object,value =" nsubsets"), which counts the
number of subsets where the variable is used (in the final, pruned model).
Nearest shrunken centroids: The difference between the class centroids and the overall centroid is
used to measure the variable influence (see pamr.predict). The larger the difference between the
varImp 211
class centroid and the overall center of the data, the larger the separation between the classes. The
training set predictions must be supplied when an object of class pamrtrained is given to varImp.
Cubist: The Cubist output contains variable usage statistics. It gives the percentage of times where
each variable was used in a condition and/or a linear model. Note that this output will probably be
inconsistent with the rules shown in the output from summary.cubist. At each split of the tree,
Cubist saves a linear model (after feature selection) that is allowed to have terms for each variable
used in the current split or any split above it. Quinlan (1992) discusses a smoothing algorithm
where each model prediction is a linear combination of the parent and child model along the tree.
As such, the final prediction is a function of all the linear models from the initial node to the terminal
node. The percentages shown in the Cubist output reflects all the models involved in prediction (as
opposed to the terminal models shown in the output). The variable importance used here is a linear
combination of the usage in the rule conditions and the model.
PART and JRip: For these rule-based models, the importance for a predictor is simply the number
of rules that involve the predictor.
C5.0: C5.0 measures predictor importance by determining the percentage of training set samples
that fall into all the terminal nodes after the split. For example, the predictor in the first split
automatically has an importance measurement of 100 percent since all samples are affected by this
split. Other predictors may be used frequently in splits, but if the terminal nodes cover only a
handful of training set samples, the importance scores may be close to zero. The same strategy
is applied to rule-based models and boosted versions of the model. The underlying function can
also return the number of times each predictor was involved in a split by using the option metric =
"usage".
Neural Networks: The method used here is based on Gevrey et al (2003), which uses combinations
of the absolute values of the weights. For classification models, the class-specific importances will
be the same.
Recursive Feature Elimination: Variable importance is computed using the ranking method used
for feature selection. For the final subset size, the importances for the models across all resamples
are averaged to compute an overall value.
Feature Selection via Univariate Filters, the percentage of resamples that a predictor was selected
is determined. In other words, an importance of 0.50 means that the predictor survived the filter in
half of the resamples.
Value
A data frame with class c("varImp.train","data.frame") for varImp.train or a matrix for
other models.
Author(s)
Max Kuhn
References
Gevrey, M., Dimopoulos, I., & Lek, S. (2003). Review and comparison of methods to study the
contribution of variables in artificial neural network models. Ecological Modelling, 160(3), 249-
264.
212 varImp.gafs
Quinlan, J. (1992). Learning with continuous classes. Proceedings of the 5th Australian Joint
Conference On Artificial Intelligence, 343-348.
Description
Variable importance scores for safs and gafs objects.
Usage
## S3 method for class 'gafs'
varImp(
object,
metric = object$control$metric["external"],
maximize = object$control$maximize["external"],
...
)
Arguments
object an safs or gafs object
metric a metric to compute importance (see Details below)
maximize are larger values of the metric better?
... not currently uses
Details
A crude measure of importance is computed for thee two search procedures. At the end of a search
process, the difference in the fitness values is computed for models with and without each feature
(based on the search history). If a predictor has at least two subsets that include and did not include
the predictor, a t-statistic is computed (otherwise a value of NA is assigned to the predictor).
This computation is done separately for each resample and the t-statistics are averaged (NA values
are ignored) and this average is reported as the importance. If the fitness value should be minimized,
the negative value of the t-statistic is used in the average.
As such, the importance score reflects the standardized increase in fitness that occurs when the
predict is included in the subset. Values near zero (or negative) indicate that the predictor may not
be important to the model.
Value
a data frame where the rownames are the predictor names and the column is the average t-statistic
Author(s)
Max Kuhn
var_seq 213
See Also
safs, gafs
Description
Usage
Arguments
Details
If the number of predictors is less than 500, a simple sequence of values of length len is generated
between 2 and p. For larger numbers of predictors, the sequence is created using log2 steps.
If len = 1, the defaults from the randomForest package are used.
Value
a numeric vector
Author(s)
Max Kuhn
Examples
Description
Lattice and ggplot functions for visualizing resampling results across models
Usage
## S3 method for class 'resamples'
xyplot(
x,
data = NULL,
what = "scatter",
models = NULL,
metric = x$metric[1],
units = "min",
...
)
...
)
Arguments
x an object generated by resamples
data Only used for the ggplot method; an object generated by resamples
what for xyplot, the type of plot. Valid options are: "scatter" (for a plot of the resam-
pled results between two models), "BlandAltman" (a Bland-Altman, aka MA
plot between two models), "tTime" (for the total time to run train versus the
metric), "mTime" (for the time to build the final model) or "pTime" (the time to
predict samples - see the timingSamps options in trainControl, rfeControl,
or sbfControl)
models a character string for which models to plot. Note: xyplot requires one or two
models whereas the other methods can plot more than two.
metric a character string for which metrics to use as conditioning variables in the plot.
splom requires exactly one metric when variables = "models" and at least two
when variables = "metrics".
units either "sec", "min" or "hour"; which what is either "tTime", "mTime" or "pTime",
how should the timings be scaled?
... further arguments to pass to either histogram, densityplot, xyplot, dotplot
or splom
variables either "models" or "metrics"; which variable should be treated as the scatter plot
variables?
panelRange a common range for the panels. If NULL, the panel ranges are derived from the
values across all the models
conf.level the confidence level for intervals about the mean (obtained using t.test)
mapping, environment
Not used.
Details
The ideas and methods here are based on Hothorn et al. (2005) and Eugster et al. (2008).
dotplot and ggplot plots the average performance value (with two-sided confidence limits) for
each model and metric.
216 xyplot.resamples
densityplot and bwplot display univariate visualizations of the resampling distributions while
splom shows the pair-wise relationships.
Value
a lattice object
Author(s)
Max Kuhn
References
Hothorn et al. The design and analysis of benchmark experiments. Journal of Computational and
Graphical Statistics (2005) vol. 14 (3) pp. 675-699
Eugster et al. Exploratory and inferential analysis of benchmark experiments. Ludwigs-Maximilians-
Universitat Munchen, Department of Statistics, Tech. Rep (2008) vol. 30
See Also
resamples, dotplot, bwplot, densityplot, xyplot, splom
Examples
## Not run:
#load(url("http://topepo.github.io/caret/exampleModels.RData"))
dotplot(resamps,
scales =list(x = list(relation = "free")),
between = list(x = 2))
bwplot(resamps,
metric = "RMSE")
densityplot(resamps,
auto.key = list(columns = 3),
pch = "|")
xyplot(resamps,
models = c("CART", "MARS"),
metric = "RMSE")
## End(Not run)
Index
218
INDEX 219
avNNet, 5 BloodBrain, 15
pcaNNet, 98 boxcox, 16, 17, 121
∗Topic print BoxCoxTrans, 15, 120, 121
print.train, 123 bwplot, 40, 216
∗Topic regression bwplot.diff.resamples, 38
bagEarth, 11 bwplot.diff.resamples
bagFDA, 13 (dotplot.diff.resamples), 40
predict.bagEarth, 113 bwplot.resamples, 130
∗Topic utilities bwplot.resamples (xyplot.resamples), 214
as.matrix.confusionMatrix, 4
BoxCoxTrans, 15 calibration, 17
confusionMatrix, 24 caretFuncs (pickSizeBest), 100
confusionMatrix.train, 27 caretGA, 57, 61
createDataPartition, 29 caretGA (gafs_initial), 61
defaultSummary, 32 caretSA, 61
downSample, 41 caretSA (safs_initial), 143
gafsControl, 58 caretSBF, 20, 151
maxDissim, 80 cars, 21
modelLookup, 83 cat, 53
nearZeroVar, 85 cforest, 118
preProcess, 118 checkConditionalX, 120
print.confusionMatrix, 122 checkConditionalX (nearZeroVar), 85
resampleSummary, 131 checkInstall (modelLookup), 83
rfeControl, 136 checkResamples (nearZeroVar), 85
sbfControl, 149 class2ind (dummyVars), 42
trainControl, 168 classDist, 22
cluster (prcomp.resamples), 111
absorp (tecator), 159 compare_models (diff.resamples), 36
anneal, 51 confusionMatrix, 4, 24, 28, 33, 90, 122, 126,
anovaScores, 150 161
anovaScores (caretSBF), 20 confusionMatrix.rfe
as.data.frame.resamples (resamples), 128 (confusionMatrix.train), 27
as.matrix.confusionMatrix, 4, 26 confusionMatrix.sbf
as.matrix.resamples (resamples), 128 (confusionMatrix.train), 27
as.table.confusionMatrix, 26 confusionMatrix.train, 27
as.table.confusionMatrix contr.dummy (dummyVars), 42
(as.matrix.confusionMatrix), 4 contr.ltfr (dummyVars), 42
avNNet, 5 contr.treatment, 43, 44
contrasts, 43, 44
bag, 8 cox2, 29
bagControl (bag), 8 cox2Class (cox2), 29
bagEarth, 11, 53, 114, 118 cox2Descr (cox2), 29
bagFDA, 13, 118 cox2IC50 (cox2), 29
bagging, 118 createDataPartition, 29
barchart, 112, 113 createFolds, 166
bbbDescr (BloodBrain), 15 createFolds (createDataPartition), 29
best, 170 createMultiFolds (createDataPartition),
best (oneSE), 93 29
binom.test, 19, 25, 26 createResample (createDataPartition), 29
220 INDEX
var_seq, 213