PREPARATION
setwd("~/Google Drive/UM/Smart Services/Thesis/Thesis/Code/Naive Bayes/3.Feature Set 2/Combined")
#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
ID <- Labels$ID
#Import Features
Features1 <- read.csv("~/Google Drive/UM/Smart Services/Thesis/Thesis/Code/Naive Bayes/3.Feature Set 2/Combined/Feature Set 2.csv")
Features1 <- Features1[-1]
#Import Features
Features2 <- read.csv("~/Google Drive/UM/Smart Services/Thesis/Thesis/Code/Naive Bayes/3.Feature Set 2/Combined/Feature Set 1 70th Percentile.csv")
Features2 <- Features2[-1]
#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:1140){
Features[,i] <- as.factor(Features[,i])
}
str(Features)
## 'data.frame': 1000 obs. of 1140 variables:
## $ amaz_jj : Factor w/ 2 levels "0","1": 1 1 1 1 2 1 1 1 1 1 ...
## $ arriv_jj : Factor w/ 2 levels "0","1": 2 1 1 1 1 1 1 1 1 1 ...
## $ bad_jj : Factor w/ 3 levels "0","1","2": 1 1 1 2 1 1 1 1 1 1 ...
## $ basic_jj : Factor w/ 3 levels "0","1","2": 1 1 2 1 1 1 1 1 1 1 ...
## $ beauti_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ befor_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ best_jjs : Factor w/ 2 levels "0","1": 2 1 1 1 1 1 1 1 1 1 ...
## $ big_jj : Factor w/ 3 levels "0","1","2": 3 1 1 1 1 1 1 1 1 3 ...
## $ build_jj : Factor w/ 3 levels "0","1","2": 1 1 1 2 1 1 1 1 1 1 ...
## $ central_jj : Factor w/ 4 levels "0","1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
## $ clean_jj : Factor w/ 5 levels "0","1","2","3",..: 1 1 1 2 1 1 2 1 2 1 ...
## $ clear_jj : Factor w/ 2 levels "0","1": 1 1 1 1 2 1 1 1 1 1 ...
## $ close_jj : Factor w/ 3 levels "0","1","2": 2 1 1 1 1 1 1 1 1 1 ...
## $ cold_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ difficult_jj : Factor w/ 3 levels "0","1","2": 1 1 2 1 1 1 1 1 1 1 ...
## $ due_jj : Factor w/ 3 levels "0","1","2": 2 1 1 1 1 1 1 1 1 1 ...
## $ earl_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ easi_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ english_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ enough_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ excel_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ extra_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ first_jj : Factor w/ 3 levels "0","1","2": 1 2 1 1 1 1 1 1 1 1 ...
## $ free_jj : Factor w/ 4 levels "0","1","2","5": 1 1 1 1 1 1 1 1 1 1 ...
## $ fresh_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ friend_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 2 1 1 ...
## $ front_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ full_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ general_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ good_jj : Factor w/ 6 levels "0","1","2","3",..: 1 1 2 1 1 2 1 3 1 4 ...
## $ great_jj : Factor w/ 6 levels "0","1","2","3",..: 1 4 1 2 1 3 1 1 1 1 ...
## $ guest_jjs : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ high_jj : Factor w/ 3 levels "0","1","2": 3 1 1 1 1 1 1 2 1 1 ...
## $ hot_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ huge_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ littl_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ locat_jj : Factor w/ 2 levels "0","1": 1 1 1 2 1 1 1 1 1 1 ...
## $ london_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ loud_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ main_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 2 1 1 1 1 ...
## $ major_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ modern_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 2 1 1 1 1 ...
## $ much_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 2 ...
## $ new_jj : Factor w/ 3 levels "0","1","2": 2 2 1 1 1 1 1 1 1 1 ...
## $ next_jj : Factor w/ 4 levels "0","1","2","3": 4 1 1 2 1 1 1 1 1 1 ...
## $ nice_jj : Factor w/ 5 levels "0","1","2","3",..: 1 1 2 4 1 1 1 1 1 1 ...
## $ nois_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ noisi_jj : Factor w/ 2 levels "0","1": 1 1 1 2 1 1 1 1 1 1 ...
## $ ok_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ old_jj : Factor w/ 3 levels "0","1","2": 1 1 1 2 1 1 1 1 1 1 ...
## $ onli_jj : Factor w/ 3 levels "0","1","2": 2 1 1 1 1 1 1 1 1 1 ...
## $ open_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ overal_jj : Factor w/ 2 levels "0","1": 1 1 1 2 1 1 1 1 1 1 ...
## $ particular_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ perfect_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ pillow_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ pleasant_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ poor_jj : Factor w/ 3 levels "0","1","3": 1 1 1 1 1 1 1 1 1 1 ...
## $ public_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ quiet_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 2 1 1 1 ...
## $ realli_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ recept_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ safe_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ second_jj : Factor w/ 3 levels "0","1","2": 1 2 1 2 1 1 1 1 1 1 ...
## $ select_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ servic_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 2 ...
## $ short_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ shower_jjr : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ sleep_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ small_jj : Factor w/ 4 levels "0","1","2","3": 2 1 1 1 1 1 1 1 1 1 ...
## $ spacious_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 2 1 1 1 ...
## $ special_jj : Factor w/ 2 levels "0","1": 2 1 1 1 1 1 1 1 1 1 ...
## $ standard_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ stay_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ steep_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ super_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ sure_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ underground_jj: Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ upgrad_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ veri_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 2 1 ...
## $ warm_jj : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ whole_jj : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ ask_vb : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ bed_vbd : Factor w/ 3 levels "0","1","2": 1 1 1 2 1 1 1 1 1 2 ...
## $ build_vb : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ came_vbd : Factor w/ 3 levels "0","1","2": 2 1 1 3 1 1 1 1 1 1 ...
## $ check_vb : Factor w/ 2 levels "0","1": 2 1 1 1 1 1 1 1 1 1 ...
## $ definit_vb : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ done_vbn : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ expens_vbz : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ gave_vbd : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ get_vb : Factor w/ 3 levels "0","1","2": 1 1 1 1 3 1 1 1 1 1 ...
## $ given_vbn : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ go_vb : Factor w/ 3 levels "0","1","2": 1 1 2 1 1 1 1 1 1 1 ...
## $ go_vbp : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ got_vbd : Factor w/ 4 levels "0","1","2","3": 3 1 1 1 1 1 1 1 1 1 ...
## $ like_vb : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
## $ love_vb : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ made_vbd : Factor w/ 3 levels "0","1","2": 3 1 1 1 1 1 2 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 1.0000000 7.907202e-20 0.003744541 9.962555e-01 0.004939682
## 2 1.0000000 4.960706e-25 1.000000000 1.098080e-10 0.996849696
## 3 1.0000000 6.155315e-20 0.999999981 1.909570e-08 0.860560964
## 4 1.0000000 3.728964e-21 0.999999983 1.665703e-08 0.999915102
## 5 0.9999997 3.255609e-07 0.832733918 1.672661e-01 0.807557848
## 6 1.0000000 1.000165e-19 0.892540056 1.074599e-01 0.996237986
## Class4: 1 Class 5: 0 Class5: 1
## 1 9.950603e-01 1.0000000 2.138378e-15
## 2 3.150304e-03 0.0196874 9.803126e-01
## 3 1.394390e-01 0.8879059 1.120941e-01
## 4 8.489764e-05 0.9996852 3.148121e-04
## 5 1.924422e-01 0.9992784 7.216476e-04
## 6 3.762014e-03 0.9793555 2.064452e-02
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 7.907202e-20 9.962555e-01 9.950603e-01 2.138378e-15
## 2 4.960706e-25 1.098080e-10 3.150304e-03 9.803126e-01
## 3 6.155315e-20 1.909570e-08 1.394390e-01 1.120941e-01
## 4 3.728964e-21 1.665703e-08 8.489764e-05 3.148121e-04
## 5 3.255609e-07 1.672661e-01 1.924422e-01 7.216476e-04
## 6 1.000165e-19 1.074599e-01 3.762014e-03 2.064452e-02
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 7.907202e-20 9.962555e-01 9.950603e-01 2.138378e-15 3 4
## 2 4.960706e-25 1.098080e-10 3.150304e-03 9.803126e-01 5 5
## 3 6.155315e-20 1.909570e-08 1.394390e-01 1.120941e-01 4 5
## 4 3.728964e-21 1.665703e-08 8.489764e-05 3.148121e-04 5 4
## 5 3.255609e-07 1.672661e-01 1.924422e-01 7.216476e-04 4 4
## 6 1.000165e-19 1.074599e-01 3.762014e-03 2.064452e-02 3 4
## 7 3.861531e-32 2.223020e-21 8.049949e-07 6.539476e-06 5 5
## 8 7.255326e-33 2.175973e-01 9.999937e-01 3.162718e-17 4 3
## 9 9.259770e-22 8.225550e-19 2.416923e-01 8.610858e-01 5 5
## 10 5.953274e-05 4.332016e-08 1.322781e-06 1.209031e-09 2 3
## 11 5.740614e-25 3.667508e-11 9.938808e-01 2.856919e-01 4 4
## 12 3.272577e-12 5.743234e-05 3.064962e-02 7.006706e-04 4 3
## 13 8.803079e-16 1.214626e-03 2.260974e-05 9.895773e-01 5 5
## 14 1.993677e-08 1.137519e-01 6.035711e-01 5.855687e-08 4 4
## 15 2.780894e-12 8.740888e-01 3.727486e-01 7.676110e-05 3 4
## 16 2.427462e-28 2.740573e-07 3.152847e-04 3.653585e-01 5 5
## 17 1.145891e-20 7.177864e-10 8.461586e-01 1.307116e-04 4 4
## 18 1.418224e-13 6.121386e-09 7.959166e-02 9.975934e-01 5 5
## 19 1.927287e-08 5.651074e-01 8.521617e-01 7.931694e-07 4 4
## 20 5.642658e-18 1.316112e-05 5.618900e-01 8.671669e-01 5 2
## 21 1.476643e-24 2.772882e-08 1.396052e-01 4.293652e-06 4 4
## 22 3.116195e-32 1.342690e-07 8.988640e-02 9.600221e-01 5 4
## 23 1.686421e-04 9.998870e-01 1.413123e-02 1.422293e-18 3 3
## 24 6.396875e-26 1.210266e-11 3.685249e-01 9.998363e-01 5 5
## 25 6.737183e-13 1.170408e-01 4.929484e-01 1.346434e-03 4 4
## 26 1.368382e-17 1.193938e-04 6.362587e-02 5.829325e-01 5 5
## 27 2.110740e-17 3.728996e-08 3.065575e-01 2.983417e-01 4 4
## 28 3.745457e-11 1.164636e-06 9.967128e-05 9.868100e-01 5 5
## 29 3.985341e-19 1.168878e-09 2.124841e-01 2.802784e-08 4 3
## 30 2.001514e-06 1.040806e-01 1.476123e-01 4.242016e-22 4 3
## 31 8.090397e-18 1.511303e-09 3.842361e-05 9.999988e-01 5 5
## 32 1.576431e-26 2.523674e-07 1.709237e-07 9.999993e-01 5 5
## 33 1.584019e-26 1.489212e-06 9.443828e-01 9.573339e-01 5 5
## 34 3.982354e-17 9.153763e-01 2.704536e-03 9.850656e-08 3 3
## 35 2.226736e-13 4.405443e-03 9.091254e-01 9.072429e-03 4 4
## 36 3.687047e-17 9.943550e-01 8.926229e-01 1.869138e-05 3 4
## 37 5.040869e-10 2.378674e-05 9.994456e-01 1.689238e-12 4 5
## 38 6.497114e-01 9.999988e-01 9.950421e-01 1.773513e-14 3 3
## 39 2.148451e-28 6.757674e-12 1.073154e-06 9.999996e-01 5 5
## 40 1.022727e-17 2.475275e-02 4.150156e-01 9.605700e-09 4 3
## 41 4.863981e-25 1.189213e-09 1.803167e-06 9.999999e-01 5 5
## 42 1.042527e-28 1.107339e-06 9.991260e-01 2.132225e-06 4 4
## 43 2.158466e-28 9.996556e-24 1.545860e-02 3.196570e-04 4 5
## 44 4.755284e-13 2.659903e-04 9.656282e-01 5.618639e-02 4 3
## 45 3.081735e-15 8.037329e-01 9.926047e-01 4.580197e-04 4 5
## 46 1.248115e-06 1.116640e-03 6.131268e-01 1.971978e-03 4 4
## 47 2.378656e-14 2.121937e-04 8.226714e-01 6.733647e-01 4 5
## 48 6.918543e-16 2.231414e-01 2.094482e-01 2.028533e-01 3 3
## 49 1.455227e-18 1.061113e-07 1.014884e-03 9.999989e-01 5 5
## 50 4.844815e-12 2.715481e-05 4.658908e-03 9.948206e-01 5 5
## 51 2.752858e-19 5.050283e-06 2.012809e-03 9.999867e-01 5 4
## 52 4.682699e-14 2.812162e-06 9.862160e-01 8.452721e-02 4 5
## 53 1.064441e-18 5.386541e-05 1.659919e-04 9.998934e-01 5 4
## 54 4.546346e-17 1.240021e-09 8.403176e-01 9.684731e-01 5 4
## 55 6.860200e-15 2.310545e-01 1.937419e-01 4.081237e-01 5 4
## 56 6.267127e-17 5.299221e-07 1.915296e-05 9.999989e-01 5 5
## 57 7.537868e-14 7.981896e-06 1.340722e-02 9.998982e-01 5 2
## 58 1.469115e-18 4.966787e-10 4.559813e-03 9.999934e-01 5 5
## 59 2.346319e-19 1.058096e-07 7.644894e-04 9.999987e-01 5 4
## 60 1.473913e-11 9.954336e-01 9.755368e-01 8.248036e-06 3 4
## 61 5.553086e-14 8.568107e-06 1.629252e-02 9.997262e-01 5 5
## 62 2.750424e-17 4.203926e-05 2.878820e-04 9.999838e-01 5 5
## 63 2.755914e-08 1.421378e-02 9.852440e-01 1.452099e-02 4 4
## 64 4.651042e-19 1.139649e-07 1.003583e-01 9.996640e-01 5 5
## 65 3.110957e-22 2.510788e-08 3.634284e-04 9.999993e-01 5 5
## 66 1.890448e-16 6.133109e-10 4.920109e-04 9.999996e-01 5 5
## 67 7.191232e-12 6.792003e-06 4.040136e-02 9.700477e-01 5 3
## 68 4.907940e-11 7.709975e-02 9.670060e-02 6.988692e-01 5 3
## 69 8.316203e-17 2.940650e-04 3.182979e-01 6.994535e-01 5 4
## 70 1.876790e-25 5.003949e-07 6.121095e-01 7.070921e-01 5 4
## 71 1.859806e-16 8.324071e-10 1.289210e-03 9.999985e-01 5 5
## 72 6.734108e-19 3.206324e-09 2.992898e-02 9.999500e-01 5 5
## 73 4.599830e-09 2.608479e-03 8.209637e-02 7.532925e-01 5 2
## 74 1.207702e-10 2.264307e-02 3.798129e-02 9.589719e-01 5 4
## 75 2.187468e-15 3.385862e-08 9.216344e-01 9.523662e-01 5 4
## 76 1.070156e-25 1.013463e-09 9.740779e-01 1.292480e-01 4 5
## 77 1.070778e-25 4.469763e-07 1.121726e-05 4.800501e-04 5 3
## 78 9.075752e-29 5.823848e-09 9.974974e-01 1.546898e-01 4 4
## 79 1.675163e-25 1.229518e-06 3.400492e-03 1.466119e-09 4 5
## 80 3.784826e-18 6.931724e-12 1.651006e-05 1.000000e+00 5 5
## 81 8.841092e-25 2.559022e-10 2.166397e-01 9.905165e-01 5 5
## 82 3.312727e-26 4.679444e-08 4.509833e-02 9.905573e-01 5 4
## 83 7.048467e-28 2.005133e-09 5.490240e-08 1.272854e-09 4 5
## 84 2.029954e-16 1.208554e-06 6.854575e-05 9.996797e-01 5 5
## 85 4.224707e-21 3.576985e-03 3.256962e-01 9.072429e-02 4 5
## 86 4.049453e-20 1.527076e-01 3.594525e-02 2.473011e-11 3 3
## 87 1.685657e-09 6.458768e-02 9.974989e-01 5.416538e-08 4 4
## 88 2.292077e-17 2.517748e-09 1.963380e-05 9.421712e-06 4 5
## 89 1.837096e-33 6.995577e-14 2.249036e-04 1.000000e+00 5 5
## 90 4.358853e-24 3.363528e-08 7.356194e-01 8.637817e-01 5 3
## 91 8.657207e-23 2.463552e-02 2.471379e-02 9.887860e-01 5 3
## 92 2.584072e-20 2.815235e-04 4.747118e-01 9.569333e-03 4 5
## 93 1.410579e-35 2.980094e-05 9.997690e-01 2.739969e-05 4 4
## 94 1.179457e-22 2.821239e-26 4.312276e-04 2.468941e-03 5 5
## 95 9.859028e-15 1.359069e-05 1.963309e-04 9.792555e-01 5 5
## 96 1.770452e-35 1.475822e-10 9.056623e-01 9.940621e-01 5 5
## 97 5.603613e-25 1.315093e-05 7.806784e-05 9.999587e-01 5 5
## 98 5.068939e-22 1.427000e-05 1.157482e-04 9.990010e-01 5 5
## 99 9.546528e-01 2.694959e-04 6.248235e-05 2.912771e-02 2 2
## 100 3.936730e-17 6.663061e-03 5.446460e-01 1.975633e-01 4 5
CM <- table(Evaluation$Actual,Evaluation$Vote)
CM
##
## 2 3 4 5
## 2 1 0 2 4
## 3 1 7 8 9
## 4 0 7 28 27
## 5 0 3 17 94
#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.625
Precision
## 2
## 0.625
Recall
## 2
## 0.602538