---
title: "Neural Network Diabetes"
author: "Sergio Uribe"
date: 2024-10-08
date-modified: last-modified
language:
title-block-published: "CREATED"
title-block-modified: "UPDATED"
format:
html:
toc: true
toc-expand: 3
code-tools: true
editor: visual
execute:
echo: true
cache: true
warning: false
message: false
---
# Quarto code
## Load necessary packages
```{r}
# Use pacman to load/install necessary packages
if (!require("pacman")) install.packages("pacman")
pacman::p_load(neuralnet, mlbench, tidyverse)
```
## Data Preparation
```{r}
# Load the dataset from the mlbench package
data("PimaIndiansDiabetes2", package = "mlbench")
```
# Create a copy of the dataset
```{r}
temp <- PimaIndiansDiabetes2
```
# Remove irrelevant columns: 'insulin' and 'triceps'
```{r}
temp <- temp |>
dplyr::select(-insulin, -triceps)
```
# Remove rows with missing data
```{r}
temp <- na.omit(temp)
```
## Target Variable Transformation
```{r}
# Recode the 'diabetes' column as binary 1 for 'pos' and 0 for 'neg'
temp <- temp %>%
mutate(diabetes = ifelse(diabetes == "pos", 1, 0))
```
## Scale the Data
```{r}
# Standardize the numerical columns (scaling)
temp <- temp %>%
mutate(across(everything(), scale))
```
## Define the Formula
```{r}
# Define the formula for the neural network model
f <- diabetes ~ pregnant + glucose + pressure + mass + pedigree + age
```
## Train Neural Network Model
```{r}
# Train the neural network using neuralnet package with 4 hidden nodes
fit <- neuralnet::neuralnet(f, data = temp, hidden = 4, algorithm = "rprop+")
```
## Visualize the Model
```{r}
# Plot the neural network model
plot(fit, intercept = TRUE, show.weights = TRUE)
```