Ian Gošnak

Importing the data and creating a 500 unit sample

set.seed(3)
mydata <- read.table("./insurance.csv", header=TRUE, sep=",", dec=".") %>% 
  sample_n(500)

head(mydata)
##   age    sex   bmi children smoker    region   charges
## 1  44 female 36.48        0     no northeast 12797.210
## 2  53 female 39.60        1     no southeast 10579.711
## 3  33 female 36.29        3     no northeast  6551.750
## 4  54 female 46.70        2     no southwest 11538.421
## 5  41   male 35.75        1    yes southeast 40273.645
## 6  44   male 21.85        3     no northeast  8891.139

A unit of observation in this dataset is the primary beneficiary (person) of health insurance

The sample size in this data set is equal to 500 units of observation

Definition of variables:

  • age: Age of primary beneficiary (in years)
  • sex: Insurance contractor gender, can be female or male
  • bmi: Body mass index, (kg / m ^ 2) using the ratio of height to weight
  • children: Number of children covered by health insurance / Number of dependents
  • smoker: Is the beneficiary a smoker or not
  • region: The beneficiary’s residential area in the US, northeast, southeast, southwest, northwest.
  • charges: Individual medical costs billed by health insurance (in USD)

The data was taken from the website Kaggle.com, more specifically from the link https://www.kaggle.com/datasets/mirichoi0218/insurance?resource=download

The research question for this analysis is to find out how we can separate the observations into different clusters based on the variables age, bmi, children and charges. (classification of 500 primary beneficiaries (person) of health insurance based on age, bmi, n of children and medical charges)

Cleaning of data (creating factors)

mydata$sexF <- factor(mydata$sex,
                          labels = c("male", "female"),
                          levels = c("male", "female")) # Adding a factor variable for "sex"

mydata$smokerF <- factor(mydata$smoker,
                             labels = c("smoker", "non-smoker"),
                             levels = c("yes", "no")) # Adding a factor variable for "smoker"

mydata$regionF <- factor(mydata$region,
                             labels = c("northeast", "southeast", "southwest", "northwest"),
                             levels = c("northeast", "southeast", "southwest", "northwest")) # Adding a factor for the variable "region"
# Descriptive statistics of the variables used in clustering
summary(mydata[c("age", "bmi", "children", "charges")])
##       age             bmi           children        charges     
##  Min.   :18.00   Min.   :17.29   Min.   :0.000   Min.   : 1137  
##  1st Qu.:26.00   1st Qu.:26.11   1st Qu.:0.000   1st Qu.: 4529  
##  Median :39.00   Median :30.28   Median :1.000   Median : 8863  
##  Mean   :38.96   Mean   :30.83   Mean   :1.084   Mean   :12863  
##  3rd Qu.:51.00   3rd Qu.:34.99   3rd Qu.:2.000   3rd Qu.:14476  
##  Max.   :64.00   Max.   :53.13   Max.   :5.000   Max.   :63770
# Checking variance before scaling the data, it is not the same
var(mydata$age)
## [1] 189.3808
var(mydata$charges)
## [1] 151953647
# Scaling the variables to be used in clustering
mydata_std <- as.data.frame(scale(mydata[c("age", "bmi", "children", "charges")]))
# After scaling we can see that both variables have the same variance
var(mydata_std$age)
## [1] 1
var(mydata_std$charges)
## [1] 1
library(Hmisc)
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:dplyr':
## 
##     src, summarize
## The following object is masked from 'package:psych':
## 
##     describe
## The following objects are masked from 'package:base':
## 
##     format.pval, units
# Checking the correlation of the variables, we want it to be low
rcorr(as.matrix(mydata_std[, c("age", "bmi", "children", "charges")]), 
      type = "pearson")
##           age  bmi children charges
## age      1.00 0.09     0.01    0.33
## bmi      0.09 1.00     0.03    0.20
## children 0.01 0.03     1.00    0.07
## charges  0.33 0.20     0.07    1.00
## 
## n= 500 
## 
## 
## P
##          age    bmi    children charges
## age             0.0507 0.8171   0.0000 
## bmi      0.0507        0.5183   0.0000 
## children 0.8171 0.5183          0.0989 
## charges  0.0000 0.0000 0.0989

