#######################################
## Classification Example
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)
#######################################
## 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 MPI
## A function to emulate lapply in parallel
mpiClacs <- function(X, FUN, ...)
{
theDots <- list(...)
parLapply(theDots$cl, X, FUN)
}
library(snow)
cl <- makeCluster(5, "MPI")
## 50 bootstrap models distributed across 5 workers
mpiControl <- trainControl(workers = 5,
number = 50,
computeFunction = mpiClacs,
computeArgs = list(cl = cl))
set.seed(1)
usingMPI <- train(medv ~ .,
data = BostonHousing,
"glmboost",
trControl = mpiControl)
stopCluster(cl)
#######################################
## Parallel Processing Example via NWS
nwsClacs <- function(X, FUN, ...)
{
theDots <- list(...)
eachElem(theDots$sObj,
fun = FUN,
elementArgs = list(X))
}
library(nws)
sObj <- sleigh(workerCount = 5)
nwsControl <- trainControl(workers = 5,
number = 50,
computeFunction = nwsClacs,
computeArgs = list(sObj = sObj))
set.seed(1)
usingNWS <- train(medv ~ .,
data = BostonHousing,
"glmboost",
trControl = nwsControl)
close(sObj)
Run the code above in your browser using DataCamp Workspace