options(warn = -1) # Load required libraries library(ggplot2) library(dplyr) library(neuralnet) library(plotly)
haberman <- read.csv(“/Users/sonal/Misclaneous/AU/sem3/MLII/haberman.csv”, header = FALSE, col.names = c(“Age”, “Op_year”, “Axil_nodes”, “Surv_status”))
haberman <- haberman %>% mutate(Age = as.numeric(Age), Op_year = as.numeric(Op_year), Axil_nodes = as.numeric(Axil_nodes), Surv_status = as.factor(Surv_status))
sum(is.na(haberman)) # There are NA values introduced by coercion, likely from the Surv_status column # Remove rows with NA values haberman <- haberman[complete.cases(haberman), ]
summary(haberman)
ggplot(haberman, aes(x = Age)) + geom_histogram(binwidth = 5, fill = “blue”, color = “white”) + labs(title = “Age Distribution”, x = “Age”, y = “Frequency”)
ggplot(haberman, aes(x = Axil_nodes)) + geom_histogram(binwidth = 5, fill = “red”, color = “white”) + labs(title = “Axillary Nodes Distribution”, x = “Number of Nodes”, y = “Frequency”)
ggplot(haberman, aes(x = as.factor(Op_year))) + geom_bar(fill = “green”, color = “white”) + labs(title = “Operation Year Distribution”, x = “Year of Operation”, y = “Frequency”)
ggplot(haberman, aes(x = Surv_status)) + geom_bar(fill = “purple”, color = “white”) + labs(title = “Survival Status Distribution”, x = “Survival Status”, y = “Frequency”)
pairs(haberman[,1:3], col = ifelse(haberman$Surv_status == 1, “blue”, “red”), main = “Pairwise Scatter Plots”, pch = 19)
#Preprocessing
normalize <- function(x) { return ((x - min(x)) / (max(x) - min(x))) }
haberman_norm <- as.data.frame(lapply(haberman[,1:3], normalize)) haberman_norm\(Surv_status <- as.numeric(haberman\)Surv_status) - 1 # Convert factor to numeric (0 and 1)
sum(is.na(haberman_norm))
haberman_norm <- haberman_norm[complete.cases(haberman_norm), ]
#Model Selection
library(neuralnet)
set.seed(123) index <- sample(1:nrow(haberman_norm), round(0.80 * nrow(haberman_norm))) trainset <- haberman_norm[index,] testset <- haberman_norm[-index,]
nn <- neuralnet(Surv_status ~ Age + Op_year + Axil_nodes, data = trainset, hidden = 3, linear.output = FALSE, act.fct = “tanh”, err.fct = “ce”, likelihood = TRUE)
plot(nn)
knitr::include_graphics(“neuralnet_plot.png”)
#Performance Evaluation
predicted <- compute(nn, testset[,1:3])$net.result predicted <- ifelse(predicted > 0.5, 1, 0)
predicted <- factor(predicted, levels = c(0, 1))
confusion_matrix <- table(predicted, testset$Surv_status) print(confusion_matrix)
accuracy <- sum(diag(confusion_matrix)) / sum(confusion_matrix) print(paste(“Accuracy:”, round(accuracy, 2)))
mse <- mean((as.numeric(predicted) - 1 - testset$Surv_status)^2) print(paste(“MSE:”, round(mse, 2)))
#Conclusion #The neural network model built to predict the survival status of patients using the Haberman’s Survival Dataset achieved an accuracy of 70% and a mean squared error (MSE) of 0.30 on the test set.
#Accuracy: The accuracy of 70% indicates that the model correctly predicted the survival status for 70% of the test instances. This level of accuracy suggests that the model has some predictive power but may still have room for improvement, particularly in terms of distinguishing between the two classes.
#Mean Squared Error (MSE): The MSE of 0.30 shows that the model’s predictions deviate from the actual survival status labels by an average squared difference of 0.30. While this is a relatively low error, it suggests that the model does not perfectly capture the relationship between the input features (Age, Operation Year, and Axillary Nodes) and the survival outcome.