#######################################
## 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
mpiCalcs <- 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 = mpiCalcs,
computeArgs = list(cl = cl))
set.seed(1)
usingMPI <- train(medv ~ .,
data = BostonHousing,
"glmboost",
trControl = mpiControl)
################################################
## Parallel Random Forest using foreach and doMPI
library(doMPI)
cl <- startMPIcluster(count = 5, verbose = TRUE)
registerDoMPI(cl)
rfMPI <- train(medv ~ .,
data = BostonHousing,
"parRF")
closeCluster(cl)
#######################################
## Parallel Processing Example via NWS
nwsCalcs <- 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 = nwsCalcs,
computeArgs = list(sObj = sObj))
set.seed(1)
usingNWS <- train(medv ~ .,
data = BostonHousing,
"glmboost",
trControl = nwsControl)
close(sObj)
#######################################
## Parallel Random Forest Models using
## the foreach package and MPI
library(doMPI)
cl <- startMPIcluster(2)
registerDoMPI(cl)
set.seed(1)
parallelRF <- train(medv ~ .,
data = BostonHousing,
"parRF")
closeCluster(cl)
Run the code above in your browser using DataLab