The correlation between the base clusetring variables is quite low which is good

# Computing the dissimilarity
mydata_std$Dissimilarity <- sqrt(mydata_std$age^2 + mydata_std$bmi^2 + mydata_std$children^2 + mydata_std$charges^2)
# Identifying unith wth high dissimilarity
head(mydata_std[order(-mydata_std$Dissimilarity), ], 5)
##              age       bmi    children    charges Dissimilarity
## 366  1.093189173  2.680950 -0.88866794  4.1297333      5.121229
## 127 -1.522791300  3.605984 -0.88866794 -0.9491366      4.124634
## 218 -0.578131685  1.174535 -0.06886357  3.7079458      3.932858
## 36   0.003197309 -2.026698  3.21035394  0.4997052      3.829307
## 338 -0.432799436  1.870736  3.21035394 -0.5027341      3.774398
print(mydata[c(366, 127), ])
##     age    sex   bmi children smoker    region   charges   sexF    smokerF   regionF
## 366  54 female 47.41        0    yes southeast 63770.428 female     smoker southeast
## 127  18   male 53.13        0     no southeast  1163.463   male non-smoker southeast

Unit 366 has very high charges compared to others and unit 127 has a very high bmi compared to others, this is why they have a high dissimilarity score.

# Removing units with high dissimilarity and rescaling the data
mydata <- mydata[c(-366, -127), ] 
mydata_std <- as.data.frame(scale(mydata[c("age", "bmi", "children", "charges")]))
library(factoextra) 

#Finding Eudlidean distances, based on 6 Cluster variables, then saving them into object Distances
Distances <- get_dist(mydata_std, 
                      method = "euclidian")

Distances2 <- Distances^2

fviz_dist(Distances2) #Showing matrix of distances

library(factoextra) 
get_clust_tendency(mydata_std, #Hopkins statistics
                   n = nrow(mydata_std) - 1,
                   graph = FALSE) 
## $hopkins_stat
## [1] 0.7680263
## 
## $plot
## NULL

H0: There are no natural groups of objects Ha: There are natural groups of objects

As the Hopkins statistic is higher than 0.5 we can see that the data is good for clustering (there are natural groups present)

library(dplyr)
WARD <- mydata_std %>% #Selecting variables
  get_dist(method = "euclidean") %>%  #Selecting distance
  hclust(method = "ward.D2") #Selecting algorithm         

WARD
## 
## Call:
## hclust(d = ., method = "ward.D2")
## 
## Cluster method   : ward.D2 
## Distance         : euclidean 
## Number of objects: 498
library(factoextra)
fviz_dend(WARD) #Dendrogram
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as of ggplot2 3.3.4.
## ℹ The deprecated feature was likely used in the factoextra package.
##   Please report the issue at <https://github.com/kassambara/factoextra/issues>.

fviz_dend(WARD, 
          k = 2,
          cex = 0.5, 
          palette = "jama",
          color_labels_by_k = TRUE, 
          rect = TRUE)
## Warning in data.frame(xmin = unlist(xleft), ymin = unlist(ybottom), xmax = unlist(xright), : row names were found from a
## short variable and have been discarded

fviz_dend(WARD, 
          k = 3,
          cex = 0.5, 
          palette = "jama",
          color_labels_by_k = TRUE, 
          rect = TRUE) 

fviz_dend(WARD, 
          k = 4,
          cex = 0.5, 
          palette = "jama",
          color_labels_by_k = TRUE, 
          rect = TRUE)

fviz_dend(WARD, 
          k = 5,
          cex = 0.5, 
          palette = "jama",
          color_labels_by_k = TRUE, 
          rect = TRUE)

From the dendograms we can see that 4 custers will probably be the best option for our analysis

set.seed(1)

library(NbClust)
OptNumber <- mydata_std %>%
  NbClust(distance = "euclidean",
          min.nc = 2, max.nc = 10, 
          method = "ward.D2", 
          index = "all") 

