Skip to content

Instantly share code, notes, and snippets.

@dettmering
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dettmering/0c81684b271edefebebb to your computer and use it in GitHub Desktop.
Save dettmering/0c81684b271edefebebb to your computer and use it in GitHub Desktop.
Machine learning template for R
library(caret)
library(doMC)
registerDoMC(cores = 4) # use multiple cores
set.seed(1234)
col.of.interest <- c(
'response', 'A', 'B', 'C'
)
inTraining <- createDataPartition(df$response, p = 0.75, list = FALSE)
tr <- df[inTraining, col.of.interest]
te <- df[-inTraining, col.of.interest]
tr$response <- as.factor(tr$response)
te$response <- as.factor(te$response)
fitControl <- trainControl(method = "repeatedcv", number = 10, repeats = 10)
fit <- train(response ~ ., data = tr, method = "rf", trainControl = fitControl)
prd <- predict(fit, te)
confusionMatrix(prd, te$response)
varImp(fit, scale = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment