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:
You can also embed plots, for example:
#Load the packages
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
library(klaR)
## Loading required package: MASS
#load the iris dataset
data(iris)
#define an 80%/20% train/test split of the 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 8 3
## virginica 0 2 7
##
## Overall Statistics
##
## Accuracy : 0.8333
## 95% CI : (0.6528, 0.9436)
## No Information Rate : 0.3333
## P-Value [Acc > NIR] : 2.444e-08
##
## Kappa : 0.75
##
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: setosa Class: versicolor Class: virginica
## Sensitivity 1.0000 0.8000 0.7000
## Specificity 1.0000 0.8500 0.9000
## Pos Pred Value 1.0000 0.7273 0.7778
## Neg Pred Value 1.0000 0.8947 0.8571
## Prevalence 0.3333 0.3333 0.3333
## Detection Rate 0.3333 0.2667 0.2333
## Detection Prevalence 0.3333 0.3667 0.3000
## Balanced Accuracy 1.0000 0.8250 0.8000
#define training control boot strap method
trainControl <- trainControl(method = "boot", number = 100)
#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: Bootstrapped (100 reps)
## Summary of sample sizes: 150, 150, 150, 150, 150, 150, ...
## Resampling results across tuning parameters:
##
## usekernel Accuracy Kappa
## FALSE 0.9505077 0.9249820
## TRUE 0.9492178 0.9230515
##
## 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 = FALSE and adjust
## = 1.
#define training control cv/k-fold method
trainControl <- trainControl(method = "cv", number = 10)
#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: Cross-Validated (10 fold)
## Summary of sample sizes: 135, 135, 135, 135, 135, 135, ...
## Resampling results across tuning parameters:
##
## usekernel Accuracy Kappa
## FALSE 0.9600000 0.94
## TRUE 0.9533333 0.93
##
## 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 = FALSE and adjust
## = 1.
#define training control repeated k-fold method
trainControl <- trainControl(method = "repeatedcv", number = 10, repeats = 3)
#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: 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.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.
#define training control leave-one out method
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.
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.