## *** : The Hubert index is a graphical method of determining the number of clusters.
##                 In the plot of Hubert index, we seek a significant knee that corresponds to a 
##                 significant increase of the value of the measure i.e the significant peak in Hubert
##                 index second differences plot. 
## 

## *** : The D index is a graphical method of determining the number of clusters. 
##                 In the plot of D index, we seek a significant knee (the significant peak in Dindex
##                 second differences plot) that corresponds to a significant increase of the value of
##                 the measure. 
##  
## ******************************************************************* 
## * Among all indices:                                                
## * 2 proposed 2 as the best number of clusters 
## * 3 proposed 3 as the best number of clusters 
## * 11 proposed 4 as the best number of clusters 
## * 1 proposed 6 as the best number of clusters 
## * 1 proposed 7 as the best number of clusters 
## * 1 proposed 8 as the best number of clusters 
## * 2 proposed 9 as the best number of clusters 
## * 2 proposed 10 as the best number of clusters 
## 
##                    ***** Conclusion *****                            
##  
## * According to the majority rule, the best number of clusters is  4 
##  
##  
## *******************************************************************

The automated number of clusters test tells us that choosing 4 clusters is the best option which confirms our suspicion

mydata$ClusterWard <- cutree(WARD, 
                             k = 4) #Number of groups

head(mydata[c("ClusterWard")])
##   ClusterWard
## 1           1
## 2           1
## 3           2
## 4           3
## 5           4
## 6           2

Creating 4 clusters

#Showing the positions of initial leaders, used as starting point for k-means clustering

Leaders_initial <- aggregate(mydata_std, 
                             by = list(mydata$ClusterWard), 
                             FUN = mean)

Leaders_initial
##   Group.1        age         bmi   children     charges
## 1       1  1.0892222  0.01479656 -0.6500566  0.06178952
## 2       2  0.1410428 -0.30309451  1.5954458 -0.09233256
## 3       3 -0.6580687 -0.08429535 -0.2823474 -0.49588008
## 4       4  0.2934465  0.90525157  0.2453486  2.45536585
library(factoextra) 

kmeans_clu <- hkmeans(mydata_std, #Data
                      k = 4, #Number of groups
                      hc.metric = "euclidean", #Distance for hierar. clus.
                      hc.method = "ward.D2") #Algorithm for hierar. clus.

