PREPARATION

setwd("~/Google Drive/UM/Smart Services/Thesis/Thesis/Code/Naive Bayes/3.Feature Set 2/Valence")
#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
Features <- read.csv("~/Google Drive/UM/Smart Services/Thesis/Thesis/Code/Naive Bayes/3.Feature Set 2/Valence/Feature Set 2.csv")

Features <- Features[-1]

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:356){
  Features[,i] <- as.factor(Features[,i])
}
str(Features)
## 'data.frame':    1000 obs. of  356 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  0.9999995 4.650167e-07 0.76428935 2.357107e-01 0.18875253 0.81124747
## 2  1.0000000 5.503472e-10 0.99998169 1.831434e-05 0.02660152 0.97339848
## 3  1.0000000 3.598072e-09 0.99999310 6.899762e-06 0.28954517 0.71045483
## 4  1.0000000 1.971280e-11 0.96478076 3.521924e-02 0.96675015 0.03324985
## 5  0.9999895 1.045234e-05 0.81506149 1.849385e-01 0.67108796 0.32891204
## 6  1.0000000 4.726344e-10 0.05224748 9.477525e-01 0.86879268 0.13120732
##   Class 5: 0    Class5: 1
## 1  0.9994594 0.0005406481
## 2  0.9408691 0.0591308893
## 3  0.8951010 0.1048989823
## 4  0.9734803 0.0265196675
## 5  0.9076362 0.0923637585
## 6  0.9482454 0.0517545566
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.650167e-07 2.357107e-01 0.81124747 0.0005406481
## 2 5.503472e-10 1.831434e-05 0.97339848 0.0591308893
## 3 3.598072e-09 6.899762e-06 0.71045483 0.1048989823
## 4 1.971280e-11 3.521924e-02 0.03324985 0.0265196675
## 5 1.045234e-05 1.849385e-01 0.32891204 0.0923637585
## 6 4.726344e-10 9.477525e-01 0.13120732 0.0517545566
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.650167e-07 2.357107e-01 8.112475e-01 5.406481e-04    4      4
## 2   5.503472e-10 1.831434e-05 9.733985e-01 5.913089e-02    4      5
## 3   3.598072e-09 6.899762e-06 7.104548e-01 1.048990e-01    4      5
## 4   1.971280e-11 3.521924e-02 3.324985e-02 2.651967e-02    3      4
## 5   1.045234e-05 1.849385e-01 3.289120e-01 9.236376e-02    4      4
## 6   4.726344e-10 9.477525e-01 1.312073e-01 5.175456e-02    3      4
## 7   9.165787e-18 5.589335e-09 6.660558e-06 7.116459e-01    5      5
## 8   5.061857e-18 2.114455e-03 9.820429e-01 3.026308e-05    4      3
## 9   1.181787e-08 1.024407e-08 3.677832e-01 6.006763e-01    5      5
## 10  9.823375e-01 1.601735e-05 4.974777e-02 3.398315e-04    2      3
## 11  2.211170e-09 8.654215e-07 7.001154e-01 5.929509e-01    4      4
## 12  9.387676e-06 2.672617e-03 1.518645e-01 2.034806e-01    5      3
## 13  5.109462e-06 1.720839e-03 4.337924e-02 8.588533e-01    5      5
## 14  8.212762e-05 1.509241e-02 4.580528e-01 9.585252e-02    4      4
## 15  3.883798e-06 1.265436e-01 4.119284e-01 1.538132e-01    4      4
## 16  3.025208e-09 1.733670e-01 1.938838e-01 1.862263e-01    4      5
## 17  8.837582e-10 1.231412e-06 9.657251e-01 4.505650e-03    4      4
## 18  4.008451e-07 3.716564e-03 1.432992e-01 9.354386e-01    5      5
## 19  2.138520e-03 2.599752e-01 6.718449e-01 3.803512e-03    4      4
## 20  1.015542e-08 1.162474e-03 4.982930e-01 7.073658e-01    5      2
## 21  4.680610e-07 5.728591e-03 2.431786e-02 7.638658e-02    5      4
## 22  6.634777e-13 4.687925e-04 5.711624e-02 9.776917e-01    5      4
## 23  1.387754e-04 7.618799e-01 7.233933e-01 4.484394e-08    3      3
## 24  3.194644e-10 7.469452e-07 3.353296e-01 9.662224e-01    5      5
## 25  8.613562e-05 7.400650e-02 4.827970e-01 5.890331e-02    4      4
## 26  3.108868e-07 5.064777e-03 1.436298e-01 3.292546e-01    5      5
## 27  4.922495e-08 9.065749e-02 2.027551e-01 1.387225e-01    4      4
## 28  1.886566e-04 1.975747e-03 1.874473e-02 9.219436e-01    5      5
## 29  1.648887e-09 1.162764e-04 4.005620e-01 9.663543e-04    4      3
## 30  3.164029e-01 2.969502e-01 3.722549e-01 8.652354e-08    4      3
## 31  2.133273e-07 3.478189e-04 7.187032e-03 9.961716e-01    5      5
## 32  1.569050e-12 2.586975e-05 3.531393e-04 9.996612e-01    5      5
## 33  1.355440e-13 3.795573e-03 4.084184e-01 8.573651e-01    5      5
## 34  1.180948e-08 4.719633e-01 1.222993e-02 4.249148e-03    3      3
## 35  3.511284e-05 1.369243e-02 5.907309e-01 5.582554e-02    4      4
## 36  2.651566e-06 3.957036e-01 5.273590e-01 1.677804e-02    4      4
## 37  4.574407e-04 2.617841e-01 8.315097e-01 1.307846e-04    4      5
## 38  7.361952e-01 9.945368e-01 6.131926e-01 5.771123e-06    3      3
## 39  1.234763e-14 5.156515e-05 1.318626e-03 9.994176e-01    5      5
## 40  1.066696e-09 5.726013e-03 3.951525e-01 4.397423e-03    4      3
## 41  1.930936e-11 2.174762e-05 1.121954e-03 9.989417e-01    5      5
## 42  2.669944e-14 2.339388e-03 9.810573e-01 2.014681e-03    4      4
## 43  1.638239e-16 1.904313e-13 7.048503e-02 5.858121e-01    5      5
## 44  5.242188e-06 3.982960e-02 4.718423e-01 4.090527e-01    4      3
## 45  3.249634e-07 6.907704e-01 8.685978e-01 1.422557e-02    4      5
## 46  3.390944e-03 4.424780e-03 2.996900e-01 8.608821e-02    4      4
## 47  4.030743e-06 4.598023e-02 3.027433e-01 7.299705e-01    5      5
## 48  2.430074e-08 1.740676e-02 4.686511e-01 7.779640e-01    5      3
## 49  5.969527e-08 1.385269e-03 6.683092e-02 9.904092e-01    5      5
## 50  2.708908e-05 5.161235e-02 1.719910e-01 6.435133e-01    5      5
## 51  6.885806e-09 1.655375e-02 6.360634e-02 9.712090e-01    5      4
## 52  1.159415e-06 3.532646e-03 6.995571e-01 3.341724e-01    4      5
## 53  1.006898e-09 7.006660e-03 7.535073e-03 9.950473e-01    5      4
## 54  3.751884e-07 1.787694e-04 1.044192e-01 9.504005e-01    5      4
## 55  2.791622e-06 3.480632e-01 2.690474e-01 4.483856e-01    5      4
## 56  4.226389e-07 1.120512e-02 8.237805e-03 9.882528e-01    5      5
## 57  1.097798e-05 1.448493e-02 1.809539e-01 9.223424e-01    5      2
## 58  7.070469e-08 5.171939e-05 1.101937e-01 9.826112e-01    5      5
## 59  1.109572e-08 4.886772e-04 2.783924e-02 9.962232e-01    5      4
## 60  2.288519e-04 7.643984e-01 7.886400e-01 3.315435e-03    4      4
## 61  3.112981e-06 1.084161e-02 2.467240e-01 8.546731e-01    5      5
## 62  1.202167e-06 3.950239e-02 2.062982e-01 8.442413e-01    5      5
## 63  2.434293e-03 6.093255e-02 8.799508e-01 4.853203e-02    4      4
## 64  1.762998e-07 2.716272e-04 1.627694e-01 9.674796e-01    5      5
## 65  1.409085e-09 2.991633e-03 5.032200e-02 9.863567e-01    5      5
## 66  8.655882e-06 8.296279e-03 8.772051e-02 9.687601e-01    5      5
## 67  5.863584e-06 7.375972e-02 2.161843e-01 6.824138e-01    5      3
## 68  2.557589e-05 1.185870e-01 1.711268e-01 6.660088e-01    5      3
## 69  2.956777e-07 1.125795e-01 2.258941e-01 3.418067e-01    5      4
## 70  1.512420e-12 1.891136e-02 7.688892e-01 9.481299e-02    4      4
## 71  7.506710e-07 1.147047e-04 8.360728e-02 9.861147e-01    5      5
## 72  5.993498e-08 1.162465e-03 2.710385e-01 9.331712e-01    5      5
## 73  2.158390e-04 2.101647e-02 1.973394e-01 6.815957e-01    5      2
## 74  4.561519e-05 6.871665e-02 1.068651e-01 8.478867e-01    5      4
## 75  1.659187e-05 3.679464e-04 8.404491e-01 3.483050e-01    4      4
## 76  1.798498e-14 7.879236e-05 8.802665e-01 3.421211e-01    4      5
## 77  2.724581e-11 2.587449e-04 6.030217e-02 2.211002e-02    4      3
## 78  4.840566e-12 5.753232e-06 9.510870e-01 2.958306e-01    4      4
## 79  2.183355e-10 9.101888e-05 1.389614e-03 2.139336e-02    5      5
## 80  1.077876e-07 6.446324e-05 6.335076e-02 9.956014e-01    5      5
## 81  7.528509e-11 7.296923e-05 2.566611e-01 8.655358e-01    5      5
## 82  7.969035e-11 3.933557e-05 4.834141e-01 4.515895e-01    4      4
## 83  1.346074e-11 3.809361e-04 2.539605e-04 2.070016e-04    3      5
## 84  2.879231e-07 1.003667e-02 5.441313e-03 9.894722e-01    5      5
## 85  2.376058e-09 1.266989e-01 3.855557e-01 9.986852e-02    4      5
## 86  1.355508e-09 9.707496e-01 8.717077e-01 1.053429e-05    3      3
## 87  4.370391e-07 3.763302e-01 2.782371e-01 1.331130e-01    3      4
## 88  6.080133e-09 3.470171e-07 1.571435e-02 7.282611e-03    4      5
## 89  1.610812e-15 1.129387e-07 8.086074e-03 9.997969e-01    5      5
## 90  1.037801e-13 8.006734e-04 2.466437e-01 9.510981e-01    5      3
## 91  1.475136e-11 5.204189e-02 3.203656e-02 9.612785e-01    5      3
## 92  2.348359e-08 1.689285e-03 3.723248e-01 1.089209e-01    4      5
## 93  4.229548e-16 1.362107e-01 7.213274e-01 8.180886e-02    4      4
## 94  3.931423e-11 4.448283e-13 4.196839e-02 7.490669e-01    5      5
## 95  7.289331e-07 1.083334e-02 5.926119e-01 2.953147e-01    4      5
## 96  3.933237e-15 1.685489e-05 8.098888e-01 7.309395e-01    4      5
## 97  9.227195e-10 5.855623e-02 6.557598e-03 9.536119e-01    5      5
## 98  1.966758e-07 3.225504e-02 1.730311e-02 7.712486e-01    5      5
## 99  4.005856e-01 1.629865e-02 2.041143e-02 3.622732e-01    2      2
## 100 1.199066e-08 2.916217e-01 5.089867e-01 2.975388e-01    4      5
CM <- table(Evaluation$Actual,Evaluation$Vote)
CM
##    
##      2  3  4  5
##   2  1  0  2  4
##   3  1  5  8 11
##   4  0  5 32 25
##   5  0  3 21 90
#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.6153846
Precision
##         2 
## 0.6153846
Recall
##         2 
## 0.5938968