# Load required libraries
library(neuralnet)
library(rsample)
library(dplyr)
library(fastDummies)

# Load and inspect dataset
data <- mtcars
str(data)

# Recode some columns as factors
data$cyl <- as.factor(data$cyl)
data$vs  <- as.factor(data$vs)
data$am  <- as.factor(data$am)
data$gear <- as.factor(data$gear)
data$carb <- as.factor(data$carb)

data <- dummy_cols(data, remove_first_dummy = TRUE, remove_selected_columns = TRUE)

# Normalize the data (all numeric columns)
normalize <- function(x) (x - min(x)) / (max(x) - min(x))
norm_data <- data.frame(lapply(data, function(col) {
  if (is.numeric(col)) normalize(col) else col
}))

# Split the normalized data into training and testing sets
set.seed(123)  # for reproducibility
split <- initial_split(norm_data, prop = 0.7)
train <- training(split)
test <- testing(split)

# Train the neural network (regression: predict mpg)
nn_model <- neuralnet(
  mpg ~ .,
  data = train,
  hidden = c(5, 3),
  linear.output = TRUE
)

# Plot the trained network
plot(nn_model)

# Predict on the test set (only the predictors)
test_inputs <- test[, setdiff(names(test), "mpg")]
predicted_values <- predict(nn_model, test_inputs)

# Evaluate performance using Mean Squared Error
mse <- mean((predicted_values - test$mpg)^2)
cat("Mean Squared Error (MSE):", round(mse, 4), "\n")