install.packages("rmarkdown") install.packages("plotly") install.packages("neuralnet") library(ggplot2) library(dplyr) library(neuralnet) library(plotly) library(caret)
habermansurvival<-read.csv("C:\Users\sanan\Downloads\haberman.data",header=FALSE,col.names=c("Age","Opyear","Axilnodes","Survstatus")) View(haberman_survival)
habermansurvival<-habermansurvival%>% mutate(Age=as.numeric(Age), Opyear=as.numeric(Opyear), Axilnodes=as.numeric(Axilnodes), Survstatus=as.factor(Survstatus))
sum(is.na(habermansurvival)) habermansurvival <- habermansurvival[complete.cases(haberman_survival), ] View(habermansurvival) summary(haberman_survival)
ggplot(habermansurvival, aes(x = Age)) + geomhistogram(binwidth = 5, fill = "green", color = "red") + labs(title = "Age Dist", x = "Age", y = "Frequency")
ggplot(habermansurvival, aes(x = Axilnodes)) + geom_histogram(binwidth = 5, fill = "yellow", color = "blue") + labs(title = "Axillary Nodes Dist", x = "No of Nodes", y = "Frequency")
ggplot(habermansurvival, aes(x = as.factor(Opyear))) + geom_bar(fill = "cyan", color = "white") + labs(title = "Operation Year Dist", x = "Year of Operation", y = "Frequency")
ggplot(habermansurvival, aes(x = Survstatus)) + geom_bar(fill = "pink", color = "brown") + labs(title = "Survival Status Dist", x = "Survival Status", y = "Frequency")
pairs( habermansurvival[,1:3], col = ifelse(habermansurvival$Surv_status == 1, "pink", "orange"), main = "Pairwise Scatter Plots", pch = 19 )
normalize <- function(x) { return ((x - min(x)) / (max(x) - min(x))) }
habermannorm <- as.data.frame(lapply(habermansurvival[,1:3], normalize)) habermannorm$Survstatus <- as.numeric(habermansurvival$Survstatus) - 1 # Convert factor to numeric (0 and 1)
sum(is.na(haberman_norm))
habermannorm <- habermannorm[complete.cases(haberman_norm), ]
library(neuralnet)
set.seed(123) index <- sample(1:nrow(habermannorm), round(0.80 * nrow(habermannorm))) trainset <- habermannorm[index,] testset <- habermannorm[-index,]
nn <- neuralnet(Survstatus ~ Age + Opyear + Axil_nodes, data = trainset, hidden = 3, linear.output = FALSE, act.fct = "tanh", err.fct = "ce", likelihood = TRUE)
pdf("neuralnet_plot.pdf", width = 8, height = 6) plot(nn) dev.off()
png("neuralnet_plot.png", width = 800, height = 600) plot(nn) dev.off()
predicted <- compute(nn, testset[,1:3])$net.result predicted <- ifelse(predicted > 0.5, 1, 0)
predicted <- factor(predicted, levels = c(0, 1))
confusionmatrix <- table(predicted, testset$Survstatus) print(confusion_matrix)
accuracy <- sum(diag(confusionmatrix)) / sum(confusionmatrix) print(paste("Accuracy:", round(accuracy, 2)))
mse <- mean((as.numeric(predicted) - 1 - testset$Surv_status)^2) print(paste("MSE:",round(mse,2)))