4
norm.values = caret::preProcess(train[, ],method="range")
train.norm=predict(norm.values, train[, ])
valid.norm=predict(norm.values, valid[, ])
accuracy = NULL
for(i in 1:11) {
knn.pred <- FNN::knn(train = train.norm[, -13], # Change the package to "FNN" and see what is different
test = valid.norm[, -13],
cl = train.norm[, 13], # cl = true classifications of training set.
k = i,
prob = TRUE)
accuracy[i] <- caret::confusionMatrix(factor(knn.pred, levels = c(1, 0)), factor(valid.norm[, 13], levels = c(1, 0)))$overall[1]
}
accuracy.df = data.frame(k=1:11, accuracy)
accuracy.df
## k accuracy
## 1 1 0.875625
## 2 2 0.916875
## 3 3 0.902500
## 4 4 0.915625
## 5 5 0.912500
## 6 6 0.918750
## 7 7 0.918125
## 8 8 0.920000
## 9 9 0.920000
## 10 10 0.919375
## 11 11 0.920000
plot(accuracy.df, type = "l")

# The best K value is 8, so we will fit kNN with K=8
knn.pred <- FNN::knn(train = train.norm[, -13], # Change the package to "FNN" and see what is different
test = valid.norm[, -13],
cl = train.norm[, 13], # cl = true classifications of training set.
k = 8,
prob = TRUE)
actual=valid.norm$Florence
pred.label = as.vector(knn.pred)
prob = attr(knn.pred, "prob")
p = c()
for (i in 1:length(pred.label)){
p[i] = ifelse(pred.label[i] == "Owner", prob[i], 1 - prob[i])
}
p
## [1] 0.000 0.125 0.000 0.125 0.125 0.125 0.375 0.000 0.250 0.125 0.000 0.000
## [13] 0.000 0.000 0.000 0.125 0.250 0.000 0.000 0.125 0.000 0.000 0.000 0.250
## [25] 0.250 0.250 0.000 0.125 0.000 0.125 0.000 0.125 0.375 0.000 0.250 0.125
## [37] 0.000 0.125 0.375 0.125 0.250 0.125 0.000 0.000 0.125 0.000 0.000 0.000
## [49] 0.000 0.000 0.250 0.000 0.000 0.000 0.000 0.000 0.125 0.375 0.000 0.125
## [61] 0.000 0.000 0.000 0.000 0.125 0.000 0.000 0.000 0.000 0.250 0.125 0.000
## [73] 0.000 0.125 0.375 0.000 0.000 0.000 0.000 0.250 0.000 0.250 0.000 0.250
## [85] 0.000 0.125 0.125 0.000 0.125 0.125 0.000 0.125 0.000 0.000 0.000 0.000
## [97] 0.125 0.000 0.125 0.000 0.000 0.000 0.250 0.125 0.000 0.000 0.000 0.000
## [109] 0.000 0.000 0.125 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.125
## [121] 0.000 0.000 0.125 0.000 0.000 0.125 0.250 0.000 0.000 0.125 0.000 0.000
## [133] 0.000 0.000 0.000 0.000 0.125 0.250 0.000 0.000 0.250 0.125 0.250 0.000
## [145] 0.000 0.250 0.375 0.000 0.000 0.250 0.375 0.000 0.000 0.000 0.250 0.000
## [157] 0.000 0.125 0.000 0.125 0.000 0.125 0.000 0.000 0.125 0.250 0.000 0.000
## [169] 0.000 0.250 0.000 0.125 0.000 0.000 0.250 0.125 0.125 0.000 0.000 0.250
## [181] 0.000 0.125 0.000 0.250 0.000 0.000 0.375 0.000 0.125 0.125 0.000 0.000
## [193] 0.000 0.375 0.000 0.000 0.250 0.500 0.000 0.000 0.000 0.125 0.125 0.000
## [205] 0.000 0.000 0.000 0.125 0.125 0.125 0.125 0.125 0.125 0.000 0.000 0.000
## [217] 0.250 0.250 0.250 0.125 0.125 0.500 0.125 0.000 0.000 0.000 0.000 0.000
## [229] 0.125 0.125 0.000 0.000 0.000 0.125 0.125 0.000 0.000 0.000 0.000 0.125
## [241] 0.125 0.000 0.125 0.125 0.000 0.250 0.125 0.000 0.000 0.000 0.000 0.000
## [253] 0.000 0.125 0.250 0.250 0.375 0.000 0.000 0.250 0.250 0.000 0.125 0.000
## [265] 0.250 0.000 0.125 0.000 0.000 0.000 0.250 0.125 0.000 0.250 0.000 0.000
## [277] 0.000 0.000 0.000 0.125 0.000 0.250 0.250 0.000 0.000 0.125 0.125 0.000
## [289] 0.250 0.000 0.000 0.000 0.250 0.375 0.125 0.000 0.000 0.000 0.000 0.000
## [301] 0.125 0.000 0.000 0.000 0.125 0.000 0.125 0.000 0.000 0.125 0.000 0.000
## [313] 0.250 0.125 0.125 0.125 0.000 0.125 0.125 0.125 0.125 0.375 0.375 0.000
## [325] 0.000 0.125 0.250 0.000 0.125 0.125 0.125 0.250 0.000 0.000 0.000 0.000
## [337] 0.000 0.125 0.125 0.000 0.125 0.250 0.000 0.000 0.000 0.000 0.000 0.250
## [349] 0.000 0.125 0.000 0.125 0.000 0.125 0.125 0.000 0.000 0.000 0.250 0.000
## [361] 0.125 0.250 0.000 0.000 0.000 0.250 0.375 0.000 0.000 0.000 0.125 0.125
## [373] 0.125 0.250 0.000 0.125 0.000 0.000 0.250 0.000 0.000 0.125 0.000 0.125
## [385] 0.125 0.000 0.250 0.000 0.000 0.000 0.250 0.000 0.000 0.125 0.250 0.000
## [397] 0.125 0.000 0.000 0.000 0.125 0.000 0.250 0.000 0.000 0.125 0.125 0.125
## [409] 0.000 0.000 0.125 0.125 0.000 0.000 0.125 0.000 0.125 0.125 0.000 0.125
## [421] 0.125 0.250 0.125 0.000 0.000 0.000 0.000 0.000 0.000 0.125 0.125 0.125
## [433] 0.000 0.125 0.000 0.000 0.000 0.125 0.125 0.125 0.250 0.375 0.000 0.000
## [445] 0.000 0.125 0.000 0.000 0.125 0.000 0.125 0.000 0.000 0.000 0.000 0.000
## [457] 0.000 0.000 0.000 0.000 0.125 0.125 0.250 0.000 0.125 0.250 0.250 0.000
## [469] 0.500 0.000 0.000 0.000 0.000 0.000 0.250 0.000 0.000 0.000 0.000 0.000
## [481] 0.375 0.000 0.000 0.000 0.125 0.000 0.250 0.250 0.000 0.125 0.000 0.000
## [493] 0.125 0.125 0.125 0.250 0.125 0.250 0.000 0.125 0.000 0.500 0.125 0.000
## [505] 0.250 0.000 0.000 0.125 0.000 0.250 0.000 0.125 0.000 0.125 0.125 0.125
## [517] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.250 0.375 0.250 0.000
## [529] 0.000 0.125 0.000 0.000 0.125 0.000 0.375 0.125 0.000 0.125 0.000 0.125
## [541] 0.000 0.125 0.125 0.375 0.000 0.125 0.000 0.000 0.125 0.000 0.000 0.125
## [553] 0.250 0.000 0.000 0.125 0.000 0.000 0.000 0.000 0.250 0.000 0.125 0.000
## [565] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.250 0.000 0.000 0.000 0.000
## [577] 0.000 0.125 0.125 0.125 0.000 0.000 0.000 0.000 0.250 0.000 0.250 0.250
## [589] 0.125 0.250 0.250 0.000 0.000 0.125 0.000 0.125 0.000 0.125 0.250 0.125
## [601] 0.125 0.125 0.000 0.000 0.000 0.000 0.125 0.000 0.125 0.250 0.125 0.125
## [613] 0.125 0.000 0.000 0.000 0.000 0.125 0.375 0.000 0.125 0.250 0.125 0.000
## [625] 0.000 0.000 0.000 0.000 0.125 0.000 0.000 0.125 0.000 0.125 0.000 0.000
## [637] 0.125 0.125 0.000 0.000 0.375 0.125 0.000 0.000 0.000 0.375 0.000 0.000
## [649] 0.250 0.000 0.000 0.000 0.125 0.000 0.125 0.125 0.125 0.125 0.000 0.000
## [661] 0.000 0.000 0.000 0.125 0.000 0.125 0.000 0.000 0.000 0.000 0.000 0.250
## [673] 0.000 0.125 0.125 0.000 0.125 0.000 0.000 0.250 0.000 0.000 0.125 0.000
## [685] 0.000 0.125 0.125 0.500 0.125 0.125 0.000 0.375 0.000 0.000 0.125 0.125
## [697] 0.375 0.250 0.000 0.125 0.250 0.125 0.000 0.375 0.125 0.125 0.000 0.125
## [709] 0.000 0.125 0.125 0.000 0.000 0.000 0.125 0.000 0.000 0.000 0.000 0.000
## [721] 0.000 0.000 0.250 0.000 0.000 0.125 0.000 0.125 0.000 0.125 0.125 0.000
## [733] 0.000 0.000 0.125 0.250 0.250 0.125 0.000 0.125 0.250 0.000 0.125 0.250
## [745] 0.125 0.000 0.125 0.125 0.125 0.000 0.125 0.000 0.125 0.000 0.000 0.000
## [757] 0.000 0.250 0.000 0.000 0.000 0.125 0.000 0.125 0.125 0.250 0.000 0.125
## [769] 0.000 0.000 0.125 0.000 0.000 0.000 0.500 0.375 0.125 0.000 0.000 0.000
## [781] 0.000 0.250 0.125 0.250 0.000 0.000 0.000 0.125 0.125 0.000 0.125 0.000
## [793] 0.125 0.125 0.125 0.125 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
## [805] 0.000 0.000 0.125 0.250 0.000 0.000 0.125 0.000 0.125 0.000 0.250 0.125
## [817] 0.125 0.000 0.125 0.125 0.125 0.000 0.125 0.125 0.125 0.125 0.000 0.250
## [829] 0.125 0.125 0.125 0.125 0.125 0.125 0.000 0.000 0.125 0.250 0.000 0.250
## [841] 0.000 0.375 0.000 0.000 0.000 0.125 0.000 0.250 0.125 0.000 0.125 0.125
## [853] 0.000 0.000 0.000 0.000 0.000 0.125 0.125 0.000 0.125 0.000 0.125 0.125
## [865] 0.125 0.000 0.125 0.250 0.250 0.000 0.000 0.000 0.125 0.125 0.250 0.000
## [877] 0.000 0.000 0.125 0.125 0.250 0.000 0.000 0.000 0.000 0.000 0.000 0.250
## [889] 0.125 0.000 0.000 0.000 0.000 0.125 0.000 0.000 0.000 0.000 0.125 0.000
## [901] 0.125 0.000 0.000 0.000 0.000 0.250 0.000 0.000 0.000 0.000 0.125 0.000
## [913] 0.000 0.125 0.125 0.000 0.000 0.000 0.000 0.125 0.250 0.000 0.000 0.125
## [925] 0.000 0.000 0.000 0.125 0.000 0.125 0.125 0.000 0.000 0.250 0.125 0.000
## [937] 0.000 0.250 0.125 0.250 0.125 0.250 0.125 0.000 0.125 0.125 0.000 0.000
## [949] 0.000 0.250 0.125 0.125 0.125 0.000 0.000 0.000 0.000 0.125 0.125 0.250
## [961] 0.000 0.125 0.125 0.000 0.125 0.000 0.125 0.000 0.125 0.000 0.000 0.125
## [973] 0.000 0.000 0.125 0.125 0.000 0.125 0.125 0.250 0.125 0.000 0.375 0.000
## [985] 0.250 0.125 0.000 0.125 0.000 0.000 0.125 0.125 0.000 0.000 0.000 0.250
## [997] 0.375 0.000 0.125 0.000 0.000 0.000 0.000 0.125 0.000 0.000 0.250 0.000
## [1009] 0.125 0.000 0.125 0.125 0.125 0.000 0.250 0.000 0.000 0.125 0.125 0.250
## [1021] 0.000 0.125 0.000 0.000 0.000 0.000 0.000 0.250 0.000 0.000 0.250 0.000
## [1033] 0.125 0.000 0.250 0.000 0.125 0.000 0.125 0.125 0.125 0.000 0.000 0.125
## [1045] 0.375 0.000 0.125 0.000 0.125 0.125 0.125 0.000 0.250 0.250 0.000 0.125
## [1057] 0.000 0.000 0.250 0.000 0.000 0.125 0.000 0.250 0.000 0.000 0.125 0.125
## [1069] 0.000 0.000 0.000 0.125 0.250 0.125 0.375 0.000 0.125 0.125 0.000 0.125
## [1081] 0.125 0.000 0.500 0.000 0.250 0.000 0.000 0.125 0.125 0.000 0.000 0.125
## [1093] 0.000 0.000 0.000 0.000 0.125 0.000 0.125 0.000 0.125 0.000 0.250 0.000
## [1105] 0.000 0.125 0.000 0.125 0.125 0.000 0.000 0.125 0.000 0.125 0.000 0.000
## [1117] 0.375 0.000 0.250 0.000 0.125 0.000 0.125 0.125 0.125 0.250 0.250 0.000
## [1129] 0.125 0.125 0.000 0.000 0.000 0.125 0.000 0.125 0.000 0.000 0.125 0.000
## [1141] 0.000 0.250 0.000 0.000 0.125 0.000 0.000 0.000 0.125 0.000 0.000 0.000
## [1153] 0.000 0.250 0.000 0.000 0.000 0.000 0.000 0.250 0.000 0.000 0.000 0.000
## [1165] 0.000 0.000 0.000 0.125 0.125 0.000 0.000 0.000 0.125 0.000 0.000 0.000
## [1177] 0.000 0.000 0.000 0.125 0.000 0.250 0.125 0.125 0.000 0.125 0.250 0.000
## [1189] 0.000 0.125 0.000 0.125 0.000 0.250 0.125 0.000 0.125 0.250 0.000 0.125
## [1201] 0.000 0.000 0.000 0.250 0.125 0.000 0.000 0.000 0.125 0.125 0.000 0.000
## [1213] 0.250 0.250 0.000 0.250 0.500 0.000 0.250 0.125 0.000 0.375 0.000 0.000
## [1225] 0.375 0.125 0.000 0.000 0.000 0.000 0.000 0.000 0.250 0.125 0.125 0.000
## [1237] 0.000 0.000 0.250 0.125 0.125 0.125 0.250 0.000 0.000 0.125 0.000 0.000
## [1249] 0.125 0.125 0.250 0.000 0.000 0.125 0.000 0.125 0.000 0.000 0.125 0.125
## [1261] 0.375 0.250 0.125 0.125 0.000 0.125 0.125 0.125 0.125 0.000 0.125 0.250
## [1273] 0.000 0.000 0.000 0.000 0.000 0.125 0.000 0.000 0.125 0.125 0.125 0.250
## [1285] 0.250 0.000 0.000 0.125 0.000 0.125 0.000 0.000 0.125 0.250 0.500 0.125
## [1297] 0.125 0.125 0.250 0.250 0.000 0.125 0.000 0.125 0.000 0.000 0.000 0.250
## [1309] 0.000 0.125 0.000 0.000 0.250 0.250 0.125 0.125 0.125 0.250 0.000 0.250
## [1321] 0.125 0.125 0.000 0.125 0.000 0.000 0.125 0.250 0.250 0.250 0.000 0.125
## [1333] 0.125 0.125 0.125 0.000 0.000 0.000 0.250 0.125 0.125 0.125 0.250 0.125
## [1345] 0.000 0.000 0.125 0.250 0.000 0.125 0.000 0.000 0.000 0.000 0.000 0.000
## [1357] 0.000 0.000 0.000 0.125 0.000 0.000 0.000 0.250 0.125 0.125 0.125 0.250
## [1369] 0.125 0.000 0.000 0.000 0.000 0.125 0.125 0.000 0.000 0.000 0.000 0.000
## [1381] 0.000 0.000 0.000 0.250 0.000 0.250 0.000 0.125 0.125 0.000 0.125 0.000
## [1393] 0.125 0.000 0.125 0.125 0.125 0.250 0.000 0.000 0.125 0.125 0.000 0.250
## [1405] 0.125 0.125 0.125 0.000 0.125 0.125 0.000 0.000 0.000 0.250 0.000 0.375
## [1417] 0.000 0.125 0.125 0.000 0.125 0.125 0.000 0.250 0.250 0.250 0.125 0.000
## [1429] 0.125 0.125 0.125 0.125 0.125 0.000 0.000 0.125 0.125 0.125 0.000 0.250
## [1441] 0.375 0.125 0.125 0.000 0.000 0.000 0.125 0.000 0.000 0.250 0.000 0.000
## [1453] 0.375 0.000 0.125 0.000 0.000 0.250 0.125 0.125 0.000 0.125 0.000 0.000
## [1465] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.125 0.125 0.125 0.125 0.125
## [1477] 0.000 0.125 0.000 0.000 0.125 0.000 0.000 0.125 0.250 0.125 0.125 0.000
## [1489] 0.000 0.000 0.125 0.125 0.125 0.000 0.000 0.000 0.000 0.000 0.250 0.250
## [1501] 0.000 0.125 0.000 0.125 0.000 0.000 0.000 0.125 0.125 0.250 0.000 0.125
## [1513] 0.000 0.000 0.125 0.000 0.000 0.000 0.000 0.000 0.250 0.125 0.375 0.000
## [1525] 0.250 0.000 0.000 0.000 0.000 0.250 0.000 0.125 0.375 0.375 0.250 0.000
## [1537] 0.250 0.125 0.125 0.000 0.000 0.125 0.000 0.125 0.250 0.000 0.000 0.125
## [1549] 0.125 0.250 0.000 0.125 0.000 0.000 0.125 0.000 0.000 0.000 0.000 0.125
## [1561] 0.000 0.000 0.125 0.000 0.125 0.125 0.125 0.000 0.000 0.000 0.125 0.000
## [1573] 0.000 0.000 0.125 0.000 0.125 0.125 0.000 0.000 0.125 0.250 0.000 0.125
## [1585] 0.000 0.125 0.000 0.000 0.000 0.250 0.000 0.125 0.000 0.125 0.500 0.125
## [1597] 0.250 0.125 0.500 0.000
plot.lift(p, actual)
## Warning in gains(actual = actual, predicted = pred.prob, groups = groups):
## Warning: Fewer distinct predicted values than groups requested

