Instantly share code, notes, and snippets.

Embed
What would you like to do?
Predictive classification example with R. Machine Learning examples.
library(AppliedPredictiveModeling)
library(caret)
data(AlzheimerDisease)
adData = data.frame(diagnosis,predictors)
trainIndex = createDataPartition(diagnosis, p = 0.60,list=FALSE)
training = adData[trainIndex,]
testing = adData[-trainIndex,]
# Logistic Regression: 81/84.
fit <- train(diagnosis ~ ., data = training, method = 'LogitBoost', tuneGrid = data.frame(nIter=50:100))
plot(x=fit$results$nIter, y=fit$results$Accuracy, type='l')
lfit <- lm(fit$results$Accuracy ~ fit$results$nIter)
abline(lfit)
results <- predict(fit, newdata = testing)
confusionMatrix(results, testing$diagnosis)
# SVM: 82/81.
fit <- train(diagnosis ~ ., data = training, method = 'svmRadial')
results <- predict(fit, newdata = testing)
confusionMatrix(results, testing$diagnosis)
# DNN: 73/73.
fit <- train(diagnosis ~ ., data = training, method = 'dnn')
results <- predict(fit, newdata = testing)
confusionMatrix(results, testing$diagnosis)
# Random Forest: 79/87.
fit <- train(diagnosis ~ ., data = training, method = 'rf')
results <- predict(fit, newdata = testing)
confusionMatrix(results, testing$diagnosis)
# Regularized Random Forest: 79/84.
fit <- train(diagnosis ~ ., data = training, method = 'RRF')
# Logistic Regression with PCA: 79/63.
training$Genotype <- seq_along(levels(training$Genotype))[training$Genotype] # Convert Genotype factor to an integer.
fit <- train(diagnosis ~ ., data = training, method = 'LogitBoost', tuneGrid = data.frame(nIter=50), preProcess = "pca")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment