Assignment 2

Please find all the script I have used for the wisconsin dataset.

# assignment2
setwd("C:/Users/olgad/OneDrive/Desktop/CETM72R")
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(readr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(forcats)
library(randomForest)
## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
## 
## Attaching package: 'randomForest'
## 
## The following object is masked from 'package:ggplot2':
## 
##     margin
## 
## The following object is masked from 'package:dplyr':
## 
##     combine
library(caret) #these libraries need to be installed first before running
## Loading required package: lattice
## 
## Attaching package: 'caret'
## 
## The following object is masked from 'package:purrr':
## 
##     lift
library (corrplot)
## corrplot 0.92 loaded
# Load the dataset
wisconsin <- read_csv("wisconsin.csv", show_col_types = FALSE)

glimpse(wisconsin)
## Rows: 699
## Columns: 10
## $ Cl.thickness    <dbl> 5, 5, 3, 6, 4, 8, 1, 2, 2, 4, 1, 2, 5, 1, 8, 7, 4, 4, …
## $ Cell.size       <dbl> 1, 4, 1, 8, 1, 10, 1, 1, 1, 2, 1, 1, 3, 1, 7, 4, 1, 1,…
## $ Cell.shape      <dbl> 1, 4, 1, 8, 1, 10, 1, 2, 1, 1, 1, 1, 3, 1, 5, 6, 1, 1,…
## $ Marg.adhesion   <dbl> 1, 5, 1, 1, 3, 8, 1, 1, 1, 1, 1, 1, 3, 1, 10, 4, 1, 1,…
## $ Epith.c.size    <dbl> 2, 7, 2, 3, 2, 7, 2, 2, 2, 2, 1, 2, 2, 2, 7, 6, 2, 2, …
## $ Bare.nuclei     <dbl> 1, 10, 2, 4, 1, 10, 10, 1, 1, 1, 1, 1, 3, 3, 9, 1, 1, …
## $ Bl.cromatin     <dbl> 3, 3, 3, 3, 3, 9, 3, 3, 1, 2, 3, 2, 4, 3, 5, 4, 2, 3, …
## $ Normal.nucleoli <dbl> 1, 2, 1, 7, 1, 7, 1, 1, 1, 1, 1, 1, 4, 1, 5, 3, 1, 1, …
## $ Mitoses         <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 5, 1, 1, 1, 1, 1, 4, 1, 1, 1, …
## $ Class           <chr> "benign", "benign", "benign", "benign", "benign", "mal…
wisconsin <- wisconsin %>%
  mutate(Bare.nuclei = replace_na(Bare.nuclei, median(Bare.nuclei, na.rm = TRUE)))

wisconsin <- wisconsin %>%
  mutate(Class = if_else(Class == "malignant", 1, 0))

summary(wisconsin)
##   Cl.thickness      Cell.size        Cell.shape     Marg.adhesion   
##  Min.   : 1.000   Min.   : 1.000   Min.   : 1.000   Min.   : 1.000  
##  1st Qu.: 2.000   1st Qu.: 1.000   1st Qu.: 1.000   1st Qu.: 1.000  
##  Median : 4.000   Median : 1.000   Median : 1.000   Median : 1.000  
##  Mean   : 4.418   Mean   : 3.134   Mean   : 3.207   Mean   : 2.807  
##  3rd Qu.: 6.000   3rd Qu.: 5.000   3rd Qu.: 5.000   3rd Qu.: 4.000  
##  Max.   :10.000   Max.   :10.000   Max.   :10.000   Max.   :10.000  
##   Epith.c.size     Bare.nuclei      Bl.cromatin     Normal.nucleoli 
##  Min.   : 1.000   Min.   : 1.000   Min.   : 1.000   Min.   : 1.000  
##  1st Qu.: 2.000   1st Qu.: 1.000   1st Qu.: 2.000   1st Qu.: 1.000  
##  Median : 2.000   Median : 1.000   Median : 3.000   Median : 1.000  
##  Mean   : 3.216   Mean   : 3.486   Mean   : 3.438   Mean   : 2.867  
##  3rd Qu.: 4.000   3rd Qu.: 5.000   3rd Qu.: 5.000   3rd Qu.: 4.000  
##  Max.   :10.000   Max.   :10.000   Max.   :10.000   Max.   :10.000  
##     Mitoses           Class       
##  Min.   : 1.000   Min.   :0.0000  
##  1st Qu.: 1.000   1st Qu.:0.0000  
##  Median : 1.000   Median :0.0000  
##  Mean   : 1.589   Mean   :0.3448  
##  3rd Qu.: 1.000   3rd Qu.:1.0000  
##  Max.   :10.000   Max.   :1.0000
# Splitting the data into training and testing sets for model validation
set.seed(123)  # Ensuring reproducibility
training_indices <- createDataPartition(wisconsin$Class, p = 0.8, list = FALSE)
train_data <- wisconsin[training_indices, ]
test_data <- wisconsin[-training_indices, ]

# Building and training the Logistic Regression model
log_model <- glm(Class ~ ., data = train_data, family = "binomial")

# Ensure 'Class' is a factor
train_data$Class <- as.factor(train_data$Class)

# Check for NAs
sum(is.na(train_data))
## [1] 0
# Developing and training the Random Forest model
library(randomForest)
rf_model <- randomForest(Class ~ ., data = train_data)

train_data$Class <- as.factor(train_data$Class)
test_data$Class <- as.factor(test_data$Class)
library(randomForest)
rf_model <- randomForest(Class ~ ., data = train_data)

# Defining a function to calculate accuracy of the models
calculate_accuracy <- function(predictions, true_values) {
  correct <- sum(predictions == true_values)
  total <- length(true_values)
  accuracy <- correct / total
  return(accuracy)
}

# Evaluating Logistic Regression model accuracy
log_predictions <- predict(log_model, test_data, type = "response") > 0.5
log_accuracy <- calculate_accuracy(log_predictions, test_data$Class)

# Evaluating Random Forest model accuracy
rf_predictions <- predict(rf_model, test_data)
rf_accuracy <- calculate_accuracy(rf_predictions, test_data$Class)

# Outputting the comparison of model accuracies
print(paste("Logistic Regression accuracy:", log_accuracy))
## [1] "Logistic Regression accuracy: 0"
print(paste("Random Forest accuracy:", rf_accuracy))
## [1] "Random Forest accuracy: 0.97841726618705"
correlation_matrix <- cor(wisconsin[,-10])  # Exclude the response variable
print(correlation_matrix)
##                 Cl.thickness Cell.size Cell.shape Marg.adhesion Epith.c.size
## Cl.thickness       1.0000000 0.6449125  0.6545891     0.4863562    0.5218162
## Cell.size          0.6449125 1.0000000  0.9068819     0.7055818    0.7517991
## Cell.shape         0.6545891 0.9068819  1.0000000     0.6830792    0.7196684
## Marg.adhesion      0.4863562 0.7055818  0.6830792     1.0000000    0.5995991
## Epith.c.size       0.5218162 0.7517991  0.7196684     0.5995991    1.0000000
## Bare.nuclei        0.5900082 0.6866728  0.7074738     0.6669710    0.5837014
## Bl.cromatin        0.5584282 0.7557210  0.7359485     0.6667153    0.6161018
## Normal.nucleoli    0.5358345 0.7228648  0.7194463     0.6033524    0.6288807
## Mitoses            0.3500339 0.4586931  0.4389109     0.4176328    0.4791015
##                 Bare.nuclei Bl.cromatin Normal.nucleoli   Mitoses
## Cl.thickness      0.5900082   0.5584282       0.5358345 0.3500339
## Cell.size         0.6866728   0.7557210       0.7228648 0.4586931
## Cell.shape        0.7074738   0.7359485       0.7194463 0.4389109
## Marg.adhesion     0.6669710   0.6667153       0.6033524 0.4176328
## Epith.c.size      0.5837014   0.6161018       0.6288807 0.4791015
## Bare.nuclei       1.0000000   0.6742147       0.5747781 0.3423971
## Bl.cromatin       0.6742147   1.0000000       0.6658778 0.3441695
## Normal.nucleoli   0.5747781   0.6658778       1.0000000 0.4283357
## Mitoses           0.3423971   0.3441695       0.4283357 1.0000000
summary(log_model)
## 
## Call:
## glm(formula = Class ~ ., family = "binomial", data = train_data)
## 
## Coefficients:
##                 Estimate Std. Error z value Pr(>|z|)    
## (Intercept)      -9.0139     1.1056  -8.153 3.54e-16 ***
## Cl.thickness      0.5063     0.1591   3.182  0.00146 ** 
## Cell.size         0.1714     0.2253   0.761  0.44680    
## Cell.shape        0.4846     0.2677   1.810  0.07030 .  
## Marg.adhesion     0.1650     0.1362   1.211  0.22578    
## Epith.c.size     -0.2064     0.2042  -1.011  0.31216    
## Bare.nuclei       0.4806     0.1114   4.315 1.60e-05 ***
## Bl.cromatin       0.3288     0.1815   1.812  0.07004 .  
## Normal.nucleoli   0.1424     0.1225   1.163  0.24497    
## Mitoses           0.4019     0.3139   1.280  0.20048    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 728.784  on 559  degrees of freedom
## Residual deviance:  89.265  on 550  degrees of freedom
## AIC: 109.26
## 
## Number of Fisher Scoring iterations: 8
accuracy_conf_int <- binom.test(sum(rf_predictions == test_data$Class), length(rf_predictions))
print(accuracy_conf_int$conf.int)
## [1] 0.9382228 0.9955268
## attr(,"conf.level")
## [1] 0.95
# Prepare the data
model_accuracies <- data.frame(
  Model = c("Logistic Regression", "Random Forest"),
  Accuracy = c(0, 0.971223021582734)
)

# Plot the accuracies
library(ggplot2)
accuracy_plot <- ggplot(model_accuracies, aes(x = Model, y = Accuracy, fill = Model)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = scales::percent(Accuracy)), vjust = -0.3) +
  labs(title = "Comparison of Model Accuracies", y = "Accuracy", x = "") +
  ylim(0, 1) +
  theme_minimal()

# Display the plot
print(accuracy_plot)

# Install if not already installed
if (!requireNamespace("corrplot", quietly = TRUE)) {
  install.packages("corrplot")
}

wisconsin_numeric <- wisconsin[, sapply(wisconsin, is.numeric)]

# Load necessary library
library(corrplot)

# Calculate correlation matrix
cor_matrix <- cor(wisconsin_numeric)

# Plot the heatmap
corrplot(cor_matrix, method = "color", type = "upper", order = "hclust",
         tl.col = "black", tl.srt = 45, 
         addCoef.col = "black", title = "Correlation Matrix Heatmap")