1 Introduction

A stroke occurs when something blocks blood supply to a part of the brain or when a blood vessel in the brain bursts. In either case, parts of the brain become damaged or die. A stroke can cause lasting brain damage, long-term disability, or even death [1]. According medical literature people with high blood pressure, high cholesterol, diabetes, obesity and tobacco users have higher risk of stroke. The following work will cluster people and examine the relationship between diabates, obesity and other factors on stroke.

2 Data description

The “full_data.csv” is a stroke prediction dataset from “McKinsey Analytics Online Hackathon - Healthcare Analytics” [2] that contains 4981 observations with 11 attributes. Each observation corresponds to one patient. The attributes are variables about the health status of each patient. 10 of attributes are input features including gender, hypertension (yes/no), heart disease (yes/no), marital status, work type (children, government job, private, self-employer), residence type (urban, rural), smoking status (formerly smoked, never smoked, smokes, unknown). The 11th attribute is the binary output attribute indicating a patient is suffered stroke or not.

head(x,10)
##    gender age hypertension heart_disease ever_married     work_type
## 1    Male  67            0             1          Yes       Private
## 2    Male  80            0             1          Yes       Private
## 3  Female  49            0             0          Yes       Private
## 4  Female  79            1             0          Yes Self-employed
## 5    Male  81            0             0          Yes       Private
## 6    Male  74            1             1          Yes       Private
## 7  Female  69            0             0           No       Private
## 8  Female  78            0             0          Yes       Private
## 9  Female  81            1             0          Yes       Private
## 10 Female  61            0             1          Yes      Govt_job
##    Residence_type avg_glucose_level  bmi  smoking_status stroke
## 1           Urban            228.69 36.6 formerly smoked      1
## 2           Rural            105.92 32.5    never smoked      1
## 3           Urban            171.23 34.4          smokes      1
## 4           Rural            174.12 24.0    never smoked      1
## 5           Urban            186.21 29.0 formerly smoked      1
## 6           Rural             70.09 27.4    never smoked      1
## 7           Urban             94.39 22.8    never smoked      1
## 8           Urban             58.57 24.2         Unknown      1
## 9           Rural             80.43 29.7    never smoked      1
## 10          Rural            120.46 36.8          smokes      1

3 Data preparation

3.1 Data cleaning

There were no rows in the database with missing values. Only all rows with unknown smoking status were deleted. After removing them, 3,481 observations remained.

x <- x[x$smoking_status != "Unknown",]
str(x)
## 'data.frame':    3481 obs. of  11 variables:
##  $ gender           : chr  "Male" "Male" "Female" "Female" ...
##  $ age              : num  67 80 49 79 81 74 69 81 61 54 ...
##  $ hypertension     : int  0 0 0 1 0 1 0 1 0 0 ...
##  $ heart_disease    : int  1 1 0 0 0 1 0 0 1 0 ...
##  $ ever_married     : chr  "Yes" "Yes" "Yes" "Yes" ...
##  $ work_type        : chr  "Private" "Private" "Private" "Self-employed" ...
##  $ Residence_type   : chr  "Urban" "Rural" "Urban" "Rural" ...
##  $ avg_glucose_level: num  229 106 171 174 186 ...
##  $ bmi              : num  36.6 32.5 34.4 24 29 27.4 22.8 29.7 36.8 27.3 ...
##  $ smoking_status   : chr  "formerly smoked" "never smoked" "smokes" "never smoked" ...
##  $ stroke           : int  1 1 1 1 1 1 1 1 1 1 ...

The dataset was also checked for outliers.

summary(x[c("age", "avg_glucose_level", "bmi")])
##       age        avg_glucose_level      bmi       
##  Min.   :10.00   Min.   : 55.12    Min.   :14.10  
##  1st Qu.:34.00   1st Qu.: 77.45    1st Qu.:25.30  
##  Median :50.00   Median : 92.49    Median :29.10  
##  Mean   :49.07   Mean   :108.77    Mean   :29.81  
##  3rd Qu.:64.00   3rd Qu.:116.25    3rd Qu.:33.50  
##  Max.   :82.00   Max.   :271.74    Max.   :48.90

All continuous values in the dataset are within the possible limits, indicating there are no outliers in the dataset.

3.2 Data Transformation

