-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplots.Rmd
More file actions
1791 lines (1565 loc) · 46.1 KB
/
Copy pathplots.Rmd
File metadata and controls
1791 lines (1565 loc) · 46.1 KB
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Plots"
date: "`r format(Sys.time(), '%B %d, %Y')`"
author: "Richard Wen <rrwen.dev@gmail.com>"
---
```{r, echo=FALSE}
knitr::opts_chunk$set(
message=FALSE,
warning=FALSE
)
options(readr.show_col_types = FALSE)
```
```{r, echo=FALSE, eval=FALSE}
#install.packages("openVA")
#install.packages("tidyverse")
#install.packages("dplyr")
#install.packages("ggrepel")
#install.packages("patchwork")
#install.packages("glue")
#install.packages("kableExtra)
#install.packages("boot")
#install.pacakges("scales")
```
# Libraries
```{r}
library(tidyverse)
library(dplyr)
library(ggrepel)
library(ggpattern)
library(patchwork)
library(glue)
library(kableExtra)
library(boot)
library(scales)
```
```{r, echo=FALSE}
cat("R Version\n---")
R.version
cat("\nPackage Versions\n---")
cat(paste0("\ntidyverse ", packageVersion("tidyverse")))
```
# Functions
## Data Functions
```{r}
# Order textual age ranges
order_arange <- function(ages) {
out <- ages %>%
data.frame %>%
rename("age_range" = ".") %>%
separate( # sep into range and unit
age_range,
into = c("age_range", "unit"),
sep = " "
) %>%
separate( # sep into min and max age
age_range,
into = c("age_min", "age_max"),
sep = "-"
) %>%
mutate( # format into correct data types
age_min = as.integer(age_min),
age_max = as.integer(age_max),
unit = str_to_lower(unit) %>%
factor(., levels = c(
"hours",
"days",
"weeks",
"months",
"years"
))
) %>%
arrange(unit, age_min, age_max) %>% # sort ages
mutate( # create ordered ages
age_range = paste0(age_min, "-", age_max, " ", unit)
) %>%
pull(age_range)
return(out)
}
```
## Metric Functions
Functions for PCCC and CSMF Accuracy.
```{r}
# Calc PCCC
calc_pccc <- function(actual, pred, k = 1, N = NULL, assume_zero = T) {
# Calc N num of causes if not known
N <- if (is.null(N)) length(unique(na.omit(c(actual, pred)))) else N
# Calc frac of deaths in top k causes
TP <- actual == pred
TP[is.na(TP) | is.null(TP)] <- FALSE # for no preds
C <- sum(TP) / length(actual)
# Calc PCCC
if (!all(TP)) {
out <- (C - (k/N)) / (1 - (k/N))
} else { # avoid division by 0 when max pccc
out <- 1
}
# Scale negative numbers to 0 for easier plotting
out <- if (assume_zero & out < 0) 0 else out
return(out)
}
# Calc CSMF accuracy
calc_csmf_acc <- function(actual, pred) {
# Get all unique causes
causes <- unique(c(actual, pred))
# Get csmfs
cases <- length(actual)
csmf_true <- table(actual) / cases
csmf_pred <- table(pred) / cases
# Correct for missing causes in either actual or pred
csmf_true <- vapply(
causes,
function(x) if (x %in% names(csmf_true)) csmf_true[x] else 0,
FUN.VALUE = numeric(1)
)
csmf_pred <- vapply(
causes,
function(x) if (x %in% names(csmf_pred)) csmf_pred[x] else 0,
FUN.VALUE = numeric(1)
)
# Calc csmf max error
csmf_max_error <- 2 * (1 - min(csmf_true))
# Calc csmf acc
out <- 1 - (sum(abs(csmf_true - csmf_pred)) / csmf_max_error)
return(out)
}
# Calc bootstrapped cis for a metric
calc_ci <- function(
x,
f = calc_pccc,
n_boot = 1000,
conf_level = 0.95,
col_actual = "cod",
col_pred = "model_cghr10",
...
) {
# Function to compute metric on a bootstrap sample
f_boot <- function(df, i) {
f(df[[col_actual]][i], df[[col_pred]][i], ...)
}
# Run bootstrap using the 'boot' package
boot_res <- boot(data = x, statistic = f_boot, R = n_boot)
# Compute percentile-based confidence interval
ci <- boot.ci(boot_res, type = "perc", conf = conf_level)$percent[4:5]
# Return named vector with metric and CI
out <- c(ci_mean = mean(boot_res$t), ci_low = ci[1], ci_hi = ci[2])
return(out)
}
```
## Plot Functions
Functions for generating plots.
```{r}
# Plot performance of models using lines and points
plot_perf <- function(
df_long,
f = calc_pccc,
group = "cod_name",
color = "model_name",
text_scale = 1,
title = NULL,
title_size = 10,
x_title = "Partial Chance Corrected Concordance (PCCC)",
y_title = NULL,
text_mid_size = 2.25 * text_scale,
text_mid_vjust = 2.5,
text_start_size = 2.75 * text_scale,
text_end_size = 3.5 * text_scale,
text_hjust = 0.2,
add_min_points = F,
add_max_points = F,
mid_points_size = 4,
min_points_size = 4,
max_points_size = 4,
x_limits = c(0, 1.15),
x_breaks = seq(0, 1, by = 0.1),
x_expand = expansion(add = 0.1),
x_color_labels = c(
"G3" = "GPT-3.5 (G3)",
"G4" = "GPT-4 (G4)",
"G5" = "GPT-5 (G5)",
"ISV" = "InSilicoVA (ISV)",
"I5" = "InterVA-5 (I5)"
),
x_shapes = 21:26,
x_title_size = 9 * text_scale,
y_title_size = 9 * text_scale,
x_text_size = 8 * text_scale,
y_text_size = 9 * text_scale,
sort_group = T,
legend_position = "bottom",
legend_title = element_blank(),
legend_margin = margin(c(-8, -2, -2, -50)),
legend_add = c("R = Range"),
...
) {
# Group data
df_plot <- df_long %>%
group_by(.data[[group]], model_name, .drop = F)
# Calc metric
if (identical(f, calc_pccc)) {
df_plot <- df_plot %>%
summarize(
metric = f(cod, model_cghr10, N = unique(ncod)[1], ...)
)
} else {
df_plot <- df_plot %>%
summarize(
metric = f(cod, model_cghr10, ...)
)
}
# Add metric value to model name
df_plot <- df_plot %>%
mutate(
model_label = paste0(model_name, ": ", round(metric, 2))
)
# Add min and max values for each group
df_ends <- df_plot %>%
group_by(.data[[group]]) %>%
summarize(
min_metric = min(metric),
max_metric = max(metric),
model_min = model_name[which.min(metric)],
model_max = model_name[which.max(metric)],
group_range = diff(range(metric))
) %>%
arrange(max_metric)
# Sort group by max model metric
if (sort_group) {
df_plot[[group]] <- factor(df_plot[[group]], levels = df_ends[[group]])
df_ends[[group]] <- factor(df_ends[[group]], levels = df_ends[[group]])
}
# Filter out the mid models
df_mids <- df_plot %>%
left_join(df_ends, by = group) %>%
filter(model_name != model_max & model_name != model_min)
# Add blank legend items
if (!is.null(legend_add)) {
# Add to manual color labels
for (i in legend_add) {
x_color_labels[i] <- i
}
# Add to factor levels
df_plot[[color]] <- factor(
df_plot[[color]],
levels = c(levels(df_plot[[color]]), legend_add)
)
}
# Get custom colors for models
default_colors <- scales::hue_pal()(length(unique(df_plot[[color]])))
default_colors <- c(default_colors, rep(NA, length(legend_add)))
names(default_colors) <- c(unique(as.character(df_plot[[color]])), legend_add)
# Plot metric for each model
out <- ggplot(df_plot, aes(x = metric, y = .data[[group]])) +
geom_segment( # min max lines
data = df_ends,
aes(
x = min_metric,
xend = max_metric,
y = .data[[group]],
yend = .data[[group]]
),
linewidth = 0.75
) +
geom_point( # points
color = "black",
aes(
shape = .data[[color]],
fill = .data[[color]]
),
size = mid_points_size
) +
geom_text( # min model text
data = df_ends,
aes(
x = min_metric,
y = .data[[group]],
label = paste0(model_min, ": ", round(min_metric, 2))
),
hjust = 1 + text_hjust,
vjust = 0.5,
size = text_start_size
) +
geom_text( # max model text
data = df_ends,
aes(
x = max_metric,
y = .data[[group]],
label = paste0(
model_max, ": ", round(max_metric, 2),
" (R=", round(group_range, 2), ")"
)
),
hjust = 0 - text_hjust,
vjust = 0.5,
size = text_end_size,
fontface = "bold"
) +
geom_text_repel( # mid model text
data = df_mids,
aes(label = model_label),
color = "gray60",
vjust = text_mid_vjust,
max.overlaps = Inf,
size = text_mid_size
) +
scale_fill_manual(
values = default_colors,
labels = x_color_labels,
drop = ifelse(is.null(legend_add), T, F)
) +
scale_shape_manual(
values = x_shapes,
labels = x_color_labels,
drop = ifelse(is.null(legend_add), T, F)
) +
scale_x_continuous(
limits = x_limits,
breaks = x_breaks,
expand = x_expand
) +
labs(
title = title,
x = x_title,
y = y_title
) +
theme_minimal() +
theme(
legend.position = legend_position,
legend.title = legend_title,
legend.margin = legend_margin,
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(size = title_size),
axis.title.x = element_text(size = x_title_size),
axis.title.y = element_text(size = y_title_size),
axis.text.x = element_text(size = x_text_size),
axis.text.y = element_text(size = y_text_size)
)
# Add min and max points
if (add_min_points) {
out <- out + geom_point( # min points
data = df_ends,
aes(x = min_metric),
size = 2
)
}
if (add_max_points) {
out <- out + geom_point( # max points
data = df_ends,
aes(x = max_metric),
size = 2
)
}
return(out)
}
# Plot performance of models using grouped bars
plot_perf_bar <- function(
df_long,
f = calc_pccc,
group = "cod_name",
color = "model_name",
text_scale = 1,
title = NULL,
title_size = 10,
x_title = "Partial Chance Corrected Concordance (PCCC)",
y_title = NULL,
x_hpos = -0.47 * text_scale,
x_limits = c(0, 1),
x_breaks = seq(0, 1, by = 0.1),
x_expand = expansion(add = ((x_hpos * -1) + 0.03)),
x_shapes = 21:26,
x_title_size = 8 * text_scale,
y_title_size = 8 * text_scale,
x_text_size = 7 * text_scale,
x_color_size = 2.5 * text_scale,
y_text_size = 8 * text_scale,
legend_position = "bottom",
legend_title = element_blank(),
legend_margin = margin(c(-8, -2, -2, -50)),
legend_key_size = 0.5,
legend_text_size = 7 * text_scale,
legend_spacing_x = unit(58, "pt"),
n_boot = 1000,
error_bar = T,
error_bar_width = 0.05,
error_bar_size = 0.3,
error_bar_label = "95% Confidence Interval (Bootstrap, 1000 Resamples)"
) {
# Group data
df_plot <- df_long %>%
group_by(.data[[group]], model_name, .drop = F)
# Calc metric
if (identical(f, calc_pccc)) {
df_plot <- df_plot %>%
group_map(~{
# Calc metric
out <- c(
metric = f(.x$cod, .x$model_cghr10, N = unique(.x$ncod)[1])
)
# Include ci if error bar
if (error_bar) {
out <- c(.y, out, calc_ci(.x, n_boot = n_boot))
}
return(out)
}, .keep = T) %>%
bind_rows
} else {
df_plot <- df_plot %>%
group_map(~{
# Calc metric
out <- c(
metric = f(.x$cod, .x$model_cghr10)
)
# Include ci if error bar
if (error_bar) {
out <- c(.y, out, calc_ci(.x, n_boot = n_boot))
}
return(out)
}, .keep = T) %>%
bind_rows
}
# Get custom colors for models
default_colors <- scales::hue_pal()(length(unique(df_plot[[color]])))
names(default_colors) <- c(unique(as.character(df_plot[[color]])))
# Format plot data
df_plot <- df_plot %>%
mutate(
x = metric,
y = .data[[group]],
color = .data[[color]]
)
# Sort bars within groups (ascending)
df_pos <- df_plot %>%
group_by(y) %>%
arrange(x, .by_group = TRUE) %>%
mutate(
rank_in_group = row_number(),
n_colors = n()
) %>%
ungroup()
# Reverse sort groups by highest-performing bar
group_levels_order <- df_pos %>%
group_by(y) %>%
summarize(max_x = max(x)) %>%
arrange(max_x) %>%
pull(y)
df_pos <- df_pos %>%
mutate(
y = factor(y, levels = group_levels_order),
group_center = as.numeric(y)
)
# Bar width and offset
group_width <- 0.8
max_items <- max(df_pos$n_colors)
bar_width <- group_width / max_items
df_pos <- df_pos %>%
mutate(
offset = (rank_in_group - (n_colors + 1)/2) * bar_width,
x_pos = group_center + offset,
label_text = paste0(color, ": ", format(round(x, 2), nsmall = 2)),
label_x = x_pos
)
# Add ci to model label
if (error_bar) {
df_pos <- df_pos %>%
mutate(
label_text = paste0(label_text, " (", round(ci_low, 2), ",", round(ci_hi, 2), ")")
)
}
# Identify top-performing model in each group
df_pos <- df_pos %>%
group_by(y) %>%
mutate(
top_model = x == max(x),
fontface = ifelse(top_model, "bold", "plain"),
alpha_val = ifelse(top_model, 1, 0.35), # transparency for non-top bars
outline_color = ifelse(top_model, "black", NA) # black outline for top bars
) %>%
ungroup()
# Compute range and modify group labels using square brackets
group_labels <- df_pos %>%
group_by(y) %>%
summarize(range_x = max(x) - min(x)) %>%
mutate(label = paste0(as.character(y), "\n[Range=", format(round(range_x, 2), nsmall = 2), "]")) %>%
arrange(match(y, levels(df_pos$y)))
# Replace factor levels with new labels including range
levels(df_pos$y) <- group_labels$label
# Plot metric for each model
df_pos$ci_label <- error_bar_label # dummy label for legend
out <- ggplot(df_pos, aes(x = x_pos, y = x, fill = color)) +
geom_col(
width = bar_width * 0.9,
position = "identity",
color = df_pos$outline_color,
alpha = df_pos$alpha_val
) +
geom_text(
aes(
x = label_x,
label = label_text,
fontface = fontface
),
y = x_hpos,
hjust = 0,
vjust = 0.5,
size = x_color_size
) +
coord_flip() +
scale_fill_manual(
values = default_colors
) +
scale_x_continuous(
breaks = seq_along(levels(df_pos$y)),
labels = levels(df_pos$y),
expand = c(0, 0)
) +
scale_y_continuous(
expand = x_expand,
breaks = x_breaks,
limits = x_limits
) +
labs(
title = title,
x = y_title,
y = x_title
) +
theme_minimal() +
theme(
legend.position = legend_position,
legend.title = legend_title,
legend.margin = legend_margin,
legend.key.size = unit(legend_key_size, "lines"),
legend.text = element_text(size = legend_text_size),
legend.spacing.x = legend_spacing_x,
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(size = title_size),
axis.title.x = element_text(size = x_title_size),
axis.title.y = element_text(size = y_title_size),
axis.text.x = element_text(size = x_text_size),
axis.text.y = element_text(size = y_text_size)
)
# Show error bars
if (error_bar) {
out <- out +
geom_errorbar(
aes(ymin = ci_low, ymax = ci_hi, color = ci_label),
width = error_bar_width,
size = error_bar_size
) +
scale_color_manual(name = "", values = "black") +
guides(
fill = guide_legend(order = 1), # bar legend first
color = guide_legend(order = 2) # ci legend last
)
}
return(out)
}
# Plot perf of models using trend lines
plot_perf_trend <- function(
df_long,
f = calc_pccc,
group = "age_range",
text_scale = 1,
title = NULL,
x_title = NULL,
y_title = NULL,
title_size = 10,
x_title_size = 9 * text_scale,
y_title_size = 9 * text_scale,
x_text_size = 8 * text_scale,
y_text_size = 9 * text_scale,
legend_text_size = 9 * text_scale,
legend_position = "bottom",
legend_direction = "horizontal",
legend_spacing = unit(0, "pt"),
y_limits = c(0, 1.15),
y_breaks = seq(0, 1, by = 0.1),
x_color_labels = c(
"G3" = "GPT-3.5",
"G4" = "GPT-4",
"G5" = "GPT-5",
"ISV" = "InSilicoVA",
"I5" = "InterVA-5"
),
x_linetypes = c(
"solid",
"solid",
"dashed",
"dotted",
"dotted"
),
x_linealpha = c(
1,
0.5,
0.9,
0.9,
0.7
),
x_linewidths = c(
2,
1,
1.15,
2,
1.15
),
plot_mean = F
) {
# Calc metric
df_plot <- df_long %>%
group_by(.data[[group]], model_name, .drop = F) %>%
summarize( # calc pccc
age = paste0(unique(age), collapse = ","),
metric = f(cod, model_cghr10, N = unique(ncod)[1])
) %>%
mutate( # add pccc to model name
model_label = paste0(model_name, " (", round(metric, 2), ")")
)
# Get custom colors for models
default_colors <- scales::hue_pal()(length(unique(df_plot$model_name)))
names(default_colors) <- unique(df_plot$model_name)
default_colors["Mean Across Models"] <- "black"
# Plot trend lines for each model
out <- ggplot(
df_plot,
aes(
x = .data[[group]],
y = metric,
color = model_name,
group = model_name
)
) +
geom_line(
aes(
linetype = model_name,
alpha = model_name,
linewidth = model_name
)
) +
scale_y_continuous(
limits = y_limits,
breaks = y_breaks
) +
scale_color_manual(
values = default_colors,
labels = x_color_labels
) +
scale_linetype_manual(
values = x_linetypes,
labels = x_color_labels
) +
scale_linewidth_manual(
values = x_linewidths,
labels = x_color_labels
) +
scale_alpha_manual(
values = x_linealpha,
labels = x_color_labels
) +
labs(
title = title,
x = x_title,
y = y_title
) +
theme_minimal() +
theme(
legend.key.width = unit(32, "pt"),
legend.title = element_blank(),
legend.text = element_text(size = legend_text_size),
legend.position = legend_position,
legend.direction = legend_direction,
legend.box.spacing = legend_spacing,
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(size = title_size),
axis.title.x = element_text(size = x_title_size),
axis.title.y = element_text(size = y_title_size),
axis.text.x = element_text(size = x_text_size),
axis.text.y = element_text(size = y_text_size)
)
# Add mean line
if (plot_mean) {
out <- out +
geom_line(
data = df_plot %>%
group_by(.data[[group]]) %>%
summarize(
metric = mean(metric),
model_name = "Mean Across Models"
),
aes(color = model_name),
se = F,
linewidth = 1.5
)
}
return(out)
}
```
# Data
## Raw Data
Load data from `data` folder.
```{r}
# Read preds
df_raw <- read_csv("../data/healsl_rd1to2_cod_v2.csv")
# Display preds
cat("Cases: ", nrow(df_raw), "\n")
print(head(df_raw))
```
## Physician Cases
Filter for cases that were coded by physicians.
```{r}
# Filter for phy cases
df <- df_raw %>%
filter(!is.na(physician_cghr10) & !is.null(physician_cghr10))
# Preview phy cases
cat("Non-coded Cases: ", nrow(df_raw) - nrow(df))
cat("\nPhysician Coded Cases: ", nrow(df))
df
```
## Inspect Data
Display case counts for raw data and prepared data after combining models and filtering for physician coded cases.
```{r}
cat("\nRaw Data\n--------\n\n")
# Physician info
raw_physicians <- df_raw$physician_cghr10
cat(paste0(
"Cases (",
length(unique(na.omit(raw_physicians))), " CODs): ",
length(raw_physicians),
"\n"
))
# Physician agreed cases
raw_physicians_agree <- df_raw %>%
filter(is_agreed == TRUE) %>%
pull(physician_cghr10)
cat(paste0(
"Agreed Cases (",
length(unique(na.omit(raw_physicians_agree))), " CODs): ",
length(raw_physicians_agree),
"\n"
))
# Models cases
raw_models <- df_raw %>% select(ends_with("_cghr10"), -physician_cghr10)
for (mcol in colnames(raw_models)) {
m <- raw_models[[mcol]]
cat(paste0(
mcol, " Predicted Cases (",
length(unique(na.omit(m))), " CODs): ",
length(m) - sum(is.na(m) | is.null(m)),
"\n"
))
}
cat("\nPrepared Data\n--------------\n")
# Physician cases
physicians <- df$physician_cghr10
cat(paste0(
"Physician Coded Cases (",
length(unique(na.omit(physicians))), " CODs): ",
length(physicians) - sum(is.na(physicians) | is.null(physicians)),
"\n"
))
# Physician agreed cases
physicians_agree <- df %>%
filter(is_agreed == TRUE) %>%
pull(physician_cghr10)
cat(paste0(
"Physician Agreed Coded Cases (",
length(unique(na.omit(physicians_agree))), " CODs): ",
length(physicians_agree) - sum(is.na(physicians_agree) | is.null(physicians_agree)),
"\n"
))
# Models cases
models <- df %>% select(ends_with("_cghr10"), -physician_cghr10)
for (mcol in colnames(models)) {
m <- models[[mcol]]
cat(paste0(
mcol, " Predicted Cases (",
length(unique(na.omit(m))), " CODs): ",
length(m) - sum(is.na(m) | is.null(m)),
"\n"
))
}
# Models agreed cases
models_agree <- df %>%
filter(is_agreed == TRUE) %>%
select(ends_with("_cghr10"), -physician_cghr10)
for (mcol in colnames(models_agree)) {
m <- models_agree[[mcol]]
cat(paste0(
mcol, " Agreed Predicted Cases (",
length(unique(na.omit(m))), " CODs): ",
length(m) - sum(is.na(m) | is.null(m)),
"\n"
))
}
```
# Preparation
Prepare data for plotting.
## Agreed Cases
Filter for agreed cases only.
```{r}
# Agreed cases only
df <- df %>% filter(is_agreed == TRUE)
```
## Rename COD Names
Rename COD names into simpler names.
```{r}
# Rename COD names
df <- df %>%
mutate(across(
c(
physician_cghr10,
gpt3_cghr10,
gpt4_cghr10,
gpt5_cghr10,
insilicova_cghr10,
interva5_cghr10
),
~ case_when(
.x == "Maternal conditions" ~ "Maternal",
.x == "Acute respiratory infections" ~ "Pneumonia",
.x == "Neonatal infections" ~ "Infections",
.x == "Road and transport injuries" ~ "Road/transport injuries",
.x == "Liver and alcohol related diseases" ~ "Liver/alcohol",
.x == "Birth asphyxia and trauma" ~ "Birth asphyxia/trauma",
.default = .x
) %>%
str_replace_all(" diseases| disease", "")
))
# Rename raw COD names
df_raw <- df_raw %>%
mutate(across(
c(
physician_cghr10,
gpt3_cghr10,
gpt4_cghr10,
gpt5_cghr10,
insilicova_cghr10,
interva5_cghr10
),
~ case_when(
.x == "Maternal conditions" ~ "Maternal",
.x == "Acute respiratory infections" ~ "Pneumonia",
.x == "Neonatal infections" ~ "Infections",
.x == "Road and transport injuries" ~ "Road/transport injuries",
.x == "Liver and alcohol related diseases" ~ "Liver/alcohol",
.x == "Birth asphyxia and trauma" ~ "Birth asphyxia/trauma",
.default = .x
) %>%
str_replace_all(" diseases| disease", "")
))
```
## Shorten COD Names
Shorten long names into new lines.
```{r}
# Shorten COD names
df <- df %>%
mutate(across(
c(
physician_cghr10,
gpt3_cghr10,
gpt4_cghr10,
gpt5_cghr10,
insilicova_cghr10,
interva5_cghr10
),
~ str_wrap(.x, width = 18)
))
# Shorten raw COD names
df_raw <- df_raw %>%
mutate(across(
c(
physician_cghr10,
gpt3_cghr10,
gpt4_cghr10,
gpt5_cghr10,
insilicova_cghr10,
interva5_cghr10
),
~ str_wrap(.x, width = 18)
))