Reading the Data Set

diabetes_data= read.csv("~/Documents/R Program/diabetes.csv")
top5_data <- head(diabetes_data, 5)
top5_data
##   preg plas pres skin insu mass  pedi age         outcome
## 1    6  148   72   35    0 33.6 0.627  50 tested_positive
## 2    1   85   66   29    0 26.6 0.351  31 tested_negative
## 3    8  183   64    0    0 23.3 0.672  32 tested_positive
## 4    1   89   66   23   94 28.1 0.167  21 tested_negative
## 5    0  137   40   35  168 43.1 2.288  33 tested_positive

Summarizing the dataset

summaryDiabetesData<-summary(diabetes_data)
summaryDiabetesData
##       preg             plas            pres            skin      
##  Min.   : 0.000   Min.   :  0.0   Min.   :  0.0   Min.   : 0.00  
##  1st Qu.: 1.000   1st Qu.: 99.0   1st Qu.: 63.0   1st Qu.: 0.00  
##  Median : 3.000   Median :117.0   Median : 72.0   Median :23.00  
##  Mean   : 3.855   Mean   :121.1   Mean   : 69.2   Mean   :20.59  
##  3rd Qu.: 6.000   3rd Qu.:140.5   3rd Qu.: 80.0   3rd Qu.:32.00  
##  Max.   :17.000   Max.   :199.0   Max.   :122.0   Max.   :99.00  
##  NA's   :2        NA's   :1       NA's   :1       NA's   :2      
##       insu             mass            pedi             age       
##  Min.   :  0.00   Min.   : 0.00   Min.   :0.0780   Min.   :21.00  
##  1st Qu.:  0.00   1st Qu.:27.30   1st Qu.:0.2437   1st Qu.:24.00  
##  Median : 40.00   Median :32.00   Median :0.3725   Median :29.00  
##  Mean   : 81.39   Mean   :31.99   Mean   :0.4719   Mean   :33.24  
##  3rd Qu.:130.00   3rd Qu.:36.60   3rd Qu.:0.6262   3rd Qu.:41.00  
##  Max.   :846.00   Max.   :67.10   Max.   :2.4200   Max.   :81.00  
##  NA's   :15                                                       
##    outcome         
##  Length:768        
##  Class :character  
##  Mode  :character  
##                    
##                    
##                    
## 

Showcasing the structure and dimension of the dataset

str(diabetes_data)
## 'data.frame':    768 obs. of  9 variables:
##  $ preg   : int  6 1 8 1 0 5 3 10 2 8 ...
##  $ plas   : int  148 85 183 89 137 116 78 115 197 125 ...
##  $ pres   : int  72 66 64 66 40 74 50 0 70 96 ...
##  $ skin   : int  35 29 0 23 35 0 32 0 45 0 ...
##  $ insu   : int  0 0 0 94 168 0 88 0 543 0 ...
##  $ mass   : num  33.6 26.6 23.3 28.1 43.1 25.6 31 35.3 30.5 0 ...
##  $ pedi   : num  0.627 0.351 0.672 0.167 2.288 ...
##  $ age    : int  50 31 32 21 33 30 26 29 53 54 ...
##  $ outcome: chr  "tested_positive" "tested_negative" "tested_positive" "tested_negative" ...
dim(diabetes_data)
## [1] 768   9

Showing the class variable in diabetes dataset?

class(diabetes_data)
## [1] "data.frame"

Changing the class type of the class variable of diabetes dataset to factor.

Showing the output after the conversion.

diabetes_data$outcome <- as.factor(diabetes_data$outcome) 
print("Outcome column after conversion:")
## [1] "Outcome column after conversion:"
str(diabetes_data$outcome)
##  Factor w/ 2 levels "tested_negative",..: 2 1 2 1 2 1 2 1 2 2 ...
colSums(is.na(diabetes_data))
##    preg    plas    pres    skin    insu    mass    pedi     age outcome 
##       2       1       1       2      15       0       0       0       0

Replace the missing values in diabetes by 0.

diabetes_data[(is.na(diabetes_data))]<-0
print("After replacing missing values with 0:")
## [1] "After replacing missing values with 0:"
colSums(is.na(diabetes_data))
##    preg    plas    pres    skin    insu    mass    pedi     age outcome 
##       0       0       0       0       0       0       0       0       0

Preparing data for plotting by removing outcome column

plotDiabetes_data=select(diabetes_data,c(1,2,3,4,5,6,7,8))
names(diabetes_data)
## [1] "preg"    "plas"    "pres"    "skin"    "insu"    "mass"    "pedi"   
## [8] "age"     "outcome"

Perform k-means clustering

kmeans.diabetesResult2 <- kmeans(plotDiabetes_data, 2)

Plotting the data

plot(plotDiabetes_data[c("plas", "age")], col = kmeans.diabetesResult2$cluster)

kmeans.diabetesResult3 <- kmeans(plotDiabetes_data, 3)
# Define custom colors for clusters
cluster_colors <- c("red", "green", "orange")
#plotting the data
plot(plotDiabetes_data[c("preg", "insu")], col = cluster_colors[kmeans.diabetesResult3$cluster],
     main = "K-Means Clustering of Diabetes Data",
     xlab = "Blood Pressure (pres)",
     ylab = "Insulin (insu)")
legend("topright", legend = unique(kmeans.diabetesResult3$cluster), col = cluster_colors, pch = 1)

#Plotting

kmeans.diabetesResult4 <- kmeans(plotDiabetes_data, 4)
# Define custom colors for clusters
cluster_colors <- c("blue","violet", "cyan", "green")
#plotting the data
plot(plotDiabetes_data[c("pres", "insu")], col = cluster_colors[kmeans.diabetesResult4$cluster],
     main = "Scatter plot of Diabetes Data",
     xlab = "Blood Pressure (pres)",
     ylab = "Insulin (insu)")

plot(plotDiabetes_data, col = kmeans.diabetesResult3$cluster)