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.
## S3 method for class 'default': train(x, y, method = "rf", ..., metric = ifelse(is.factor(y), "Accuracy", "RMSE"), trControl = trainControl(), tuneGrid = NULL, tuneLength = 3)
- a data frame containing training data where samples are in rows and features are in columns.
- a numeric or factor vector containing the outcome for each sample.
- a string specifying which classification or regression model to use. Possible values are:
- arguments passed to the classification or regression routine (such as
randomForest). Errors will occur if values for tuning parameters are passed here.
- a string that specifies what summary metric will be used to select the optimal model. Possible values are "RMSE" and "Rsquared" for regression and "Accuracy" and "Kappa" for classification.(NOTE: If given, this argument must be named.)
- a list of values that define how this function acts. See
trainControl. (NOTE: If given, this argument must be named.)
- 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
- an integer denoting the number of levels for each tuning parameters that should be
createGrid. (NOTE: If given, this argument must be named.)
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.
train function does not support model specification via a formula. It assumes that all of the predictors are numeric (perhaps generated by
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
method Value Package Tuning Parameter(s)
Boosted regression models
Partial least squares
Support Vector Machines (RBF)
Support Vector Machines (polynomial)
Linear least squares
Linear discriminant analysis
Regularized discriminant analysis
Flexible discriminant analysis (MARS)
k nearest neighbors
Nearest shrunken centroids
Generalized partial least squares
Learned vector quantization
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
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
The vignette entitled "caret Manual -- Model Building" has more details and examples related to this function.
- A list is returned of class
modelType an identifier of the model type. results a data frame the training error rate and values of the tuning 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. trControl the list of control parameters. finalModel an fit object using the best parameters trainingData a data frame resample A data frame with columns for each performance metric. Each row corresponds to each resample. If leave-group-out cross-validation or out-of-bag estimation methods are requested, this will be
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 = 4, trace = FALSE, maxit = 2000) library(mlbench) data(BostonHousing) # for illustration, converting factors trainX <- model.matrix(medv ~ . - 1, BostonHousing) lmFit <- train(trainX, BostonHousing$medv, "lm") library(rpart) rpartFit <- train(trainX, BostonHousing$medv, "rpart", tuneLength = 9)