train  R Documentation 
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.
train(x, ...) ## Default S3 method: train( x, y, method = "rf", preProcess = NULL, ..., weights = NULL, metric = ifelse(is.factor(y), "Accuracy", "RMSE"), maximize = ifelse(metric %in% c("RMSE", "logLoss", "MAE", "logLoss"), FALSE, TRUE), trControl = trainControl(), tuneGrid = NULL, tuneLength = ifelse(trControl$method == "none", 1, 3) ) ## S3 method for class 'formula' train(form, data, ..., weights, subset, na.action = na.fail, contrasts = NULL) ## S3 method for class 'recipe' train( x, data, method = "rf", ..., metric = ifelse(is.factor(y_dat), "Accuracy", "RMSE"), maximize = ifelse(metric %in% c("RMSE", "logLoss", "MAE"), FALSE, TRUE), trControl = trainControl(), tuneGrid = NULL, tuneLength = ifelse(trControl$method == "none", 1, 3) )
x 
For the default method, 
... 
Arguments passed to the classification or
regression routine (such as

y 
A numeric or factor vector containing the outcome for each sample. 
method 
A string specifying which classification or
regression model to use. Possible values are found using

preProcess 
A string vector that defines a preprocessing
of the predictor data. Current possibilities are "BoxCox",
"YeoJohnson", "expoTrans", "center", "scale", "range",
"knnImpute", "bagImpute", "medianImpute", "pca", "ica" and
"spatialSign". The default is no preprocessing. See

weights 
A numeric vector of case weights. This argument will only affect models that allow case weights. 
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 
tuneGrid 
A data frame with possible tuning values. The
columns are named the same as the tuning parameters. Use

tuneLength 
An integer denoting the amount of granularity
in the tuning parameter grid. By default, this argument is the
number of levels for each tuning parameters that should be
generated by 
form 
A formula of the form 
data 
Data frame from which variables specified in

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 
contrasts 
A list of contrasts to be used for some or all the factors appearing as variables in the model formula. 
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 heldout
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.
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.
preprocessing). 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/modeltrainingandtuning.html.
A variety of models are currently available and are enumerated by tag (i.e. their model characteristics) at http://topepo.github.io/caret/trainmodelsbytag.html.
More details on using recipes can be found at
http://topepo.github.io/caret/usingrecipeswithtrain.html.
Note that case weights can be passed into train
using a
role of "case weight"
for a single variable. Also, if
there are nonpredictor 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.
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 
finalModel 
A 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 leaveoneout
crossvalidation or outofbag estimation methods are requested,
this will be 
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: 
Max Kuhn (the guts of train.formula
were based
on Ripley's nnet.formula
)
http://topepo.github.io/caret/
Kuhn (2008), “Building Predictive Models in R Using the caret” (doi: 10.18637/jss.v028.i05)
https://topepo.github.io/recipes/
models
, trainControl
,
update.train
, modelLookup
,
createFolds
, recipe
## Not run: ####################################### ## 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, method = "lm") library(rpart) rpartFit < train(medv ~ ., data = BostonHousing, method = "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, method = "earth", tuneGrid = marsGrid, metric = "MAD", maximize = FALSE, trControl = robustControl) ####################################### ## Example with a recipe data(cox2) cox2 < cox2Descr cox2$potency < cox2IC50 library(recipes) cox2_recipe < recipe(potency ~ ., data = cox2) %>% ## Log the outcome step_log(potency, base = 10) %>% ## Remove sparse and unbalanced predictors step_nzv(all_predictors()) %>% ## Surface area predictors are highly correlated so ## conduct PCA just on these. step_pca(contains("VSA"), prefix = "surf_area_", threshold = .95) %>% ## Remove other highly correlated predictors step_corr(all_predictors(), starts_with("surf_area_"), threshold = .90) %>% ## Center and scale all of the nonPCA predictors step_center(all_predictors(), starts_with("surf_area_")) %>% step_scale(all_predictors(), starts_with("surf_area_")) 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) ## NOTE: don't run models form RWeka when using ### multicore. The session will crash. ## The code for train() does not change: set.seed(1) usingMC < train(medv ~ ., data = BostonHousing, method = "glmboost") ## or use: ## library(doMPI) or ## library(doParallel) or ## library(doSMP) and so on ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.