-
Notifications
You must be signed in to change notification settings - Fork 0
/
DD_DC_EDA.Rmd
535 lines (391 loc) · 16.9 KB
/
DD_DC_EDA.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
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
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
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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
---
title: "EDA"
author: "Dmitrii Zhakota"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(readxl)
# library(openxlsx)
library(dlookr)
# library(dplyr)
library(flextable)
library(ggstatsplot)
library(tidyverse)
```
# Load modified data v2
```{r}
# Переименованы колонки. Добавлен префикс Before_ для группы Präoperativ After_ для группы Postoperativ FUEcho_ для FUEcho.
# В excel сопоставлены признаки STERBEDAT, LAST_FU и FUEcho_DATUM.
# Заменены в STERBEDAT unbekannt на пропуски, т.к. в признаке VERSTORBEN они все закодированы как 1.
# Заменены в STERBEDAT значения в формате YYYY на формат даты 31.12.YYYY. Item с такими изменениями сохранены во вкладке mod. Оставшиеся unbekannt заменены на пропуски.
# Переназначены ячейки столбцов, в которых числя записаны как текст
# Заменены в числах запятые на точки.
df_read_excel <- read_excel("data/raw/Epic_Supra_V_1_mod_2.xlsx",
col_types = c("numeric", "date", "numeric",
"numeric", "date", "numeric", "date",
"text", "numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "text", "date", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "text", "numeric", "numeric",
"numeric", "text", "text", "numeric",
"text", "numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "text", "numeric", "text",
"date", "numeric", "text", "date", "numeric",
"text", "date", "numeric", "text", "date",
"numeric", "text", "date", "numeric", "text",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric", "numeric",
"numeric", "numeric", "numeric", "numeric",
"numeric", "numeric", "numeric", "numeric",
"numeric", "date"))
Epic_Supra_dates_v2 <- read_excel("data/raw/Epic_Supra_dates_v2.xlsx")
```
# Replace date columns
```{r}
# Copy and replace columns from Epic_Supra_dates_v2 to df_read_excel
df <- df_read_excel
df$LAST_FU <- Epic_Supra_dates_v2$LAST_FU
df$FUEcho_DATUM <- Epic_Supra_dates_v2$FUEcho_DATUM
df$STERBEDAT <- Epic_Supra_dates_v2$STERBEDAT
# df$STERBEDAT_unbekant <- Epic_Supra_dates_v2$STERBEDAT_unbekant
# df$STERBEDAT_only_year <- Epic_Supra_dates_v2$STERBEDAT_only_year
# df$LAST_FU_only_year <- Epic_Supra_dates_v2$LAST_FU_only_year
```
# Rename and Recode
```{r}
rename <- read.table(file = 'data/raw/Column_Renamer.tsv', sep = '\t', header = TRUE)
# Convert to vector column English_Column_Name_Short for rename
English_Column_Name_Short <- as.vector(rename$English_Column_Name_Short)
# Rename columns names in df_read_excel with vector English_Column_Name_Short
df_en <- df %>%
rename_with(~English_Column_Name_Short, everything())
```
# Data preparation
```{r}
# Сопоставляем колонки с датами, которые принимаются за конечную точку.
# df_followup <- df_read_excel %>%
# select(Item, VERSTORBEN, STERBEDAT, LAST_FU, FUEcho_DATUM)
df_followup <- df_en %>%
select(Case_Num, OS_Event, OS_Date, Last_FU, FUEcho_Date)
# Common rename df.
df <- df_en
# Перекодировать значения в колонке GESCHLECHT в 1 и 2. 1 - M, 2 - W. Будет 1 - male 2 - female
df <- df %>%
mutate(Gender = ifelse(Gender == "M", 1, 2))
# Round all numeric columns
df <- df %>%
mutate_if(is.numeric, round, digits = 2)
# !!!! Потребуется если работать с первичным файлом. В файле mod_2 уже исправлено в excel !!!
# if column is numeric then replace , to .
# df <- df %>%
# mutate_if(is.numeric, ~gsub(",", ".", .x)) %>%
# mutate_if(is.numeric, as.numeric)
# Удаляем нулевые значения
# df <- df %>%
# filter(BMI != 0)
# Перекодируем даты из YYYY-MM-DD в DD.MM.YYYY. Проблема - превращает в формат character.
# df <- df %>%
# mutate(GEBURT = format(as.Date(GEBURT, format = "%Y-%m-%d"), "%d.%m.%Y"))
#
# df <- df %>%
# mutate(STERBEDAT = format(as.Date(STERBEDAT, format = "%Y-%m-%d"), "%d.%m.%Y"))
#
# df <- df %>%
# mutate(OP_DATUM = format(as.Date(OP_DATUM, format = "%Y-%m-%d"), "%d.%m.%Y"))
#
# df <- df %>%
# mutate(FUEcho_DATUM = format(as.Date(FUEcho_DATUM, format = "%Y-%m-%d"), "%d.%m.%Y"))
```
# Diagnosis Data
```{r}
df %>%
diagnose() %>%
flextable()
df %>%
plot_na_pareto()
df %>%
summary()
# Удаляем лишние датафреймы
# rm("df_read_excel")
```
# Data clearning
```{r}
# Привести к факторам или числам
factors_auto <- df %>% select_if(function(x) {all(unique(x) %in% c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, NA))}) %>% colnames()
factors_list <- c("Case_Num", "Gender")
df %>% mutate(across(
c(all_of(factors_auto), all_of(factors_list)), as.factor
)) -> df1
# Convert columns if class is POSIXct or POSIXt to date
# df1 <- df1 %>%
# mutate(across(c(GEBURT, STERBEDAT, LAST_FU, OP_DATUM, REOP_DAT, REOP_DAT2, REOP_DAT3, REOP_DAT4, REOP_DAT5, FUEcho_DATUM), ~ as.Date(. )))
df1 <- df1 %>%
mutate(across(c(Birth_Date, OS_Date, Last_FU, Sx_Date, FUEcho_Date), ~ as.Date(. )))
class(df1$Birth_Date)
#Remove columns where date class
# df1_no_date <- df1 %>%
# select(-c(GEBURT, STERBEDAT, LAST_FU, OP_DATUM, REOP_DAT, REOP_DAT2, REOP_DAT3, REOP_DAT4, REOP_DAT5, FUEcho_DATUM))
df1_no_date <- df1 %>%
select(-c(Birth_Date, OS_Date, Last_FU, Sx_Date, FUEcho_Date))
# Delete columns if more than 50% of values are NA
df1_no_na_col <- df1_no_date %>% select_if(~sum(!is.na(.))/length(.) >= 0.5)
```
# Save data frames to interim folder
```{r}
# Сохраняем датафреймы
write.csv(df_read_excel, "data/interim/df_read_excel.csv", row.names = FALSE) # Без изменений после загрузки из excel
write.csv(df_en, "data/interim/df_en.csv", row.names = FALSE) # Переимнование колонок в English_Column_Name_Short
write.csv(df, "data/interim/df.csv", row.names = FALSE) # Удаление лишних колонок и перекодировка пола
write.csv(df1, "data/interim/df1.csv", row.names = FALSE) # Назначены классы признакам (фактор, дата, количественный)
write.csv(df1_no_date, "data/interim/df1_no_date.csv", row.names = FALSE) # Удалены все даты
write.csv(df1_no_na_col, "data/interim/df1_no_na_col.csv", row.names = FALSE) # Удалены все даты и колонки с NA > 50%
# Удаляем полностью среду
rm(list=ls())
```
#Pre-report EDA in dlookr
```{r eval=FALSE, include=FALSE}
read.csv("data/interim/df1_no_na_col.csv") -> df1_no_na_col
factors_auto <- df1_no_na_col %>% select_if(function(x) {all(unique(x) %in% c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, NA))}) %>% colnames()
factors_list <- c("Case_Num", "Gender")
df1_no_na_col %>% mutate(across(
c(all_of(factors_auto), all_of(factors_list)), as.factor
)) -> df1_no_na_col
read.csv("data/interim/df1_no_date.csv") -> df1_no_date
factors_auto <- df1_no_date %>% select_if(function(x) {all(unique(x) %in% c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, NA))}) %>% colnames()
factors_list <- c("Case_Num", "Gender")
df1_no_date %>% mutate(across(
c(all_of(factors_auto), all_of(factors_list)), as.factor
)) -> df1_no_date
# Отчёт по датафрейму со всеми признаками
diagnose_web_report(df1_no_date,
output_file = "Diagn_df1.html",
title = "Data Diagnosis Web",
subtitle = deparse(substitute(.data))
)
eda_web_report(df1_no_date,
output_file = "EDA_df1.html",
title = "Exploratory Data Analysis",
subtitle = deparse(substitute(.data))
)
#######################################################################################################################
# Отчёт по датафрейму после удаления признаков, в которых больше 50% пропущенных значений ######
#######################################################################################################################
diagnose_web_report(df1_no_na_col,
output_file = "Diagn_df1_no_na_col.html",
title = "Data Diagnosis",
subtitle = deparse(substitute(.data))
)
eda_web_report(df1_no_na_col,
output_file = "EDA_df1_no_na_col.html",
title = "Exploratory Data Analysis",
subtitle = deparse(substitute(.data))
)
```
# Static report in dlookr !!! OPTION !!!
```{r eval=FALSE, include=FALSE}
# Статичтные отчёты Diagos Data
diagnose_paged_report(df1_no_date,
output_format = "html",
output_file = "Diagn_df1.html",
output_dir = "."
)
diagnose_paged_report(df1_no_na_col,
output_file = "Diagn_df1_no_na_col.html",
output_format = "html",
output_dir = "."
)
# Статичные отчёты EDA
eda_paged_report(df1_no_date,
output_file = "EDA_df1.html",
output_format = "html",
output_dir = "."
)
eda_paged_report(df1_no_na_col,
output_file = "EDA_df1_no_na_col.html",
output_format = "html",
output_dir = "."
)
# diagnose_paged_report(df1_no_na_col,
# output_dir = "./data/interim/",
# output_format = "html")
#
# diagnose_paged_report(df1_no_na_col,
# output_format = "pdf")
```
# Exploratory Data Analysis
## Descriptive statistics
```{r}
rm(list=ls())
df <- read.csv("data/interim/df1.csv")
# Привести к факторам или числам
factors_auto <- df %>% select_if(function(x) {all(unique(x) %in% c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, NA))}) %>% colnames()
factors_list <- c("Case_Num", "Gender")
df %>% mutate(across(
c(all_of(factors_auto), all_of(factors_list)), as.factor
)) -> df
# Convert columns if class is POSIXct or POSIXt to date
# df1 <- df1 %>%
# mutate(across(c(GEBURT, STERBEDAT, LAST_FU, OP_DATUM, REOP_DAT, REOP_DAT2, REOP_DAT3, REOP_DAT4, REOP_DAT5, FUEcho_DATUM), ~ as.Date(. )))
df <- df %>%
mutate(across(c(Birth_Date, OS_Date, Last_FU, Sx_Date, FUEcho_Date), ~ as.Date(. )))
# df1 <- df %>%
# dplyr::filter(!is.na(Last_FU))
df1 <- df %>%
mutate(Gender = recode(Gender, '1' = "Male", '2' = "Female")) %>%
mutate(Upsizing = recode(Upsizing, '0' = "Without upsizing", '1' = "With upsizing"))
#####################################################################################################
# Count of follow-ups by year
ggplot(df1, aes(x = Last_FU)) +
geom_histogram(binwidth = 365) +
scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
theme_bw() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
# Count genger by upsizing
ggbarstats(data = df1,
x = Gender,
y = Upsizing
)
# Count Gender by STS
ggbetweenstats(data = df1,
x = Gender,
y = STS
)
grouped_ggbetweenstats(data = df1,
x = Gender,
y = STS,
grouping.var = Upsizing
)
# Count Gender by Age
ggbetweenstats(data = df1,
x = Gender,
y = Age
)
grouped_ggbetweenstats(data = df1,
x = Gender,
y = Age,
grouping.var = Upsizing
)
ggbarstats(data = df1,
x = Upsizing,
y = Sx_Urgency
)
grouped_ggbarstats(data = df1,
x = Gender,
y = Sx_Urgency,
grouping.var = Upsizing
)
grouped_ggbetweenstats(data = df1,
x = Upsizing,
y = Sx_Aort_Clamp_t,
grouping.var = Gender,
ylab = "Aortic clamp time (min)",
)
```
## Clustering
```{r}
rm(list = ls())
df <- read.csv("data/interim/df1_no_date.csv")
# Привести к факторам или числам
factors_auto <- df %>% select_if(function(x) {all(unique(x) %in% c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, NA))}) %>% colnames()
factors_list <- c("Gender")
df %>% mutate(across(
c(all_of(factors_auto), all_of(factors_list)), as.factor
)) -> df
str(df) # Обязательно проверить присвоение классов. Иногда не срабатывает
df_new <- df %>%
select(where(is.numeric)) %>%
select(-c(Case_Num)) %>% # Удаляем Item, так как он не несёт смысловой нагрузки
mutate_all(round, 1)
# stats::na.omit()
library(pheatmap)
df_new_scaled <- scale(df_new)
df_new_dist <- dist(df_new_scaled,
method = "euclidean"
)
as.matrix(df_new_dist)[1:42,1:42]
pheatmap(df_new_scaled,
scale = "row",
clastering_distance_rows = df_new_dist,
claster_method = "ward.D2",
cutree_rows = 5, # группируем (условно)
cutree_cols = length(colnames(df_new_scaled))
)
```
## PCA
<!-- Проводим PCA анализ, что бы снизить размерность данных. Сравнить с тем, что порекомендует Охотин объединить. В первую очередь это касается ЭхоКГ. -->
```{r}
# Удаляем полностью среду
rm(list=ls())
# Датафрейм только с удалёнными датами содержит много пропуcков. В итоге удаляются все строки.
# read.csv("data/interim/df1_no_date.csv") -> df1_no_date
#
# # # Привести к факторам или числам
# factors_auto <- df1_no_date %>% select_if(function(x) {all(unique(x) %in% c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, NA))}) %>% colnames()
# factors_list <- c("Item", "GESCHLECHT")
#
# df1_no_date %>% mutate(across(
# c(all_of(factors_auto), all_of(factors_list)), as.factor
# )) -> df1_no_date
#
# df1_pca <- df1_no_date %>%
# select(where(is.numeric)) %>%
# # select(-c(MORTALITAT_TAGE)
# mutate_all(round, 1) %>%
# stats::na.omit()
# Датафрейм без дат и без колонок с количеством пропущенных значений больше 50% годится в работу.
read.csv("data/interim/df1_no_na_col.csv") -> df1_no_na_col
# Привести к факторам или числам
factors_auto <- df1_no_na_col %>% select_if(function(x) {all(unique(x) %in% c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, NA))}) %>% colnames()
factors_list <- c("Gender")
df1_no_na_col %>% mutate(across(
c(all_of(factors_auto), all_of(factors_list)), as.factor
)) -> df1_no_na_col
str(df1_no_na_col) # Обязательно проверить присвоение классов. Иногда не срабатывает. Если не сработало - презапустить сессию и запустить этот чанк отдельно.
df1_pca <- df1_no_na_col %>%
select(where(is.numeric)) %>%
# select(-c(MORTALITAT_TAGE)
mutate_all(round, 1) %>%
stats::na.omit()
library(FactoMineR)
df_full.pca <- prcomp(df1_pca,
scale = TRUE)
summary(df_full.pca)
library(factoextra)
fviz_eig(df_full.pca, addlabels = TRUE)
fviz_pca_var(df_full.pca, col.var = "contrib")
fviz_pca_var(df_full.pca,
select.var = list(contrib = 3), # Задаём число здесь
col.var = "contrib")
fviz_contrib(df_full.pca, choice = "var", axes = 1, top = 24) # 1
fviz_contrib(df_full.pca, choice = "var", axes = 2, top = 24) # 2
fviz_contrib(df_full.pca, choice = "var", axes = 3, top = 24) # 3
# PCA с группировкой по UPSIZING
library(ggbiplot)
df0 <- df1_no_na_col %>%
select(c(Case_Num, Upsizing))
df1_pca2 <- left_join(df1_pca, df0, "Case_Num" )
ggbiplot(df_full.pca,
scale=0,
groups = as.factor(df1_pca2$Upsizing),
ellipse = TRUE,
# circle = TRUE,
alpha = 0.2) +
theme_minimal()
```