kmeans_clu
## Hierarchical K-means clustering with 4 clusters of sizes 153, 118, 170, 57
## 
## Cluster means:
##           age         bmi   children     charges
## 1  0.95557530 -0.01207594 -0.5919312 -0.05253726
## 2  0.07696557 -0.02121580  1.3859640 -0.19243157
## 3 -0.95275396 -0.23665091 -0.4676727 -0.61326969
## 4  0.11724940  0.78213574  0.1144927  2.36843813
## 
## Clustering vector:
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30 
##   1   1   2   2   4   2   3   3   3   1   2   2   3   3   1   1   1   1   3   1   2   4   1   1   2   3   3   2   4   4 
##  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60 
##   2   1   2   2   2   2   1   1   3   3   3   2   3   2   1   2   3   2   1   2   1   3   2   3   3   3   1   3   1   2 
##  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90 
##   3   4   1   2   4   3   2   1   4   3   1   4   2   2   3   3   1   3   3   2   1   1   1   3   3   2   2   2   3   1 
##  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 
##   3   2   3   2   3   3   1   4   1   4   3   1   1   3   1   3   2   2   2   3   2   1   4   3   3   3   1   4   4   1 
## 121 122 123 124 125 126 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 
##   1   1   3   3   4   1   2   1   1   3   4   3   1   1   4   1   1   4   3   1   2   2   3   3   4   3   1   3   3   1 
## 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 
##   3   2   1   2   2   1   2   3   1   1   3   4   3   3   2   2   1   2   3   2   1   2   3   4   1   3   4   3   3   3 
## 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 
##   1   1   2   1   1   4   4   1   3   2   3   3   1   2   3   1   3   1   3   3   4   3   1   2   2   1   1   4   1   1 
## 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 
##   3   2   1   4   3   3   4   4   3   2   1   1   3   1   1   2   2   2   2   3   1   1   3   1   2   3   1   1   3   1 
## 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 
##   1   4   2   2   1   1   1   2   1   4   3   3   2   2   1   4   1   2   4   3   2   1   3   3   4   3   1   3   1   4 
## 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 
##   2   1   3   1   3   1   3   4   4   1   1   1   2   4   2   3   3   3   3   2   3   3   2   1   1   2   1   2   2   3 
## 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 
##   2   1   2   2   1   1   4   3   1   3   1   1   2   3   1   3   4   3   2   4   4   3   1   3   1   3   2   3   2   1 
## 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 
##   3   2   1   3   3   3   2   1   2   1   1   1   3   2   1   3   2   2   1   1   3   1   1   1   1   2   4   1   4   3 
## 362 363 364 365 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 
##   2   3   2   3   1   4   3   1   1   3   3   2   1   3   4   1   1   1   4   3   3   1   3   3   2   3   3   1   3   2 
## 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 
##   3   2   1   3   1   3   3   3   3   2   1   3   3   3   2   1   1   1   3   1   1   4   3   2   3   2   3   3   2   2 
## 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 
##   3   2   2   1   4   3   2   2   4   3   1   1   3   3   3   3   3   1   3   2   1   3   2   3   2   2   2   3   4   2 
## 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 
##   3   1   4   1   2   4   3   2   3   3   2   3   2   2   1   3   3   3   2   3   4   4   3   4   1   3   3   2   3   4 
## 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 
##   3   3   3   1   1   1   1   3   1   2   3   2   1   1   3   4   1   3 
## 
## Within cluster sum of squares by cluster:
## [1] 244.1748 280.5454 262.9785 153.6563
##  (between_SS / total_SS =  52.6 %)
## 
## Available components:
## 
##  [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss" "betweenss"    "size"        
##  [8] "iter"         "ifault"       "data"         "hclust"

The ratio between the sum of squares and the total sum of squares is 52.6%. This number indicates a relatively good fit.

library(factoextra)
fviz_cluster(kmeans_clu, 
             palette = "Set1", 
             repel = T,
             ggtheme = theme_bw())
## Warning: ggrepel: 397 unlabeled data points (too many overlaps). Consider increasing max.overlaps

mydata$ClusterK_Means <- kmeans_clu$cluster
head(mydata[c("ClusterWard", "ClusterK_Means")])
##   ClusterWard ClusterK_Means
## 1           1              1
## 2           1              1
## 3           2              2
## 4           3              2
## 5           4              4
## 6           2              2
table(mydata$ClusterWard)
## 
##   1   2   3   4 
## 122  85 242  49
table(mydata$ClusterK_Means)
## 
##   1   2   3   4 
## 153 118 170  57
table(mydata$ClusterWard, mydata$ClusterK_Means)
##    
##       1   2   3   4
##   1 114   6   1   1
##   2   4  79   1   1
##   3  35  32 168   7
##   4   0   1   0  48

From this table we can see that 87 units were re-classified when using k-means clustering. The most were re-classified from the 3rd cluster to the 1st (35) and 2nd (32) one.

Leaders_final <- kmeans_clu$centers

Figure <- as.data.frame(Leaders_final)
Figure$ID <- 1:nrow(Figure)

library(tidyr)
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:reshape2':
## 
##     smiths
Figure <- pivot_longer(Figure, cols = c("age", "bmi", "children", "charges"))

Figure$Group <- factor(Figure$ID, 
                       levels = c(1, 2, 3, 4), 
                       labels = c("1", "2", "3", "4"))

Figure$NameF <- factor(Figure$name, 
                       levels = c("age", "bmi", "children", "charges"), 
                       labels = c("Age", "Bmi", "N of Children", "Charges"))

