-
Notifications
You must be signed in to change notification settings - Fork 1
/
exploratory_data_analysis.Rmd
324 lines (250 loc) · 8.98 KB
/
exploratory_data_analysis.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
---
title: "Exploratory Data Analysis"
output: html_notebook
---
# Importing Required Libraries
```{r}
require(tidyverse)
```
# Loading in Data
```{r}
data <- read_csv("../data/diabetes_data_upload.csv")
data
```
```{r}
data %>% select(class) %>% filter(class == "Positive") %>% summarise(n())
```
```{r}
data %>% select(class) %>% filter(class == "Negative") %>% summarise(n())
```
```{r}
data %>%
ggplot(aes(x=class, fill=class)) +
geom_bar(show.legend = FALSE) +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2')) +
theme(panel.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2')) +
labs(title="Incidence of Diabetes", x="Diabetes Class", y="Count")
ggsave("../plots/Diabetes_Incidence.png", dpi = 1200, width = 4, height = 4, units = "in")
```
We have 320 positive and 200 negative patients.
```{r}
colnames(data)
```
The response variable is class, and all other variables are predictors. All predictors are categorical except for Age which is a ordinal variable.
# Correlation Plot
```{r}
# replcaing categorical labels to numeric ones
data_clean <- data.frame(lapply(data, function(x) {
gsub("Yes", "1", x)
}))
data_clean <- data.frame(lapply(data_clean, function(x) {
gsub("No", "0", x)
}))
data_clean <- data.frame(lapply(data_clean, function(x) {
gsub("Positive", "1", x)
}))
data_clean <- data.frame(lapply(data_clean, function(x) {
gsub("Negative", "0", x)
}))
data_clean <- data.frame(lapply(data_clean, function(x) {
gsub("Male", "0", x)
}))
data_clean <- data.frame(lapply(data_clean, function(x) {
gsub("Female", "1", x)
}))
# converting all columns to numeric
data_clean <- data.frame(sapply( data_clean, as.numeric ))
data_clean
write_csv(data_clean, path="../data/clean_numeric_data.csv")
```
```{r}
require(ggcorrplot)
corr <- round(cor(data_clean),2)
corr
```
```{r}
ggcorrplot(corr, method = "square") +
#theme_bw() +
labs(title="Feature Correlation Matrix") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2')) +
theme(panel.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2'))+
theme(legend.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2'))
ggsave("../plots/corrplot.png", dpi = 400, width = 7, height = 8, units = "in")
```
# Find Mean of Ages by Class
```{r}
Age_Mean <- data %>%
group_by(class) %>%
summarize(mean = mean(Age))
```
# Diabetes vs Age
```{r}
data %>%
ggplot(aes(x=Age, group=class)) +
geom_density(aes(fill = class),alpha=0.25) +
geom_vline(data = Age_Mean, aes(xintercept = mean, color = class), size= 0.5, show.legend = FALSE) +
theme_bw() +
labs(title="Diabetes Incidence by Age", fill="Diabetes", x="Age", y="Density") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position="bottom") +
theme(plot.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2')) +
theme(panel.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2'))+
theme(legend.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2'))
ggsave("../plots/Diabetes_Incidence_by_Age.png", dpi = 1200, width = 4, height = 4, units = "in")
```
There does not appear to be a very large difference in diabetes status based on age.
# Diabetes vs Gender
```{r}
data %>%
ggplot(aes(x=Gender, fill=class)) +
geom_bar() +
theme_bw() +
labs(title="Diabetes Incidence by Gender", fill="Diabetes", x="Gender", y="Frequency") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position="bottom") +
theme(plot.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2')) +
theme(panel.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2'))+
theme(legend.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2'))
ggsave("../plots/Diabetes_Incidence_by_Gender.png", dpi = 1200, width = 4, height = 4, units = "in")
```
## chi square test of independence
h0: Gender and diabetes status are independent
h1: not independent
```{r}
table(data$Gender, data$class)
```
```{r}
test <- chisq.test(table(data$Gender, data$class))
test
```
```{r}
test$expected
```
With a p-value < 2.2e-16, we reject our null hypothesis at all significance levels and conclude that gender and diabetes class are not independent.
# Diabetes vs Polyuria
```{r}
data %>%
ggplot(aes(x=Polyuria, fill=class)) +
geom_bar() +
theme_bw() +
labs(title="Diabetes Incidence by Polyuria", fill="Diabetes", x="Polyuria", y="Frequency") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position="bottom") +
theme(plot.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2')) +
theme(panel.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2'))+
theme(legend.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2'))
ggsave("../plots/Diabetes_Incidence_by_Polyuria.png", dpi = 1200, width = 4, height = 4, units = "in")
```
## chi square test of independence
h0: Polyuria and diabetes status are independent
h1: not independent
```{r}
table(data$Polyuria, data$class)
```
```{r}
test <- chisq.test(table(data$Polyuria, data$class))
test
```
```{r}
test$expected
```
With a p-value < 2.2e-16, we reject our null hypothesis at all significance levels and conclude that polyuria and diabetes class are not independent.
# Diabetes vs Polydipsia
```{r}
data %>%
ggplot(aes(x=Polydipsia, fill=class)) +
geom_bar() +
theme_bw() +
labs(title="Diabetes Incidence by Polydipsia", fill="Diabetes", x="Polydipsia", y="Frequency") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position="bottom") +
theme(plot.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2')) +
theme(panel.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2'))+
theme(legend.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2'))
ggsave("../plots/Diabetes_Incidence_by_Polydipsia.png", dpi = 1200, width = 4, height = 4, units = "in")
```
## chi square test of independence
h0: Polydipsia and diabetes status are independent
h1: not independent
```{r}
table(data$Polydipsia, data$class)
```
```{r}
test <- chisq.test(table(data$Polydipsia, data$class))
test
```
```{r}
test$expected
```
With a p-value < 2.2e-16, we reject our null hypothesis at all significance levels and conclude that polydipsia and diabetes class are not independent.
# Diabetes vs sudden.weight.loss
```{r}
data %>%
ggplot(aes(x=`sudden weight loss`, fill=class)) +
geom_bar() +
theme_bw() +
labs(title="Diabetes Incidence by Sudden Weight Loss", fill="Diabetes", x="Sudden Weight Loss", y="Frequency") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position="bottom") +
theme(plot.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2')) +
theme(panel.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2'))+
theme(legend.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2'))
ggsave("../plots/Diabetes_Incidence_by_Sudden_Weight_Loss.png", dpi = 1200, width = 4, height = 4, units = "in")
```
## chi square test of independence
h0: Sudden weight loss and diabetes status are independent
h1: not independent
```{r}
table(data$`sudden weight loss`, data$class)
```
```{r}
test <- chisq.test(table(data$`sudden weight loss`, data$class))
test
```
```{r}
test$expected
```
With a p-value < 2.2e-16, we reject our null hypothesis at all significance levels and conclude that sudden weight loss and diabetes class are not independent.
# Diabetes vs Partial Paresis
```{r}
data %>%
ggplot(aes(x=`partial paresis`, fill=class)) +
geom_bar() +
theme_bw() +
labs(title="Diabetes Incidence by Partial Paresis", fill="Diabetes", x="Partial Paresis", y="Frequency") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position="bottom") +
theme(plot.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2')) +
theme(panel.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2')) +
theme(legend.background = element_rect(fill = '#f2f2f2', colour = '#f2f2f2'))
ggsave("../plots/Diabetes_Incidence_by_Partial_Paresis.png", dpi = 1200, width = 4, height = 4, units = "in")
```
## chi square test of independence
h0: Partial paresis and diabetes status are independent
h1: not independent
```{r}
table(data$`partial paresis`, data$class)
```
```{r}
test <- chisq.test(table(data$`partial paresis`, data$class))
test
```
```{r}
test$expected
```
With a p-value < 2.2e-16, we reject our null hypothesis at all significance levels and conclude that partial paresis and diabetes class are not independent.
```{r}
chi2 <- c()
variable <- c()
for (i in 2:(ncol(data)-1)){
statistic <- chisq.test(table(data[[i]], data[[17]]))$statistic
name <- colnames(data)[[i]]
chi2 <- c(chi2, statistic)
variable <- c(variable, name)
}
d <- data.frame(variable, chi2)
d %>% arrange(desc(chi2))
```