สำหรับโปรเจคนี้มีเป้าหมายในการแบ่งกลุ่มลูกค่าก่อนที่จะนำไปคำนวนค่ากรมธรรม์ของแต่ละกลุ่ม โดยจะทำการเลือกการแจกแจงความถี่ของการเอาประกันในแต่ละกลุ่ม โดยในโปรเจคนี้นำชุดข้อมูลจาก kaggle ที่มีชื่อชึ่อชุดข้อมูลว่า Insurance Claim Analysis: Demographic and Health และชุดข้อมูลนี้มีข้อมูลเชิงลึกที่เกี่ยวข้องกับการเคลมประกัน โดยข้อมูลประกอบด้วยข้อมูลเกี่ยวกับอายุของผู้ป่วย เพศ BMI (ดัชนีมวลกาย) ระดับความดันโลหิต สถานะโรคเบาหวาน จำนวนเด็ก สถานะการสูบบุหรี่ และภูมิภาค
https://www.kaggle.com/datasets/thedevastator/insurance-claim-analysis-demographic-and-health
library(readxl)
library(ggplot2)
library(magrittr)
library(cluster)
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(plotly)
## Warning: package 'plotly' was built under R version 4.3.2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(cluster)
data <- read_excel("C:/Users/Gungnang_xyz/Downloads/insurance_data.xlsx")
View(data)
str(data)
## tibble [1,340 × 11] (S3: tbl_df/tbl/data.frame)
## $ index : num [1:1340] 0 1 2 3 4 5 6 7 8 9 ...
## $ PatientID : num [1:1340] 1 2 3 4 5 6 7 8 9 10 ...
## $ age : num [1:1340] 39 24 NA NA NA NA NA 19 20 30 ...
## $ gender : chr [1:1340] "male" "male" "male" "male" ...
## $ bmi : num [1:1340] 23.2 30.1 33.3 33.7 34.1 34.4 37.3 41.1 43 53.1 ...
## $ bloodpressure: num [1:1340] 91 87 82 80 100 96 86 100 86 97 ...
## $ diabetic : chr [1:1340] "Yes" "No" "Yes" "No" ...
## $ children : num [1:1340] 0 0 0 0 0 0 0 0 0 0 ...
## $ smoker : chr [1:1340] "No" "No" "No" "No" ...
## $ region : chr [1:1340] "southeast" "southeast" "southeast" "northwest" ...
## $ claim : num [1:1340] 1122 1132 1136 1136 1137 ...
หลังจากดูประเภทของฟีเจอร์เบื้องต้น จะเห็นได้ว่า index เป็นฟีเจอร์ที่ไม่เกี่ยวข้องต้องการแบ่งกลุ่มลูกค้า จึงทำการตัวคอลลัมน์ index ออก
##delete a "index" column
data$index <- NULL
summary(data)
## PatientID age gender bmi
## Min. : 1.0 Min. :18.00 Length:1340 Min. :16.00
## 1st Qu.: 335.8 1st Qu.:29.00 Class :character 1st Qu.:26.27
## Median : 670.5 Median :38.00 Mode :character Median :30.40
## Mean : 670.5 Mean :38.08 Mean :30.67
## 3rd Qu.:1005.2 3rd Qu.:47.00 3rd Qu.:34.70
## Max. :1340.0 Max. :60.00 Max. :53.10
## NA's :5
## bloodpressure diabetic children smoker
## Min. : 80.00 Length:1340 Min. :0.000 Length:1340
## 1st Qu.: 86.00 Class :character 1st Qu.:0.000 Class :character
## Median : 92.00 Mode :character Median :1.000 Mode :character
## Mean : 94.16 Mean :1.093
## 3rd Qu.: 99.00 3rd Qu.:2.000
## Max. :140.00 Max. :5.000
##
## region claim
## Length:1340 Min. : 1122
## Class :character 1st Qu.: 4720
## Mode :character Median : 9370
## Mean :13253
## 3rd Qu.:16604
## Max. :63770
##
ทำการตรวจสอบค่าว่าง (Missing values) ในชุดข้อมูล
5.1) ตรวจสอบ missing values โดยใช้คำสั่ง is.na()
##check missing values
print(colSums(is.na(data)))
## PatientID age gender bmi bloodpressure
## 0 5 0 0 0
## diabetic children smoker region claim
## 0 0 0 3 0
จะเห็นได้ว่าคอลัมน์ที่มี Missing values คือ age และ region
5.2) ทำการตัดแถวที่มี missing values ออก และเรียกดูค่า missing values อีกครั้ง
##Remove rows with missing values
data <- na.omit(data)
##check missing values
print(colSums(is.na(data)))
## PatientID age gender bmi bloodpressure
## 0 0 0 0 0
## diabetic children smoker region claim
## 0 0 0 0 0
ขั้นตอนการตรวจสอบ outliers โดยขั้นตอนหลักจะใช้คำสั่ง boxplot และ x$out เพื่อแสดงค่า outliers ออกมา สำหรับคอลัมน์ที่เป็น num และสำหรับคอลัมน์ที่เป็น chr จะใช้คำสั่ง unique() เพื่อตรวจสอบว่าคอลัมน์นั้น ไม่มีข้อมูลที่ผิดปกติ
6.1) ข้อมูลประเภท num
Age <- boxplot(data$age)
Age$out
## numeric(0)
BMI <- boxplot(data$bmi)
BMI_outliers <- boxplot(data$bmi, plot=FALSE)$out
print(BMI_outliers)
## [1] 53.1 50.4 48.1 47.7 49.1 52.6 47.6
จะเห็นได้ว่า bmi มี outliers อยู่จึงต้องจำการตัดแถวนั้นออกไป
remove_outliers <- function(data, variable, factor = 1.5) {
q <- quantile(data[[variable]])
iqr <- IQR(data[[variable]])
lower_bound <- q[2] - factor * iqr
upper_bound <- q[4] + factor * iqr
return(data[which(data[[variable]] >= lower_bound & data[[variable]] <= upper_bound), ])}
จึงทำการเขียนฟังก์ชันสำหรับการเอา outliers ออก
data <- remove_outliers(data, 'bmi')
BMI_outliers <- boxplot(data$bmi, plot=FALSE)$out
print(BMI_outliers)
## [1] 47.5 47.4
data <- remove_outliers(data, 'bmi')
BMI_outliers <- boxplot(data$bmi, plot=FALSE)$out
print(BMI_outliers)
## numeric(0)
BMI <- boxplot(data$bmi)
จากนั้นจึงใช้ฟังก์ชันที่เขียนไปข้างต้นตัด outliers ออกจาก bmi
data <- remove_outliers(data, 'bmi')
BMI_outliers <- boxplot(data$bmi, plot=FALSE)$out
print(BMI_outliers)
## numeric(0)
data <- remove_outliers(data, 'bmi')
BMI_outliers <- boxplot(data$bmi, plot=FALSE)$out
print(BMI_outliers)
## numeric(0)
BMI <- boxplot(data$bmi)
Bloodpressure <- boxplot(data$bloodpressure)
Bloodpressure_outliers <- boxplot(data$bloodpressure, plot=FALSE)$out
print(Bloodpressure_outliers)
## [1] 128 135 128 126 126 137 137 119 123 130 139 132 122 129 129 136 136 125 124
## [20] 140 129 131 126 135 140 140 124 127 139 119 131 124 140 123 137 131 125 119
## [39] 138 139 139 132 131 128 124 120 135 124 125 120 127 140 123 130 134 137 121
## [58] 133 120 127 125 120
จะเห็นได้ว่า bloodpressure มี outliers จึงทำการตัดออก
data <- remove_outliers(data, 'bloodpressure')
Bloodpressure_outliers <- boxplot(data$bloodpressure, plot=FALSE)$out
print(Bloodpressure_outliers)
## [1] 118 118 117 117 117 117 117
data <- remove_outliers(data, 'bloodpressure')
Bloodpressure_outliers <- boxplot(data$bloodpressure, plot=FALSE)$out
print(Bloodpressure_outliers)
## numeric(0)
Bloodpressure <- boxplot(data$bloodpressure)
Children <- boxplot(data$children)
Children$out
## numeric(0)
Claim <- boxplot(data$claim)
Claim_outliers <- boxplot(data$claim, plot=FALSE)$out
print(Claim_outliers)
## [1] 28476.73 28868.66 28923.14 28950.47 29186.48 29330.98 29523.17 30063.58
## [9] 30166.62 30184.94 30284.64 30942.19 32108.66 32548.34 32734.19 32787.46
## [17] 33307.55 33471.97 33475.82 33750.29 33907.55 34303.17 34439.86 34472.84
## [25] 34617.84 34779.62 34806.47 34838.87 35069.37 35147.53 35160.13 35585.58
## [33] 36021.01 36085.22 36189.10 36219.41 36397.58 36580.28 36837.47 36898.73
## [41] 37133.90 37465.34 37607.53 37701.88 37742.58 37829.72 38126.25 38344.57
## [49] 38415.47 38709.18 38998.55 39047.29 39125.33 39241.44 39556.49 39725.52
## [57] 39871.70 39983.43 40003.33 40103.89 40182.25 40273.65 40419.02 40720.55
## [65] 40904.20 40941.29 41034.22 41097.16 41661.60 41676.08 41919.10 42124.52
## [73] 42211.14 42760.50 42856.84 42983.46 43254.42 43813.87 43921.18 44202.65
## [81] 44260.75 44400.41 44423.80 44585.46 44641.20 45702.02 45710.21 45863.21
## [89] 46151.12 46200.99 46255.11 46661.44 46718.16 46889.26 47269.85 47291.06
## [97] 47305.31 47403.88 47462.89 47496.49 47928.03 48173.36 48549.18 48673.56
## [105] 48824.45 48885.14 48970.25 55135.40 60021.40 62592.87
จะเห็นได้ว่า claim มี outliers จึงทำการตัดออก
data <- remove_outliers(data, 'claim')
Claim_outliers <- boxplot(data$claim, plot=FALSE)$out
print(Claim_outliers)
## [1] 24535.70 24603.05 24667.42 24671.66 24869.84 24873.38 24915.05 24915.22
## [9] 25081.77 25309.49 25333.33 25382.30 25517.11 25656.58 25678.78 25992.82
## [17] 26109.33 26125.67 26236.58 27000.98 27117.99 27218.44 27322.73 27346.04
## [25] 27533.91 27724.29 27941.29
data <- remove_outliers(data, 'claim')
Claim_outliers <- boxplot(data$claim, plot=FALSE)$out
print(Claim_outliers)
## [1] 23807.24 23887.66 23967.38 24059.68 24106.91 24180.93 24227.34 24393.62
## [9] 24476.48
data <- remove_outliers(data, 'claim')
Claim_outliers <- boxplot(data$claim, plot=FALSE)$out
print(Claim_outliers)
## [1] 23563.02 23568.27
data <- remove_outliers(data, 'claim')
Claim_outliers <- boxplot(data$claim, plot=FALSE)$out
print(Claim_outliers)
## numeric(0)
Claim <- boxplot(data$claim)
6.2) ข้อมูลประเภท chr
diabetic
Diabetic <- unique(data$diabetic)
Diabetic
## [1] "Yes" "No"smoker
Smoker <- unique(data$smoker)
Smoker
## [1] "No" "Yes"region
Region <- unique(data$region)
Region
## [1] "southeast" "northwest" "southwest" "northeast"str(data)
## tibble [1,104 × 10] (S3: tbl_df/tbl/data.frame)
## $ PatientID : num [1:1104] 1 2 8 9 11 12 13 17 18 19 ...
## $ age : num [1:1104] 39 24 19 20 36 37 19 35 41 49 ...
## $ gender : chr [1:1104] "male" "male" "male" "male" ...
## $ bmi : num [1:1104] 23.2 30.1 41.1 43 19.8 20.3 20.7 34.1 34.4 35.4 ...
## $ bloodpressure: num [1:1104] 91 87 100 86 88 90 81 90 84 97 ...
## $ diabetic : chr [1:1104] "Yes" "No" "No" "No" ...
## $ children : num [1:1104] 0 0 0 0 0 0 0 0 0 0 ...
## $ smoker : chr [1:1104] "No" "No" "No" "No" ...
## $ region : chr [1:1104] "southeast" "southeast" "northwest" "northwest" ...
## $ claim : num [1:1104] 1122 1132 1147 1149 1242 ...
## - attr(*, "na.action")= 'omit' Named int [1:8] 3 4 5 6 7 14 15 16
## ..- attr(*, "names")= chr [1:8] "3" "4" "5" "6" ...
7.1) เลือกคอลัมน์ที่จะนำมาจัดกลุ่ม ได้แก่ age, bmi, bloodpressure, claim
selected_col <- data[, c("age", "bmi", "bloodpressure", "claim")]
7.2) ทำการแบ่งกลุ่มด้วยวิธี K-mean โดยแบ่งเป็น 6 กลุ่ม
kmm_test <- kmeans(x = scale(selected_col), centers = 6)
7.3) นำกลุ่มที่ถูกแบ่งแล้วมาหาืำการทดสอบหาค่า K ที่เหมาะสมกับข้อมูลมากที่สุด
##Find Optimal Number of Cluster
#Silhouette Method
mean(silhouette(kmm_test$cluster, daisy(selected_col))[,'sil_width'])
## [1] -0.1066558
Silhouette <- rep(0,9)
for(i in 2:10){
kmm_test <- kmeans(x = selected_col, centers = i)
Sil <- mean(silhouette(kmm_test$cluster, daisy(selected_col))[,'sil_width'])
Silhouette[i-1] <- Sil
}
Silhouette
## [1] 0.6000454 0.6124658 0.5937399 0.5620750 0.5613390 0.5643509 0.5695310
## [8] 0.5506484 0.5416509
plot(x = 2:10, y = Silhouette, type = 'b')
#Elbow Method
Elbow <- rep(0,9)
for(i in 2:10){
kmm_test <- kmeans(x = selected_col, centers = i)
Elbow[i-1] <- kmm_test$tot.withinss
}
plot(x = 2:10, y = Elbow, type = 'b')
จากผลลัพธ์จากกราฟของ Silhouette Method และ Elbow Method จะเห็นได้ว่าค่า K ที่เหมาะสมที่สุด คือ K=3
7.4) นำค่า K = 3 มาใช้กับข้อมูล
kmm <- kmeans(x = scale(selected_col), centers = 3)
print(kmm$centers)
## age bmi bloodpressure claim
## 1 -0.8545733 0.06096873 -0.2388628 -0.5060039
## 2 -0.1201974 -0.14535832 0.9844461 1.1426485
## 3 0.9404189 0.04017101 -0.4465364 -0.2889810
จากค่าของค่าต้นจะเห็นได้ว่า
cluster ที่ 2 เป็นตัวแทนของกลุ่มค่าเคลมสูง จะเห็นได้ว่ากลุ่มลูกค้าต้องมีความดันเลือดที่สูง และอายุจะอยู่ในช่วงกลาง และ bmi ไม่ได้สูงมาก
cluster ที่ 3 เป็นตัวแทนของกลุ่มค่าเคลมกลาง จะเห็ฯได้ว่ากลุ่มลูกค้าต้องมีอายุมาก และ bmi ในระดับกลางๆ และความดันเลือดต่ำ
#Create data frame for plot graph
plot.df <- data.frame(
Cluster = factor(kmm$cluster),
PatientID = data$PatientID,
Age = data$age,
Gender = data$gender,
BMI = data$bmi,
Bloodpressure = data$bloodpressure,
Diabetic = data$diabetic,
Children = data$children,
Smoker = data$smoker,
Region = data$region,
Claim = data$claim)
7.5) ทำการพล็อตข้อมูลของแต่ละ cluster ออกมาดู จะประกอบไปด้วย
กราฟที่แสดงความสัมพันธ์ค่าเคลม (Claim) กับ อายุ (Age)
กราฟที่แสดงความสัมพันธ์ค่าเคลม (Claim) กับ bmi
กราฟที่แสดงความสัมพันธ์ค่าเคลม (Claim) กับ ความดันเลือด (Blood pressure)
#Crossectional plot
ggplot(data = plot.df, aes(x = Age, y = Claim, color = Cluster)) + geom_point(size = 1.5)
ggplot(data = plot.df, aes(x = BMI, y = Claim, color = Cluster)) + geom_point(size = 1.5)
ggplot(data = plot.df, aes(x = Claim, y = Bloodpressure, color = Cluster)) + geom_point(size = 1.5)
7.6) ทำการพล็อตข้อมูลของแต่ละ cluster ออกมาในมุมมอง 3 มิติ เพื่อทำให้มองเห็นภาพมากขึ้น
#3D plot with plotly
plot_ly(plot.df, x=~Age, y=~Claim, z=~BMI, color=~Cluster) %>% add_markers(size=1.5)
จากกราฟสามมิติข้างต้นจะเห็นได้ว่า K = 3 ค่อนข้างที่จะแบ่งข้อมูลออกมาได้ชัดเจน
clusters <- kmm$cluster
print(clusters)
## [1] 1 1 1 1 1 1 1 1 3 3 3 3 1 1 3 3 1 1 1 3 1 1 3 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2 2 2 2 2 3 2 3 2 2 2 3 2 2 2 2 2
## [1000] 3 2 2 2 2 2 2 3 1 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 3 2 3 2
## [1037] 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [1074] 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 3 2 2 2 2 2 3 2 2 2 2 2 2 2
ขั้นตอนนี้จะทำการแบ่งชุดข้อมูลอกเป็น 3 ชุดข้อมูลตาม cluster ที่แบ่งไว้และดูค่าสถิติเบื้องต้นของแต่ละ cluster และพล็อตกราฟเพื่อดูการกระจายของข้อมูล
8.1) ชุดข้อมูลจาก cluster 1
Group_1 <- subset(plot.df, Cluster == "1")
Group_1
summary(Group_1)
## Cluster PatientID Age Gender BMI
## 1:410 Min. : 1.0 Min. :18.0 Length:410 Min. :16.00
## 2: 0 1st Qu.: 199.2 1st Qu.:25.0 Class :character 1st Qu.:25.73
## 3: 0 Median : 382.5 Median :29.0 Mode :character Median :30.25
## Mean : 404.8 Mean :28.8 Mean :30.41
## 3rd Qu.: 588.5 3rd Qu.:33.0 3rd Qu.:34.17
## Max. :1043.0 Max. :41.0 Max. :46.50
## Bloodpressure Diabetic Children Smoker
## Min. : 80.00 Length:410 Min. :0.000 Length:410
## 1st Qu.: 85.25 Class :character 1st Qu.:0.000 Class :character
## Median : 90.00 Mode :character Median :1.000 Mode :character
## Mean : 89.79 Mean :1.068
## 3rd Qu.: 94.00 3rd Qu.:2.000
## Max. :101.00 Max. :5.000
## Region Claim
## Length:410 Min. : 1122
## Class :character 1st Qu.: 3066
## Mode :character Median : 5267
## Mean : 5893
## 3rd Qu.: 8230
## Max. :18903
hist(Group_1$Claim, main = "Histogram of Claim (Group 1)", xlab = "Claim", col = "skyblue", border = "black")
plot(density(Group_1$Claim), main = "Density Plot of Claim (Group 1)", xlab = "Values", col = "skyblue", border = "black")
## Warning in plot.window(...): "border" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "border" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "border" is not a
## graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "border" is not a
## graphical parameter
## Warning in box(...): "border" is not a graphical parameter
## Warning in title(...): "border" is not a graphical parameter
จากกราฟข้างต้นข้อมูลจาก cluster 1 มีการแจงแจกแบบ Log-Normal Distribution
8.2) ชุดข้อมูลจาก cluster 2
Group_2 <- subset(plot.df, Cluster == "2")
Group_2
summary(Group_2)
## Cluster PatientID Age Gender BMI
## 1: 0 Min. : 480.0 Min. :19.00 Length:285 Min. :16.80
## 2:285 1st Qu.: 787.0 1st Qu.:30.00 Class :character 1st Qu.:25.20
## 3: 0 Median : 944.0 Median :37.00 Mode :character Median :28.50
## Mean : 911.7 Mean :36.94 Mean :29.18
## 3rd Qu.:1040.0 3rd Qu.:44.00 3rd Qu.:33.10
## Max. :1118.0 Max. :60.00 Max. :46.80
## Bloodpressure Diabetic Children Smoker
## Min. : 80.00 Length:285 Min. :0.00 Length:285
## 1st Qu.: 95.00 Class :character 1st Qu.:0.00 Class :character
## Median :100.00 Mode :character Median :1.00 Mode :character
## Mean : 99.14 Mean :1.07
## 3rd Qu.:105.00 3rd Qu.:2.00
## Max. :110.00 Max. :5.00
## Region Claim
## Length:285 Min. : 6641
## Class :character 1st Qu.:11188
## Mode :character Median :13881
## Mean :14820
## 3rd Qu.:18805
## Max. :23401
hist(Group_2$Claim, main = "Histogram of Claim (Group 2)", xlab = "Claim", col = "skyblue", border = "black")
plot(density(Group_2$Claim), main = "Density Plot of Claim (Group 2)", xlab = "Values", col = "skyblue", border = "black")
## Warning in plot.window(...): "border" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "border" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "border" is not a
## graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "border" is not a
## graphical parameter
## Warning in box(...): "border" is not a graphical parameter
## Warning in title(...): "border" is not a graphical parameter
จากกราฟข้างต้นข้อมูลจาก cluster 2 มีการแจงแจกแบบ Log-Normal Distribution
8.3) ชุดข้อมูลจาก cluster 3
Group_3 <- subset(plot.df, Cluster == "3")
Group_3
summary(Group_3)
## Cluster PatientID Age Gender BMI
## 1: 0 Min. : 18.0 Min. :36.00 Length:409 Min. :16.80
## 2: 0 1st Qu.: 233.0 1st Qu.:44.00 Class :character 1st Qu.:26.10
## 3:409 Median : 471.0 Median :48.00 Mode :character Median :29.80
## Mean : 481.1 Mean :48.69 Mean :30.29
## 3rd Qu.: 690.0 3rd Qu.:54.00 3rd Qu.:34.30
## Max. :1111.0 Max. :60.00 Max. :46.70
## Bloodpressure Diabetic Children Smoker
## Min. : 80.0 Length:409 Min. :0.000 Length:409
## 1st Qu.: 83.0 Class :character 1st Qu.:0.000 Class :character
## Median : 88.0 Mode :character Median :1.000 Mode :character
## Mean : 88.2 Mean :1.112
## 3rd Qu.: 93.0 3rd Qu.:2.000
## Max. :101.0 Max. :5.000
## Region Claim
## Length:409 Min. : 1262
## Class :character 1st Qu.: 3491
## Mode :character Median : 6548
## Mean : 7068
## 3rd Qu.: 9630
## Max. :23046
hist(Group_3$Claim, main = "Histogram of Claim (Group 3)", xlab = "Claim", col = "skyblue", border = "black")
plot(density(Group_3$Claim), main = "Density Plot of Claim (Group 3)", xlab = "Values", col = "skyblue", border = "black")
## Warning in plot.window(...): "border" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "border" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "border" is not a
## graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "border" is not a
## graphical parameter
## Warning in box(...): "border" is not a graphical parameter
## Warning in title(...): "border" is not a graphical parameter
จากกราฟข้างต้นข้อมูลจาก cluster 3 มีการแจงแจกแบบ Log-Normal Distribution
นำข้อมูลจากแต่ละกลุ่มมาเข้า GLM model และหาค่าประสิทธิภาพของโมเดลผ่านค่า AIC
9.1) ชุดข้อมูลจาก cluster 1
G1_model <- Group_1
str(G1_model)
## 'data.frame': 410 obs. of 11 variables:
## $ Cluster : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
## $ PatientID : num 1 2 8 9 11 12 13 17 22 23 ...
## $ Age : num 39 24 19 20 36 37 19 35 34 18 ...
## $ Gender : chr "male" "male" "male" "male" ...
## $ BMI : num 23.2 30.1 41.1 43 19.8 20.3 20.7 34.1 31.1 35.5 ...
## $ Bloodpressure: num 91 87 100 86 88 90 81 90 96 100 ...
## $ Diabetic : chr "Yes" "No" "No" "No" ...
## $ Children : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Smoker : chr "No" "No" "No" "No" ...
## $ Region : chr "southeast" "southeast" "northwest" "northwest" ...
## $ Claim : num 1122 1132 1147 1149 1242 ...
G1_model$PatientID <- NULL
G1_model$Cluster <- NULL
str(G1_model)
## 'data.frame': 410 obs. of 9 variables:
## $ Age : num 39 24 19 20 36 37 19 35 34 18 ...
## $ Gender : chr "male" "male" "male" "male" ...
## $ BMI : num 23.2 30.1 41.1 43 19.8 20.3 20.7 34.1 31.1 35.5 ...
## $ Bloodpressure: num 91 87 100 86 88 90 81 90 96 100 ...
## $ Diabetic : chr "Yes" "No" "No" "No" ...
## $ Children : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Smoker : chr "No" "No" "No" "No" ...
## $ Region : chr "southeast" "southeast" "northwest" "northwest" ...
## $ Claim : num 1122 1132 1147 1149 1242 ...
## delete "smoker"
G1_model_glm <- glm(Claim ~ Age + Gender + BMI + Bloodpressure + Diabetic + Children + Region, data = G1_model)
summary(G1_model_glm) ## AIC: 7834.2
##
## Call:
## glm(formula = Claim ~ Age + Gender + BMI + Bloodpressure + Diabetic +
## Children + Region, data = G1_model)
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 14281.456 2659.134 5.371 1.33e-07 ***
## Age -53.624 27.386 -1.958 0.050917 .
## Gendermale -260.529 331.763 -0.785 0.432752
## BMI 94.659 25.400 3.727 0.000222 ***
## Bloodpressure -84.384 26.516 -3.182 0.001574 **
## DiabeticYes 2.554 305.906 0.008 0.993343
## Children 722.177 127.971 5.643 3.16e-08 ***
## Regionnorthwest -3171.977 531.449 -5.969 5.28e-09 ***
## Regionsoutheast -3250.269 527.032 -6.167 1.70e-09 ***
## Regionsouthwest -2859.046 546.224 -5.234 2.68e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for gaussian family taken to be 9405989)
##
## Null deviance: 4749433604 on 409 degrees of freedom
## Residual deviance: 3762395759 on 400 degrees of freedom
## AIC: 7758.7
##
## Number of Fisher Scoring iterations: 2
summary(G1_model)
## Age Gender BMI Bloodpressure
## Min. :18.0 Length:410 Min. :16.00 Min. : 80.00
## 1st Qu.:25.0 Class :character 1st Qu.:25.73 1st Qu.: 85.25
## Median :29.0 Mode :character Median :30.25 Median : 90.00
## Mean :28.8 Mean :30.41 Mean : 89.79
## 3rd Qu.:33.0 3rd Qu.:34.17 3rd Qu.: 94.00
## Max. :41.0 Max. :46.50 Max. :101.00
## Diabetic Children Smoker Region
## Length:410 Min. :0.000 Length:410 Length:410
## Class :character 1st Qu.:0.000 Class :character Class :character
## Mode :character Median :1.000 Mode :character Mode :character
## Mean :1.068
## 3rd Qu.:2.000
## Max. :5.000
## Claim
## Min. : 1122
## 1st Qu.: 3066
## Median : 5267
## Mean : 5893
## 3rd Qu.: 8230
## Max. :18903
Claim_G1 <- G1_model$Claim
# Calculate sample variance
Var_G1 <- var(Claim_G1)
Var_G1
## [1] 11612307
9.2) ชุดข้อมูลจาก cluster 2
G2_model <- Group_2
str(G2_model)
## 'data.frame': 285 obs. of 11 variables:
## $ Cluster : Factor w/ 3 levels "1","2","3": 2 2 2 2 2 2 2 2 2 2 ...
## $ PatientID : num 480 499 536 590 591 605 606 614 622 625 ...
## $ Age : num 35 31 37 37 38 34 45 36 39 42 ...
## $ Gender : chr "male" "female" "female" "female" ...
## $ BMI : num 16.8 17.3 35.3 33.4 23.4 25.3 26.6 33.2 27.2 20.4 ...
## $ Bloodpressure: num 98 100 100 100 96 100 99 98 95 97 ...
## $ Diabetic : chr "No" "No" "No" "No" ...
## $ Children : num 2 2 0 1 3 0 0 3 0 3 ...
## $ Smoker : chr "No" "No" "No" "No" ...
## $ Region : chr "southeast" "southeast" "southwest" "southeast" ...
## $ Claim : num 6641 6878 7348 8241 8252 ...
G2_model$PatientID <- NULL
G2_model$Cluster <- NULL
str(G2_model)
## 'data.frame': 285 obs. of 9 variables:
## $ Age : num 35 31 37 37 38 34 45 36 39 42 ...
## $ Gender : chr "male" "female" "female" "female" ...
## $ BMI : num 16.8 17.3 35.3 33.4 23.4 25.3 26.6 33.2 27.2 20.4 ...
## $ Bloodpressure: num 98 100 100 100 96 100 99 98 95 97 ...
## $ Diabetic : chr "No" "No" "No" "No" ...
## $ Children : num 2 2 0 1 3 0 0 3 0 3 ...
## $ Smoker : chr "No" "No" "No" "No" ...
## $ Region : chr "southeast" "southeast" "southwest" "southeast" ...
## $ Claim : num 6641 6878 7348 8241 8252 ...
G2_model_glm <- glm(Claim ~ Age + Gender + BMI + Bloodpressure + Diabetic + Children + Smoker + Region, data = G2_model, family = gaussian(link = "log"))
summary(G2_model_glm) ## AIC: 6478.8
##
## Call:
## glm(formula = Claim ~ Age + Gender + BMI + Bloodpressure + Diabetic +
## Children + Smoker + Region, family = gaussian(link = "log"),
## data = G2_model)
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9.909876 0.207517 47.755 < 2e-16 ***
## Age 0.001784 0.001472 1.212 0.226693
## Gendermale 0.028428 0.027828 1.022 0.307889
## BMI 0.006785 0.002842 2.388 0.017635 *
## Bloodpressure -0.007034 0.001801 -3.906 0.000118 ***
## DiabeticYes 0.002713 0.027302 0.099 0.920906
## Children 0.007573 0.010710 0.707 0.480109
## SmokerYes 0.372533 0.032051 11.623 < 2e-16 ***
## Regionnorthwest 0.003369 0.036984 0.091 0.927483
## Regionsoutheast -0.021960 0.037383 -0.587 0.557393
## Regionsouthwest -0.041213 0.037736 -1.092 0.275726
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for gaussian family taken to be 11198871)
##
## Null deviance: 5301722508 on 284 degrees of freedom
## Residual deviance: 3068480004 on 274 degrees of freedom
## AIC: 5447.5
##
## Number of Fisher Scoring iterations: 6
summary(G2_model)
## Age Gender BMI Bloodpressure
## Min. :19.00 Length:285 Min. :16.80 Min. : 80.00
## 1st Qu.:30.00 Class :character 1st Qu.:25.20 1st Qu.: 95.00
## Median :37.00 Mode :character Median :28.50 Median :100.00
## Mean :36.94 Mean :29.18 Mean : 99.14
## 3rd Qu.:44.00 3rd Qu.:33.10 3rd Qu.:105.00
## Max. :60.00 Max. :46.80 Max. :110.00
## Diabetic Children Smoker Region
## Length:285 Min. :0.00 Length:285 Length:285
## Class :character 1st Qu.:0.00 Class :character Class :character
## Mode :character Median :1.00 Mode :character Mode :character
## Mean :1.07
## 3rd Qu.:2.00
## Max. :5.00
## Claim
## Min. : 6641
## 1st Qu.:11188
## Median :13881
## Mean :14820
## 3rd Qu.:18805
## Max. :23401
Claim_G2 <- G2_model$Claim
# Calculate sample variance
Var_G2 <- var(Claim_G2)
Var_G2
## [1] 18668037
9.3) ชุดข้อมูลจาก cluster 3
G3_model <- Group_3
str(G3_model)
## 'data.frame': 409 obs. of 11 variables:
## $ Cluster : Factor w/ 3 levels "1","2","3": 3 3 3 3 3 3 3 3 3 3 ...
## $ PatientID : num 18 19 20 21 24 25 29 32 34 35 ...
## $ Age : num 41 49 48 45 42 50 58 49 52 43 ...
## $ Gender : chr "male" "male" "male" "male" ...
## $ BMI : num 34.4 35.4 33.3 23.2 36.9 20.8 31.1 21.8 36.9 38.2 ...
## $ Bloodpressure: num 84 97 91 85 93 85 87 80 81 86 ...
## $ Diabetic : chr "No" "Yes" "Yes" "Yes" ...
## $ Children : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Smoker : chr "No" "No" "No" "No" ...
## $ Region : chr "southwest" "southwest" "southeast" "southeast" ...
## $ Claim : num 1262 1263 1392 1515 1534 ...
G3_model$PatientID <- NULL
G3_model$Cluster <- NULL
str(G3_model)
## 'data.frame': 409 obs. of 9 variables:
## $ Age : num 41 49 48 45 42 50 58 49 52 43 ...
## $ Gender : chr "male" "male" "male" "male" ...
## $ BMI : num 34.4 35.4 33.3 23.2 36.9 20.8 31.1 21.8 36.9 38.2 ...
## $ Bloodpressure: num 84 97 91 85 93 85 87 80 81 86 ...
## $ Diabetic : chr "No" "Yes" "Yes" "Yes" ...
## $ Children : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Smoker : chr "No" "No" "No" "No" ...
## $ Region : chr "southwest" "southwest" "southeast" "southeast" ...
## $ Claim : num 1262 1263 1392 1515 1534 ...
G3_model_glm <- glm(Claim ~ Age + Gender + BMI + Bloodpressure + Diabetic + Children + Smoker + Region, data = G3_model, family = gaussian(link = "log"))
summary(G3_model_glm) ## AIC: 4680.7
##
## Call:
## glm(formula = Claim ~ Age + Gender + BMI + Bloodpressure + Diabetic +
## Children + Smoker + Region, family = gaussian(link = "log"),
## data = G3_model)
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9.413171 0.455465 20.667 < 2e-16 ***
## Age 0.003156 0.004179 0.755 0.450511
## Gendermale -0.123669 0.058885 -2.100 0.036342 *
## BMI 0.014320 0.004361 3.284 0.001116 **
## Bloodpressure -0.010475 0.004554 -2.300 0.021951 *
## DiabeticYes -0.095996 0.049272 -1.948 0.052081 .
## Children 0.071911 0.018403 3.908 0.000110 ***
## SmokerYes 0.855870 0.075846 11.284 < 2e-16 ***
## Regionnorthwest -0.271424 0.069201 -3.922 0.000103 ***
## Regionsoutheast -0.376662 0.069597 -5.412 1.08e-07 ***
## Regionsouthwest -0.254954 0.070971 -3.592 0.000369 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for gaussian family taken to be 12791602)
##
## Null deviance: 7299379560 on 408 degrees of freedom
## Residual deviance: 5091070807 on 398 degrees of freedom
## AIC: 7866.5
##
## Number of Fisher Scoring iterations: 6
summary(G3_model)
## Age Gender BMI Bloodpressure
## Min. :36.00 Length:409 Min. :16.80 Min. : 80.0
## 1st Qu.:44.00 Class :character 1st Qu.:26.10 1st Qu.: 83.0
## Median :48.00 Mode :character Median :29.80 Median : 88.0
## Mean :48.69 Mean :30.29 Mean : 88.2
## 3rd Qu.:54.00 3rd Qu.:34.30 3rd Qu.: 93.0
## Max. :60.00 Max. :46.70 Max. :101.0
## Diabetic Children Smoker Region
## Length:409 Min. :0.000 Length:409 Length:409
## Class :character 1st Qu.:0.000 Class :character Class :character
## Mode :character Median :1.000 Mode :character Mode :character
## Mean :1.112
## 3rd Qu.:2.000
## Max. :5.000
## Claim
## Min. : 1262
## 1st Qu.: 3491
## Median : 6548
## Mean : 7068
## 3rd Qu.: 9630
## Max. :23046
Claim_G3 <- G3_model$Claim
# Calculate sample variance
Var_G3 <- var(Claim_G3)
Var_G3
## [1] 17890636