# Read the CSV file
mydata <- read.table("insurance/insurance.csv",
header=TRUE, sep=",", dec=".")
head(mydata)
## age sex bmi children smoker region charges
## 1 19 female 27.900 0 yes southwest 16884.924
## 2 18 male 33.770 1 no southeast 1725.552
## 3 28 male 33.000 3 no southeast 4449.462
## 4 33 male 22.705 0 no northwest 21984.471
## 5 32 male 28.880 0 no northwest 3866.855
## 6 31 female 25.740 0 no southeast 3756.622
unit of observation: primary beneficiary (person) of health insurance
sample size: 1338 units of observation, since it is too large sample I will reduce number of units
age: age of primary beneficiary
sex: insurance contractor gender, female, male
bmi: Body mass index
children: Number of children covered by health insurance / Number of dependents
smoker: Smoking
region: the beneficiaryโs residential area in the US, northeast, southeast, southwest, northwest.
charges: Individual medical costs billed by health insurance
source: https://www.kaggle.com/datasets/mirichoi0218/insurance?resource=download
set.seed(1)
reduced_data <- mydata[sample(nrow(mydata), size = 300 ), ]
summary(reduced_data[c("age", "bmi", "children", "charges")])
## age bmi children charges
## Min. :18.00 Min. :15.96 Min. :0.000 Min. : 1136
## 1st Qu.:26.00 1st Qu.:27.09 1st Qu.:0.000 1st Qu.: 4744
## Median :39.00 Median :30.79 Median :1.000 Median : 9325
## Mean :39.23 Mean :31.01 Mean :1.087 Mean :13343
## 3rd Qu.:52.00 3rd Qu.:34.44 3rd Qu.:2.000 3rd Qu.:15158
## Max. :64.00 Max. :48.07 Max. :5.000 Max. :62593
Median for age: 50% of primary beneficiary (person) are up to 39 years old and 50% of them more than that. Min for bmi: The minimum value for bmi index is 15.96 index points. 3rd Qu for children: 75% of primary beneficiary (person) have up to 2 children.
Standardized_data <- as.data.frame(scale(reduced_data[c("age", "bmi", "children", "charges")]))
reduced_data$SexFactor <- factor(reduced_data$sex,
levels = c("female", "male"),
labels = c("F", "M"))
reduced_data$SmokerFactor <- factor(reduced_data$smoker,
levels = c("no", "yes"),
labels = c("No", "Yes"))
reduced_data$RegionFactor <- factor(reduced_data$region,
levels = c("southeast", "northwest", "northeast","southwest" ),
labels = c("southeast", "northwest", "northeast","southwest" ))
library(Hmisc)
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
##
## format.pval, units
rcorr(as.matrix(Standardized_data[, c("age", "bmi", "children", "charges")]),
type = "pearson")
## age bmi children charges
## age 1.00 0.09 -0.02 0.30
## bmi 0.09 1.00 0.02 0.12
## children -0.02 0.02 1.00 -0.02
## charges 0.30 0.12 -0.02 1.00
##
## n= 300
##
##
## P
## age bmi children charges
## age 0.1080 0.6890 0.0000
## bmi 0.1080 0.7413 0.0430
## children 0.6890 0.7413 0.7421
## charges 0.0000 0.0430 0.7421
reduced_data$Dissimilarity <- sqrt(Standardized_data$age^2 + Standardized_data$bmi^2 + Standardized_data$children^2 + Standardized_data$charges^2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:Hmisc':
##
## src, summarize
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
reduced_data <- reduced_data %>%
mutate(ID = row_number())
head (reduced_data[order(-reduced_data$Dissimilarity), ], 15)
## age sex bmi children smoker region charges SexFactor
## 1231 52 male 34.485 3 yes northwest 60021.399 M
## 1301 45 male 30.360 0 yes southeast 62592.873 M
## 282 54 male 40.565 3 yes northeast 48549.178 M
## 33 19 female 28.600 5 no southwest 4687.797 F
## 1246 28 male 24.300 5 no southwest 5615.369 M
## 1147 60 male 32.800 0 yes southwest 52590.829 M
## 40 60 male 39.900 0 yes southwest 48173.361 M
## 952 51 male 42.900 2 yes southeast 47462.894 M
## 675 44 female 43.890 2 yes southeast 46200.985 F
## 878 33 male 33.440 5 no southeast 6653.789 M
## 421 64 male 33.880 0 yes southeast 46889.261 M
## 329 64 female 33.800 1 yes southwest 47928.030 F
## 252 63 female 32.200 2 yes southwest 47305.305 F
## 56 58 male 36.955 2 yes northwest 47496.494 M
## 173 18 male 15.960 0 no northeast 1694.796 M
## SmokerFactor RegionFactor Dissimilarity ID
## 1231 Yes northwest 4.218093 135
## 1301 Yes southeast 4.082289 10
## 282 Yes northeast 3.758947 82
## 33 No southwest 3.683171 212
## 1246 No southwest 3.622826 299
## 1147 Yes southwest 3.604243 203
## 40 Yes southwest 3.593713 33
## 952 Yes southeast 3.547136 186
## 675 Yes southeast 3.482065 247
## 878 No southeast 3.398147 15
## 421 Yes southeast 3.368326 147
## 329 Yes southwest 3.309721 65
## 252 Yes southwest 3.295091 58
## 56 Yes northwest 3.286967 260
## 173 No northeast 3.165365 238
library(dplyr)
reduced_data <- reduced_data %>%
filter(!ID %in% c(135, 10))
Standardized_data <- as.data.frame(scale(reduced_data[c("age", "bmi", "children", "charges")]))
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.3.2
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
distance <- get_dist(reduced_data[c("age", "bmi", "children", "charges")],
method = "euclidian")
distance2 <- distance^2
fviz_dist(distance)
get_clust_tendency(Standardized_data[c("age", "bmi", "children", "charges")],
n = nrow(Standardized_data) - 1,
graph = FALSE)
## $hopkins_stat
## [1] 0.7591662
##
## $plot
## NULL
library(dplyr)
WARD <- Standardized_data[c ("age", "bmi", "children", "charges")] %>%
get_dist(method = "euclidean") %>%
hclust (method = "ward.D2")
WARD
##
## Call:
## hclust(d = ., method = "ward.D2")
##
## Cluster method : ward.D2
## Distance : euclidean
## Number of objects: 298
library(factoextra)
fviz_dend (WARD)
## 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>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
reduced_data$ClusterWard <- cutree (WARD,
k= 4)
head(reduced_data)
## age sex bmi children smoker region charges SexFactor SmokerFactor
## 1 19 female 24.605 1 no northwest 2709.244 F No
## 2 56 male 36.100 3 no southwest 12363.547 M No
## 3 32 female 17.765 2 yes northwest 32734.186 F Yes
## 4 41 male 34.210 1 no southeast 6289.755 M No
## 5 27 male 32.670 0 no southeast 2497.038 M No
## 6 31 male 34.390 3 yes northwest 38746.355 M Yes
## RegionFactor Dissimilarity ID ClusterWard
## 1 northwest 1.9631623 1 1
## 2 southwest 2.1667450 2 2
## 3 northwest 2.8262980 3 2
## 4 southeast 0.7851452 4 1
## 5 southeast 1.5529109 5 1
## 6 northwest 2.7224765 6 3
Initial_leaders <- aggregate(Standardized_data,
by = list(reduced_data$ClusterWard),
FUN = mean)
Initial_leaders
## Group.1 age bmi children charges
## 1 1 -0.87987964 -0.23862397 -0.4213705 -0.64395879
## 2 2 0.09746019 0.05989382 1.1743567 -0.22016701
## 3 3 -0.04497374 0.70709737 -0.1456319 2.23640869
## 4 4 1.05214783 -0.07563273 -0.7611919 0.09616958
library(factoextra)
kmeans_clu <- hkmeans(Standardized_data,
k = 4,
hc.metric = "euclidean",
hc.method = "ward.D2")
kmeans_clu
## Hierarchical K-means clustering with 4 clusters of sizes 102, 79, 38, 79
##
## Cluster means:
## age bmi children charges
## 1 -0.88046261 -0.2326442112 -0.4193596 -0.61370196
## 2 0.08292744 0.0649546605 1.3105747 -0.25060550
## 3 -0.06547765 0.4880261943 -0.1823035 2.20278997
## 4 1.08536797 0.0006746327 -0.6814327 -0.01658967
##
## Clustering vector:
## [1] 1 2 3 1 1 3 4 1 2 4 2 1 1 2 2 4 3 1 1 4 1 1 4 2 4 4 1 1 4 1 2 3 2 1 1 2 3
## [38] 3 1 1 1 2 4 1 1 2 2 2 3 1 2 2 4 2 4 1 3 2 4 4 2 2 2 3 2 2 3 1 3 4 1 3 4 4
## [75] 4 1 4 1 2 1 3 4 4 4 3 4 1 1 3 4 3 2 4 1 4 1 2 2 2 2 1 1 2 1 2 4 2 4 1 1 1
## [112] 1 1 2 4 1 3 4 2 4 1 1 1 2 4 2 2 4 1 2 4 1 2 2 2 1 2 2 4 2 1 1 1 3 3 1 1 1
## [149] 4 1 1 1 1 3 2 1 1 2 2 2 2 1 4 3 4 4 4 3 1 1 4 1 1 4 1 2 3 1 4 2 1 1 4 3 2
## [186] 4 1 2 3 2 2 1 1 1 4 4 3 1 3 4 3 2 1 1 4 3 2 1 3 2 3 1 2 4 1 1 1 2 4 4 2 2
## [223] 1 1 1 1 1 4 4 1 3 4 2 2 4 1 1 4 2 2 4 2 4 1 3 1 4 4 1 4 4 4 2 2 4 4 2 3 1
## [260] 4 1 4 1 3 2 1 3 4 2 2 1 3 4 2 4 1 4 4 4 3 4 4 1 2 1 1 2 4 1 2 1 4 1 4 2 2
## [297] 2 4
##
## Within cluster sum of squares by cluster:
## [1] 168.2283 191.1222 108.0579 113.4837
## (between_SS / total_SS = 51.1 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault" "data"
## [11] "hclust"
library(factoextra)
fviz_cluster(kmeans_clu,
palette = "Set1",
repel - TRUE,
ggtheme = theme_bw())
reduced_data$ClusterK_Means <- kmeans_clu$cluster
head(reduced_data[c("ClusterWard", "ClusterK_Means")])
## ClusterWard ClusterK_Means
## 1 1 1
## 2 2 2
## 3 2 3
## 4 1 1
## 5 1 1
## 6 3 3
table(reduced_data$ClusterWard)
##
## 1 2 3 4
## 99 89 34 76
table(reduced_data$ClusterK_Means)
##
## 1 2 3 4
## 102 79 38 79
table(reduced_data$ClusterWard, reduced_data$ClusterK_Means)
##
## 1 2 3 4
## 1 95 3 0 1
## 2 6 76 1 6
## 3 0 0 33 1
## 4 1 0 4 71
Leaders_final <- kmeans_clu$centers
Leaders_final
## age bmi children charges
## 1 -0.88046261 -0.2326442112 -0.4193596 -0.61370196
## 2 0.08292744 0.0649546605 1.3105747 -0.25060550
## 3 -0.06547765 0.4880261943 -0.1823035 2.20278997
## 4 1.08536797 0.0006746327 -0.6814327 -0.01658967
Figure <- as.data.frame (Leaders_final)
Figure$ID <- 1: nrow (Figure)
library (tidyr)
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", "number 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)
fit <- aov(cbind(age, bmi, children, charges) ~ as.factor(ClusterK_Means),
data = reduced_data)
summary(fit)
## Response age :
## Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(ClusterK_Means) 3 35191 11730 136.43 < 2.2e-16 ***
## Residuals 294 25279 86
## ---
## 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 558.3 186.101 5.1778 0.001683 **
## Residuals 294 10567.0 35.942
## ---
## 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 267.63 89.209 178.08 < 2.2e-16 ***
## Residuals 294 147.28 0.501
## ---
## 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 3.1990e+10 1.0663e+10 322.52 < 2.2e-16 ***
## Residuals 294 9.7203e+09 3.3062e+07
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
H0: ๐1 = ๐2 = ๐3 = ๐4
H1: At least one ๐ is different from others.
Based on this p-values I will reject the null hypothesis for all of them and by that conclude that all of the cluster variables classifies units into groups.
chi_square <- chisq.test(reduced_data$SexFactor, as.factor(reduced_data$ClusterK_Means))
chi_square
##
## Pearson's Chi-squared test
##
## data: reduced_data$SexFactor and as.factor(reduced_data$ClusterK_Means)
## X-squared = 4.2558, df = 3, p-value = 0.2351
addmargins(chi_square$observed)
##
## reduced_data$SexFactor 1 2 3 4 Sum
## F 54 40 13 41 148
## M 48 39 25 38 150
## Sum 102 79 38 79 298
addmargins(round(chi_square$expected, 2))
##
## reduced_data$SexFactor 1 2 3 4 Sum
## F 50.66 39.23 18.87 39.23 147.99
## M 51.34 39.77 19.13 39.77 150.01
## Sum 102.00 79.00 38.00 79.00 298.00
round(chi_square$res, 2)
##
## reduced_data$SexFactor 1 2 3 4
## F 0.47 0.12 -1.35 0.28
## M -0.47 -0.12 1.34 -0.28
library(effectsize)
effectsize::cramers_v(reduced_data$SexFactor, reduced_data$ClusterK_Means)
## Cramer's V (adj.) | 95% CI
## --------------------------------
## 0.06 | [0.00, 1.00]
##
## - One-sided CIs: upper bound fixed at [1.00].
With a p-value greater than 5%, we can not reject null hypothesis which indicates that there is no statistically significant association between the two variables.(Sex and other clustering variables)
chi_square <- chisq.test(reduced_data$RegionFactor, as.factor(reduced_data$ClusterK_Means))
chi_square
##
## Pearson's Chi-squared test
##
## data: reduced_data$RegionFactor and as.factor(reduced_data$ClusterK_Means)
## X-squared = 9.4529, df = 9, p-value = 0.3966
addmargins(chi_square$observed)
##
## reduced_data$RegionFactor 1 2 3 4 Sum
## southeast 34 20 12 18 84
## northwest 25 20 7 17 69
## northeast 24 15 10 27 76
## southwest 19 24 9 17 69
## Sum 102 79 38 79 298
addmargins(round(chi_square$expected, 2))
##
## reduced_data$RegionFactor 1 2 3 4 Sum
## southeast 28.75 22.27 10.71 22.27 84
## northwest 23.62 18.29 8.80 18.29 69
## northeast 26.01 20.15 9.69 20.15 76
## southwest 23.62 18.29 8.80 18.29 69
## Sum 102.00 79.00 38.00 79.00 298
round(chi_square$res, 2)
##
## reduced_data$RegionFactor 1 2 3 4
## southeast 0.98 -0.48 0.39 -0.90
## northwest 0.28 0.40 -0.61 -0.30
## northeast -0.39 -1.15 0.10 1.53
## southwest -0.95 1.33 0.07 -0.30
library(effectsize)
effectsize::cramers_v(reduced_data$RegionFactor, reduced_data$ClusterK_Means)
## Cramer's V (adj.) | 95% CI
## --------------------------------
## 0.02 | [0.00, 1.00]
##
## - One-sided CIs: upper bound fixed at [1.00].
H0: There is no association between the variables
H1: There is association between the variables
With a p-value greater than 5%, we can not reject null hypothesis which indicates that there is no statistically significant association between the two variables. (region and other clustering variables)
chi_square <- chisq.test(reduced_data$SmokerFactor, as.factor(reduced_data$ClusterK_Means))
chi_square
##
## Pearson's Chi-squared test
##
## data: reduced_data$SmokerFactor and as.factor(reduced_data$ClusterK_Means)
## X-squared = 130.44, df = 3, p-value < 2.2e-16
addmargins(chi_square$observed)
##
## reduced_data$SmokerFactor 1 2 3 4 Sum
## No 89 68 3 74 234
## Yes 13 11 35 5 64
## Sum 102 79 38 79 298
addmargins(round(chi_square$expected, 2))
##
## reduced_data$SmokerFactor 1 2 3 4 Sum
## No 80.09 62.03 29.84 62.03 233.99
## Yes 21.91 16.97 8.16 16.97 64.01
## Sum 102.00 79.00 38.00 79.00 298.00
round(chi_square$res, 2)
##
## reduced_data$SmokerFactor 1 2 3 4
## No 1.00 0.76 -4.91 1.52
## Yes -1.90 -1.45 9.39 -2.91
library(effectsize)
effectsize::cramers_v(reduced_data$SmokerFactor, reduced_data$ClusterK_Means)
## Cramer's V (adj.) | 95% CI
## --------------------------------
## 0.66 | [0.55, 1.00]
##
## - One-sided CIs: upper bound fixed at [1.00].
H0: There is no association between the variables H1: There is association between the variables
With a p-value less than 5% (p<0.001), we can reject null hypothesis and conclude that there is statistically significant association between the two variables. (smoker and other clustering variables)
We utilized hierarchical clustering on 300 units, considering 4 cluster variables using the Wardโs algorithm and squared Euclidean distance. Subsequently, we refined the classification further through the K-means clustering method.
Description of group 3: The cluster 3 represents the smaller group with 38 people, which is 12.7% (38/289). They are on the average regarding the age, highest in the bmi and slightly bellow the average regarding the number of children. As said before, group 3 has the peak meaning that they have the highest charges. There is more than expected smokers (alpha= 0.1%) and less than expected number of non smokers (alpha=0.1%).