# load needed libraries
library(rsample)
library(neuralnet)

# load dataset
data <- iris

# split the data
split <- initial_split(data, prop = 0.7)
train <- training(split)
test <- testing(split)

# build the neural network
nn_model <- neuralnet(
  Species ~ .,
  data = train,
  hidden = c(10, 5),
  linear.output = FALSE
)

# display the neural network
plot(nn_model)

# make predictions on the test data
predictions <- predict(nn_model, test)
predicted_labels <- apply(predictions, 1, which.max)
predicited_species <- levels(data$Species)[predicted_labels]

# evaluate model performance
accuracy <- sum(predicited_species == test$Species) / nrow(test)
cat("Accuracy of the neural network model:", accuracy)
