This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
#load Packages
library(caret)
## Warning: package 'caret' was built under R version 4.3.3
## Loading required package: ggplot2
## Loading required package: lattice
library(klaR)
## Warning: package 'klaR' was built under R version 4.3.3
## Loading required package: MASS
#load data
data(iris)
#define an 80%/20% train/test split of dataset
trainIndex <- createDataPartition(iris$Species, p = 0.80, list = FALSE)
dataTrain <- iris[trainIndex,]
dataTest <- iris[-trainIndex,]
#train a naive bayes model
fit<- NaiveBayes(Species~., data= dataTrain)
#make predictions
predictions <- predict(fit, dataTest[,1:4])
#summarize results
confusionMatrix(predictions$class, dataTest$Species)
## Confusion Matrix and Statistics
##
## Reference
## Prediction setosa versicolor virginica
## setosa 10 0 0
## versicolor 0 10 3
## virginica 0 0 7
##
## Overall Statistics
##
## Accuracy : 0.9
## 95% CI : (0.7347, 0.9789)
## No Information Rate : 0.3333
## P-Value [Acc > NIR] : 1.665e-10
##
## Kappa : 0.85
##
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: setosa Class: versicolor Class: virginica
## Sensitivity 1.0000 1.0000 0.7000
## Specificity 1.0000 0.8500 1.0000
## Pos Pred Value 1.0000 0.7692 1.0000
## Neg Pred Value 1.0000 1.0000 0.8696
## Prevalence 0.3333 0.3333 0.3333
## Detection Rate 0.3333 0.3333 0.2333
## Detection Prevalence 0.3333 0.4333 0.2333
## Balanced Accuracy 1.0000 0.9250 0.8500
library(caret)
data(iris)
trainControl <- trainControl(method = "boot", number = 1000)
fit<- train(Species~., data = iris, trControl= trainControl, method = "nb")
## Warning in FUN(X[[i]], ...): Numerical 0 probability for all classes with
## observation 40
## Warning in FUN(X[[i]], ...): Numerical 0 probability for all classes with
## observation 45
## Warning in FUN(X[[i]], ...): Numerical 0 probability for all classes with
## observation 19
print(fit)
## Naive Bayes
##
## 150 samples
## 4 predictor
## 3 classes: 'setosa', 'versicolor', 'virginica'
##
## No pre-processing
## Resampling: Bootstrapped (1000 reps)
## Summary of sample sizes: 150, 150, 150, 150, 150, 150, ...
## Resampling results across tuning parameters:
##
## usekernel Accuracy Kappa
## FALSE 0.9519983 0.9272566
## TRUE 0.9540298 0.9303334
##
## Tuning parameter 'fL' was held constant at a value of 0
## Tuning
## parameter 'adjust' was held constant at a value of 1
## Accuracy was used to select the optimal model using the largest value.
## The final values used for the model were fL = 0, usekernel = TRUE and adjust
## = 1.
trainControl <- trainControl(method= "repeatedcv", number =10, repeats = 3)
fit <- train(Species~., data = iris, trControl= trainControl, method = "nb")
print(fit)
## Naive Bayes
##
## 150 samples
## 4 predictor
## 3 classes: 'setosa', 'versicolor', 'virginica'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold, repeated 3 times)
## Summary of sample sizes: 135, 135, 135, 135, 135, 135, ...
## Resampling results across tuning parameters:
##
## usekernel Accuracy Kappa
## FALSE 0.9533333 0.9300000
## TRUE 0.9577778 0.9366667
##
## Tuning parameter 'fL' was held constant at a value of 0
## Tuning
## parameter 'adjust' was held constant at a value of 1
## Accuracy was used to select the optimal model using the largest value.
## The final values used for the model were fL = 0, usekernel = TRUE and adjust
## = 1.
# define training control
trainControl <-trainControl(method="LOOCV")
# evaluate the model
fit <- train(Species~., data=iris, trControl=trainControl, method="nb")
# display the results
print(fit)
## Naive Bayes
##
## 150 samples
## 4 predictor
## 3 classes: 'setosa', 'versicolor', 'virginica'
##
## No pre-processing
## Resampling: Leave-One-Out Cross-Validation
## Summary of sample sizes: 149, 149, 149, 149, 149, 149, ...
## Resampling results across tuning parameters:
##
## usekernel Accuracy Kappa
## FALSE 0.9533333 0.93
## TRUE 0.9600000 0.94
##
## Tuning parameter 'fL' was held constant at a value of 0
## Tuning
## parameter 'adjust' was held constant at a value of 1
## Accuracy was used to select the optimal model using the largest value.
## The final values used for the model were fL = 0, usekernel = TRUE and adjust
## = 1.
library(mlbench)
library(caret)
data("PimaIndiansDiabetes")
# Prepare training scheme
trainControl <-trainControl(method="repeatedcv", number=10, repeats=3)
#CART
fit.cart <- train(diabetes~., data=PimaIndiansDiabetes, method="rpart", trControl=trainControl)
print(fit.cart )
## CART
##
## 768 samples
## 8 predictor
## 2 classes: 'neg', 'pos'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold, repeated 3 times)
## Summary of sample sizes: 691, 692, 691, 691, 691, 691, ...
## Resampling results across tuning parameters:
##
## cp Accuracy Kappa
## 0.01741294 0.7438767 0.4163808
## 0.10447761 0.7230463 0.3628402
## 0.24253731 0.7027056 0.2915315
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was cp = 0.01741294.
set.seed(7)
#LDA
set.seed(7)
fit.lda <- train(diabetes~., data=PimaIndiansDiabetes, method="lda", trControl=trainControl)
print(fit.lda)
## Linear Discriminant Analysis
##
## 768 samples
## 8 predictor
## 2 classes: 'neg', 'pos'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold, repeated 3 times)
## Summary of sample sizes: 691, 691, 691, 691, 691, 691, ...
## Resampling results:
##
## Accuracy Kappa
## 0.7791069 0.4862025
#SVM
set.seed(7)
fit.svm<- train(diabetes~., data=PimaIndiansDiabetes, method="svmRadial", trControl=trainControl)
print(fit.svm)
## Support Vector Machines with Radial Basis Function Kernel
##
## 768 samples
## 8 predictor
## 2 classes: 'neg', 'pos'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold, repeated 3 times)
## Summary of sample sizes: 691, 691, 691, 691, 691, 691, ...
## Resampling results across tuning parameters:
##
## C Accuracy Kappa
## 0.25 0.7712919 0.4621585
## 0.50 0.7625769 0.4485309
## 1.00 0.7560549 0.4339951
##
## Tuning parameter 'sigma' was held constant at a value of 0.124824
## Accuracy was used to select the optimal model using the largest value.
## The final values used for the model were sigma = 0.124824 and C = 0.25.
#KNN
set.seed(7)
fit.knn<- train(diabetes~., data=PimaIndiansDiabetes, method="knn", trControl=trainControl)
print(fit.knn)
## k-Nearest Neighbors
##
## 768 samples
## 8 predictor
## 2 classes: 'neg', 'pos'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold, repeated 3 times)
## Summary of sample sizes: 691, 691, 691, 691, 691, 691, ...
## Resampling results across tuning parameters:
##
## k Accuracy Kappa
## 5 0.7191900 0.3580128
## 7 0.7261734 0.3779733
## 9 0.7369503 0.3984995
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was k = 9.
#Random Forest
library(randomForest)
## Warning: package 'randomForest' was built under R version 4.3.3
## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
##
## Attaching package: 'randomForest'
## The following object is masked from 'package:ggplot2':
##
## margin
set.seed(7)
fit.rf<- train(diabetes~., data=PimaIndiansDiabetes, method="rf", trControl=trainControl)
print(fit.rf)
## Random Forest
##
## 768 samples
## 8 predictor
## 2 classes: 'neg', 'pos'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold, repeated 3 times)
## Summary of sample sizes: 691, 691, 691, 691, 691, 691, ...
## Resampling results across tuning parameters:
##
## mtry Accuracy Kappa
## 2 0.7638528 0.4630809
## 5 0.7634256 0.4664261
## 8 0.7599738 0.4596437
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
# Collect
results <- resamples(list(CART = fit.cart, LDA = fit.lda, SVM = fit.svm, KNN = fit.knn, RF = fit.rf))
print(results)
##
## Call:
## resamples.default(x = list(CART = fit.cart, LDA = fit.lda, SVM = fit.svm, KNN
## = fit.knn, RF = fit.rf))
##
## Models: CART, LDA, SVM, KNN, RF
## Number of resamples: 30
## Performance metrics: Accuracy, Kappa
## Time estimates for: everything, final model fit
summary(results)
##
## Call:
## summary.resamples(object = results)
##
## Models: CART, LDA, SVM, KNN, RF
## Number of resamples: 30
##
## Accuracy
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## CART 0.6493506 0.7272727 0.7451299 0.7438767 0.7759740 0.8311688 0
## LDA 0.7142857 0.7508117 0.7662338 0.7791069 0.8000256 0.9078947 0
## SVM 0.7236842 0.7508117 0.7631579 0.7712919 0.7915243 0.8947368 0
## KNN 0.6753247 0.7036056 0.7272727 0.7369503 0.7662338 0.8311688 0
## RF 0.6842105 0.7305195 0.7597403 0.7638528 0.8019481 0.8421053 0
##
## Kappa
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## CART 0.2187500 0.3620724 0.4237889 0.4163808 0.4844424 0.6127660 0
## LDA 0.3011551 0.4192537 0.4662541 0.4862025 0.5308596 0.7812500 0
## SVM 0.3391908 0.3997116 0.4460612 0.4621585 0.5234605 0.7475083 0
## KNN 0.2553191 0.3406000 0.3841761 0.3984995 0.4539789 0.6195363 0
## RF 0.2951613 0.3778304 0.4640696 0.4630809 0.5447483 0.6426332 0
table(PimaIndiansDiabetes$diabetes)
##
## neg pos
## 500 268
prop.table(table(PimaIndiansDiabetes$diabetes))
##
## neg pos
## 0.6510417 0.3489583
scales <- list( x = list(relation = "free"), y = list(relation = "free"))
bwplot(results, scale = scales)
splom(results)
diffs <- diff(results)
summary(diffs)
##
## Call:
## summary.diff.resamples(object = diffs)
##
## p-value adjustment: bonferroni
## Upper diagonal: estimates of the difference
## Lower diagonal: p-value for H0: difference = 0
##
## Accuracy
## CART LDA SVM KNN RF
## CART -0.035230 -0.027415 0.006926 -0.019976
## LDA 0.0442284 0.007815 0.042157 0.015254
## SVM 0.0603866 0.9156892 0.034342 0.007439
## KNN 1.0000000 6.68e-05 0.0002941 -0.026902
## RF 0.4540067 0.4490617 1.0000000 0.0183793
##
## Kappa
## CART LDA SVM KNN RF
## CART -0.0698216 -0.0457776 0.0178813 -0.0467001
## LDA 0.1715518 0.0240440 0.0877029 0.0231215
## SVM 0.5504705 0.3562734 0.0636589 -0.0009225
## KNN 1.0000000 0.0003858 0.0040823 -0.0645814
## RF 0.5679555 1.0000000 1.0000000 0.0158974
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.