ABALONE CLASSIFICATION USING ARTIFICIAL NEUTRAL NETWORK (ANN)
abalone <- read.csv("abalone.csv", header =T, na.strings=c("","NA"))
suppressWarnings(suppressMessages(library(dplyr)))
#create an age column for different age group
train1 <- abalone %>%
mutate(age=case_when(
rings %in% 1:5 ~ "young",
rings %in% 6:13 ~ "adult",
rings %in% 14:30 ~ "old"
))
#convert AGE into factor
train1$age <- as.factor(train1$age)
# since we have 3 AGE groups (adult, young, old), create extra variables into numeric classes
# class 1 as ADULT, CLASS 2 AS YOUNG, CLASS 3 AS OLD
train1$is_class_1 <- as.numeric(train1$age == "adult")
train1$is_class_2 <- as.numeric(train1$age == "young")
train1$is_class_3 <- as.numeric(train1$age == "old")
# remove variables - RING and AGE and SEX
myvars <- names(train1) %in% c("rings", "age", "sex")
train1 <- train1[!myvars]
#RANDOMIZE AND BREAK INTO TRAINSET AND TESTSET
train1 <- train1[sample(1:4177),]
# trainset = 60% , testset = 40% (any %)
trainset <- train1[1:2506, ]
testset <- train1[2507:4177, ]
# supports numeric only
suppressWarnings(suppressMessages(library(neuralnet)))
my_abalone_net <- neuralnet(is_class_1+is_class_2+is_class_3 ~ length + diameter + height + weight + shucked + viscera + shell,
trainset, hidden = 4, lifesign = "minimal", linear.output = FALSE,
threshold = 0.1)
## hidden: 4 thresh: 0.1 rep: 1/1 steps: 1782 error: 212.81245 time: 1.94 secs
plot(my_abalone_net)
# subset testset
temp_test <- subset(testset, select = c("length","diameter","height","weight","shucked","viscera","shell"))
# apply NN model to temp_test dataset
my_abalone_net.results <- compute(my_abalone_net, temp_test)
# view the results dataframe
results <- data.frame(testset$is_class_1, testset$is_class_2, testset$is_class_3,prediction = my_abalone_net.results$net.result)
# round up to 0 for comparison rather than estimated values
results$prediction_1 <- round(results$prediction.1)
results$prediction_2 <- round(results$prediction.2)
results$prediction_3 <- round(results$prediction.3)
# class 1 is classification by ADULT
table(results$prediction_1, results$testset.is_class_1)
##
## 0 1
## 0 102 50
## 1 171 1348
# CLASS 2 AS YOUNG, CLASS 3 AS OLD
table(results$prediction_2, results$testset.is_class_2)
##
## 0 1
## 0 1589 33
## 1 9 40
# CLASS 3 AS OLD
table(results$prediction_3, results$testset.is_class_3)
##
## 0 1
## 0 1427 135
## 1 44 65