This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE
parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
library(nnet)
## Warning: package 'nnet' was built under R version 4.4.3
library(caret)
## Warning: package 'caret' was built under R version 4.4.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.4.3
## Loading required package: lattice
library(NeuralNetTools)
## Warning: package 'NeuralNetTools' was built under R version 4.4.3
# 1. Load dataset
data(iris)
# 2. Preprocessing: Normalisasi dan ubah target jadi faktor
iris$Species <- as.factor(iris$Species)
# 3. Bagi data menjadi training dan testing
set.seed(123) # biar hasil konsisten
index <- createDataPartition(iris$Species, p = 0.7, list = FALSE)
train_data <- iris[index, ]
test_data <- iris[-index, ]
binary_data <- subset(train_data, Species != "virginica")
binary_data$Species <- factor(binary_data$Species)
# 4. Latih model neural network
# size = jumlah neuron di hidden layer, decay = regularisasi
nn_model <- nnet(Species ~ ., data = train_data, size = 5, decay = 0.01, maxit = 200)
## # weights: 43
## initial value 120.360750
## iter 10 value 51.984377
## iter 20 value 45.083133
## iter 30 value 23.080317
## iter 40 value 8.534328
## iter 50 value 7.295741
## iter 60 value 6.745967
## iter 70 value 6.547466
## iter 80 value 6.492817
## iter 90 value 6.466873
## iter 100 value 6.400929
## iter 110 value 6.355214
## iter 120 value 6.332091
## iter 130 value 6.329752
## iter 140 value 6.328830
## iter 150 value 6.328617
## final value 6.328607
## converged
# Plot NN
plotnet(nn_model)
# 5. Prediksi
predictions <- predict(nn_model, newdata = test_data, type = "class")
# 6. Evaluasi performa
predictions <- as.factor(predictions)
actual <- as.factor(test_data$Species)
confusionMatrix(predictions, actual)
## Confusion Matrix and Statistics
##
## Reference
## Prediction setosa versicolor virginica
## setosa 15 0 0
## versicolor 0 15 1
## virginica 0 0 14
##
## Overall Statistics
##
## Accuracy : 0.9778
## 95% CI : (0.8823, 0.9994)
## No Information Rate : 0.3333
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.9667
##
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: setosa Class: versicolor Class: virginica
## Sensitivity 1.0000 1.0000 0.9333
## Specificity 1.0000 0.9667 1.0000
## Pos Pred Value 1.0000 0.9375 1.0000
## Neg Pred Value 1.0000 1.0000 0.9677
## Prevalence 0.3333 0.3333 0.3333
## Detection Rate 0.3333 0.3333 0.3111
## Detection Prevalence 0.3333 0.3556 0.3111
## Balanced Accuracy 1.0000 0.9833 0.9667