PREPARATION
setwd("~/Google Drive/UM/Smart Services/Thesis/Thesis/Code/Naive Bayes/4.Feature Set 3/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/5.Feature Set 4/Combined/Feature Set 3 TF.csv")
Features1 <- Features1[-1]
#Import Features
Features2 <- read.csv("~/Google Drive/UM/Smart Services/Thesis/Thesis/Code/Naive Bayes/5.Feature Set 4/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:1222){
Features[,i] <- as.factor(Features[,i])
}
str(Features)
## 'data.frame': 1000 obs. of 1222 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 4.515636e-20 0.004648895 9.953511e-01 0.005314261
## 2 1.0000000 2.832954e-25 1.000000000 8.836666e-11 0.997072197
## 3 1.0000000 3.515171e-20 0.999999985 1.536703e-08 0.869145732
## 4 1.0000000 1.626963e-21 0.999999991 9.436797e-09 0.999679409
## 5 0.9999998 1.859210e-07 0.860849911 1.391501e-01 0.818718417
## 6 1.0000000 4.363762e-20 0.936145801 6.385420e-02 0.985937448
## Class4: 1 Class 5: 0 Class5: 1
## 1 0.9946857390 1.00000000 2.664917e-15
## 2 0.0029278033 0.01585921 9.841408e-01
## 3 0.1308542682 0.86405687 1.359431e-01
## 4 0.0003205905 0.99985874 1.412630e-04
## 5 0.1812815830 0.99910082 8.991813e-04
## 6 0.0140625521 0.99063132 9.368676e-03
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 4.515636e-20 9.953511e-01 0.9946857390 2.664917e-15
## 2 2.832954e-25 8.836666e-11 0.0029278033 9.841408e-01
## 3 3.515171e-20 1.536703e-08 0.1308542682 1.359431e-01
## 4 1.626963e-21 9.436797e-09 0.0003205905 1.412630e-04
## 5 1.859210e-07 1.391501e-01 0.1812815830 8.991813e-04
## 6 4.363762e-20 6.385420e-02 0.0140625521 9.368676e-03
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 4.515636e-20 9.953511e-01 9.946857e-01 2.664917e-15 3 4
## 2 2.832954e-25 8.836666e-11 2.927803e-03 9.841408e-01 5 5
## 3 3.515171e-20 1.536703e-08 1.308543e-01 1.359431e-01 5 5
## 4 1.626963e-21 9.436797e-09 3.205905e-04 1.412630e-04 4 4
## 5 1.859210e-07 1.391501e-01 1.812816e-01 8.991813e-04 4 4
## 6 4.363762e-20 6.385420e-02 1.406255e-02 9.368676e-03 3 4
## 7 2.208717e-32 1.686325e-22 4.974256e-05 4.183739e-08 4 5
## 8 4.143364e-33 1.828791e-01 9.999932e-01 3.941484e-17 4 3
## 9 5.288060e-22 6.619411e-19 2.284836e-01 8.853870e-01 5 5
## 10 3.399876e-05 3.486137e-08 1.229081e-06 1.506735e-09 2 3
## 11 3.278344e-25 2.951382e-11 9.934173e-01 3.326386e-01 4 4
## 12 1.868900e-12 4.621849e-05 2.854050e-02 8.730483e-04 4 3
## 13 5.027253e-16 9.776870e-04 2.100820e-05 9.916194e-01 5 5
## 14 1.138547e-08 9.361993e-02 5.858650e-01 7.297551e-08 4 4
## 15 3.227040e-11 4.388707e-01 6.482814e-01 6.811627e-06 4 4
## 16 1.386272e-28 2.205443e-07 2.929579e-04 4.177405e-01 5 5
## 17 6.543943e-21 5.776299e-10 8.363497e-01 1.628919e-04 4 4
## 18 8.099177e-14 4.926111e-09 7.437305e-02 9.980680e-01 5 5
## 19 1.100633e-08 5.111676e-01 8.426644e-01 9.884737e-07 4 4
## 20 1.242468e-19 3.464781e-07 8.862404e-01 8.107141e-01 4 2
## 21 6.442656e-25 1.570936e-08 3.799833e-01 1.926325e-06 4 4
## 22 1.779593e-32 1.080513e-07 8.405443e-02 9.676656e-01 5 4
## 23 6.214414e-06 9.974409e-01 6.127580e-02 1.241823e-18 3 3
## 24 1.060810e-25 1.772202e-11 4.068896e-01 9.997228e-01 5 5
## 25 3.847463e-13 9.639005e-02 4.746024e-01 1.677415e-03 4 4
## 26 7.814542e-18 9.608295e-05 5.938655e-02 6.352830e-01 5 5
## 27 1.205399e-17 3.000864e-08 2.911650e-01 3.463592e-01 5 4
## 28 2.138952e-11 9.372265e-07 9.261166e-05 9.893885e-01 5 5
## 29 2.275944e-19 9.406407e-10 2.004498e-01 3.492922e-08 4 3
## 30 9.789474e-06 1.518806e-01 1.745532e-01 5.281624e-23 4 3
## 31 4.620255e-18 1.216203e-09 3.570195e-05 9.999990e-01 5 5
## 32 9.002666e-27 2.030896e-07 1.588163e-07 9.999994e-01 5 5
## 33 9.046001e-27 1.198425e-06 9.403955e-01 9.654729e-01 5 5
## 34 2.274239e-17 8.969589e-01 2.513440e-03 1.227621e-07 3 3
## 35 1.271642e-13 3.548279e-03 9.028704e-01 1.128116e-02 4 4
## 36 2.105595e-17 9.929949e-01 8.853753e-01 2.329371e-05 3 4
## 37 2.878734e-10 1.914218e-05 9.994034e-01 2.105185e-12 4 5
## 38 3.820557e-01 1.000000e+00 9.822022e-01 2.864433e-15 3 3
## 39 1.226935e-28 5.438156e-12 9.971370e-07 9.999997e-01 5 5
## 40 5.840577e-18 2.001622e-02 3.972974e-01 1.197094e-08 4 3
## 41 2.777717e-25 9.570048e-10 1.675438e-06 9.999999e-01 5 5
## 42 1.512227e-29 2.085217e-07 9.997881e-01 1.109248e-06 4 4
## 43 9.417481e-29 9.354194e-23 7.873981e-03 1.434374e-04 4 5
## 44 2.715642e-13 2.140636e-04 9.631045e-01 6.906582e-02 4 3
## 45 5.110519e-15 8.570710e-01 9.937020e-01 2.704595e-04 4 5
## 46 7.127726e-07 8.987982e-04 5.955615e-01 2.456352e-03 4 4
## 47 1.358400e-14 1.707674e-04 8.116982e-01 7.198199e-01 4 5
## 48 3.951034e-16 1.877508e-01 1.975427e-01 2.407760e-01 5 3
## 49 8.310494e-19 8.539175e-08 9.430621e-04 9.999991e-01 5 5
## 50 2.766772e-12 2.185262e-05 4.330321e-03 9.958397e-01 5 5
## 51 1.572099e-19 4.064158e-06 1.870497e-03 9.999893e-01 5 4
## 52 2.674191e-14 2.263054e-06 9.851808e-01 1.031928e-01 4 5
## 53 6.078799e-19 4.334799e-05 1.542355e-04 9.999144e-01 5 4
## 54 2.596323e-17 9.978915e-10 8.302110e-01 9.745437e-01 5 4
## 55 3.917716e-15 1.947235e-01 1.825230e-01 4.621719e-01 5 4
## 56 3.579024e-17 4.264484e-07 1.779627e-05 9.999992e-01 5 5
## 57 4.304718e-14 6.423343e-06 1.246935e-02 9.999183e-01 5 2
## 58 8.389805e-19 3.996961e-10 4.238185e-03 9.999947e-01 5 5
## 59 1.339933e-19 8.514899e-08 7.103748e-04 9.999990e-01 5 4
## 60 8.417208e-12 9.943319e-01 9.737209e-01 1.027895e-05 3 4
## 61 3.171250e-14 6.895091e-06 1.515592e-02 9.997803e-01 5 5
## 62 1.570709e-17 3.383086e-05 2.674951e-04 9.999870e-01 5 5
## 63 1.573844e-08 1.147020e-02 9.841369e-01 1.803206e-02 4 4
## 64 2.656112e-19 9.171189e-08 9.391698e-02 9.997304e-01 5 5
## 65 1.776602e-22 2.020526e-08 3.376934e-04 9.999994e-01 5 5
## 66 1.365688e-17 8.034608e-10 2.039942e-03 9.999979e-01 5 5
## 67 4.106761e-12 5.465789e-06 3.764725e-02 9.758226e-01 5 3
## 68 2.802821e-11 6.299343e-02 9.047046e-02 7.430810e-01 5 3
## 69 4.749208e-17 2.366588e-04 3.025732e-01 7.436110e-01 5 4
## 70 1.071795e-25 4.026867e-07 5.945286e-01 7.505277e-01 5 4
## 71 1.062096e-16 6.698695e-10 1.197998e-03 9.999988e-01 5 5
## 72 3.845707e-19 2.580250e-09 2.786803e-02 9.999599e-01 5 5
## 73 2.626866e-09 2.100211e-03 7.672722e-02 7.918933e-01 5 2
## 74 6.896930e-11 1.830266e-02 3.538606e-02 9.668093e-01 5 4
## 75 4.765762e-16 9.577430e-09 9.602625e-01 9.542552e-01 4 4
## 76 6.111437e-26 8.155718e-10 9.721568e-01 1.561051e-01 4 5
## 77 6.114988e-26 3.596988e-07 1.042269e-05 5.981837e-04 5 3
## 78 5.182971e-29 4.686671e-09 9.973071e-01 1.857061e-01 4 4
## 79 9.566501e-26 9.894402e-07 3.160377e-03 1.827126e-09 4 5
## 80 2.161434e-18 5.578221e-12 1.534058e-05 1.000000e+00 5 5
## 81 5.048961e-25 2.059342e-10 2.044311e-01 9.923760e-01 5 5
## 82 1.891829e-26 3.765726e-08 4.203805e-02 9.924089e-01 5 4
## 83 5.850714e-29 1.990803e-10 2.804698e-08 5.349492e-09 4 5
## 84 1.159264e-16 9.725693e-07 6.369058e-05 9.997430e-01 5 5
## 85 2.412641e-21 2.880547e-03 3.097720e-01 1.105930e-01 4 5
## 86 8.894451e-19 9.264689e-02 1.849790e-02 1.109501e-11 3 3
## 87 9.626429e-10 5.264002e-02 9.973088e-01 6.750269e-08 4 4
## 88 7.640330e-18 1.004181e-09 5.398844e-06 6.349927e-05 5 5
## 89 1.049127e-33 5.629606e-14 2.089757e-04 1.000000e+00 5 5
## 90 2.489249e-24 2.706758e-08 7.210857e-01 8.876727e-01 5 3
## 91 4.943949e-23 1.992096e-02 2.300344e-02 9.909817e-01 5 3
## 92 1.475709e-20 2.265651e-04 4.564336e-01 1.189759e-02 4 5
## 93 6.154416e-36 1.688351e-05 9.999388e-01 1.229289e-05 4 4
## 94 1.979239e-21 7.980307e-27 1.088201e-04 2.575714e-03 5 5
## 95 5.630283e-15 1.093697e-05 1.824262e-04 9.832857e-01 5 5
## 96 1.011068e-35 1.187650e-10 8.991954e-01 9.952297e-01 5 5
## 97 3.200106e-25 1.058308e-05 7.253826e-05 9.999669e-01 5 5
## 98 2.894764e-22 1.148364e-05 1.075500e-04 9.991982e-01 5 5
## 99 9.232092e-01 2.168849e-04 5.805664e-05 3.604142e-02 2 2
## 100 2.248184e-17 5.369002e-03 5.263733e-01 2.347883e-01 4 5
CM <- table(Evaluation$Actual,Evaluation$Vote)
CM
##
## 2 3 4 5
## 2 1 0 3 3
## 3 1 6 8 10
## 4 0 7 30 25
## 5 0 3 16 95
#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.6346154
Precision
## 2
## 0.6346154
Recall
## 2
## 0.6102651