1. Importing Libraries
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(nnet)
library(caTools)
library(caret)
## Loading required package: lattice
##
## Attaching package: 'caret'
##
## The following object is masked from 'package:purrr':
##
## lift
library(corrplot)
## corrplot 0.92 loaded
3. Viewing the Dataset
glimpse(Fish)
## Rows: 159
## Columns: 7
## $ Species <fct> Bream, Bream, Bream, Bream, Bream, Bream, Bream, Bream, Bream,…
## $ Weight <dbl> 242, 290, 340, 363, 430, 450, 500, 390, 450, 500, 475, 500, 50…
## $ Length1 <dbl> 23.2, 24.0, 23.9, 26.3, 26.5, 26.8, 26.8, 27.6, 27.6, 28.5, 28…
## $ Length2 <dbl> 25.4, 26.3, 26.5, 29.0, 29.0, 29.7, 29.7, 30.0, 30.0, 30.7, 31…
## $ Length3 <dbl> 30.0, 31.2, 31.1, 33.5, 34.0, 34.7, 34.5, 35.0, 35.1, 36.2, 36…
## $ Height <dbl> 11.5200, 12.4800, 12.3778, 12.7300, 12.4440, 13.6024, 14.1795,…
## $ Width <dbl> 4.0200, 4.3056, 4.6961, 4.4555, 5.1340, 4.9274, 5.2785, 4.6900…
dim(Fish)
## [1] 159 7
6. Model Creation & Prediction
6.1. Creating the Multinomial Logistic Regression Model
attach(Fish) # attaching the dataset
model <- multinom(Species~., train)
## # weights: 56 (42 variable)
## initial value 249.076499
## iter 10 value 208.424358
## iter 20 value 26.684804
## iter 30 value 1.052520
## iter 40 value 0.019562
## iter 50 value 0.001828
## final value 0.000006
## converged
summary(model) # checking the summary
## Call:
## multinom(formula = Species ~ ., data = train)
##
## Coefficients:
## (Intercept) Weight Length1 Length2 Length3 Height
## Parkki 75.022379 -0.77620481 331.23947 132.818380 -430.76412 163.1969
## Perch 363.768801 0.08201382 -151.70593 748.906417 -607.76902 105.9532
## Pike -404.868154 0.23706517 -70.71147 -2.211488 114.05685 -204.8255
## Roach -6.560714 -1.19299283 526.79088 -779.115651 252.54932 -172.8412
## Smelt 591.218773 0.94649029 268.36711 -40.855067 -186.76526 -110.2535
## Whitefish -202.780623 0.72991445 -408.58866 383.180050 14.16448 -129.5151
## Width
## Parkki -93.64487
## Perch 64.17247
## Pike 83.82029
## Roach 520.72515
## Smelt -24.95847
## Whitefish 120.28619
##
## Std. Errors:
## (Intercept) Weight Length1 Length2 Length3
## Parkki 6.831060e-02 1.489171e+01 1.707765e+00 1.810231e+00 1.912697e+00
## Perch 1.056936e+00 4.546846e+00 3.783465e+01 3.692776e+01 2.584637e+01
## Pike 5.442806e-27 6.803507e-24 2.830259e-25 3.047971e-25 3.249355e-25
## Roach 1.269236e-17 1.396160e-15 2.538473e-16 2.792320e-16 2.982705e-16
## Smelt 2.405418e-22 7.697338e-21 3.006773e-21 3.295423e-21 3.535965e-21
## Whitefish 1.079614e+00 4.418060e+00 3.833181e+01 3.744501e+01 2.636912e+01
## Height Width
## Parkki 4.896504e-01 2.830791e-01
## Perch 3.112159e+01 6.405598e+00
## Pike 5.816346e-26 3.801746e-26
## Roach 7.009358e-17 5.070599e-17
## Smelt 8.486315e-22 4.808912e-22
## Whitefish 3.102747e+01 6.484591e+00
##
## Residual Deviance: 1.211579e-05
## AIC: 84.00001
6.2. Predicting the Test Data
predicted <- predict(model, newdata=test, type = "class")
print(predicted)
## [1] Bream Bream Bream Bream Whitefish Bream Bream
## [8] Roach Roach Roach Roach Roach Parkki Parkki
## [15] Perch Perch Perch Perch Perch Perch Perch
## [22] Perch Perch Perch Perch Pike Pike Pike
## [29] Smelt Smelt Smelt
## Levels: Bream Parkki Perch Pike Roach Smelt Whitefish
6.3. Checking the Efficiency
conf_matrix <- confusionMatrix(predicted, reference = test$Species) # Creating the Confusion Matrix
accuracy <- conf_matrix$overall['Accuracy']
print(paste("Accuracy:", round(accuracy,3))) # Display the accuracy
## [1] "Accuracy: 0.935"