library(nnet)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── 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(GGally)
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
# Load the mtcars dataset
data("mtcars")
mtcars <- na.omit(mtcars)

# Select only the mpg, hp, and wt variables
mtcars <- mtcars %>% select(mpg, hp, wt)

# Set seed for reproducibility
set.seed(123)

# Shuffle the dataset 
mtcars <- mtcars[sample(nrow(mtcars)),]

# Splitting into training (60%), validation (20%), and holdout/test (20%) datasets
train_index <- 1:round(0.6 * nrow(mtcars))
val_index <- (max(train_index) + 1):(max(train_index) + round(0.2 * nrow(mtcars)))

train_data <- mtcars[train_index, ]
val_data <- mtcars[val_index, ]
test_data <- mtcars[-c(train_index, val_index), ]

# Normalize our data using mean and standard deviation of train set.
mean <- apply(train_data, 2, mean)
std <- apply(train_data, 2, sd)

train_data   <- scale(train_data , center = mean , scale = std )
val_data     <- scale(val_data , center = mean , scale = std )
test_data    <- scale(test_data , center = mean , scale = std )

# Convert matrices back to data frames
train_data <- as.data.frame(train_data)
val_data <- as.data.frame(val_data)
test_data <- as.data.frame(test_data)

# Create a pair plot
p <- ggpairs(data.frame(train_data))

# Convert the ggplot to a plotly object for interactivity
p <- suppressWarnings(ggplotly(p))

# The plot will be printed automatically
p