forked from ghurault/EczemaTreat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07_plot_performance.R
151 lines (122 loc) · 5.93 KB
/
07_plot_performance.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
147
148
149
150
151
# Notes -------------------------------------------------------------------
# Plot performance at a given iteration (as a pointrange) for all severity items and aggregates (x axis), models (colour)
# Initialisation ----------------------------------------------------------
rm(list = ls()) # Clear Workspace (better to restart the session)
source(here::here("analysis", "00_init.R"))
#### OPTIONS
metric <- "lpd" # for comparing learning curves, not paired comparisons
t_horizon <- 4 # horizon that was used for forward chaining
it <- 16 # Iteration to plot the performance for
max_horizon <- 14 # restrict prediction horizon
pred_horizon <- 4 # prediction horizon
####
metric <- match.arg(metric, c("lpd", "RPS"))
stopifnot(t_horizon > 0,
max_horizon > 0)
dataset <- "PFDC"
item_dict <- detail_POSCORAD() %>%
mutate(Component = case_when(Name == "extent" ~ "Extent",
Name %in% c("itching", "sleep") ~ "Subjective symptoms",
Name %in% detail_POSCORAD("Intensity signs")$Name ~ "Intensity signs",
TRUE ~ "Aggregates"))
list_models <- available_models() %>%
select(Model) %>%
mutate(ModelGroup = "EczemaPred")
list_models <- tibble(Model = c("uniform", "historical"),
ModelGroup = "Reference") %>%
bind_rows(list_models)
mdl_names <- list_models[["Model"]]
list_models <- list_models %>%
expand_grid(item_dict) %>%
rename(Item = Name)
list_models[["File"]] <- vapply(1:nrow(list_models),
function(i) {
get_results_files(outcome = list_models$Item[i],
model = list_models$Model[i],
dataset = dataset,
val_horizon = t_horizon,
root_dir = here())$Val
},
character(1))
stopifnot(all(file.exists(list_models$File)))
# Processing --------------------------------------------------------------
fc_it <- load_PFDC()$POSCORAD %>%
rename(Time = Day) %>%
detail_fc_training(df = ., horizon = t_horizon)
perf <- lapply(1:nrow(list_models),
function(i) {
readRDS(list_models$File[i]) %>%
filter(Horizon <= max_horizon,
Iteration > 0 | list_models$Model[i] != "RW") %>%
estimate_performance(metric, ., fc_it, adjust_horizon = !(list_models$Model[i] %in% c("historical", "uniform"))) %>%
bind_cols(., list_models[i, ])
}) %>%
bind_rows()
# Performance at given iteration --------------------------------------------------------------------
pal <- rev(cbbPalette[1:length(unique(list_models[["Model"]]))])
tmp <- perf %>%
filter(Variable == "Fit",
Iteration == it,
Horizon == pred_horizon) %>%
mutate(ModelGroup = factor(ModelGroup, levels = rev(c("EczemaPred", "Reference"))),
Model = factor(Model, levels = mdl_names))
brk <- c(.01, .1, .25, .5, 1)
## Performance items
item_names <- detail_POSCORAD("Items")$Name
p1 <- tmp %>%
filter(Item %in% item_names) %>%
ggplot(aes(x = Item, y = Mean, ymin = Mean - SE, ymax = Mean + SE, colour = Model, shape = ModelGroup)) +
facet_grid(rows = vars(Component), space = "free", scale = "free") +
geom_pointrange(position = position_dodge(width = .66), size = 1, fill = "white") +
scale_colour_manual(values = pal) +
scale_y_continuous(breaks = log(brk), labels = paste0("log(", brk, ")")) +
scale_shape_manual(values = c(16, 21)) +
coord_flip() +
labs(x = "", y = metric, colour = "", shape = "") +
theme(legend.position = "none",
panel.grid.minor = element_blank(),
axis.text.x = element_text(angle = 30, vjust = .5, hjust = .5))
p1
## Performance for aggregate scores
p2 <- tmp %>%
filter(Item %in% c("oSCORAD", "SCORAD")) %>%
ggplot(aes(x = Item, y = Mean, ymin = Mean - SE, ymax = Mean + SE, colour = Model, shape = ModelGroup)) +
facet_grid(rows = vars(Component), space = "free", scale = "free") +
geom_pointrange(position = position_dodge(width = .66), size = 1, fill = "white") +
scale_colour_manual(values = pal) +
scale_shape_manual(values = c(16, 21)) +
coord_flip() +
labs(x = "", y = metric, colour = "", shape = "") +
theme(legend.position = "none",
panel.grid.minor = element_blank(),
axis.text.x = element_text(angle = 30, vjust = .5, hjust = .5))
## Custom legend
legend1 <- tmp %>%
filter(Item %in% item_names) %>%
filter(ModelGroup == "EczemaPred") %>%
ggplot(aes(x = Item, y = Mean, ymin = Mean - SE, ymax = Mean + SE, colour = Model)) +
geom_pointrange(position = position_dodge(width = .66), size = 1, fill = "white", shape = 21) +
scale_colour_manual(values = pal[-(1:2)], name = "EczemaPred models") +
labs(x = "", y = metric, colour = "") +
theme(legend.position = "top") +
guides(colour = guide_legend(title.position = "top"))
legend1 <- get_legend(legend1)
legend2 <- tmp %>%
filter(Item %in% item_names) %>%
filter(ModelGroup == "Reference") %>%
ggplot(aes(x = Item, y = Mean, ymin = Mean - SE, ymax = Mean + SE, colour = Model)) +
geom_pointrange(position = position_dodge(width = .66), size = 1, shape = 16) +
scale_colour_manual(values = pal[1:2], name = "Reference models") +
labs(x = "", y = metric, colour = "") +
theme(legend.position = "top") +
guides(colour = guide_legend(title.position = "top"))
legend2 <- get_legend(legend2)
legend <- plot_grid(NULL, legend1, NULL, legend2, NULL, nrow = 1)
## Combine plots
plot_grid(legend,
plot_grid(p1, p2,
labels = "AUTO",
ncol = 1, rel_heights = c(9, 2), align = "v"),
ncol = 1,
rel_heights = c(1, 11))
# ggsave(here("results", "performance.jpg"), width = 13, height = 8, units = "cm", dpi = 300, scale = 3.5, bg = "white")