3.2.1 Converting to numeric values

To perform clustering, all non-numeric values were converted to numeric values.
gender: Female - 0, Male - 1
work_type: children - 0, Govt_job - 1, Private - 2, Self-employed - 3
residence_type: rural - 0, urban - 1
ever_married: no - 0, yes - 1
smoking_status: never smoked - 0, formerly smoked - 1, smokes - 2

x[c("gender", "work_type", "Residence_type", "ever_married")] <- lapply(x[c("gender", "work_type", "Residence_type", "ever_married")], factor)

x$smoking_status <- factor(x$smoking_status, levels = c("never smoked", "formerly smoked", "smokes"), ordered = TRUE)


x_numeric <- x

x_numeric[c("gender", "work_type", "Residence_type", "ever_married", "smoking_status")] <- lapply(x[c("gender", "work_type", "Residence_type", "ever_married", "smoking_status")], as.numeric)
x_numeric$gender <- ifelse(x_numeric$gender == 1, 0, 1)
x_numeric$ever_married <- ifelse(x_numeric$ever_married == 1, 0, 1)
x_numeric[c( "smoking_status","work_type","Residence_type")] <- x_numeric[c( "smoking_status","work_type","Residence_type")] - 1
str(x_numeric)
## 'data.frame':    3481 obs. of  11 variables:
##  $ gender           : num  1 1 0 0 1 1 0 0 0 0 ...
##  $ age              : num  67 80 49 79 81 74 69 81 61 54 ...
##  $ hypertension     : int  0 0 0 1 0 1 0 1 0 0 ...
##  $ heart_disease    : int  1 1 0 0 0 1 0 0 1 0 ...
##  $ ever_married     : num  1 1 1 1 1 1 0 1 1 1 ...
##  $ work_type        : num  2 2 2 3 2 2 2 2 1 2 ...
##  $ Residence_type   : num  1 0 1 0 1 0 1 0 0 1 ...
##  $ avg_glucose_level: num  229 106 171 174 186 ...
##  $ bmi              : num  36.6 32.5 34.4 24 29 27.4 22.8 29.7 36.8 27.3 ...
##  $ smoking_status   : num  1 0 2 0 1 0 0 0 2 2 ...
##  $ stroke           : int  1 1 1 1 1 1 1 1 1 1 ...

3.2.2 Discretization

To make the results easier to understand, continuous values such as average glucose level, bmi and have been discretized. Average glucose level has been discretized into four medical levels: hypoglycemia, normoglycemia, prediabetes, and diabetes [3]. Similarly, the BMI attribute has been discretized into four levels: underweight, healthy weight, overweight, and obesity [4]. The dataset contains patients of age ranging from 10 to 82 years old. The age attribute has been discretized into 8 different levels, each every 10 years.

x_numeric$avg_glucose_level <- cut(x_numeric$avg_glucose_level, breaks = c(-Inf, 70, 100, 125, Inf), labels = c("hypoglycemia", "normoglycemia", "prediabetes", "diabetes"))
x_numeric$bmi <- cut(x_numeric$bmi, breaks = c(-Inf, 18.5, 25, 30, Inf), labels = c("underweight", "healthy weight", "overweight", "obesity"))
x_numeric$age <- cut(x_numeric$age, breaks = c(-Inf, 20, 30, 40, 50, 60, 70, 80, Inf))

x_numeric[c("avg_glucose_level", "bmi", "age")] <- lapply(x_numeric[c("avg_glucose_level", "bmi", "age" )], as.numeric)
x_numeric[c("avg_glucose_level", "bmi", "age")] <- x_numeric[c("avg_glucose_level", "bmi", "age")] - 1

str(x_numeric)
## 'data.frame':    3481 obs. of  11 variables:
##  $ gender           : num  1 1 0 0 1 1 0 0 0 0 ...
##  $ age              : num  5 6 3 6 7 6 5 7 5 4 ...
##  $ hypertension     : int  0 0 0 1 0 1 0 1 0 0 ...
##  $ heart_disease    : int  1 1 0 0 0 1 0 0 1 0 ...
##  $ ever_married     : num  1 1 1 1 1 1 0 1 1 1 ...
##  $ work_type        : num  2 2 2 3 2 2 2 2 1 2 ...
##  $ Residence_type   : num  1 0 1 0 1 0 1 0 0 1 ...
##  $ avg_glucose_level: num  3 2 3 3 3 1 1 1 2 2 ...
##  $ bmi              : num  3 3 3 1 2 2 1 2 3 2 ...
##  $ smoking_status   : num  1 0 2 0 1 0 0 0 2 2 ...
##  $ stroke           : int  1 1 1 1 1 1 1 1 1 1 ...