library(ggplot2)
ggplot(Figure, aes(x = NameF, y = value)) +
  geom_hline(yintercept = 0) +
  theme_bw() +
  geom_point(aes(shape = Group, col = Group), size = 3) +
  geom_line(aes(group = ID), linewidth = 1) +
  ylab("Averages") +
  xlab("Cluster variables")+
  ylim(-2.4, 2.4)

From the graph we can see how the clusters differ from each other based on the 4 base variables. The 0 line represents the average. We can see that the 1st group is above average in age (they are the oldest), for BMI they are the exact average between the 4 groups, they have the least amount of children covered by their insurance and are the group with the second highest charges, although they are still below the total average. Group 4 has the highest BMI and group 3 the lowest. For the number of children, group 2 has the most for the amount of charges the 4th group has the highest.

#Are all the cluster variables successful at classifing units into groups? Performing ANOVAs.

fit <- aov(cbind(age, bmi, children, charges) ~ as.factor(ClusterK_Means), 
           data = mydata)

summary(fit)
##  Response age :
##                            Df Sum Sq Mean Sq F value    Pr(>F)    
## as.factor(ClusterK_Means)   3  55793   18598   241.5 < 2.2e-16 ***
## Residuals                 494  38043      77                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response bmi :
##                            Df  Sum Sq Mean Sq F value    Pr(>F)    
## as.factor(ClusterK_Means)   3  1637.7  545.89   16.18 4.786e-10 ***
## Residuals                 494 16667.2   33.74                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response children :
##                            Df Sum Sq Mean Sq F value    Pr(>F)    
## as.factor(ClusterK_Means)   3 473.86 157.952  293.06 < 2.2e-16 ***
## Residuals                 494 266.26   0.539                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response charges :
##                            Df     Sum Sq    Mean Sq F value    Pr(>F)    
## as.factor(ClusterK_Means)   3 5.7132e+10 1.9044e+10  589.41 < 2.2e-16 ***
## Residuals                 494 1.5961e+10 3.2310e+07                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Because all the variables in our clustering analysis are statistically significant we can claim that all are successful at classifying units into groups.

chi_square <- chisq.test(mydata$sexF, as.factor(mydata$ClusterK_Means))
chi_square
## 
##  Pearson's Chi-squared test
## 
## data:  mydata$sexF and as.factor(mydata$ClusterK_Means)
## X-squared = 3.8192, df = 3, p-value = 0.2817
addmargins(chi_square$observed)
##            
## mydata$sexF   1   2   3   4 Sum
##      male    73  55  84  35 247
##      female  80  63  86  22 251
##      Sum    153 118 170  57 498
addmargins(round(chi_square$expected, 2)) 
##            
## mydata$sexF      1      2      3     4    Sum
##      male    75.89  58.53  84.32 28.27 247.01
##      female  77.11  59.47  85.68 28.73 250.99
##      Sum    153.00 118.00 170.00 57.00 498.00
round(chi_square$res, 2) 
##            
## mydata$sexF     1     2     3     4
##      male   -0.33 -0.46 -0.03  1.27
##      female  0.33  0.46  0.03 -1.26
library(effectsize)
## 
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
## 
##     phi
effectsize::cramers_v(mydata$sexF, mydata$ClusterK_Means)
## Cramer's V (adj.) |       95% CI
## --------------------------------
## 0.04              | [0.00, 1.00]
## 
## - One-sided CIs: upper bound fixed at [1.00].

H0: There is no assiciation between the variables Ha: There is association between the variables

With this ChiSquared test we are testing if there is a statistically significant association between the genders and the clustering groups. The test is not significant at p > 0.05, this means that there is no statistically significant association between the two variables.

