
train(x, ...)## S3 method for class 'default':
train(x, y,
method = "rf",
preProcess = NULL,
...,
weights = NULL,
metric = ifelse(is.factor(y), "Accuracy", "RMSE"),
maximize = ifelse(metric == "RMSE", FALSE, TRUE),
trControl = trainControl(),
tuneGrid = NULL,
tuneLength = 3)
## S3 method for class 'formula':
train(form, data, ..., weights, subset, na.action, contrasts = NULL)
train
containing:NULL
or an object of class preProcess
NULL
. The returnResamp
argument of trainControl
controls how much of the resampled results are saved.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
)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. Across each data set, the performance of held-out samples is calculated and the mean and standard 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.A variety of models are currently available. The table below enumerates the models and the values of the method
argument, as well as the complexity parameters used by train
.
method
Value Package Tuning Parameter(s)
Generalized linear model glm
glmStepAIC
gam
select
, method
gamLoess
span
, degree
gamSpline
df
Recursive partitioning rpart
cp
rpart2
maxdepth
ctree
mincriterion
ctree2
maxdepth
Boosted trees gbm
interaction depth
,
n.trees
, shrinkage
blackboost
maxdepth
, mstop
ada
maxdepth
, iter
, nu
bstTree
maxdepth
, mstop
, nu
Boosted regression models glmboost
mstop
gamboost
mstop
logitBoost
nIter
bstLs
mstop
, nu
bstSm
mstop
, nu
Random forests rf
mtry
parRF
mtry
cforest
mtry
Boruta
mtry
Bagging treebag
bag
vars
logicBag
ntrees
, nleaves
Other Trees nodeHarvest
maxinter
, node
partDSA
cut.off.growth
, MPD
Logic Regression logreg
ntrees
, treesize
Elastic net (glm) glmnet
alpha
, lambda
Neural networks nnet
decay
, size
neuralnet
layer1
, layer2
, layer3
pcaNNet
decay
, size
avNNet
decay
, size
, bag
Projection pursuit regression ppr
nterms
Principal component regression pcr
ncomp
Independent component regression icr
n.comp
Partial least squares pls
ncomp
simpls
ncomp
widekernelpls
ncomp
Sparse partial least squares spls
K
, eta
, kappa
Support vector machines svmLinear
C
svmRadial
sigma
, C
svmRadialCost
C
svmPoly
scale
, degree
, C
Relevance vector machines rvmLinear
rvmRadial
sigma
rvmPoly
scale
, degree
Least squares support vector machines lssvmRadial
sigma
Gaussian processes guassprLinearl
guassprRadial
sigma
guassprPoly
scale
, degree
Linear least squares lm
lmStepAIC
leapForward
nvmax
leapBackward
nvmax
leapSeq
nvmax
Robust linear regression rlm
earth
degree
, nprune
gcvEarth
degree
Bagged MARS bagEarth
degree
, nprune
Rule Based Regression M5Rules
pruned
, smoothed
M5
pruned
, smoothed
, rules
cubist
committees
, neighbors
Penalized linear models penalized
lambda1
, lambda2
ridge
lambda
enet
lambda
, fraction
lars
fraction
lars2
steps
enet
fraction
foba
lambda
, k
Supervised principal components superpc
n.components
, threshold
Quantile regression forests qrf
mtry
Quantile regression neural networks qrnn
n.hidden
, penalty
, bag
Linear discriminant analysis lda
Linda
qda
QdaCov
slda
hda
newdim
, lambda
, gamma
Stepwise discriminant analysis stepLDA
maxvar
, direction
stepQDA
maxvar
, direction
Stepwise diagonal discriminant analysis sddaLDA
sddaQDA
sda
diagonal
Sparse linear discriminant analysis sparseLDA
NumVars
, lambda
Regularized discriminant analysis rda
lambda
, gamma
Mixture discriminant analysis mda
subclasses
Sparse mixture discriminant analysis smda
NumVars
, R
, lambda
Penalized discriminant analysis pda
lambda
pda2
df
Stabilised linear discriminant analysis slda
hdda
model
, threshold
Flexible discriminant analysis (MARS) fda
degree
, nprune
Robust Regularized Linear Discriminant Analysis rrlda
lambda
, alpha
Bagged FDA bagFDA
degree
, nprune
Logistic/multinomial regression multinom
decay
Penalized logistic regression plr
lambda
, cp
Rule--based classification J48
C
OneR
PART
threshold
, pruned
JRip
NumOpt
Logic Forests logforest
vbmpRadial
estimateTheta
k nearest neighbors knn3
k
Nearest shrunken centroids pam
threshold
scrda
alpha
, delta
Naive Bayes nb
usekernel
, fL
Generalized partial least squares gpls
K.prov
Learned vector quantization lvq
size
, k
ROC Curves rocc
rocc
xgenes
}
By default, the function createGrid
is used to define the candidate values of the tuning parameters. The user can also specify their own. To do this, a data fame is created with columns for each tuning parameter in the model. The column names must be the same as those listed in the table above with a leading dot. For example, ncomp
would have the column heading .ncomp
. This data frame can then be passed to createGrid
.
In some cases, models may require control arguments. These can be passed via the three dots argument. Note that some models can specify tuning parameters in the control objects. If specified, these values will be superseded by those given in the createGrid
argument.
The vignette entitled "caret Manual -- Model Building" has more details and examples related to this function.
train
can be used with "explicit parallelism", where different resamples (e.g. cross-validation group) and models can be split up and run on multiple machines or processors. By default, train
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 train
does not change; prior to the call to train
, a parallel backend is registered with
trainControl
, createGrid
, createFolds
#######################################
## Classification Example
data(iris)
TrainData <- iris[,1:4]
TrainClasses <- iris[,5]
knnFit1 <- train(TrainData, TrainClasses,
method = "knn",
preProcess = c("center", "scale"),
tuneLength = 10,
trControl = trainControl(method = "cv"))
knnFit2 <- train(TrainData, TrainClasses,
method = "knn",
preProcess = c("center", "scale"),
tuneLength = 10,
trControl = trainControl(method = "boot"))
library(MASS)
nnetFit <- train(TrainData, TrainClasses,
method = "nnet",
preProcess = "range",
tuneLength = 2,
trace = FALSE,
maxit = 100)
#######################################
## Regression Example
library(mlbench)
data(BostonHousing)
lmFit <- train(medv ~ . + rm:lstat,
data = BostonHousing,
"lm")
library(rpart)
rpartFit <- train(medv ~ .,
data = BostonHousing,
"rpart",
tuneLength = 9)
#######################################
## Example with a custom metric
madSummary <- function (data,
lev = NULL,
model = NULL)
{
out <- mad(data$obs - data$pred,
na.rm = TRUE)
names(out) <- "MAD"
out
}
robustControl <- trainControl(summaryFunction = madSummary)
marsGrid <- expand.grid(.degree = 1,
.nprune = (1:10) * 2)
earthFit <- train(medv ~ .,
data = BostonHousing,
"earth",
tuneGrid = marsGrid,
metric = "MAD",
maximize = FALSE,
trControl = robustControl)
#######################################
## Parallel Processing Example via multicore package
library(doMC)
registerDoMC(2)
## The code for train() does not change:
set.seed(1)
usingMC <- train(medv ~ .,
data = BostonHousing,
"glmboost",
trControl = mcControl)
## or use:
## library(doMPI) or
## library(doSMP) and so on
Run the code above in your browser using DataLab