3.3 Data reduction

3.3.1 Correlation checking

correletion <- cor(x_numeric, method="pearson")
corrplot(correletion)

We observe that the majority of variables are not strongly correlated. The only significant correlations are found between age and variables such as ever_married, hypertension, and heart_disease. This outcome is expected, as with advancing age, individuals are more likely to have been ever married or to experience hypertension or heart disease. Based on the above diagram, we can also observe that age is the primary factor correlated with stroke. Additionally, there is a weak correlation between hypertension, heart disease, and blood glucose levels. Also noteworthy is the weak correlation between stroke and marital status.

3.3.2 Dimension reduction

In the first part of this work, the focus is on clustering individuals who have already experienced a stroke to identify any correlated groups. To achieve this, a subset will be created containing observations with a history of stroke. There are only 201 observations in this set. Due to the small number of observations, there will be no subdivision based on age or gender. However, it’s important to note that the age and gender of the observations will not be taken into account to investigate the influence of other factors on stroke. The remaining variables considered for clustering are presented below in normalized form.

x_numeric_stroke = x_numeric[x_numeric$stroke == 1 & x_numeric$smoking_status != 4,]
x_numeric_stroke_full = x_numeric_stroke

x_numeric_stroke$stroke <- NULL
x_numeric_stroke$age <- NULL
x_numeric_stroke$gender <- NULL
x_stroke_norm <- as.data.frame(lapply(x_numeric_stroke, scale))
summary(x_stroke_norm)
##   hypertension     heart_disease      ever_married       work_type      
##  Min.   :-0.6662   Min.   :-0.4894   Min.   :-2.9204   Min.   :-1.8694  
##  1st Qu.:-0.6662   1st Qu.:-0.4894   1st Qu.: 0.3407   1st Qu.:-0.2567  
##  Median :-0.6662   Median :-0.4894   Median : 0.3407   Median :-0.2567  
##  Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000  
##  3rd Qu.: 1.4936   3rd Qu.:-0.4894   3rd Qu.: 0.3407   3rd Qu.: 1.3559  
##  Max.   : 1.4936   Max.   : 2.0330   Max.   : 0.3407   Max.   : 1.3559  
##  Residence_type    avg_glucose_level      bmi          smoking_status   
##  Min.   :-1.0537   Min.   :-1.7200   Min.   :-3.0951   Min.   :-0.9891  
##  1st Qu.:-1.0537   1st Qu.:-0.7931   1st Qu.:-0.3784   1st Qu.:-0.9891  
##  Median : 0.9443   Median : 0.1337   Median :-0.3784   Median : 0.3019  
##  Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000  
##  3rd Qu.: 0.9443   3rd Qu.: 1.0606   3rd Qu.: 0.9799   3rd Qu.: 0.3019  
##  Max.   : 0.9443   Max.   : 1.0606   Max.   : 0.9799   Max.   : 1.5928

4 Clustering people after a stroke

4.1 Test for clusterability of data

To assess how well the data can be clustered, the Hopkins statistic will be utilized. For the previously prepared dataset, the Hopkins statistic is

hopkins(x_stroke_norm)
## [1] 0.9582056

We observe value near 1. We can reject the null hypothesis that it is unlikely that there are statistically significant clusters.

4.2 Determining the optimal number of clusters

4.2.1 Average silhouette width

opt1<-NbClust(x_stroke_norm, distance="euclidean", min.nc=2, max.nc=10, method="complete", index="ch")
opt1$All.index
##       2       3       4       5       6       7       8       9      10 
## 31.3591 29.2761 21.2707 21.1014 18.5800 18.7017 18.4624 17.8919 18.5164
opt1$Best.nc
## Number_clusters     Value_Index 
##          2.0000         31.3591
fviz_nbclust(x_stroke_norm, FUN = hcut, method = "silhouette")

