-
Notifications
You must be signed in to change notification settings - Fork 1
/
naiveBays-v2.0.r
74 lines (52 loc) · 1.75 KB
/
naiveBays-v2.0.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#NaiveBayes Classification - V2.0
#set working directory and import Data
setwd("~/R/work-space")
dataset <- read.csv("bank-full.csv",
header = TRUE,
sep = ";",
stringsAsFactors = TRUE)
## [1]- Exploring the Dataset
#view the structure of data
str(dataset)
#view the summary of data
summary(dataset)
#encoding the target vaiable (y) as a factor of two levels
dataset$y = factor(dataset$y,
levels = c('yes', 'no'),
labels = c(1, 0))
#proportions of target vaiable (y)
table(dataset$y)
prop.table(table(dataset$y))
## [2]- prepare our model
#splitting the dataset into trainingSet and testSet
#install.packages('caret')
library(caret)
set.seed(123)
splitSet <- createDataPartition(y = dataset$y, p = 0.75, list = FALSE)
trainSet <- dataset[splitSet,]
testSet <- dataset[-splitSet,]
#check the rows and proportions of trainSet and testSet
nrow(trainSet)
nrow(testSet)
prop.table(table(trainSet$y))
prop.table(table(testSet$y))
## [3]- train our model on trainingSet
library(e1071)
library(rminer)
#create the naiveBayes classifier model
model <- naiveBayes(y ~ ., data = trainSet)
model
## [4]- making predictions
nb_prediction <- predict(model, testSet, type = "class")
confusionMatrix(nb_prediction, testSet$y)
mmetric(testSet$y, nb_prediction, c("ACC", "PRECISION", "TPR", "F1"))
##--------- Evaluation of model ------------
test_pred_score = predict(model, newdata = testSet, type = 'raw')
head(test_pred_score)
hist(test_pred_score)
pred = prediction(test_pred_score[,1], testSet$y)
eval = performance(pred, "acc")
plot(eval)
##----- ROC Curve -----
roc = performance(pred, "tpr", "fpr")
plot(roc, colorize=T, main="ROC Curve", ylab="Sensitivity", xlab="1-Specifity")