library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(neuralnet)
##
## Attaching package: 'neuralnet'
## The following object is masked from 'package:dplyr':
##
## compute
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(caret)
## Loading required package: lattice
#loading dataset
haberman_survival<-read.csv("C:\\Users\\sanan\\Downloads\\haberman.data",header=FALSE,col.names=c("Age","Op_year","Axil_nodes","Surv_status"))
View(haberman_survival)
#convert to numeric
haberman_survival<-haberman_survival%>%
mutate(Age=as.numeric(Age),
Op_year=as.numeric(Op_year),
Axil_nodes=as.numeric(Axil_nodes),
Surv_status=as.factor(Surv_status))
# Check for NA values and remove rows with NA values
sum(is.na(haberman_survival))
## [1] 0
haberman_survival <- haberman_survival[complete.cases(haberman_survival), ]
View(haberman_survival)
summary(haberman_survival)
## Age Op_year Axil_nodes Surv_status
## Min. :30.00 Min. :58.00 Min. : 0.000 1:225
## 1st Qu.:44.00 1st Qu.:60.00 1st Qu.: 0.000 2: 81
## Median :52.00 Median :63.00 Median : 1.000
## Mean :52.46 Mean :62.85 Mean : 4.026
## 3rd Qu.:60.75 3rd Qu.:65.75 3rd Qu.: 4.000
## Max. :83.00 Max. :69.00 Max. :52.000
# Distribution of Age (continuous)
ggplot(haberman_survival, aes(x = Age)) +
geom_histogram(binwidth = 5, fill = "green", color = "red") +
labs(title = "Age Dist", x = "Age", y = "Frequency")

# Distribution of Axillary Nodes (continuous)
ggplot(haberman_survival, aes(x = Axil_nodes)) +
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(haberman_survival, aes(x = as.factor(Op_year))) +
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(haberman_survival, aes(x = Surv_status)) +
geom_bar(fill = "pink", color = "brown") +
labs(title = "Survival Status Dist", x = "Survival Status", y = "Frequency")

# Scatter plots
pairs(
haberman_survival[,1:3],
col = ifelse(haberman_survival$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
haberman_norm <- as.data.frame(lapply(haberman_survival[,1:3], normalize))
haberman_norm$Surv_status <- as.numeric(haberman_survival$Surv_status) - 1 # Convert factor to numeric (0 and 1)
# Check for NA values after normalization
sum(is.na(haberman_norm))
## [1] 0
# Handle NA values by removing rows with NA
haberman_norm <- haberman_norm[complete.cases(haberman_norm), ]
# Load the neural network library
library(neuralnet)
# Split the data
set.seed(123)
index <- sample(1:nrow(haberman_norm), round(0.80 * nrow(haberman_norm)))
trainset <- haberman_norm[index,]
testset <- haberman_norm[-index,]
# Train the neural network
nn <- neuralnet(Surv_status ~ Age + Op_year + Axil_nodes,
data = trainset, hidden = 3,
linear.output = FALSE,
act.fct = "tanh",
err.fct = "ce",
likelihood = TRUE)
## Warning in log(x): NaNs produced
## Warning: 'err.fct' does not fit 'data' or 'act.fct'
# Save the neural network plot as PDF
pdf("neuralnet_plot.pdf", width = 8, height = 6)
plot(nn)
dev.off()
## png
## 2
# Save the neural network plot as PNG
png("neuralnet_plot.png", width = 800, height = 600)
plot(nn)
dev.off()
## png
## 2
# 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
confusion_matrix <- table(predicted, testset$Surv_status)
print(confusion_matrix)
##
## predicted 0 1
## 0 43 18
## 1 0 0
# Calculate accuracy
accuracy <- sum(diag(confusion_matrix)) / sum(confusion_matrix)
print(paste("Accuracy:", round(accuracy, 2)))
## [1] "Accuracy: 0.7"
# Calculate MSE
mse <- mean((as.numeric(predicted) - 1 - testset$Surv_status)^2)
print(paste("MSE:",round(mse,2)))
## [1] "MSE: 0.3"