Based on the chart above we can conclude that the optimal number of clusters is 2.

4.2.2 Elbow

opt<-Optimal_Clusters_KMeans(x_stroke_norm, max_clusters=10, plot_clusters = TRUE)

In the graph above there is not a siginificant inflection point. We can estimate that it is around 2 or 3 clusters.

4.2.3 Other statistics

gap_stat <- clusGap(x_stroke_norm, FUN = hcut, nstart = 25, K.max = 15, B = 50)
fviz_gap_stat(gap_stat)

opt3<-Optimal_Clusters_KMeans(x_stroke_norm, max_clusters=15, plot_clusters=TRUE, criterion="silhouette")

The two methods presented above indicate 13 and 8 clusters, respectively. In this case, the number of clusters appears to be significantly too high. For the purpose of analysis, 2 clusters will be considered.

4.3 Clustering algorithm

The hclust algorithm was chosen for clustering because it provided the highest value for the average silhouette width and score of explanation of variance for the first two dimensions. Other clustering algorithms, including kmeans, pam, clara, diana, and dbscan with different values of epsilon and minimum number of points, were also tested.

cluster_kmeans<-eclust(x_stroke_norm, "hclust", k= 2)
fviz_cluster(cluster_kmeans)

We observe that the first two dimensions explain only 34,8% of variance. While this result may not be high, it aligns with the expectations derived from the earlier findings in determining the optimal number of clusters.

4.4 Checking the qualit of partitioning

fviz_silhouette(cluster_kmeans)
##   cluster size ave.sil.width
## 1       1  180          0.29
## 2       2   21          0.24

The groups are not equal. The first cluster contains 180 observations, the second 21. The average silhoutte index in both clusters is 0.28.

4.5 Checking statistics within the groups

cluster_assignments <- cluster_kmeans$cluster
x_cluster <- x_numeric_stroke_full
x_cluster$Cluster <- cluster_assignments
ag1 <- aggregate(data = x_cluster, cbind(age,gender,Residence_type,avg_glucose_level,bmi,smoking_status,hypertension,heart_disease,work_type,ever_married,stroke) ~ Cluster, mean)
avg_all <- melt(lapply(x_numeric, mean))
melted_data <- melt(ag1, id.vars = c("Cluster"))
ag1
##   Cluster      age    gender Residence_type avg_glucose_level      bmi
## 1       1 5.222222 0.4444444      0.5055556          1.905556 2.294444
## 2       2 5.238095 0.2857143      0.7142857          1.428571 2.142857
##   smoking_status hypertension heart_disease work_type ever_married stroke
## 1      0.8055556    0.2833333     0.1888889  2.166667            1      1
## 2      0.4285714    0.5238095     0.2380952  2.095238            0      1

