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
#Import Features
Features <- read.csv("~/Google Drive/UM/Smart Services/Thesis/Thesis/Code/Naive Bayes/4.Feature Set 3/Negations/Feature Set 3 TF.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:417){
  Features[,i] <- as.factor(Features[,i])
}
str(Features)
## 'data.frame':    1000 obs. of  417 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.9999997 2.675233e-07 0.80096246 1.990375e-01 0.19916559 0.8008344
## 2  1.0000000 3.166137e-10 0.99998524 1.475687e-05 0.02838203 0.9716180
## 3  1.0000000 2.069964e-09 0.99999444 5.559505e-06 0.30343888 0.6965611
## 4  1.0000000 8.664322e-12 0.97971270 2.028730e-02 0.88432958 0.1156704
## 5  0.9999940 6.013239e-06 0.84543275 1.545672e-01 0.68562162 0.3143784
## 6  1.0000000 2.077360e-10 0.08857613 9.114239e-01 0.63518382 0.3648162
##   Class 5: 0    Class5: 1
## 1  0.9993310 0.0006689738
## 2  0.9278382 0.0721618438
## 3  0.8733417 0.1266582896
## 4  0.9880090 0.0119909750
## 5  0.8881522 0.1118477796
## 6  0.9762619 0.0237381484
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 2.675233e-07 1.990375e-01 0.8008344 0.0006689738
## 2 3.166137e-10 1.475687e-05 0.9716180 0.0721618438
## 3 2.069964e-09 5.559505e-06 0.6965611 0.1266582896
## 4 8.664322e-12 2.028730e-02 0.1156704 0.0119909750
## 5 6.013239e-06 1.545672e-01 0.3143784 0.1118477796
## 6 2.077360e-10 9.114239e-01 0.3648162 0.0237381484
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   2.675233e-07 1.990375e-01 8.008344e-01 6.689738e-04    4      4
## 2   3.166137e-10 1.475687e-05 9.716180e-01 7.216184e-02    4      5
## 3   2.069964e-09 5.559505e-06 6.965611e-01 1.266583e-01    4      5
## 4   8.664322e-12 2.028730e-02 1.156704e-01 1.199098e-02    4      4
## 5   6.013239e-06 1.545672e-01 3.143784e-01 1.118478e-01    4      4
## 6   2.077360e-10 9.114239e-01 3.648162e-01 2.373815e-02    3      4
## 7   2.721574e-17 1.475385e-10 2.295659e-05 2.673768e-01    5      5
## 8   2.912077e-18 1.704426e-03 9.808296e-01 3.745072e-05    4      3
## 9   6.798799e-09 8.254181e-09 3.524340e-01 6.505342e-01    5      5
## 10  9.696937e-01 1.290606e-05 4.669131e-02 4.205123e-04    2      3
## 11  1.272082e-09 6.973153e-07 6.859449e-01 6.432005e-01    4      4
## 12  5.400734e-06 2.154586e-03 1.434814e-01 2.402013e-01    5      3
## 13  2.939470e-06 1.387033e-03 4.069730e-02 8.827676e-01    5      5
## 14  4.724952e-05 1.219650e-02 4.415675e-01 1.159785e-01    4      4
## 15  4.539994e-05 1.608008e-02 6.862808e-01 1.576366e-02    4      4
## 16  1.740397e-09 1.445590e-01 1.836835e-01 2.206960e-01    5      5
## 17  5.084245e-10 9.922126e-07 9.634503e-01 5.569846e-03    4      4
## 18  2.306055e-07 2.996792e-03 1.353135e-01 9.471752e-01    5      5
## 19  1.231405e-03 2.206166e-01 6.569930e-01 4.702652e-03    4      4
## 20  1.483969e-09 2.193868e-04 8.049292e-01 5.553026e-01    4      2
## 21  2.692747e-07 4.620966e-03 2.278633e-02 9.284500e-02    5      4
## 22  3.816975e-13 3.777650e-04 5.363262e-02 9.818958e-01    5      4
## 23  5.151466e-06 1.237017e-01 9.230272e-01 3.887990e-08    4      3
## 24  5.336899e-10 1.095135e-06 3.738782e-01 9.437257e-01    5      5
## 25  4.955548e-05 6.050062e-02 4.661872e-01 7.188795e-02    4      4
## 26  1.788526e-07 4.084973e-03 1.356286e-01 3.779042e-01    5      5
## 27  2.831903e-08 7.435689e-02 1.921995e-01 1.661951e-01    4      4
## 28  1.085425e-04 1.592574e-03 1.755787e-02 9.359655e-01    5      5
## 29  9.486017e-10 9.369207e-05 3.846772e-01 1.195603e-03    4      3
## 30  6.951738e-01 3.946409e-01 4.216636e-01 1.069745e-08    2      3
## 31  1.227268e-07 2.802748e-04 6.726957e-03 9.969041e-01    5      5
## 32  9.026714e-13 2.084471e-05 3.303876e-04 9.997262e-01    5      5
## 33  7.797822e-14 3.060547e-03 3.924259e-01 8.814967e-01    5      5
## 34  6.793970e-09 4.186680e-01 1.145076e-02 5.253080e-03    3      3
## 35  2.020065e-05 1.106212e-02 5.745327e-01 6.818087e-02    4      4
## 36  1.525443e-06 3.453870e-01 5.107300e-01 2.068065e-02    4      4
## 37  2.632160e-04 2.222339e-01 8.219692e-01 1.618427e-04    4      5
## 38  4.837668e-01 9.999496e-01 3.050258e-01 9.255879e-07    3      3
## 39  7.103571e-15 4.154914e-05 1.233747e-03 9.995293e-01    5      5
## 40  6.136685e-10 4.618884e-03 3.793464e-01 5.436195e-03    4      3
## 41  1.110864e-11 1.752326e-05 1.049722e-03 9.991446e-01    5      5
## 42  3.901475e-15 4.419211e-04 9.953740e-01 1.041779e-03    4      4
## 43  7.200515e-17 1.784191e-12 3.715882e-02 3.865429e-01    5      5
## 44  3.015828e-06 3.234302e-02 4.552776e-01 4.613826e-01    5      3
## 45  5.428764e-07 7.660904e-01 8.866711e-01 8.389208e-03    4      5
## 46  1.953617e-03 3.568343e-03 2.858975e-01 1.044007e-01    4      4
## 47  2.318883e-06 3.738255e-02 2.888682e-01 7.698700e-01    5      5
## 48  1.398017e-08 1.407311e-02 4.521026e-01 8.125927e-01    5      3
## 49  3.434258e-08 1.116484e-03 6.279426e-02 9.922357e-01    5      5
## 50  1.558448e-05 4.200791e-02 1.627100e-01 6.907761e-01    5      5
## 51  3.961392e-09 1.338125e-02 5.975197e-02 9.766056e-01    5      4
## 52  6.670099e-07 2.848391e-03 6.853720e-01 3.831335e-01    4      5
## 53  5.792663e-10 5.653325e-03 7.052876e-03 9.959940e-01    5      4
## 54  2.158453e-07 1.440488e-04 9.835146e-02 9.595349e-01    5      4
## 55  1.606017e-06 3.007892e-01 2.561494e-01 5.014773e-01    5      4
## 56  2.431434e-07 9.048242e-03 7.710988e-03 9.904860e-01    5      5
## 57  6.315644e-06 1.170419e-02 1.712894e-01 9.362977e-01    5      2
## 58  4.067628e-08 4.167343e-05 1.038293e-01 9.859016e-01    5      5
## 59  6.383347e-09 3.937900e-04 2.609186e-02 9.969459e-01    5      4
## 60  1.316709e-04 7.233155e-01 7.773220e-01 4.099669e-03    4      4
## 61  1.790895e-06 8.754085e-03 2.345527e-01 8.791960e-01    5      5
## 62  6.916047e-07 3.207526e-02 1.956033e-01 8.702572e-01    5      5
## 63  1.401894e-03 4.968459e-02 8.727334e-01 5.937466e-02    4      4
## 64  1.014250e-07 2.188757e-04 1.538936e-01 9.735561e-01    5      5
## 65  8.106443e-10 2.411916e-03 4.723202e-02 9.889463e-01    5      5
## 66  6.299388e-07 1.085407e-02 2.867514e-01 8.640251e-01    5      5
## 67  3.373317e-06 6.029595e-02 2.051094e-01 7.267095e-01    5      3
## 68  1.471392e-05 9.780468e-02 1.618833e-01 7.116257e-01    5      3
## 69  1.701029e-07 9.273921e-02 2.144578e-01 3.912291e-01    5      4
## 70  8.700922e-13 1.529405e-02 7.568398e-01 1.147484e-01    4      4
## 71  4.318598e-07 9.242558e-05 7.864270e-02 9.887497e-01    5      5
## 72  3.448049e-08 9.368701e-04 2.580787e-01 9.452959e-01    5      5
## 73  1.241832e-04 1.700348e-02 1.869995e-01 7.259597e-01    5      2
## 74  2.624285e-05 5.611764e-02 1.006712e-01 8.733851e-01    5      4
## 75  9.545338e-06 2.964948e-04 8.313120e-01 3.980987e-01    4      4
## 76  1.034673e-14 6.348808e-05 8.730653e-01 3.915619e-01    4      5
## 77  1.567446e-11 2.084947e-04 5.663591e-02 2.721853e-02    4      3
## 78  2.784769e-12 4.635684e-06 9.478930e-01 3.420601e-01    4      4
## 79  1.256080e-10 7.333994e-05 1.300173e-03 2.634075e-02    5      5
## 80  6.201003e-08 5.194204e-05 5.951090e-02 9.964426e-01    5      5
## 81  4.331137e-11 5.879594e-05 2.441585e-01 8.884648e-01    5      5
## 82  4.584571e-11 3.169496e-05 4.668022e-01 5.047134e-01    5      4
## 83  1.473284e-12 5.380884e-05 3.214161e-05 2.394445e-03    5      5
## 84  1.656416e-07 8.102866e-03 5.092416e-03 9.914755e-01    5      5
## 85  1.366942e-09 1.046638e-01 3.698988e-01 1.207251e-01    4      5
## 86  2.999313e-08 9.495602e-01 7.756890e-01 4.693106e-06    3      3
## 87  2.514278e-07 3.271435e-01 2.650582e-01 1.596808e-01    3      4
## 88  2.041704e-09 1.385791e-07 4.400738e-03 4.680133e-02    5      5
## 89  9.266970e-16 9.100058e-08 7.568886e-03 9.998359e-01    5      5
## 90  5.970452e-14 6.452446e-04 2.344751e-01 9.601094e-01    5      3
## 91  8.486429e-12 4.236109e-02 3.003388e-02 9.684760e-01    5      3
## 92  1.351007e-08 1.361592e-03 3.568931e-01 1.313921e-01    4      5
## 93  1.859004e-16 8.210513e-02 9.077830e-01 3.817813e-02    4      4
## 94  6.646038e-10 1.259851e-13 1.100460e-02 7.556664e-01    5      5
## 95  4.193540e-07 8.747394e-03 5.764348e-01 3.415027e-01    4      5
## 96  2.262784e-15 1.358090e-05 7.994193e-01 7.707409e-01    4      5
## 97  5.308388e-10 4.772464e-02 6.137567e-03 9.621784e-01    5      5
## 98  1.131472e-07 2.615343e-02 1.620603e-02 8.066643e-01    5      5
## 99  2.777013e-01 1.317438e-02 1.912110e-02 4.127989e-01    5      2
## 100 6.898207e-09 2.490847e-01 4.923336e-01 3.439048e-01    4      5
CM <- table(Evaluation$Actual,Evaluation$Vote)
CM
##    
##      2  3  4  5
##   2  0  0  3  4
##   3  2  5  6 12
##   4  0  4 33 25
##   5  0  2 19 93
#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.6298077
Precision
##         2 
## 0.6298077
Recall
##         2 
## 0.5962693