# train

0th

Percentile

##### Fit Predictive Models over Different Tuning Parameters

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.

Keywords
models
##### Usage
train(x, ...)## S3 method for class 'default':
train(x, y,
method = "rf",
...,
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, ..., subset, na.action, contrasts = NULL)
##### Arguments
x
a data frame containing training data where samples are in rows and features are in columns.
y
a numeric or factor vector containing the outcome for each sample.
form
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.)
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
contrasts
a list of contrasts to be used for some or all of the factors appearing as variables in the model formula.
method
a string specifying which classification or regression model to use. Possible values are: lm, rda, lda, gbm, rf, nnet, multinom, gpls, lvq
...
arguments passed to the classification or regression routine (such as randomForest). Errors will occur if values for tuning parameters are passed here.
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
maximize
a logical: should the metric be maximized or minimized?
trControl
a list of values that define how this function acts. See trainControl. (NOTE: If given, this argument must be named.)
tuneGrid
a data frame with possible tuning values. The columns are named the same as the tuning parameters in each method preceded by a period (e.g. .decay, .lambda). See the function createGrid in this
tuneLength
an integer denoting the number of levels for each tuning parameters that should be generated by createGrid. (NOTE: If given, this argument must be named.)
##### 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. 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.

lccc{ Model method Value Package Tuning Parameter(s) Recursive partitioning rpart rpart maxdepth ctree party mincriterion Boosted trees gbm gbm interaction depth, n.trees, shrinkage blackboost mboost maxdepth, mstop ada ada maxdepth, iter, nu Boosted regression models glmboost mboost mstop gamboost mboost mstop logitboost caTools nIter Random forests rf randomForest mtry cforest party mtry Bagged trees treebag ipred None Neural networks nnet nnet decay, size Projection pursuit regression ppr stats nterms Partial least squares pls pls, caret ncomp Support vector machines (RBF) svmradial kernlab sigma, C Support vector machines (polynomial) svmpoly kernlab scale, degree, C Relevance vector machines (RBF) rvmradial kernlab sigma Relevance vector machines (polynomial) rvmpoly kernlab scale, degree Least squares support vector machines (RBF) lssvmradial kernlab sigma Gaussian processes (RBF) guassprRadial kernlab sigma Gaussian processes (polynomial) guassprPoly kernlab scale, degree Linear least squares lm stats None Multivariate adaptive regression splines earth earth degree, nprune Bagged MARS bagEarth caret, earth degree, nprune M5 rules M5Rules RWeka pruned Elastic net enet elasticnet lambda, fraction The Lasso enet elasticnet fraction Penalized linear models penalized penalized lambda1, lambda2 Supervised principal components superpc superpc n.components, threshold Linear discriminant analysis lda MASS None Stabilised Linear discriminant analysis slda ipred None Stepwise diagonal discriminant analysis sddaLDA, sddaQDA SDDA None Logistic/multinomial regression multinom nnet decay Regularized discriminant analysis rda klaR lambda, gamma Stabilised linear discriminant analysis slda ipred None Flexible discriminant analysis (MARS) fda mda, earth degree, nprune Bagged FDA bagFDA caret, earth degree, nprune C4.5 decision trees J48 RWeka C k nearest neighbors knn3 caret k Nearest shrunken centroids pam pamr threshold Naive Bayes nb klaR usekernel Generalized partial least squares gpls gpls K.prov Learned vector quantization lvq class k }

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.

##### Value

• A list is returned of class train containing:
• modelTypean identifier of the model type.
• resultsa data frame the training error rate and values of the tuning parameters.
• callthe (matched) function call with dots expanded
• dotsa list containing any ... values passed to the original call
• metrica string that specifies what summary metric will be used to select the optimal model.
• trControlthe list of control parameters.
• finalModelan fit object using the best parameters
• trainingDataa data frame
• resampleA data frame with columns for each performance metric. Each row corresponds to each resample. If leave-one-out cross-validation or out-of-bag estimation methods are requested, this will be NULL. The returnResamp argument of trainControl controls how much of the resampled results are saved.
• perfNamesa character vector of performance metrics that are produced by the summary function
• maximizea logical recycled from the function arguments.

trainControl, createGrid, createFolds

##### Aliases
• train
• train.default
• train.formula
##### Examples
data(iris)
TrainData <- iris[,1:4]
TrainClasses <- iris[,5]

knnFit1 <- train(TrainData, TrainClasses,
"knn",
tuneLength = 10,
trControl = trainControl(method = "cv"))

knnFit2 <- train(TrainData, TrainClasses,
"knn", tuneLength = 10,
trControl = trainControl(method = "boot"))

library(MASS)
nnetFit <- train(TrainData, TrainClasses,
"nnet",
tuneLength = 2,
trace = FALSE,
maxit = 100)

library(mlbench)
data(BostonHousing)

lmFit <- train(medv ~ . + rm:lstat,
data = BostonHousing,
"lm")

library(rpart)
rpartFit <- train(medv ~ .,
data = BostonHousing,
"rpart",
tuneLength = 9)
Documentation reproduced from package caret, version 3.45, License: GPL-2

### Community examples

Looks like there are no examples yet.