Skip to content

Instantly share code, notes, and snippets.

@rguliev
Last active October 30, 2018 13:50
Show Gist options
  • Save rguliev/e61565066219fc57ff9424631ecd9227 to your computer and use it in GitHub Desktop.
Save rguliev/e61565066219fc57ff9424631ecd9227 to your computer and use it in GitHub Desktop.
Caret code exmaples
fitControl <- trainControl(
method = 'LOOCV',
savePredictions = TRUE,
classProbs = TRUE,
summaryFunction = twoClassSummary)
fitControlOversampling <- trainControl(
method = 'LOOCV',
savePredictions = FALSE,
classProbs = TRUE,
summaryFunction = twoClassSummary,
sampling = "up"
)
# ===== LDA =====
# Center and scale at each CV iteration
lda.fit <- train(y=y, x=X, method="lda", preProcess=c("center", "scale"), trControl = fitControl)
# ===== GLM =====
glm.fit <- train(y=y, x=X, method="glm", preProcess=c("center", "scale"), trControl = trControl, family=binomial(link = "logit"))
# ===== rpart =====
Grid <- expand.grid(cp=seq(0, 0.2, 0.005))
fit.rpart <- train(y=y, x=X, method = 'rpart', trControl=fitControl, tuneGrid = Grid, na.action = na.pass)
# plot(fit.rpart)
# table(y, predict(fit.rpart, newdata = X, type = 'raw', na.action = na.pass))
# ===== rpart2 =====
Grid<-expand.grid(.maxdepth=2:20)
fit.rpart2 <- train(y=y, x=X, method = 'rpart2', trControl=fitControl, tuneGrid=Grid, na.action = na.pass)
# plot(fit.rpart2)
# table(y, predict(fit.rpart2, newdata = X, type = 'raw', na.action = na.pass))
# ===== C5.0 =====
Grid <- expand.grid(.trials = c(1:30), .model = c("tree", "rules"), .winnow = c(TRUE, FALSE))
fit.c5 <- train(y=y, x=X, method = 'C5.0', trControl=fitControl, tuneGrid=Grid, na.action = na.pass)
# plot(fit.c5)
# table(y, predict(fit.c5, newdata = X, type = 'raw', na.action = na.pass)
# ==== randomForest =====
Grid <- expand.grid(.mtry=seq(2, 30))
# OOB estimation can be used. ntree sets number of trees
fit.rf <- train(y=y, x=X, method = 'rf', trControl=trainControl(method = 'oob', savePredictions = FALSE), tuneGrid=Grid, ntree=2000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment