PREPARATION
setwd("~/Google Drive/UM/Smart Services/Thesis/Thesis/Code/Naive Bayes/4.Feature Set 3/Negations")
#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/4.Feature Set 3/Combined/Feature Set 3 TF.csv")
Features1 <- Features1[-1]
#Import Features
Features2 <- read.csv("~/Google Drive/UM/Smart Services/Thesis/Thesis/Code/Naive Bayes/4.Feature Set 3/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:1201){
Features[,i] <- as.factor(Features[,i])
}
str(Features)
## 'data.frame': 1000 obs. of 1201 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 Class4: 1
## 1 1.0000000 4.548999e-20 0.00464307 9.953569e-01 0.00527817 0.9947218303
## 2 1.0000000 2.853885e-25 1.00000000 8.847803e-11 0.99705213 0.0029478708
## 3 1.0000000 3.541142e-20 0.99999998 1.538640e-08 0.86836460 0.1316353995
## 4 1.0000000 1.638983e-21 0.99999999 9.448690e-09 0.99967721 0.0003227937
## 5 0.9999998 1.872946e-07 0.86069897 1.393010e-01 0.81769940 0.1823006000
## 6 1.0000000 4.396002e-20 0.93607047 6.392953e-02 0.98584215 0.0141578549
## Class 5: 0 Class5: 1
## 1 1.00000000 2.646273e-15
## 2 0.01596917 9.840308e-01
## 3 0.86487944 1.351206e-01
## 4 0.99985973 1.402749e-04
## 5 0.99910710 8.928961e-04
## 6 0.99069626 9.303740e-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.548999e-20 9.953569e-01 0.9947218303 2.646273e-15
## 2 2.853885e-25 8.847803e-11 0.0029478708 9.840308e-01
## 3 3.541142e-20 1.538640e-08 0.1316353995 1.351206e-01
## 4 1.638983e-21 9.448690e-09 0.0003227937 1.402749e-04
## 5 1.872946e-07 1.393010e-01 0.1823006000 8.928961e-04
## 6 4.396002e-20 6.392953e-02 0.0141578549 9.303740e-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.548999e-20 9.953569e-01 9.947218e-01 2.646273e-15 3 4
## 2 2.853885e-25 8.847803e-11 2.947871e-03 9.840308e-01 5 5
## 3 3.541142e-20 1.538640e-08 1.316354e-01 1.351206e-01 5 5
## 4 1.638983e-21 9.448690e-09 3.227937e-04 1.402749e-04 4 4
## 5 1.872946e-07 1.393010e-01 1.823006e-01 8.928961e-04 4 4
## 6 4.396002e-20 6.392953e-02 1.415785e-02 9.303740e-03 3 4
## 7 1.146595e-31 5.867980e-23 2.774573e-06 9.670541e-07 4 5
## 8 4.173976e-33 1.830674e-01 9.999933e-01 3.913909e-17 4 3
## 9 5.327129e-22 6.627754e-19 2.296935e-01 8.846726e-01 5 5
## 10 3.424994e-05 3.490531e-08 1.237530e-06 1.496194e-09 2 3
## 11 3.302565e-25 2.955102e-11 9.934620e-01 3.310819e-01 4 4
## 12 1.882708e-12 4.627674e-05 2.873106e-02 8.669456e-04 4 3
## 13 5.064395e-16 9.789180e-04 2.115262e-05 9.915609e-01 5 5
## 14 1.146959e-08 9.372686e-02 5.875262e-01 7.246496e-08 4 4
## 15 3.250882e-11 4.391809e-01 6.498419e-01 6.763972e-06 4 4
## 16 1.396514e-28 2.208223e-07 2.949712e-04 4.160338e-01 5 5
## 17 6.592291e-21 5.783579e-10 8.372852e-01 1.617525e-04 4 4
## 18 8.159015e-14 4.932320e-09 7.484605e-02 9.980544e-01 5 5
## 19 1.108765e-08 5.114823e-01 8.435705e-01 9.815582e-07 4 4
## 20 8.245375e-19 2.481503e-06 8.419828e-01 7.712940e-01 4 2
## 21 8.495099e-25 2.234255e-08 1.317937e-01 5.313449e-06 4 4
## 22 1.792741e-32 1.081875e-07 8.458337e-02 9.674452e-01 5 4
## 23 6.260327e-06 9.974441e-01 6.167105e-02 1.233135e-18 3 3
## 24 1.068647e-25 1.774435e-11 4.085440e-01 9.997208e-01 5 5
## 25 3.875889e-13 9.649981e-02 4.763110e-01 1.665699e-03 4 4
## 26 7.872277e-18 9.620403e-05 5.977040e-02 6.336547e-01 5 5
## 27 1.214305e-17 3.004646e-08 2.925810e-01 3.447714e-01 5 4
## 28 2.154755e-11 9.384077e-07 9.324825e-05 9.893145e-01 5 5
## 29 2.292759e-19 9.418262e-10 2.015500e-01 3.468485e-08 4 3
## 30 9.861799e-06 1.520429e-01 1.755425e-01 5.244672e-23 4 3
## 31 4.654390e-18 1.217735e-09 3.594737e-05 9.999990e-01 5 5
## 32 9.069179e-27 2.033456e-07 1.599080e-07 9.999994e-01 5 5
## 33 9.112835e-27 1.199936e-06 9.407784e-01 9.652381e-01 5 5
## 34 2.291041e-17 8.970753e-01 2.530675e-03 1.219033e-07 3 3
## 35 1.281037e-13 3.552735e-03 9.034696e-01 1.120312e-02 4 4
## 36 2.121151e-17 9.930037e-01 8.860687e-01 2.313075e-05 3 4
## 37 2.900003e-10 1.916630e-05 9.994075e-01 2.090456e-12 4 5
## 38 3.837951e-01 1.000000e+00 9.823215e-01 2.844393e-15 3 3
## 39 1.236000e-28 5.445010e-12 1.003992e-06 9.999997e-01 5 5
## 40 5.883728e-18 2.004094e-02 3.989390e-01 1.188719e-08 4 3
## 41 2.798239e-25 9.582109e-10 1.686956e-06 9.999999e-01 5 5
## 42 1.523400e-29 2.087845e-07 9.997895e-01 1.101488e-06 4 4
## 43 9.487059e-29 9.365983e-23 7.927680e-03 1.424341e-04 4 5
## 44 2.735706e-13 2.143333e-04 9.633471e-01 6.861577e-02 4 3
## 45 5.148276e-15 8.572253e-01 9.937447e-01 2.685678e-04 4 5
## 46 7.180387e-07 8.999300e-04 5.972106e-01 2.439209e-03 4 4
## 47 1.368436e-14 1.709826e-04 8.127431e-01 7.184017e-01 4 5
## 48 3.980225e-16 1.879430e-01 1.986309e-01 2.394949e-01 5 3
## 49 8.371894e-19 8.549937e-08 9.495389e-04 9.999991e-01 5 5
## 50 2.787213e-12 2.188016e-05 4.359959e-03 9.958105e-01 5 5
## 51 1.583714e-19 4.069280e-06 1.883332e-03 9.999893e-01 5 4
## 52 2.693948e-14 2.265906e-06 9.852805e-01 1.025449e-01 4 5
## 53 6.123711e-19 4.340262e-05 1.552956e-04 9.999138e-01 5 4
## 54 2.615505e-17 9.991492e-10 8.311745e-01 9.743690e-01 5 4
## 55 3.946661e-15 1.949211e-01 1.835474e-01 4.604272e-01 5 4
## 56 3.605467e-17 4.269859e-07 1.791860e-05 9.999992e-01 5 5
## 57 4.336522e-14 6.431439e-06 1.255399e-02 9.999177e-01 5 2
## 58 8.451790e-19 4.001999e-10 4.267196e-03 9.999947e-01 5 5
## 59 1.349833e-19 8.525631e-08 7.152547e-04 9.999990e-01 5 4
## 60 8.479396e-12 9.943390e-01 9.738956e-01 1.020704e-05 3 4
## 61 3.194680e-14 6.903782e-06 1.525852e-02 9.997787e-01 5 5
## 62 1.582314e-17 3.387350e-05 2.693335e-04 9.999869e-01 5 5
## 63 1.585472e-08 1.148449e-02 9.842435e-01 1.790816e-02 4 4
## 64 2.675736e-19 9.182748e-08 9.450159e-02 9.997285e-01 5 5
## 65 1.789728e-22 2.023072e-08 3.400140e-04 9.999994e-01 5 5
## 66 1.375778e-17 8.044735e-10 2.053936e-03 9.999979e-01 5 5
## 67 4.137103e-12 5.472678e-06 3.789624e-02 9.756564e-01 5 3
## 68 2.823529e-11 6.306782e-02 9.103577e-02 7.417384e-01 5 3
## 69 4.784296e-17 2.369570e-04 3.040208e-01 7.422702e-01 5 4
## 70 1.079714e-25 4.031942e-07 5.961790e-01 7.492108e-01 5 4
## 71 1.069943e-16 6.707138e-10 1.206223e-03 9.999988e-01 5 5
## 72 3.874120e-19 2.583502e-09 2.805423e-02 9.999596e-01 5 5
## 73 2.646274e-09 2.102852e-03 7.721395e-02 7.907339e-01 5 2
## 74 6.947886e-11 1.832530e-02 3.562066e-02 9.665832e-01 5 4
## 75 1.258446e-15 2.728165e-08 9.166857e-01 9.611534e-01 5 4
## 76 6.156590e-26 8.165996e-10 9.723416e-01 1.551824e-01 4 5
## 77 6.160166e-26 3.601521e-07 1.049434e-05 5.940012e-04 5 3
## 78 5.221263e-29 4.692577e-09 9.973254e-01 1.846467e-01 4 4
## 79 9.637181e-26 9.906872e-07 3.182034e-03 1.814343e-09 4 5
## 80 2.177403e-18 5.585251e-12 1.544603e-05 1.000000e+00 5 5
## 81 5.086264e-25 2.061937e-10 2.055475e-01 9.923227e-01 5 5
## 82 1.905806e-26 3.770472e-08 4.231481e-02 9.923558e-01 5 4
## 83 7.714581e-29 2.831410e-10 6.946986e-09 1.475574e-08 5 5
## 84 1.167829e-16 9.737950e-07 6.412839e-05 9.997412e-01 5 5
## 85 2.430466e-21 2.884167e-03 3.112387e-01 1.099043e-01 4 5
## 86 8.960165e-19 9.275283e-02 1.862270e-02 1.101739e-11 3 3
## 87 9.697551e-10 5.270287e-02 9.973271e-01 6.703042e-08 4 4
## 88 7.696778e-18 1.005446e-09 5.435958e-06 6.305504e-05 5 5
## 89 1.056878e-33 5.636701e-14 2.104120e-04 1.000000e+00 5 5
## 90 2.507640e-24 2.710169e-08 7.224615e-01 8.869708e-01 5 3
## 91 4.980475e-23 1.994557e-02 2.315792e-02 9.909187e-01 5 3
## 92 1.486612e-20 2.268506e-04 4.581338e-01 1.181533e-02 4 5
## 93 6.199886e-36 1.690478e-05 9.999392e-01 1.220689e-05 4 4
## 94 1.993862e-21 7.990364e-27 1.095681e-04 2.557739e-03 5 5
## 95 5.671880e-15 1.095076e-05 1.836800e-04 9.831699e-01 5 5
## 96 1.018538e-35 1.189147e-10 8.998146e-01 9.951963e-01 5 5
## 97 3.223749e-25 1.059641e-05 7.303688e-05 9.999666e-01 5 5
## 98 2.916151e-22 1.149811e-05 1.082892e-04 9.991926e-01 5 5
## 99 9.237294e-01 2.171582e-04 5.845572e-05 3.579829e-02 2 2
## 100 2.264794e-17 5.375733e-03 5.280809e-01 2.335292e-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 29 26
## 5 0 3 15 96
#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.6088105