-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiabetes_prediction.Rmd
340 lines (280 loc) · 11 KB
/
diabetes_prediction.Rmd
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
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
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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
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
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
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
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
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
332
333
334
335
336
337
338
---
title: "Diabetes prediction"
author: "Xin Bu"
date: "2023-11-15"
output: html_document
html_document:
toc: true
toc_depth: 6
number_sections: true
toc_float: true
code_folding: hide
theme: flatly
code_download: true
---
-------------
### Introduction
The dataset used in this project is originally from National Institute of Diabetes and Digestive and Kidney Diseases (NIDDK). This project aims at building a logistic regression model to predict diabetes, based on certain diagnostic measurements. The predictors are BMI, insulin level, age, pregnancies, glucose plasma, blood pressure, skin thickness, and diabetes pedigree function. The outcome variable is a binary one indicating whether or not the patients have diabetes.The resampling method Monte Carlo simulation is used to cross-validate the model.
```{r setup, include=FALSE}
library(tidyverse)
library(dplyr)
library(ggcorrplot)
library(pROC)
library(naniar)
library(knitr)
knitr::opts_chunk$set(echo = TRUE)
```
#### Load data
```{r}
db <- read_csv("health care diabetes.csv")
head(db)
```
#### Check the vlaue of the two levels of the binary outcome variable
```{r}
unique(db$Outcome)
```
#### Visually check if the data have any missing values
```{r}
vis_miss(db)
```
### Correlation
#### Detect correlations between the variables
```{r}
correlation <- cor(db[,1:9])
ggcorrplot(correlation,
hc.order = TRUE,
type = "lower",
outline.color = "white",
ggtheme = ggplot2::theme_gray,
colors = c("#6D9EC1","white","#E46726"),
lab = TRUE)
```
#### Pairwise correlation
```{r}
pairs(db,
cex.labels = 0.8,
col = c("#E46726","#6D9EC1"),
pch = 21,
main = "Pairwise Correlation")
```
#### Split the data to train and test the model
```{r}
set.seed(11162023)
train_indices <- sample(1:nrow(db), 0.7*nrow(db))
train_data <- db[train_indices,]
test_data <- db[-train_indices,]
```
#### Train the logistic regression model
```{r}
model <- glm(Outcome ~ ., family = binomial, data=train_data)
summary(model)
```
#### Remove the variable Bloodpressure to see if there are any differences
```{r}
model_2 <- glm(Outcome ~ ., family = binomial, data=train_data[-3])
summary(model_2)
```
#### Optimize the model using Akaike Information Criterion (AIC). The step() function found the most efficient model with the lowest AIC.
```{r}
aic_model <- step(model)
summary(aic_model)
```
#### According to the AIC score, the variable Skinthickness should be excluded to make an efficient model. Test the data and calculate the accuracy of the AIC model.
```{r}
predictions <- predict(aic_model,
type = "response",
newdata = test_data[-4])
predicted_db <- ifelse(predictions > 0.5, 1, 0)
actual_db <- test_data$Outcome
accuracy <- mean(predicted_db == actual_db)
accuracy <- round(accuracy*100,2)
print(accuracy)
coefficients <- coef(aic_model)
print(coefficients)
print(predicted_db)
print(actual_db)
```
### Cross-validation of the model
#### Monte Carlo Simulation
```{r}
db_1 <- subset(db, select = -c(SkinThickness))
lg <- replicate(100, {
index <- sample(1:nrow(db), 0.7*nrow(db_1))
train_data <- db_1%>% slice(index)
test_data <- db_1%>% slice(-index)
model <- glm(Outcome~.,
family = binomial,
data=train_data)
predicted_db_1 <- predict(model,
type = "response",
newdata= test_data)
predicted_classes <- ifelse(predicted_db_1 > 0.5, 1,0)
corrected_predictions <- sum(predicted_classes == test_data$Outcome)
total_predictions <- length(predicted_classes)
accuracy <- corrected_predictions/total_predictions
return(accuracy)
})
paste("Cross-validated accuracy of the model:", round(mean(lg),4)*100)
```
### Visualize the data
#### Pregnancies plot
```{r}
ggplot() +
geom_point(aes(db_1$Pregnancies, db_1$Outcome)) +
geom_smooth(aes(db_1$Pregnancies, db_1$Outcome),
method = "glm", se = FALSE, method.args = list(family = "binomial"),
color = "blue", linewidth = 1.2)+
ggtitle("Diabetes Diagnosis from Pregnancies") +
ylab("Diabetes Diagnosis") +
xlab("Pregnancies") +
scale_y_continuous(breaks = c(0, 1), labels = c("No", "Yes")) +
theme(plot.title = element_text(size = 20, face="bold",
margin = margin(10,0,10,0)),
plot.background = element_rect(fill = "#E46726"),
panel.background = element_rect(fill = "#6D9EC1"),
axis.text.x = element_text(size = 12),
axis.title.x = element_text(size = 15, margin = margin(11,0,10,0)),
axis.text.y = element_text(size = 12),
axis.title.y = element_text(size = 15, margin=margin(0,10,0,11)),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.margin = margin(0,0.5,0,0, "cm"))
```
#### Glucose plot
```{r}
ggplot() +
geom_point(aes(db_1$Glucose, db_1$Outcome)) +
geom_smooth(aes(db_1$Glucose, db_1$Outcome),
method = "glm", se = FALSE, method.args = list(family = "binomial"),
color = "blue", linewidth = 1.2) +
ggtitle("Diabetes Diagnosis from Glucose") +
ylab("Diabetes Diagnosis") +
xlab("Glucose") +
scale_y_continuous(breaks = c(0, 1), labels = c("No", "Yes")) +
theme(plot.title = element_text(size = 20, face="bold",
margin = margin(10,0,10,0)),
plot.background = element_rect(fill = "#E46726"),
panel.background = element_rect(fill = "#6D9EC1"),
axis.text.x = element_text(size = 12),
axis.title.x = element_text(size = 15, margin = margin(11,0,10,0)),
axis.text.y = element_text(size = 12),
axis.title.y = element_text(size = 15, margin=margin(0,10,0,11)),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.margin = margin(0,0.5,0,0, "cm"))
```
#### BloodPressure plot
```{r}
ggplot() +
geom_point(aes(db_1$BloodPressure, db_1$Outcome)) +
geom_smooth(aes(db_1$BloodPressure, db_1$Outcome),
method = "glm", se = FALSE, method.args = list(family = "binomial"),
color = "blue", linewidth = 1.2) +
ggtitle("Diabetes Diagnosis from BloodPressure") +
ylab("Diabetes Diagnosis") +
xlab("BloodPressure") +
scale_y_continuous(breaks = c(0, 1), labels = c("No", "Yes")) +
theme(plot.title = element_text(size = 20, face="bold",
margin = margin(10,0,10,0)),
plot.background = element_rect(fill = "#E46726"),
panel.background = element_rect(fill = "#6D9EC1"),
axis.text.x = element_text(size = 12),
axis.title.x = element_text(size = 15, margin = margin(11,0,10,0)),
axis.text.y = element_text(size = 12),
axis.title.y = element_text(size = 15, margin=margin(0,10,0,11)),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.margin = margin(0,0.5,0,0, "cm"))
```
#### Insulin plot
```{r}
ggplot() +
geom_point(aes(db_1$Insulin, db_1$Outcome)) +
geom_smooth(aes(db_1$Insulin, db_1$Outcome),
method = "glm", se = FALSE, method.args = list(family = "binomial"),
color = "blue", linewidth = 1.2) +
ggtitle("Diabetes Diagnosis from Insulin") +
ylab("Diabetes Diagnosis") +
xlab("Insulin") +
scale_y_continuous(breaks = c(0, 1), labels = c("No", "Yes")) +
theme(plot.title = element_text(size = 20, face="bold",
margin = margin(10,0,10,0)),
plot.background = element_rect(fill = "#E46726"),
panel.background = element_rect(fill = "#6D9EC1"),
axis.text.x = element_text(size = 12),
axis.title.x = element_text(size = 15, margin = margin(11,0,10,0)),
axis.text.y = element_text(size = 12),
axis.title.y = element_text(size = 15, margin=margin(0,10,0,11)),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.margin = margin(0,0.5,0,0, "cm"))
```
#### BMI plot
```{r}
ggplot() +
geom_point(aes(db_1$BMI, db_1$Outcome)) +
geom_smooth(aes(db_1$BMI, db_1$Outcome),
method = "glm", se = FALSE, method.args = list(family = "binomial"),
color = "blue", linewidth = 1.2) +
ggtitle("Diabetes Diagnosis from BMI") +
ylab("Diabetes Diagnosis") +
xlab("BMI") +
scale_y_continuous(breaks = c(0, 1), labels = c("No", "Yes")) +
theme(plot.title = element_text(size = 20, face="bold",
margin = margin(10,0,10,0)),
plot.background = element_rect(fill = "#E46726"),
panel.background = element_rect(fill = "#6D9EC1"),
axis.text.x = element_text(size = 12),
axis.title.x = element_text(size = 15, margin = margin(11,0,10,0)),
axis.text.y = element_text(size = 12),
axis.title.y = element_text(size = 15, margin=margin(0,10,0,11)),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.margin = margin(0,0.5,0,0, "cm"))
```
#### DiabetesPedigreeFunction plot
```{r}
ggplot() +
geom_point(aes(db_1$DiabetesPedigreeFunction, db_1$Outcome)) +
geom_smooth(aes(db_1$DiabetesPedigreeFunction, db_1$Outcome),
method = "glm", se = FALSE, method.args = list(family = "binomial"),
color = "blue", linewidth = 1.2) +
ggtitle("Diabetes Diagnosis from DiabetesPedigreeFunction") +
ylab("Diabetes Diagnosis") +
xlab("DiabetesPedigreeFunction") +
scale_y_continuous(breaks = c(0, 1), labels = c("No", "Yes")) +
theme(plot.title = element_text(size = 18, face="bold",
margin = margin(10,0,10,0)),
plot.background = element_rect(fill = "#E46726"),
panel.background = element_rect(fill = "#6D9EC1"),
axis.text.x = element_text(size = 12),
axis.title.x = element_text(size = 15, margin = margin(11,0,10,0)),
axis.text.y = element_text(size = 12),
axis.title.y = element_text(size = 15, margin=margin(0,10,0,11)),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.margin = margin(0,0.5,0,0, "cm"))
```
#### Age plot
```{r}
ggplot() +
geom_point(aes(db_1$Age, db_1$Outcome)) +
geom_smooth(aes(db_1$Age, db_1$Outcome),
method = "glm", se = FALSE, method.args = list(family = "binomial"),
color = "blue", linewidth = 1.2) +
ggtitle("Diabetes Diagnosis from Age") +
ylab("Diabetes Diagnosis") +
xlab("Age") +
scale_y_continuous(breaks = c(0, 1), labels = c("No", "Yes")) +
theme(plot.title = element_text(size = 20, face="bold",
margin = margin(10,0,10,0)),
plot.background = element_rect(fill = "#E46726"),
panel.background = element_rect(fill = "#6D9EC1"),
axis.text.x = element_text(size = 12),
axis.title.x = element_text(size = 15, margin = margin(11,0,10,0)),
axis.text.y = element_text(size = 12),
axis.title.y = element_text(size = 15, margin=margin(0,10,0,11)),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.margin = margin(0,0.5,0,0, "cm"))
```