install.packages("rmarkdown") install.packages("plotly") install.packages("neuralnet") library(ggplot2) library(dplyr) library(neuralnet) library(plotly) library(caret)

loading dataset

habermansurvival<-read.csv("C:\Users\sanan\Downloads\haberman.data",header=FALSE,col.names=c("Age","Opyear","Axilnodes","Survstatus")) View(haberman_survival)

convert to numeric

habermansurvival<-habermansurvival%>% mutate(Age=as.numeric(Age), Opyear=as.numeric(Opyear), Axilnodes=as.numeric(Axilnodes), Survstatus=as.factor(Survstatus))

Check for NA values and remove rows with NA values

sum(is.na(habermansurvival)) habermansurvival <- habermansurvival[complete.cases(haberman_survival), ] View(habermansurvival) summary(haberman_survival)

Distribution of Age (continuous)

ggplot(habermansurvival, aes(x = Age)) + geomhistogram(binwidth = 5, fill = "green", color = "red") + labs(title = "Age Dist", x = "Age", y = "Frequency")

Distribution of Axillary Nodes (continuous)

ggplot(habermansurvival, aes(x = Axilnodes)) + geom_histogram(binwidth = 5, fill = "yellow", color = "blue") + labs(title = "Axillary Nodes Dist", x = "No of Nodes", y = "Frequency")

Distribution of Operation Year (discrete, so we use geom_bar)

ggplot(habermansurvival, aes(x = as.factor(Opyear))) + geom_bar(fill = "cyan", color = "white") + labs(title = "Operation Year Dist", x = "Year of Operation", y = "Frequency")

Survival status distribution (discrete, use geom_bar)

ggplot(habermansurvival, aes(x = Survstatus)) + geom_bar(fill = "pink", color = "brown") + labs(title = "Survival Status Dist", x = "Survival Status", y = "Frequency")

Scatter plots

pairs( habermansurvival[,1:3], col = ifelse(habermansurvival$Surv_status == 1, "pink", "orange"), main = "Pairwise Scatter Plots", pch = 19 )

NORMALISATION

Normalization function

normalize <- function(x) { return ((x - min(x)) / (max(x) - min(x))) }

Normalize the numeric columns

habermannorm <- as.data.frame(lapply(habermansurvival[,1:3], normalize)) habermannorm$Survstatus <- as.numeric(habermansurvival$Survstatus) - 1 # Convert factor to numeric (0 and 1)

Check for NA values after normalization

sum(is.na(haberman_norm))

Handle NA values by removing rows with NA

habermannorm <- habermannorm[complete.cases(haberman_norm), ]

Load the neural network library

library(neuralnet)

Split the data

set.seed(123) index <- sample(1:nrow(habermannorm), round(0.80 * nrow(habermannorm))) trainset <- habermannorm[index,] testset <- habermannorm[-index,]

Train the neural network

nn <- neuralnet(Survstatus ~ Age + Opyear + Axil_nodes, data = trainset, hidden = 3, linear.output = FALSE, act.fct = "tanh", err.fct = "ce", likelihood = TRUE)

Save the neural network plot as PDF

pdf("neuralnet_plot.pdf", width = 8, height = 6) plot(nn) dev.off()

Save the neural network plot as PNG

png("neuralnet_plot.png", width = 800, height = 600) plot(nn) dev.off()

Predict on test data

predicted <- compute(nn, testset[,1:3])$net.result predicted <- ifelse(predicted > 0.5, 1, 0)

Convert predicted to factor for comparison with Surv_status

predicted <- factor(predicted, levels = c(0, 1))

Confusion matrix

confusionmatrix <- table(predicted, testset$Survstatus) print(confusion_matrix)

Calculate accuracy

accuracy <- sum(diag(confusionmatrix)) / sum(confusionmatrix) print(paste("Accuracy:", round(accuracy, 2)))

Calculate MSE

mse <- mean((as.numeric(predicted) - 1 - testset$Surv_status)^2) print(paste("MSE:",round(mse,2)))