Chapter 7 confusionMatrix(classPred, inibina$resposta, positive = “positiva”)

print(inibina, n = 32)

modKnn1_01 = knn3(resposta ~ difinib, data = inibina, k = 1) predito = predict(modKnn1_01, inibina, type = “class”) confusionMatrix(predito, inibina$resposta, positive = “positiva”)

modKnn3_01 = knn3(resposta ~ difinib, data = inibina, k = 3) predito = predict(modKnn3_01, inibina, type = “class”) confusionMatrix(predito, inibina$resposta, positive = “positiva”)

modKnn5_01 = knn3(resposta ~ difinib, data = inibina, k = 5) predito = predict(modKnn5_01, inibina, type = “class”) confusionMatrix(predito, inibina$resposta, positive = “positiva”)

library(e1071) modNaiveBayes01 = naiveBayes(resposta ~ difinib, data = inibina) predito = predict(modNaiveBayes01, inibina) confusionMatrix(predito, inibina$resposta, positive = “positiva”)

library(rpart) library(rpart.plot)

modArvDec01 = rpart(resposta ~ difinib, data = inibina) prp(modArvDec01, faclen=0, #use full names for factor labels extra=1, #display number of observations for each terminal node roundint=F, #don’t round to integers in output digits=5)

predito = predict(modArvDec01, type = “class”) confusionMatrix(predito, inibina$resposta, positive = “positiva”)

x = 1:32 plot(inibina\(difinib ~x, col = inibina\)resposta) modSVM01 = svm(resposta ~ difinib, data = inibina, kernel = “linear”)

predito = predict(modSVM01, type = “class”) confusionMatrix(predito, inibina$resposta, positive = “positiva”)

library(neuralnet)

modRedNeural01 = neuralnet(resposta ~ difinib, data = inibina, hidden = c(2,4,3)) plot(modRedNeural01)

ypred = neuralnet::compute(modRedNeural01, inibina) yhat = ypred$net.result

round(yhat)

yhat=data.frame(“yhat”=ifelse(max.col(yhat[ ,1:2])==1, “negativa”, “positiva”))

cm = confusionMatrix(inibina\(resposta, as.factor(yhat\)yhat)) print(cm)

library(caret) trControl <- trainControl(method = “LOOCV”)

fit <- train(resposta ~ difinib, method = “glm”, data = inibina, trControl = trControl, metric = “Accuracy”) fit <- train(resposta ~ difinib, method = “lda”, data = inibina, prior = c(0.5, 0.5), trControl = trControl, metric = “Accuracy”) fit <- train(resposta ~ difinib, method = “lda”, data = inibina, prior = c(0.65, 0.35), trControl = trControl, metric = “Accuracy”) fit <- train(resposta ~ difinib, method = “knn”, data = inibina, tuneGrid = expand.grid(k = 1:5), trControl = trControl, metric = “Accuracy”) # fit <- train(resposta ~ difinib, method = “svm”, data = inibina, # trControl = trControl, metric = “Accuracy”) # fit <- train(formula = resposta ~ difinib, method = “neuralnet”, data = data.frame(inibina), hidden = c(2,3), # linear.output = T, # trControl = trControl, metric = “Accuracy”)

totalAcerto = 0 for (i in 1:nrow(inibina)){ treino = inibina[-i,] teste = inibina[i,] modelo = svm(resposta ~ difinib, data = treino) predito = predict(modelo, newdata = teste, type = “class”) if(predito == teste$resposta[1]) totalAcerto = totalAcerto+1 }

iris = tibble(iris) irisS = iris[,1:4]

d <- dist(irisS, method = “maximum”) grup = hclust(d, method = “ward.D”) plot(grup, cex = 0.6)

groups <- cutree(grup, k=3) table(groups, iris$Species) rect.hclust(grup, k=3, border=“red”)

library(factoextra) library(ggpubr)

km1 = kmeans(irisS, 4) p1 = fviz_cluster(km1, data=irisS, palette = c(“#2E9FDF”, “#FC4E07”, “#E7B800”, “#E7B700”), star.plot=FALSE, # repel=TRUE, ggtheme=theme_bw()) p1 groups = km1\(cluster table(groups, iris\)Species)