6
logistic.model1=glm(Florence ~ ., data= train.norm, family = "binomial")
pred1=predict(logistic.model1, newdata=valid.norm, type = "response")
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading
actual=valid.norm$Florence
plot.lift(pred1, actual)

# Model 2 with selected predictors
logistic.model2=glm(Florence ~ Gender+ArtBks+GeogBks+Rcode, data= train.norm, family = "binomial")
pred2=predict(logistic.model2, newdata=valid.norm, type = "response")
actual=valid.norm$Florence
plot.lift(pred2, actual)

# Model 3 with most significant predictors
logistic.model3=glm(Florence ~ ArtBks+GeogBks+Rcode, data= train.norm, family = "binomial")
pred3=predict(logistic.model3, newdata=valid.norm, type = "response")
actual=valid.norm$Florence
plot.lift(pred3, actual)

7
pred.label1 = as.numeric(pred1>0.3)
pred.label2 = as.numeric(pred2>0.3)
pred.label3 = as.numeric(pred3>0.3)
table(pred.label1, pred.label2)
## pred.label2
## pred.label1 0 1
## 0 1573 5
## 1 3 19
table(pred.label1, pred.label3)
## pred.label3
## pred.label1 0 1
## 0 1572 6
## 1 3 19
table(pred.label2, pred.label3)
## pred.label3
## pred.label2 0 1
## 0 1570 6
## 1 5 19
table(pred.label1, pred.label2, pred.label3)
## , , pred.label3 = 0
##
## pred.label2
## pred.label1 0 1
## 0 1569 3
## 1 1 2
##
## , , pred.label3 = 1
##
## pred.label2
## pred.label1 0 1
## 0 4 2
## 1 2 17
confusionMatrix(factor(pred.label1), factor(actual), positive = "1")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 1457 121
## 1 14 8
##
## Accuracy : 0.9156
## 95% CI : (0.9009, 0.9288)
## No Information Rate : 0.9194
## P-Value [Acc > NIR] : 0.7274
##
## Kappa : 0.0845
##
## Mcnemar's Test P-Value : <2e-16
##
## Sensitivity : 0.06202
## Specificity : 0.99048
## Pos Pred Value : 0.36364
## Neg Pred Value : 0.92332
## Prevalence : 0.08063
## Detection Rate : 0.00500
## Detection Prevalence : 0.01375
## Balanced Accuracy : 0.52625
##
## 'Positive' Class : 1
##
confusionMatrix(factor(pred.label2), factor(actual), positive = "1")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 1454 122
## 1 17 7
##
## Accuracy : 0.9131
## 95% CI : (0.8982, 0.9265)
## No Information Rate : 0.9194
## P-Value [Acc > NIR] : 0.8328
##
## Kappa : 0.0679
##
## Mcnemar's Test P-Value : <2e-16
##
## Sensitivity : 0.054264
## Specificity : 0.988443
## Pos Pred Value : 0.291667
## Neg Pred Value : 0.922589
## Prevalence : 0.080625
## Detection Rate : 0.004375
## Detection Prevalence : 0.015000
## Balanced Accuracy : 0.521353
##
## 'Positive' Class : 1
##
confusionMatrix(factor(pred.label3), factor(actual), positive = "1")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 1452 123
## 1 19 6
##
## Accuracy : 0.9112
## 95% CI : (0.8962, 0.9247)
## No Information Rate : 0.9194
## P-Value [Acc > NIR] : 0.8913
##
## Kappa : 0.0531
##
## Mcnemar's Test P-Value : <2e-16
##
## Sensitivity : 0.04651
## Specificity : 0.98708
## Pos Pred Value : 0.24000
## Neg Pred Value : 0.92190
## Prevalence : 0.08063
## Detection Rate : 0.00375
## Detection Prevalence : 0.01562
## Balanced Accuracy : 0.51680
##
## 'Positive' Class : 1
##
# When accounting for only statistically significant variables we are still reasonably successful at predicting. There are only minor changes in accuracy, but the model is much easier to understand and interpret because it is a much simpler model.