-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path002_Data_exploration.R
146 lines (123 loc) · 5.02 KB
/
002_Data_exploration.R
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
library(tidyverse)
library(markovchain)
library(corrplot)
source("001_data_import.R")
df <- import_data()
# ----------- histogram of signs ---------------------
len <- length(df$ID)
concat_symptoms = tibble(type = c(rep('dryness', len), rep('itching', len), rep('sleep', len), rep('redness', len), rep('oozing', len), rep('edema', len)),
value = c(df$dry, df$itching, df$sleep, df$redness, df$oozing, df$edema))
stats_symptoms <- concat_symptoms %>%
group_by(type, value) %>%
summarize(count = n()) %>%
group_by(type) %>%
mutate(density = count / sum(count))
ggplot(data = stats_symptoms,
aes(x = value, y = density)) +
geom_col(width = 0.9) +
facet_wrap(~type) +
coord_cartesian(ylim = c(0, 1)) +
labs(x = "Sign score ", y = "Frequency") +
scale_y_continuous(expand = c(0, 0)) +
theme_bw(base_size = 20)
if (FALSE) {
ggsave(file.path("Plots", "hist_signs.jpg"),
width = 13, height = 8, units = "cm", dpi = 300, scale = 2)
}
# -------------- N(t) : number of patients observations at day t ----------
nt <- df %>%
group_by(day = as.integer(day)) %>%
summarize(n_t = n())
ggplot(data = nt)+
geom_point(aes(day, n_t))+
labs(x = latex2exp::TeX("$\\textit{t}$ (day)"),
y = latex2exp::TeX("$\\textit{N(t)}$")) +
theme_bw() +
theme(axis.title.y = element_text(size = 20, face = "bold"),
axis.text.x = element_text(size = 15),
axis.text.y = element_text(size = 15),
legend.title = element_text(size = 13, face = "bold"),
legend.text = element_text(size = 11),
axis.title.x = element_text(size = 20, face = "bold"),
strip.text.x = element_text(size = 11, face = "bold"),
legend.position = "top")
if (FALSE) {
ggsave(file.path("Plots", "n(t).jpg"),
width = 13, height = 8, units = "cm", dpi = 300, scale = 2)
}
# -------------- Patient symptoms trajectories --------------------------
plot_trajectories <- function(df, patient_id) {
# Plot symptoms trajectories
#
# Args:
# df: Dataframe
# patient_id: Patient ID
#
# Returns:
# Ggplot
old_lbl <- c("itching", "sleep", "redness", "dry", "oozing", "edema", "symptom")
lbl <- c("itching", "sleep", "redness", "dryness", "oozing", "edema", "AD state")
stopifnot(is.data.frame(df),
all(colnames(c("ID", "day", old_lbl) %in% colnames(df))),
patient_id %in% unique(df[["ID"]]))
plot <- df %>%
filter(ID == patient_id)
# Add missing values for "blanks" to appear
day_mis <- setdiff(1:max(plot$day), plot$day)
if (length(day_mis) > 0) {
plot <- bind_rows(plot,
data.frame(ID = patient_id, day = day_mis)) %>%
arrange(day)
}
plot <- plot %>%
rename(dryness = dry, `AD state` = symptom) %>%
select(all_of(c("day", lbl))) %>%
pivot_longer(cols = all_of(lbl), names_to = "symptom", values_to = "score")
p1 <- ggplot(data = filter(plot, symptom != "AD state"),
aes(day, score)) +
geom_path(size = 1) +
geom_point() +
facet_wrap(~symptom, ncol = 1, strip.position = "right", scales = "free") +
scale_y_continuous(breaks = 0:4, limits = c(0, 4)) +
labs(y = "") +
theme_bw(base_size = 15) +
theme(legend.position = "none",
panel.grid.minor.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_text(size = 10),
axis.title.x = element_blank(),
axis.title.y = element_text(size = 17),
strip.text = element_text(size = 11),
strip.text.x = element_text(size = 9))
p2 <- ggplot(data = filter(plot, symptom == "AD state"),
aes(day, score)) +
geom_path(size = 1) +
geom_point() +
facet_wrap(~symptom, ncol = 1, strip.position = "right", scales = "free")+
scale_y_continuous(breaks = c(0, 1), limits = c(0, 1)) +
labs(x = latex2exp::TeX("$\\textit{t}$ (day)"), y = "") +
theme_bw(base_size = 15) +
theme(legend.position = "none",
panel.grid.minor.y = element_blank(),
axis.text.x = element_text(size = 11),
axis.text.y = element_text(size = 10),
axis.title.x = element_text(size = 17),
axis.title.y = element_text(size = 17),
strip.text = element_text(size = 11),
strip.text.x = element_text(size = 9))
ggpubr::ggarrange(p1, p2, ncol = 1,
common.legend = TRUE, legend = "top", heights = c(3, 1))
}
if (FALSE) {
# 179 for Figure 1
lapply(c("068", "084", "108", "120", "126", "134", "179", "180", "188", "193", "195"),
function(x) {
plot_trajectories(df, paste0("AD-", x))
ggsave(file.path("Plots", paste0("trajectory_", x, ".jpg")),
width = 13, height = 7, units = "cm", dpi = 300, scale = 2.2)
})
}
# --------------- Sign correlations --------------
col2 <- colorRampPalette(c('red', 'white', 'blue'))
M <- cor(df[, c('itching', 'sleep', 'redness', 'dry', 'oozing', 'edema') ], method = 'spearman')
corrplot(M, method = "number", type = 'upper', order = 'AOE', col = col2(100), cl.lim = c(0, 1))