Preparação dos dados
library(class)
library(e1071)
library(dplyr)
##
## 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
abalone <- read.csv("abalone.csv", header =T)
suppressWarnings(suppressMessages(library(dplyr)))
Criando a coluna sexo numérica
abalone <- abalone %>%
mutate(sex_num =case_when(
sex %in% 'M' ~ 0,
sex %in% "F" ~ 1,
sex %in% "I" ~ 2
))
Criando a coluna da Idade
abalone <- abalone %>%
mutate(age=case_when(
rings %in% 1:5 ~ "young",
rings %in% 6:13 ~ "adult",
rings %in% 14:30 ~ "old"
))
removendo rings, sex
abalone <- abalone[c(-1, -9)]
str(abalone)
## 'data.frame': 4177 obs. of 9 variables:
## $ length : num 0.455 0.35 0.53 0.44 0.33 0.425 0.53 0.545 0.475 0.55 ...
## $ diameter: num 0.365 0.265 0.42 0.365 0.255 0.3 0.415 0.425 0.37 0.44 ...
## $ height : num 0.095 0.09 0.135 0.125 0.08 0.095 0.15 0.125 0.125 0.15 ...
## $ ww : num 0.514 0.226 0.677 0.516 0.205 ...
## $ sw : num 0.2245 0.0995 0.2565 0.2155 0.0895 ...
## $ vw : num 0.101 0.0485 0.1415 0.114 0.0395 ...
## $ shell : num 0.15 0.07 0.21 0.155 0.055 0.12 0.33 0.26 0.165 0.32 ...
## $ sex_num : num 0 0 1 0 2 2 1 1 0 1 ...
## $ age : chr "old" "adult" "adult" "adult" ...
Preparando o conjunto de treinamento e teste
### A variável dependente é idade, com diferentes valores: Jovem, adulto e idoso.
### standardize the predictors
set.seed(100)
abalone_scale <- data.frame(scale(abalone[1:8]))
### add the target variable to the data set abalone_scale
abalone$age <- as.factor(abalone$age)
abalone_scale <- cbind(abalone_scale, age = abalone$age)
i <- sample(4177, 2088)
abalone_train <- abalone_scale[i,]
abalone_test <- abalone_scale[-i,]
O valor de K é importante no algoritmo KNN, porque a precisão da previsão no conjunto de testes depende disso. O valor ideal de K é o valor que leva à maior precisão de previsão.
### Nós usamos a função tune.knn do pacote e1071 para determinar um bom número k
### essa função executa uma validação cruzada 10 vezes
suppressWarnings(suppressMessages(library(e1071)))
t_knn <- tune.knn(abalone_train[,-9], factor(abalone_train[,9]), k = 1:100)
t_knn # names(t_knn) to see the list of variables
##
## Parameter tuning of 'knn.wrapper':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## k
## 15
##
## - best performance: 0.1398601
plot(t_knn)

Rodando a predição
library(class)
age <- abalone_train$age
pred <- knn(train = abalone_train[,-9], test = abalone_test[,-9], cl = age, k = t_knn$best.parameters)
Checando o resultado
### get the prediction accuracy in the test set
mean(pred == abalone_test$age)
## [1] 0.8697942
Matriz de confusão
table(pred,abalone_test$age)
##
## pred adult old young
## adult 1727 218 34
## old 9 30 0
## young 11 0 60