chi_square <- chisq.test(mydata$regionF, as.factor(mydata$ClusterK_Means))
chi_square
## 
##  Pearson's Chi-squared test
## 
## data:  mydata$regionF and as.factor(mydata$ClusterK_Means)
## X-squared = 29.902, df = 9, p-value = 0.0004558
addmargins(chi_square$observed)
##               
## mydata$regionF   1   2   3   4 Sum
##      northeast  52  23  37   8 120
##      southeast  36  36  53  31 156
##      southwest  34  27  33  12 106
##      northwest  31  32  47   6 116
##      Sum       153 118 170  57 498
addmargins(round(chi_square$expected, 2)) 
##               
## mydata$regionF      1      2      3     4    Sum
##      northeast  36.87  28.43  40.96 13.73 119.99
##      southeast  47.93  36.96  53.25 17.86 156.00
##      southwest  32.57  25.12  36.18 12.13 106.00
##      northwest  35.64  27.49  39.60 13.28 116.01
##      Sum       153.01 118.00 169.99 57.00 498.00
round(chi_square$res, 2) 
##               
## mydata$regionF     1     2     3     4
##      northeast  2.49 -1.02 -0.62 -1.55
##      southeast -1.72 -0.16 -0.03  3.11
##      southwest  0.25  0.38 -0.53 -0.04
##      northwest -0.78  0.86  1.18 -2.00
library(effectsize)
effectsize::cramers_v(mydata$regionF, mydata$ClusterK_Means)
## Cramer's V (adj.) |       95% CI
## --------------------------------
## 0.12              | [0.00, 1.00]
## 
## - One-sided CIs: upper bound fixed at [1.00].

H0: There is no assiciation between the variables Ha: There is association between the variables

With this ChiSquared test we are testing if there ia a statistically significant association between the regions and the clustering groups. The test is significant at p < 0.001, this means that there is statistically significant assiciation. When looking at the std residuals we can see that there are more than expected people from the northeast in the 1st group (a = 0.05), more than expected people from southeast in the 4th group (a = 0.01) and less than expected people from the northwest in the 4th group (a = 0.05)

chi_square <- chisq.test(mydata$smokerF, as.factor(mydata$ClusterK_Means))
chi_square
## 
##  Pearson's Chi-squared test
## 
## data:  mydata$smokerF and as.factor(mydata$ClusterK_Means)
## X-squared = 242.67, df = 3, p-value < 2.2e-16
addmargins(chi_square$observed)
##               
## mydata$smokerF   1   2   3   4 Sum
##     smoker      15  15  15  56 101
##     non-smoker 138 103 155   1 397
##     Sum        153 118 170  57 498
addmargins(round(chi_square$expected, 2)) 
##               
## mydata$smokerF      1      2      3     4 Sum
##     smoker      31.03  23.93  34.48 11.56 101
##     non-smoker 121.97  94.07 135.52 45.44 397
##     Sum        153.00 118.00 170.00 57.00 498
round(chi_square$res, 2) 
##               
## mydata$smokerF     1     2     3     4
##     smoker     -2.88 -1.83 -3.32 13.07
##     non-smoker  1.45  0.92  1.67 -6.59
library(effectsize)
effectsize::cramers_v(mydata$smokerF, mydata$ClusterK_Means)
## Cramer's V (adj.) |       95% CI
## --------------------------------
## 0.69              | [0.62, 1.00]
## 
## - One-sided CIs: upper bound fixed at [1.00].

H0: There is no assiciation between the variables Ha: There is association between the variables

With this ChiSquared test we are testing if there is a statistically significant association between smoking/not-smoking and the clustering groups. The test is significant at p < 0.001, this means that there is assiciation. When looking at the std residuals we can see that there are more than expected people who are smokers, and less then expected who are not smokers in the 4th group (both a = 0.001). Additionally there are less than expected smokers in the 1st and 3rd group (a = 0.01)

Conclusion

We performed hierarchical clustering of 500 units based on 4 cluster variables. This was done with the ward’s algorithm and the euclidean squared distance. After, the classification was further optimized by the K-means clustering method.

Description of cluster/group 1:

The cluster1 represents the second largest group with 158 observations (people who are insurance beneficiaries), this represents 31.72% of the whole. They are the oldest out of the 4 groups, average in the BMI, lowest in number of children and the second highest in amount of charges (although they are still below the total average). More than expected, the 1st group is from the northeast (a = 0.05), and this grup has less smokers than expected (a = 0.01).