4.5.1 Gender and age

 gender_age_plots_list <- list()
 
 variable = "gender"
 subset_data <- melted_data[melted_data$variable == variable, ]
 gender_age_plots_list[[length(gender_age_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Percentage of males")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,1), labels = scales::percent)+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
 variable = "age"
 subset_data <- melted_data[melted_data$variable == variable, ]
 gender_age_plots_list[[length(gender_age_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Average of categorized age")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,6), n.breaks = 7,  labels = c("below 20", "20-30", "30-40", "40-50","50-60","70-80","above 80" ))+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
 grid.arrange(grobs = gender_age_plots_list, ncol=2)

4.5.2 Medical data

 medical_plots_list <- list()
 
 variable = "avg_glucose_level"
 subset_data <- melted_data[melted_data$variable == variable, ]
 medical_plots_list[[length(medical_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Average of categorized glucose level")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,3), labels = c("hypoglycemia", "normoglycemia", "prediabetes", "diabetes"))+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
  variable = "hypertension"
 subset_data <- melted_data[melted_data$variable == variable, ]
 medical_plots_list[[length(medical_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Percentage of people with hypertension")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,1),labels = scales::percent)+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
 variable = "bmi"
 subset_data <- melted_data[melted_data$variable == variable, ]
 medical_plots_list[[length(medical_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Average of categorized BMI")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,3), labels = c("underweight", "healthy weight", "overweight", "obesity"))+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
 variable = "heart_disease"
 subset_data <- melted_data[melted_data$variable == variable, ]
 medical_plots_list[[length(medical_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Percentage of people with heart diseases")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,1),labels = scales::percent)+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
 grid.arrange(grobs = medical_plots_list, ncol=2)

4.5.3 Social data

 social_plots_list <- list()
 
variable = "smoking_status"
 subset_data <- melted_data[melted_data$variable == variable, ]
 social_plots_list[[length(social_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Average of categorized smoking status")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,2),n.breaks = 3, labels = c("never smoked", "formerly smoked", "smokes"))+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
 variable = "ever_married"
 subset_data <- melted_data[melted_data$variable == variable, ]
 social_plots_list[[length(social_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Percentage of people ever married")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,1),labels = scales::percent)+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
 variable = "work_type"
 subset_data <- melted_data[melted_data$variable == variable, ]
 social_plots_list[[length(social_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Average of categorized work types")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,3), labels = c("children", "Govt_job", "Private", "Self-employed"))+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
 variable = "Residence_type"
 subset_data <- melted_data[melted_data$variable == variable, ]
 social_plots_list[[length(social_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Percentage of people living in urban")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,1),labels = scales::percent)+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)

 grid.arrange(grobs = social_plots_list, ncol=2)

From a medical perspective, we observe that in the first cluster, the average glucose level is higher, close to the prediabetic range, significantly surpassing the average level of all patients. Additionally, the average BMI is higher, suggesting that a majority of individuals in this cluster are overweight. In the second cluster, both the average glucose level and BMI are lower than the study population average, but a substantial number of individuals exhibit comorbidities. Half of them have hypertension, and a quarter have heart diseases. We can distinguish two groups of people at risk of stroke: one characterized by high glucose levels and high BMI, and the other by comorbidities.

In the analysis of social data, it is apparent that the first group of people represent a less healthy lifestyle. They smoke more frequently than the average population. Additionally, they tend to live in rural areas and are more often employed in the private sector or are self-employed which may be equated with potentially more stressful jobs. The second cluster consists of individuals who smoke less frequently than the average and are more likely to reside in urban areas.The factor of being married was omitted in the considerations.

Considering age, the averages are comparable in both clusters, but it is noteworthy that the first cluster has a significantly higher percentage of males than the second cluster.

In summary, we can distinguish two groups at risk of stroke. The larger group exhibits an unhealthy lifestyle characterized by high glucose level, elevated BMI, and frequent smoking. The smaller group comprises individuals with comorbidities.

5 Clustering all people

5.1 Data preparation

x_numeric_all = x_numeric[x_numeric$smoking_status != 4,]
x_numeric_all_full = x_numeric_all
x_numeric_all$stroke <- NULL
x_numeric_all$age <- NULL
x_numeric_all$gender <- NULL
x_all_norm <- as.data.frame(lapply(x_numeric_all, scale))
summary(x_all_norm)
##   hypertension     heart_disease      ever_married       work_type        
##  Min.   :-0.3749   Min.   :-0.2641   Min.   :-1.7940   Min.   :-3.092462  
##  1st Qu.:-0.3749   1st Qu.:-0.2641   1st Qu.: 0.5573   1st Qu.: 0.004894  
##  Median :-0.3749   Median :-0.2641   Median : 0.5573   Median : 0.004894  
##  Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.000000  
##  3rd Qu.:-0.3749   3rd Qu.:-0.2641   3rd Qu.: 0.5573   3rd Qu.: 0.004894  
##  Max.   : 2.6669   Max.   : 3.7856   Max.   : 0.5573   Max.   : 1.553572  
##  Residence_type    avg_glucose_level      bmi          smoking_status   
##  Min.   :-1.0193   Min.   :-1.4925   Min.   :-2.6596   Min.   :-0.8567  
##  1st Qu.:-1.0193   1st Qu.:-0.4758   1st Qu.:-0.2261   1st Qu.:-0.8567  
##  Median : 0.9808   Median :-0.4758   Median :-0.2261   Median :-0.8567  
##  Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000  
##  3rd Qu.: 0.9808   3rd Qu.: 0.5409   3rd Qu.: 0.9906   3rd Qu.: 0.3761  
##  Max.   : 0.9808   Max.   : 1.5576   Max.   : 0.9906   Max.   : 1.6088

The dataset has been normalized, and the same parameters as in the previous case will be applied for clustering. Also the age and gender of the observation will not be considered to investigate the influence of other factors on stroke.

5.2 Test for clusterability of data

To assess how well the data can be clustered, the Hopkins statistic will be utilized. The Hopkins statistic for whole dataset is

hopkins(x_all_norm)
## [1] 0.982337

We can observe value near 1. We can reject the null hypothesis that it is unlikely that there are statistically significant clusters.

5.3 Determining the optimal number of clusters

5.3.1 Average silhouette width

opt1<-NbClust(x_all_norm, distance="euclidean", min.nc=2, max.nc=10, method="complete", index="ch")
opt1$All.index
##        2        3        4        5        6        7        8        9 
## 520.6242 368.4745 468.8693 416.1949 355.6529 377.3662 332.8145 292.5662 
##       10 
## 304.2985
opt1$Best.nc
## Number_clusters     Value_Index 
##          2.0000        520.6242
fviz_nbclust(x_all_norm, FUN = hcut, method = "silhouette")

Based on the chart above, it can be concluded that the optimal number of clusters is 4.

5.3.2 Elbow

opt<-Optimal_Clusters_KMeans(x_all_norm, max_clusters=10, plot_clusters = TRUE)

We estimate the inflection point to be around 4 clusters. Based on the average silhouette width and the elbow chart, 4 clusters were selected.

5.4 Clustering algorithm

As in the previous clustering, the hclust algorithm was used because it provided the best results among all the algorithms tested.

cluster_hclust<-eclust(x_all_norm, "hclust", k= 4)
fviz_cluster(cluster_hclust)

We observe that the first two dimensions explain only 32.3% of the variance. This result is lower than in the previous clustering, but it is expected since we are clustering a larger group of people using the same factors.

5.5 Checking the qualit of partitioning

fviz_silhouette(cluster_hclust)
##   cluster size ave.sil.width
## 1       1  227          0.29
## 2       2 2120          0.25
## 3       3  370          0.28
## 4       4  764          0.22

We can see that we got groups of different sizes. The largest of them has 2,120 people, and the smallest only 227. This was the expected result, because the considered set is not balanced. People have different health statuses and are not equally divided into healthy and sick one.

5.6 Checking statistics within the groups

cluster_assignments_all <- cluster_hclust$cluster
x_cluster_all <- x_numeric_all_full
x_cluster_all$Cluster <- cluster_assignments_all

ag1_all <- aggregate(data = x_cluster_all, cbind(age,gender,Residence_type,avg_glucose_level,bmi,smoking_status,hypertension,heart_disease,work_type,ever_married,stroke) ~ Cluster, mean)

melted_data_all <- melt(ag1_all, id.vars = c("Cluster"))

5.6.1 Stroke

 melted_data_all_stroke <- melt(ag1_all[,c("Cluster","stroke" )], id.vars = c("Cluster"))
  
 ggplot(melted_data_all_stroke, aes(fill = as.factor(Cluster), y = value, x = "stroke")) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Percentage of people after a stroke")+
   xlab("Clusters")+
   scale_y_continuous(labels = scales::percent)+
   geom_hline(data = avg_all[avg_all$L1 == "stroke",], aes(yintercept = value), linetype = "dashed", size = 1)

5.6.2 Gender and age

 gender_age_plots_list <- list()
 
 variable = "gender"
 subset_data <- melted_data_all[melted_data_all$variable == variable, ]
 gender_age_plots_list[[length(gender_age_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Percentage of males")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,1), labels = scales::percent)+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
 variable = "age"
 subset_data <- melted_data_all[melted_data_all$variable == variable, ]
 gender_age_plots_list[[length(gender_age_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Average of categorized age")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,6), n.breaks = 7,  labels = c("below 20", "20-30", "30-40", "40-50","50-60","70-80","above 80" ))+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
 grid.arrange(grobs = gender_age_plots_list, ncol=2)

5.6.3 Medical data

 medical_plots_list <- list()
 
 variable = "avg_glucose_level"
 subset_data <- melted_data_all[melted_data_all$variable == variable, ]
 medical_plots_list[[length(medical_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Average of categorized glucose level")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,3), labels = c("hypoglycemia", "normoglycemia", "prediabetes", "diabetes"))+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
  variable = "hypertension"
 subset_data <- melted_data_all[melted_data_all$variable == variable, ]
 medical_plots_list[[length(medical_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Percentage of people with hypertension")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,1),labels = scales::percent)+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
 variable = "bmi"
 subset_data <- melted_data_all[melted_data_all$variable == variable, ]
 medical_plots_list[[length(medical_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Average of categorized BMI")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,3), labels = c("underweight", "healthy weight", "overweight", "obesity"))+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
 variable = "heart_disease"
 subset_data <- melted_data_all[melted_data_all$variable == variable, ]
 medical_plots_list[[length(medical_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Percentage of people with heart diseases")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,1),labels = scales::percent)+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
 grid.arrange(grobs = medical_plots_list, ncol=2)

5.6.4 Social data

 social_plots_list <- list()
 
variable = "smoking_status"
 subset_data <- melted_data_all[melted_data_all$variable == variable, ]
 social_plots_list[[length(social_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Average of categorized smoking status")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,2),n.breaks = 3, labels = c("never smoked", "formerly smoked", "smokes"))+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
 variable = "ever_married"
 subset_data <- melted_data_all[melted_data_all$variable == variable, ]
 social_plots_list[[length(social_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Percentage of people ever married")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,1),labels = scales::percent)+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
 variable = "work_type"
 subset_data <- melted_data_all[melted_data_all$variable == variable, ]
 social_plots_list[[length(social_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Average of categorized work types")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,3), labels = c("children", "Govt_job", "Private", "Self-employed"))+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)
 
 variable = "Residence_type"
 subset_data <- melted_data_all[melted_data_all$variable == variable, ]
 social_plots_list[[length(social_plots_list) + 1]] <- ggplot(subset_data, aes(fill = as.factor(Cluster), y = value, x = variable)) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x =element_blank(), y = element_blank(), fill = "Cluster") +
   theme(plot.title = element_text(hjust = 0.5,size = 14), 
         axis.ticks.x=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y = element_text( size = 12),
         legend.title = element_text( size = 12)) +
   ggtitle("Percentage of people living in urban")+
   xlab("Clusters")+
   scale_y_continuous(limits = c(0,1),labels = scales::percent)+
   geom_hline(data = avg_all[avg_all$L1 == variable,], aes(yintercept = value), linetype = "dashed", size = 1)

 grid.arrange(grobs = social_plots_list, ncol=2)

The first thing noteworthy is that the significantly higher percentage of people with history of stroke in clusters one and three. In the first cluster, it was three times higher than the average, and in the third cluster, it was twice as much. Individuals in these two clusters will be considered at a higher risk of stroke.

First of all, let’s look at the parameters that were taken into account in clustering. In terms of medical data, there is a greater share of people with high glucose level and high BMI in cluster one and three. Cluster four exhibits the lowest levels of glucose and BMI, closest to normal. People with comorbidities were included only in clusters one and three.

According to social data we can see that first cluster has higher share of smokers than average. In first and third cluster people more often represent self-employed type of work what can be identified as more stressful jobs. Living area doesn’t show any strong dependencies. As well value ever married. Only in fourth cluster there are no people have ever been married, a detail that this becomes evident when examining the average age in this cluster.

Considering factors not used in clustering, there is a higher percentage of males in clusters associated with a higher risk of stroke. Additionally, these clusters have a considerably higher average age, while cluster fourth is represented by younger individuals.

In conclusion, the charts suggest that individuals with high glucose and BMI levels, comorbidities, and more stressful jobs are more likely to have a stroke.

6 Conclusions

Based on the above study, several key factors were identified in groups where the rate of stroke was higher:
- High average glucose level
- High BMI
- Presence of comorbidities such as heart attack and hypertension
- Involvement in more stressful jobs

These factors align with existing literature, as indicated by a WHO article stating, “People with high blood pressure, high cholesterol, heart disease, diabetes, or a high blood-sugar level are vulnerable to brain stroke” [5].

Upon closer examination of individuals who had a stroke, two distinct groups emerged. The larger group exhibited an unhealthy lifestyle characterized by high glucose levels, elevated BMI, and frequent smoking. The smaller group comprised individuals with comorbidities.