PREPARATION
setwd("~/Google Drive/UM/Smart Services/Thesis/Thesis/Code/Naive Bayes/6.Feature Set 5/Directives")
#install.packages("naivebayes")
library(naivebayes)
## Warning: package 'naivebayes' was built under R version 3.4.3
library(dplyr)
## Warning: Installed Rcpp (0.12.16) different from Rcpp used to build dplyr (0.12.11).
## Please reinstall dplyr to avoid random crashes or undefined behavior.
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(psych)
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
library(e1071)
library(readxl)
#Import Labels
Labels <- read_excel("~/Google Drive/UM/Smart Services/Thesis/Thesis/Code/Naive Bayes/1.Labels/Source Data.xlsx")
Label <- Labels$Score
#Import Features
Features1 <- read.csv("~/Google Drive/UM/Smart Services/Thesis/Thesis/Code/Naive Bayes/6.Feature Set 5/Combined/Feature Set 1 70th Percentile.csv")
Features1 <- Features1[-1]
#Import Features
Features2 <- read.csv("~/Google Drive/UM/Smart Services/Thesis/Thesis/Code/Naive Bayes/6.Feature Set 5/Directives/Directives.csv")
Features2 <- Features2[1:1000,]
Features2 <- Features2[4:10]
#Import Features
Features <- cbind(Features1,Features2)
RECODE LABELS FOR ONE-VS-ALL
#Class 2
Label2 <- list()
for(i in 1:1000){
if(Label[i]==3| Label[i]==4){
Label2[i] <- 1
}else{
Label2[i] <- 0
}
}
#As Factor
Label2 <- as.factor(unlist(Label2))
#Class 3
Label3 <- list()
for(i in 1:1000){
if(Label[i]==5| Label[i]==6){
Label3[i] <- 1
}else{
Label3[i] <- 0
}
}
#As Factor
Label3 <- as.factor(unlist(Label3))
#Class 4
Label4 <- list()
for(i in 1:1000){
if(Label[i]==7| Label[i]==8){
Label4[i] <- 1
}else{
Label4[i] <- 0
}
}
#As Factor
Label4 <- as.factor(unlist(Label4))
#Class 5
Label5 <- list()
for(i in 1:1000){
if(Label[i]==9| Label[i]==10){
Label5[i] <- 1
}else{
Label5[i] <- 0
}
}
#As Factor
Label5 <- as.factor(unlist(Label5))
#All Labels
All <- list()
for(i in 1:1000){
if(Label[i]==9| Label[i]==10){
All[i] <- 5
}else if(Label[i]==7| Label[i]==8){
All[i] <- 4
}else if(Label[i]==5| Label[i]==6){
All[i] <- 3
}else{
All[i] <- 2
}
}
#As Factor
All <- as.factor(unlist(All))
#Control
Control.df <- data.frame(matrix(seq(1,1000),ncol=1,nrow=1000))
Control.df$Actual <- Label
Control.df$All <- All
Control.df$Label2 <- Label2
Control.df$Label3 <- Label3
Control.df$Label4 <- Label4
Control.df$Label5 <- Label5
Control.df[1:10,2:7]
## Actual All Label2 Label3 Label4 Label5
## 1 3 2 1 0 0 0
## 2 8 4 0 0 1 0
## 3 7 4 0 0 1 0
## 4 4 2 1 0 0 0
## 5 7 4 0 0 1 0
## 6 7 4 0 0 1 0
## 7 5 3 0 1 0 0
## 8 10 5 0 0 0 1
## 9 7 4 0 0 1 0
## 10 8 4 0 0 1 0
TRANSFORM FEATURES TO FACTOR VARIABLES
#Transform Integer to Factor
for(i in 1:445){
Features[,i] <- as.factor(Features[,i])
}
str(Features)
## 'data.frame': 1000 obs. of 791 variables:
## $ abl : Factor w/ 2 levels "0","1": 2 1 1 1 1 1 1 1 1 1 ...
## $ absolut : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ access : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ accommod : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ actual : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 2 1 ...
## $ adequ : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ adult : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ advanc : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ adverti : Factor w/ 2 levels "0","1": 1 1 1 1 2 1 1 1 1 1 ...
## $ advic : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ affect : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ after : Factor w/ 2 levels "0","1": 1 1 1 1 2 1 1 1 1 1 ...
## $ air : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ airport : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ all : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ alloc : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ allow : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ alreadi : Factor w/ 3 levels "0","1","2": 1 1 1 2 1 1 1 1 1 1 ...
## $ also : Factor w/ 5 levels "0","1","2","3",..: 1 1 1 4 1 1 2 1 2 1 ...
## $ although : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ alway : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ amaz : Factor w/ 3 levels "0","1","2": 1 1 1 1 2 1 1 1 1 1 ...
## $ amen : Factor w/ 2 levels "0","1": 1 2 1 1 1 1 1 1 1 1 ...
## $ amsterdam : Factor w/ 3 levels "0","1","2": 1 2 1 2 1 1 1 1 1 1 ...
## $ and : Factor w/ 3 levels "0","1","2": 2 1 1 1 1 1 1 1 1 1 ...
## $ annoy : Factor w/ 3 levels "0","1","2": 2 1 1 1 1 1 1 1 1 1 ...
## $ anoth : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ answer : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ anymor : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ anyon : Factor w/ 3 levels "0","1","2": 1 2 1 1 1 1 1 1 1 1 ...
## $ anyth : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ anyway : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ apart : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 2 1 1 ...
## $ appear : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ appreci : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ architectur : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ area : Factor w/ 3 levels "0","1","2": 1 1 1 2 1 1 1 1 1 1 ...
## $ arena : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ around : Factor w/ 3 levels "0","1","2": 1 1 1 2 1 1 1 1 1 1 ...
## $ arriv : Factor w/ 4 levels "0","1","2","3": 2 1 1 1 2 1 1 1 1 1 ...
## $ ask : Factor w/ 3 levels "0","1","2": 2 1 2 1 1 1 1 1 1 1 ...
## $ aspect : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ atmosph : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ attent : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ attract : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ avail : Factor w/ 3 levels "0","1","2": 2 1 1 1 1 1 1 1 1 1 ...
## $ averag : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ awar : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ away : Factor w/ 4 levels "0","1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
## $ awesom : Factor w/ 3 levels "0","1","3": 1 1 1 1 1 2 1 1 1 1 ...
## $ back : Factor w/ 4 levels "0","1","2","3": 1 2 2 1 1 1 1 1 1 1 ...
## $ bacon : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ bad : Factor w/ 3 levels "0","1","2": 1 1 1 2 1 1 1 1 1 1 ...
## $ bag : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ bang : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ bank : Factor w/ 3 levels "0","1","3": 1 1 1 1 1 1 1 1 1 1 ...
## $ bar : Factor w/ 3 levels "0","1","2": 1 1 2 2 1 1 1 1 1 1 ...
## $ bare : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ basement : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ basic : Factor w/ 3 levels "0","1","2": 1 1 2 1 1 1 1 1 1 1 ...
## $ bath : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ bathroom : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ beauti : Factor w/ 3 levels "0","1","2": 2 1 1 1 1 1 2 1 1 1 ...
## $ bed : Factor w/ 4 levels "0","1","2","3": 1 1 1 3 1 1 2 1 1 2 ...
## $ bedroom : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ beer : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ believ : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ best : Factor w/ 2 levels "0","1": 2 1 1 1 1 1 1 1 1 1 ...
## $ better : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ big : Factor w/ 3 levels "0","1","2": 3 1 1 1 1 1 1 1 1 3 ...
## $ bigger : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ bike : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ birthday : Factor w/ 3 levels "0","1","2": 1 1 1 1 2 1 1 1 1 1 ...
## $ bit : Factor w/ 3 levels "0","1","2": 1 3 2 1 1 1 1 1 1 1 ...
## $ black : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ block : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ board : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ boiler : Factor w/ 3 levels "0","1","2": 1 1 2 1 1 1 1 1 1 1 ...
## $ bonus : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ book : Factor w/ 6 levels "0","1","2","3",..: 6 1 1 1 4 1 1 1 1 1 ...
## $ boutiqu : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ box : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ bread : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ breakfast : Factor w/ 6 levels "0","1","2","3",..: 1 1 2 1 1 1 1 2 1 2 ...
## $ bright : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 2 1 1 1 ...
## $ brilliant : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ broken : Factor w/ 3 levels "0","1","2": 2 1 1 1 1 1 1 1 1 1 ...
## $ brought : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ buffet : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ build : Factor w/ 4 levels "0","1","2","3": 1 1 1 3 2 1 1 1 1 1 ...
## $ busi : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ but : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ buy : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ cab : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ cafe : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ call : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ came : Factor w/ 3 levels "0","1","2": 2 1 1 3 1 1 1 1 1 1 ...
## $ can : Factor w/ 4 levels "0","1","2","3": 4 2 1 1 1 1 1 1 1 1 ...
## $ car : Factor w/ 3 levels "0","1","3": 1 1 1 1 1 1 1 1 1 1 ...
## [list output truncated]
PARTITIONING TRAINING & VALIDATION
#Features
set.seed(1234)
ind <- sample(2,nrow(Features),replace = T, prob =c(0.8,0.2))
train <- Features[ind == 1,]
test <- Features[ind ==2,]
Labels
train.labels.2 <- Label2[ind == 1]
test.labels.2 <- Label2[ind ==2]
train.labels.3 <- Label3[ind == 1]
test.labels.3 <- Label3[ind ==2]
train.labels.4 <- Label4[ind == 1]
test.labels.4 <- Label4[ind ==2]
train.labels.5 <- Label5[ind == 1]
test.labels.5 <- Label5[ind ==2]
train.labels <- All[ind == 1]
test.labels <- All[ind ==2]
NAIVE BAYES MODEL
NB2 <- naiveBayes(x = train,y = train.labels.2)
NB3 <- naiveBayes(x = train,y = train.labels.3)
NB4 <- naiveBayes(x = train,y = train.labels.4)
NB5 <- naiveBayes(x = train,y = train.labels.5)
PREDICTIONS
NB.Pred2 <- predict(NB2, test,type ="raw")
NB.Pred3 <- predict(NB3, test,type ="raw")
NB.Pred4 <- predict(NB4, test,type ="raw")
NB.Pred5 <- predict(NB5, test,type ="raw")
VOTING
Voting.df <- data.frame(NB.Pred2, NB.Pred3,NB.Pred4,NB.Pred5)
colnames(Voting.df) <- c("Class 2: 0","Class2: 1","Class 3: 0","Class3: 1","Class 4: 0","Class4: 1","Class 5: 0","Class5: 1")
head(Voting.df)
## Class 2: 0 Class2: 1 Class 3: 0 Class3: 1 Class 4: 0
## 1 0.000000e+00 1 0.000000e+00 1 1.711604e-136
## 2 0.000000e+00 1 2.694639e-213 1 8.118255e-89
## 3 4.166838e-291 1 3.665784e-155 1 5.005179e-18
## 4 0.000000e+00 1 2.545360e-236 1 1.296761e-53
## 5 0.000000e+00 1 7.326677e-218 1 1.000000e+00
## 6 0.000000e+00 1 1.918002e-259 1 1.099237e-35
## Class4: 1 Class 5: 0 Class5: 1
## 1 1.000000e+00 1.000000e+00 3.049914e-169
## 2 1.000000e+00 2.028720e-129 1.000000e+00
## 3 1.000000e+00 1.406879e-23 1.000000e+00
## 4 1.000000e+00 7.925971e-06 9.999921e-01
## 5 1.939931e-14 1.000000e+00 4.316213e-120
## 6 1.000000e+00 1.000000e+00 1.150027e-49
Transformed.Voting.df <- Voting.df[seq(2,8,2)]
colnames(Transformed.Voting.df) <- c("2","3","4","5")
head(Transformed.Voting.df)
## 2 3 4 5
## 1 1 1 1.000000e+00 3.049914e-169
## 2 1 1 1.000000e+00 1.000000e+00
## 3 1 1 1.000000e+00 1.000000e+00
## 4 1 1 1.000000e+00 9.999921e-01
## 5 1 1 1.939931e-14 4.316213e-120
## 6 1 1 1.000000e+00 1.150027e-49
Evaluation <- Transformed.Voting.df
Index <- as.numeric(apply(Transformed.Voting.df,MARGIN = 1,which.max))
Index <- Index+1
Evaluation$Vote <- Index
Evaluation$Actual <- test.labels
head(Evaluation,100)
## 2 3 4 5 Vote Actual
## 1 1 1 1.000000e+00 3.049914e-169 2 4
## 2 1 1 1.000000e+00 1.000000e+00 2 5
## 3 1 1 1.000000e+00 1.000000e+00 2 5
## 4 1 1 1.000000e+00 9.999921e-01 2 4
## 5 1 1 1.939931e-14 4.316213e-120 2 4
## 6 1 1 1.000000e+00 1.150027e-49 2 4
## 7 1 1 1.383696e-108 1.000000e+00 2 5
## 8 1 1 1.000000e+00 0.000000e+00 2 3
## 9 1 1 1.000000e+00 9.554510e-48 2 5
## 10 1 1 9.990023e-01 7.574436e-208 2 3
## 11 1 1 1.000000e+00 1.000000e+00 2 4
## 12 1 1 1.000000e+00 3.595895e-61 2 3
## 13 1 1 1.000000e+00 1.000000e+00 2 5
## 14 1 1 1.000000e+00 7.671060e-11 2 4
## 15 1 1 3.863788e-20 1.000000e+00 2 4
## 16 1 1 7.652401e-02 1.000000e+00 2 5
## 17 1 1 1.000000e+00 1.000000e+00 2 4
## 18 1 1 6.769995e-03 1.000000e+00 2 5
## 19 1 1 1.000000e+00 4.387848e-96 2 4
## 20 1 1 1.000000e+00 1.000000e+00 2 2
## 21 1 1 9.999992e-01 1.996703e-23 2 4
## 22 1 1 1.000000e+00 1.000000e+00 2 4
## 23 1 1 1.592283e-33 2.166432e-150 2 3
## 24 1 1 1.000000e+00 1.000000e+00 2 5
## 25 1 1 1.000000e+00 1.000000e+00 2 4
## 26 1 1 1.000000e+00 1.000000e+00 2 5
## 27 1 1 1.000000e+00 1.000000e+00 2 4
## 28 1 1 1.000000e+00 1.527659e-03 2 5
## 29 1 1 1.000000e+00 7.580286e-167 2 3
## 30 1 1 1.000000e+00 6.287304e-150 2 3
## 31 1 1 1.000000e+00 1.000000e+00 2 5
## 32 1 1 1.000000e+00 1.000000e+00 2 5
## 33 1 1 1.000000e+00 1.000000e+00 2 5
## 34 1 1 1.000000e+00 1.000000e+00 2 3
## 35 1 1 1.000000e+00 4.792436e-02 2 4
## 36 1 1 9.999986e-01 1.704324e-31 2 4
## 37 1 1 1.000000e+00 5.945840e-62 2 5
## 38 1 1 2.331038e-30 7.441376e-55 2 3
## 39 1 1 9.999430e-01 1.000000e+00 2 5
## 40 1 1 1.000000e+00 2.591596e-28 2 3
## 41 1 1 2.766496e-03 1.000000e+00 2 5
## 42 1 1 1.000000e+00 9.999008e-01 2 4
## 43 1 1 1.000000e+00 9.999976e-01 2 5
## 44 1 1 1.000000e+00 1.728601e-16 2 3
## 45 1 1 1.000000e+00 1.000000e+00 2 5
## 46 1 1 1.000000e+00 6.050722e-52 2 4
## 47 1 1 1.000000e+00 1.000000e+00 2 5
## 48 1 1 1.000000e+00 6.696584e-22 2 3
## 49 1 1 1.000000e+00 1.000000e+00 2 5
## 50 1 1 1.000000e+00 1.000000e+00 2 5
## 51 1 1 1.000000e+00 1.000000e+00 2 4
## 52 1 1 1.000000e+00 1.000000e+00 2 5
## 53 1 1 1.000000e+00 5.489626e-07 2 4
## 54 1 1 1.000000e+00 1.000000e+00 2 4
## 55 1 1 1.000000e+00 1.000000e+00 2 4
## 56 1 1 1.000000e+00 1.000000e+00 2 5
## 57 1 1 1.000000e+00 1.000000e+00 2 2
## 58 1 1 1.000000e+00 1.000000e+00 2 5
## 59 1 1 1.000000e+00 1.000000e+00 2 4
## 60 1 1 1.000000e+00 2.605015e-66 2 4
## 61 1 1 1.000000e+00 1.000000e+00 2 5
## 62 1 1 1.000000e+00 1.000000e+00 2 5
## 63 1 1 1.000000e+00 1.000000e+00 2 4
## 64 1 1 1.000000e+00 1.000000e+00 2 5
## 65 1 1 1.000000e+00 1.000000e+00 2 5
## 66 1 1 8.722298e-10 1.000000e+00 2 5
## 67 1 1 1.000000e+00 1.000000e+00 2 3
## 68 1 1 1.000000e+00 1.000000e+00 2 3
## 69 1 1 1.000000e+00 1.000000e+00 2 4
## 70 1 1 1.704657e-05 1.000000e+00 2 4
## 71 1 1 1.000000e+00 1.000000e+00 2 5
## 72 1 1 1.000000e+00 1.330218e-16 2 5
## 73 1 1 1.000000e+00 1.000000e+00 2 2
## 74 1 1 1.000000e+00 1.000000e+00 2 4
## 75 1 1 1.000000e+00 1.000000e+00 2 4
## 76 1 1 1.000000e+00 6.022453e-91 2 5
## 77 1 1 1.000000e+00 1.257171e-152 2 3
## 78 1 1 1.000000e+00 9.995374e-01 2 4
## 79 1 1 1.000000e+00 1.112571e-294 2 5
## 80 1 1 1.502497e-19 1.000000e+00 2 5
## 81 1 1 3.414081e-01 1.000000e+00 2 5
## 82 1 1 1.000000e+00 1.000000e+00 2 4
## 83 1 1 1.000000e+00 9.059125e-232 2 5
## 84 1 1 1.000000e+00 1.570727e-05 2 5
## 85 1 1 1.000000e+00 1.000000e+00 2 5
## 86 1 1 1.000000e+00 4.494179e-66 2 3
## 87 1 1 1.000000e+00 8.478130e-61 2 4
## 88 1 1 1.000000e+00 3.996519e-61 2 5
## 89 1 1 9.884830e-01 1.000000e+00 2 5
## 90 1 1 1.000000e+00 1.616919e-08 2 3
## 91 1 1 1.000000e+00 1.000000e+00 2 3
## 92 1 1 9.769245e-28 1.000000e+00 2 5
## 93 1 1 1.000000e+00 1.000000e+00 2 4
## 94 1 1 5.874835e-22 1.000000e+00 2 5
## 95 1 1 1.700445e-67 1.000000e+00 2 5
## 96 1 1 1.000000e+00 1.000000e+00 2 5
## 97 1 1 1.000000e+00 1.000000e+00 2 5
## 98 1 1 1.563386e-40 1.000000e+00 2 5
## 99 1 1 1.972679e-99 9.999857e-01 2 2
## 100 1 1 1.000000e+00 9.999974e-01 2 5
CM <- table(Evaluation$Actual,Evaluation$Vote)
CM
##
## 2
## 2 7
## 3 25
## 4 62
## 5 114
#Proportions
Overall <- length(Evaluation$Actual)
Length2 <- length(which(Evaluation$Actual==2))
Length3 <- length(which(Evaluation$Actual==3))
Length4 <- length(which(Evaluation$Actual==4))
Length5 <- length(which(Evaluation$Actual==5))
#Accuracy
Accuracy <- sum(diag(CM))/sum(CM)
#Precision
Precision <- diag(CM)/rowSums(CM)
Precision <- (Precision[1]*Length2+Precision[2]*Length3+Precision[3]*Length4+Precision[4]*Length5)/208
#Recall
Recall <- diag(CM)/colSums(CM)
Recall <- (Recall[1]*Length2+Recall[2]*Length3+Recall[3]*Length4+Recall[4]*Length5)/208
Accuracy
## [1] 0.03365385
Precision
## 2
## 0.1346154
Recall
## 2
## NA