Repository navigation
Expand file tree
/
Copy path_batch.R
More file actions
1040 lines (794 loc) · 33.5 KB
/
Copy path_batch.R
File metadata and controls
1040 lines (794 loc) · 33.5 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
wl_batch_func <- function(model_group, group_name){
require(tidyverse)
require(fuzzyjoin)
require(NHSRwaitinglist)
source("utils.R")
require(purrr)
require(furrr)
require(future.apply)
require(parallel)
require(BSOLwaitinglist)
require(writexl)
library(scales)
library(glue)
######################## Targets & population growth ##############
#last_data_date <- as.Date(last_data_date, '%d/%m/%Y')
target_dts <-
data.frame(
startdate = as.Date(c('01/07/2026', '01/04/2027', "01/04/2029"), '%d/%m/%Y'),
enddate = as.Date(c('31/03/2027', '31/03/2029', "31/03/2031"), '%d/%m/%Y'),
value = c(0.65, 0.92, 0.92),
descr = c("RTT65%", "RTT92%", "RTT92%")
)
# No Non-demo
# if(substr(group_name,1,2) == "bc"){
# # #bc
# population_growth <-
# tibble::tribble(
# ~start_date, ~end_date, ~ratio_increase, ~population, ~adjustment_factor,
# #"01/09/2025", "31/03/2026", 1, 1254329, 1.013762439,
# "01/07/2026", "31/03/2027", 1.005597424, 1261350, 1.014112691,
# "01/04/2027", "31/03/2028", 1.011238882, 1268426, 1.014032883,
# "01/04/2028", "31/03/2029", 1.016872582, 1275493, 1.013953973,
# "01/04/2029", "31/03/2030", 1.02287851, 1283026, 1.014080133,
# "01/04/2030", "31/03/2031", 1.028903111, 1290583, 1.013893177
# )
# } else {
# # Bsol
# population_growth <-
# tibble::tribble(
# ~start_date, ~end_date, ~ratio_increase, ~population, ~adjustment_factor,
# #"01/09/2025", "31/03/2026", 1, 1590793, 0.991027558,
# #"01/03/2026", "31/03/2026", 1, 1590793, 0.991027558,
# "01/07/2026", "31/03/2027", 1.003309, 1596056.425, 0.991161751,
# "01/04/2027", "31/03/2028", 1.006733, 1601503.998, 0.991296965,
# "01/04/2028", "31/03/2029", 1.010701, 1607815.611, 0.991226216,
# "01/04/2029", "31/03/2030", 1.015318, 1615161.207, 0.991370238,
# "01/04/2030", "31/03/2031", 1.020006, 1622704.257, 0.991201425
# )
# }
# #+ 1% Non-demographic growth
if(substr(group_name,1,2) == "bc"){
# #bc
population_growth <-
tibble::tribble(
~start_date, ~end_date, ~ratio_increase, ~population, ~adjustment_factor,
#"01/09/2025", "31/03/2026", 1, 1254329, 1.013762439,
"01/07/2026", "31/03/2027", 1.015597424, 1261350, 1.014112691,
"01/04/2027", "31/03/2028", 1.021238882, 1268426, 1.014032883,
"01/04/2028", "31/03/2029", 1.026872582, 1275493, 1.013953973,
"01/04/2029", "31/03/2030", 1.03287851, 1283026, 1.014080133,
"01/04/2030", "31/03/2031", 1.038903111, 1290583, 1.013893177
)
} else {
# Bsol
population_growth <-
tibble::tribble(
~start_date, ~end_date, ~ratio_increase, ~population, ~adjustment_factor,
#"01/09/2025", "31/03/2026", 1, 1590793, 0.991027558,
#"01/03/2026", "31/03/2026", 1, 1590793, 0.991027558,
"01/07/2026", "31/03/2027", 1.013309, 1596056.425, 0.991161751,
"01/04/2027", "31/03/2028", 1.016733, 1601503.998, 0.991296965,
"01/04/2028", "31/03/2029", 1.020701, 1607815.611, 0.991226216,
"01/04/2029", "31/03/2030", 1.025318, 1615161.207, 0.991370238,
"01/04/2030", "31/03/2031", 1.030006, 1622704.257, 0.991201425
)
}
# Convert Year to Date
population_growth$start_date <- as.Date(population_growth$start_date, '%d/%m/%Y')
population_growth$end_date <- as.Date(population_growth$end_date, '%d/%m/%Y')
##################Load data#############################
ICB <- TRUE
##################Prepare data###########################
############### Convert to list of data.frames per specialty#####################
# Split data frame by Specialty, into a list.
# Each slot in the list is a data.frame for a specialty
df_list <- model_group |> #test_input %>%
mutate(ratio_increase = as.numeric(NA),
#adjustment_factor = as.numeric(NA)
) |>
group_by(Specialty) %>%
group_split()
## TF 300 in BSOL
#df_list <- df_list[10]
# Check lists are equal length
#lapply(df_list, nrow)
#View(df_list[[1]])
#.x <- df_list[[1]]
#print(df_list[[1]][18,8], n = 30)
# Correct removals to balance waiting list
df_list <- map(df_list, function(.x) {
sub <- .x[1,, drop = FALSE]
.x$Removals <- data.table::shift(.x$Waiting.list.size, 1, type = "lag") + .x$Referrals - .x$Waiting.list.size
.x <- rbind(sub, .x[2:nrow(.x),, drop = FALSE])
.x$Referrals <- as.integer(.x$Referrals / 4.33) # divide by 4.33 to turn monthly to weekly, needs to be done here after balance
.x$Removals <- as.integer(.x$Removals / 4.33) # divide by 4.33 to turn monthly to weekly
.x
})
# Now make same month-to-week adjustment and formatting outside look, for later plotting
model_group$Referrals <- as.integer(model_group$Referrals/ 4.33) # divide by 4.33 to turn monthly to weekly
model_group$Removals <- as.integer(model_group$Removals / 4.33) # divide by 4.33 to turn monthly to weekly
# # Chop September as it's wonky due to PAS implementation
# df_list <- map(df_list, function(.x) {
# .x <- .x[.x$end_date < as.Date("01/09/2025", "%d/%m/%Y"),]
# .x
# })
# Last row based on data for iteration elements. Another taken after extending later for full data + simualtion period.
last_data_row <- nrow(df_list[[1]])
#last_data_row <- map(df_list, nrow)
# Calculate coefficients of variation (how each list behaves)
# cv_demand and cv_capacity for each specialty
tf_summary <- map_dfr(df_list, function(.x) {
.x <- filter(.x, start_date < as.Date("2025-10-01"))
cv_demand <- sd(.x$Referrals, na.rm = TRUE) / mean(.x$Referrals, na.rm = TRUE)
cv_capacity <- sd(.x$Removals, na.rm = TRUE) / mean(.x$Removals, na.rm = TRUE)
tibble(
Specialty = first(.x$Specialty),
cv_demand = cv_demand,
cv_capacity = cv_capacity
)
})
# Append rows for population growth periods
df_append <- data.frame(Commissioner_Code = NA,
Specialty = NA,
start_date = as.Date(population_growth$start_date, '%d/%m/%Y'),
end_date = as.Date(population_growth$end_date, '%d/%m/%Y'),
Referrals = NA, Removals = NA, Waiting.list.size = NA,
ratio_increase = population_growth$ratio_increase,
adjustment = df_list[[1]]$adjustment[1]
#adjustment_factor = population_growth$adjustment_factor
)
# Apply to data.frames in list.
df_list <- map(df_list, ~ rbind(.x, df_append))
#print(df_list[[1]], n = 50)
last_row <- map(df_list, nrow)
#### Populate values logically ####
######## Populate values ####
# fill in values, specialty
df_list <- map(df_list, function(.x) {
spec <- first(.x$Specialty)
.x$Specialty[is.na(.x$Specialty)] <- spec
.x
})
df_list <- map(df_list, function(.x) {
com <- first(.x$Commissioner_Code)
.x$Commissioner_Code[is.na(.x$Commissioner_Code)] <- com
.x
})
# median capacity over last 12 months. Longer seems a bit extreme
#j <- 1
df_list <- map(df_list, function(.x) {
med <- as.integer(median(.x[(last_data_row - 11):last_data_row,]$Removals, na.rm = TRUE))
.x$Removals[is.na(.x$Removals)] <- med
#
#j <<- j + 1
.x
})
#
# # Fill in referrals based on last known value and growth ratios
df_list <- map(df_list, function(.x) {
.x %>%
mutate(
Referrals = {
last_val <- as.integer(median(.x[(last_data_row - 11):last_data_row,]$Referrals, na.rm = TRUE))
new_vals <- .x$Referrals
for (i in seq_along(new_vals)) {
if (is.na(new_vals[i])) {
if (i == 1 || !is.na(new_vals[i - 1])) {
if(ICB == TRUE) {
new_vals[i] <- last_val * ratio_increase[i]
} else {
new_vals[i] <- last_val * (((ratio_increase[i] - 1) * adjustment[i]) + 1)
}
} else {
if(ICB == TRUE) {
new_vals[i] <- new_vals[i - 1] * ratio_increase[i]
} else {
new_vals[i] <- new_vals[i - 1] * (((ratio_increase[i] - 1) * adjustment[i]) + 1)
}
}
}
}
as.integer(ceiling(new_vals))
}
)
})
#View(df_list[[1]])
#print(df_list[[1]], n = 50)
#print(df_list[[2]], n = 50)
#.x <- df_list[[1]]
# Add targets into data.frames in list
df_list <- map(df_list, function(.x) {
.x <- fuzzy_left_join(.x, target_dts, by = c("start_date" = "startdate", "end_date" = "enddate")
, match_fun = list(`>=`, `<=`)) |>
mutate(time_to_target = floor(as.numeric(difftime(enddate, start_date, units = "weeks")))) |>
select(-startdate,-enddate,-descr) |>
rename(target = value)
.x
})
#.x<-df_list[[1]]
# Initialize columns for later
#j <- 1
df_list <- map(df_list, function(.x) {
.x$target_wl <- NA
.x$target_capacity <- NA
.x$relief_capacity_cur <- NA
.x$relief_capacity_rel <- NA
.x$Waiting.list.size_relief <- NA
.x$wl_performance_cur <- NA
.x$wl_performance_rel <- NA
# Copy over right waiting list size as easier than using 2 columns in sim function.
.x$Waiting.list.size_relief[last_data_row] <- .x$Waiting.list.size[last_data_row]
#j <<- j + 1
.x
})
#Fill in waiting list performance at last point
df_list <- map(df_list, function(.x) {
.x$wl_performance_cur[last_data_row] <- est_wait_performance(.x$Referrals[last_data_row], .x$Waiting.list.size[last_data_row], 18)
.x$wl_performance_rel[last_data_row] <- est_wait_performance(.x$Referrals[last_data_row], .x$Waiting.list.size[last_data_row], 18)
.x
})
#print(df_list[[16]], n = 92)
#print(df_list[[2]], n = 92)
#####
# Need to loop this now to build WL, then calculate targets, and sim next
#i <- 86L
#df <-
#df_list[[5]]
#View(df_list[[1]])
#i = 20
#.x <- df
plan(sequential)
gc()
# Create workers and pre-load Rcpp compilation on each
cl <- future::makeClusterPSOCK(workers = 6)
clusterEvalQ(cl, {
library(BSOLwaitinglist)
})
#Optional sanity check: should be TRUE on each worker
parallel::clusterEvalQ(cl, exists("bsol_montecarlo_WL3", mode = "function"))
# Use the cluster in your plan
plan(cluster, workers = cl)
#print(df_list[[1]], n = 50)
#df_list <- df_list[1]
#.x <- df_list[[1]]
#i <- 28
for (i in (last_data_row + 1):nrow(df_list[[1]])) {
# run NHSR waiting list functions over each data.frame in list (Specialty)
#.x <- df_list[[1]]
df_list <- map(df_list, function(.x) {
# Add a step that says, is it 2026/27? If wl_performance[i-1] < 0.65, 0.65, else wl_performance[i-1] + 0.07
.x$target[i] <-
ifelse(
.x$start_date[i] >= target_dts$startdate[1] &
.x$end_date[i] <= target_dts$enddate[1] &
.x$wl_performance_rel[i - 1] >= target_dts$value[1],
ifelse((.x$wl_performance_rel[i - 1] + 0.07) <= 1, .x$wl_performance_rel[i - 1] + 0.07, 0.99),
.x$target[i]
)
.x$target_wl[i] <- floor(calc_target_queue_size(
demand = .x$Referrals[i],
target_wait = 18,
factor = qexp(.x$target[i]) # Need to amend for target 0.65 or +0.07
))
dscr <- first(.x$Specialty)
cv_demand <- tf_summary %>% filter(Specialty == dscr) %>% pull(cv_demand)
cv_capacity <- tf_summary %>% filter(Specialty == dscr) %>% pull(cv_capacity)
# Assuming we meet target list size in each period
#.x$Waiting.list.size <- coalesce(.x$Waiting.list.size, .x$target_wl)
.x$target_capacity[i] <-
ceiling(calc_target_capacity(
demand = .x$Referrals[i],
target_wait = 18,
factor = qexp(.x$target[i]),
cv_demand = 1,
cv_capacity = 1))
.x$relief_capacity_cur[i] <-
ceiling(calc_relief_capacity(
demand = .x$Referrals[i],
queue_size = .x$Waiting.list.size[i - 1],
target_queue_size = .x$target_wl[i],
time_to_target = .x$time_to_target[i],
cv_demand = 1
))
.x$relief_capacity_rel[i] <-
ceiling(calc_relief_capacity(
demand = .x$Referrals[i],
queue_size = .x$Waiting.list.size_relief[i - 1],
target_queue_size = .x$target_wl[i],
time_to_target = .x$time_to_target[i],
cv_demand = 1
))
# manual correction for 65% target year, if capacity already higher, dont' reduce.
# .x$relief_capacity_rel[i] <- ifelse(
# .x$start_date[i] >= target_dts$startdate[1] &
# .x$end_date[i] <= target_dts$enddate[1] &
# .x$relief_capacity_rel[i] < .x$Removals[i],
# .x$Removals[i],
# .x$relief_capacity_rel[i]
#
#
# )
.x
})
# Sim with current capacity projected forward
sim_func_cur <- function(df) {
current_wl <- data.frame(
Referral = rep(as.Date(df$start_date[i] - 1)
, df$Waiting.list.size[i - 1]),
Removal = rep(as.Date(NA), df$Waiting.list.size[i - 1])
)
sim <- wl_simulator_cpp(
start_date = as.Date(df[i,]$start_date),
end_date = as.Date(df[i,]$end_date),
demand = df[i,]$Referrals,
capacity = df[i,]$Removals, # project last point forward
waiting_list = current_wl
)
data.frame(Specialty = df$Specialty[1], queue = tail(wl_queue_size(sim)[, 2],1),
mean_wait = wl_stats(sim)$mean_wait)
}
# Apply to each specialty in df_list
results_cur <- map(df_list, function(df) {
# Run 50 simulations for this specialty
sims <- future_replicate(50, sim_func_cur(df), simplify = FALSE)
# Combine into one data frame
bind_rows(sims)
})
#plan(sequential)
#saveRDs(results, "./data/results_didsinput2025.rds")
# Combine all specialties into one data frame
all_results_cur <- bind_rows(results_cur)
# Summarize mean and median queue per Specialty
summary_results_cur <- all_results_cur %>%
group_by(Specialty) %>%
summarise(
mean_queue = mean(queue, na.rm = TRUE),
median_queue = median(queue, na.rm = TRUE),
.groups = "drop"
)
#summary_results
#print(df_list[[1]], n = 92) # 15458 110
#print(df_list[[2]], n = 92) # 163286 99999
# Update df_list last row plus 1 to get to end of 2025/26row 19 with median_queue
df_list <- map(df_list, function(df) {
dscr <- df$Specialty[1]
mean_val <- round(summary_results_cur$mean_queue[summary_results_cur$Specialty == dscr])
df$Waiting.list.size[i] <- mean_val
df$wl_performance_cur[i] <- est_wait_performance(df$Referrals[i], df$Waiting.list.size[i], 18)
df
})
# Now using relief capacity instead
sim_func_rel <- function(df) {
current_wl <- data.frame(
Referral = rep(as.Date(df$start_date[i] - 1)
, df$Waiting.list.size_relief[i - 1]),
Removal = rep(as.Date(NA), df$Waiting.list.size_relief[i - 1])
)
sim_rel <- wl_simulator_cpp(
start_date = as.Date(df[i,]$start_date),
end_date = as.Date(df[i,]$end_date),
demand = df[i,]$Referrals,
capacity = coalesce(df[i,]$relief_capacity_rel, df[i,]$Removals), # Coalesce added here to counter against NA's is targets dont' start at beginning of projection period.
waiting_list = current_wl
)
data.frame(Specialty = df$Specialty[1], queue = tail(wl_queue_size(sim_rel)[, 2],1),
mean_wait = wl_stats(sim_rel)$mean_wait)
}
# Apply to each specialty in df_list
results_rel <- map(df_list, function(df) {
# Run 50 simulations for this specialty
sims_rel <- future_replicate(50, sim_func_rel(df), simplify = FALSE)
# Combine into one data frame
bind_rows(sims_rel)
})
#plan(sequential)
#saveRDs(results, "./data/results_didsinput2025.rds")
# Combine all specialties into one data frame
all_results_rel <- bind_rows(results_rel)
# Summarize mean and median queue per Specialty
summary_results_rel <- all_results_rel %>%
group_by(Specialty) %>%
summarise(
mean_queue = mean(queue, na.rm = TRUE),
median_queue = median(queue, na.rm = TRUE),
.groups = "drop"
)
#summary_results
#print(df_list[[1]], n = 92) # 15458 110
#print(df_list[[2]], n = 92) # 163286 99999
#j <<- j + 1
# Update df_list last row plus 1 to get to end of 2025/26row 19 with median_queue
df_list <- map(df_list, function(df) {
dscr <- df$Specialty[1]
mean_val <- round(summary_results_rel$mean_queue[summary_results_rel$Specialty == dscr])
df$Waiting.list.size_relief[i] <- mean_val
df$wl_performance_rel[i] <- est_wait_performance(df$Referrals[i], df$Waiting.list.size_relief[i], 18)
df
})
}
parallel::stopCluster(cl)
gc()
plan(sequential)
#502, x02,
#print(df_list[[16]], n = 90)
#View(df_list[[16]])
#.x <- df_list[[1]]
######### Now add new capacity column #########################
df_list <- map(df_list, function(.x) {
# .x$calc_capacity <- round(ifelse(.x$start_date >= target_dts[1,]$startdate & .x$start_date < target_dts[1,]$enddate,
# ifelse(.x$relief_capacity_65 < .x$Removals, .x$Removals, .x$relief_capacity_65),
# ifelse(.x$start_date >= target_dts[2,]$startdate & .x$start_date < target_dts[2,]$enddate,
# .x$relief_capacity_92,
# ifelse(.x$start_date < target_dts[1,]$startdate, .x$Removals,
# .x$Referrals)))) # have to keep pace with demand
#
# .x
# Version used in first activity plan with 65, then 92
# .x$calc_capacity <- ceiling(ifelse(.x$start_date < target_dts[1,]$startdate,
# .x$Removals, # Actual capacity
# ifelse(.x$start_date > target_dts[2,]$enddate,
# .x$target_capacity, # peg at target capacity for
# # ifelse(.x$start_date >= tail(target_dts,1)$enddate,
# # .x$target_capacity,
# .x$relief_capacity_rel))#) # Calcualted relief capacity
# )
# Version used in second activity plan with 65, then 92
.x$calc_capacity <- ceiling(ifelse(.x$start_date < target_dts[1,]$startdate,
.x$Removals, # Actual capacity
ifelse(.x$start_date > target_dts[3,]$enddate,
.x$target_capacity, # peg at target capacity for
# ifelse(.x$start_date >= tail(target_dts,1)$enddate,
# .x$target_capacity,
.x$relief_capacity_rel))#) # Calcualted relief capacity
)
#92% only assumption
# .x$calc_capacity <- ceiling(ifelse(.x$start_date < target_dts[1,]$startdate,
# .x$Removals, # Actual capacity
# ifelse(.x$start_date > target_dts[1,]$enddate,
# .x$target_capacity, # peg at target capacity for
# # ifelse(.x$start_date >= tail(target_dts,1)$enddate,
# # .x$target_capacity,
# .x$relief_capacity_rel))#) # Calcualted relief capacity
#)
.x
})
#print(df_list[[5]], n = 90)
#View(df_list[[5]])
#a <- df_list[[1]]
################################################################
#library(furrr)
#library(future.mirai)
# Create workers and pre-load Rcpp compilation on each
cl <- future::makeClusterPSOCK(workers = 6)
clusterEvalQ(cl, {
library(BSOLwaitinglist)
})
# Does the Rcpp wrapper exist on workers?
parallel::clusterEvalQ(cl, exists("bsol_montecarlo_WL3", mode = "function"))
# Use the cluster in your plan
plan(cluster, workers = cl)
#plan(sequential)
#plansequential()#plan(mirai_multisession, workers = 6)
start_time <- Sys.time()
#df_list2 <- df_list[1]
#df <- df_list[[1]]
sim_results_rel <- map(df_list, function(df) {
#Rcpp::sourceCpp("wl_simulator.cpp")
# Extract starting_wl from first row
start_wl <- df[last_data_row, "Waiting.list.size_relief", drop = TRUE]
if (is.na(start_wl)) start_wl <- 0
df <- df[df$start_date >= target_dts$startdate[1],]
#df <- df[df$start_date >= last_data_date,]
# Inner parallel map (optional)
future_map(1:50, function(i) {
#Rcpp::sourceCpp("wl_simulator.cpp")
bsol_montecarlo_WL3(
.data = df,
run_id = i,
start_date_name = "start_date",
end_date_name = "end_date",
adds_name = "Referrals",
removes_name = "calc_capacity",
starting_wl = start_wl
)
}, .options = furrr_options(seed = NULL, globals = TRUE
, packages = c("Rcpp", "NHSRwaitinglist", "BSOLwaitinglist")
)
)
})
end_time <- Sys.time()
#saveRDS(sim_results, "./data/bsol_sims.rds")
end_time - start_time
#inner_sequential <- end_time - start_time
# end parallel sessions
#plan(sequential)
#tail(sim_results[[1]][[2]])
# Bind each run together within first level of list, per speciality (each list slot is specialty)
mc_bind_rel <- map(sim_results_rel, function(.x) {do.call("rbind", .x)})
# Aggregate each function within list slot (each list slot is specialty)
mc_agg_rel <- map(mc_bind_rel, function(.x) {
aggregate(
queue_size ~ dates
, data = .x
, FUN = \(x) {
c(mean_q = mean(x),
median_q = median(x),
lower_95CI = mean(x) - (1.96 * (sd(x) / sqrt(length(x)))),
upper_95CI = mean(x) + (1.96 * (sd(x) / sqrt(length(x)))),
q_25 = quantile(x, .025, names = FALSE),
q_75 = quantile(x, .975, names = FALSE))
}
)
})
#mc_agg_rel[[1]]
# Rename funky column names form nest list in aggregate step
mc_agg_rel <- map(mc_agg_rel, ~ data.frame(
dates = as.Date(.x$dates),
unlist(.x$queue_size)
)
)
#plan(mirai_multisession, workers = 6)
start_time <- Sys.time()
#df_list2 <- df_list[1]
#df <- df_list2[[1]]
sim_results_cur <- map(df_list, function(df) {
#Rcpp::sourceCpp("wl_simulator.cpp")
# Extract starting_wl from first row
start_wl <- df[last_data_row, "Waiting.list.size", drop = TRUE]
if (is.na(start_wl)) start_wl <- 0
df <- df[df$start_date >= target_dts$startdate[1],]
#df <- df[df$start_date >= last_data_date,]
# Inner parallel map (optional)
future_map(1:50, function(i) {
#Rcpp::sourceCpp("wl_simulator.cpp")
bsol_montecarlo_WL3(
.data = df,
run_id = i,
start_date_name = "start_date",
end_date_name = "end_date",
adds_name = "Referrals",
removes_name = "Removals",
starting_wl = start_wl
)
}, .options = furrr_options(seed = NULL, globals = TRUE))
}
)
# ggplot(b, aes(y=queue_size, x=dates, col = run_id))+
# geom_line()
end_time <- Sys.time()
#saveRDS(sim_results, "./data/bsol_sims.rds")
end_time - start_time
#inner_sequential <- end_time - start_time
# end parallel sessions
plan(sequential)
stopCluster(cl)
# Bind each run together within first level of list, per speciality (each list slot is specialty)
mc_bind_cur <- map(sim_results_cur, function(.x) {do.call("rbind", .x)})
# Aggregate each function within list slot (each list slot is specialty)
mc_agg_cur <- map(mc_bind_cur, function(.x) {
aggregate(
queue_size ~ dates
, data = .x
, FUN = \(x) {
c(mean_q = mean(x),
median_q = median(x),
lower_95CI = mean(x) - (1.96 * (sd(x) / sqrt(length(x)))),
upper_95CI = mean(x) + (1.96 * (sd(x) / sqrt(length(x)))),
q_25 = quantile(x, .025, names = FALSE),
q_75 = quantile(x, .975, names = FALSE))
}
)
})
#mc_agg_cur[[1]]
# Rename funk column names form nest list in aggregate step
mc_agg_cur <- map(mc_agg_cur, ~ data.frame(
dates = as.Date(.x$dates),
unlist(.x$queue_size)
)
)
gc()
# install.packages("writexl")
# Ensure list elements are named (used as sheet names)
names(df_list) <- model_group |> distinct(Specialty) |> pull()
write_xlsx(df_list, path = paste0("./output/sense_check_includes_nondemo/", group_name, ".xlsx"))
out_long <- bind_rows(df_list, .id = "source")
write_csv(out_long, paste0("./output/sense_check_includes_nondemo/", group_name, "_long.csv"))
# ggplot elements.
# --- Sanity checks: all lists are the same length ---
n <- length(df_list)
stopifnot(
length(mc_bind_cur) == n,
length(mc_agg_cur) == n,
length(mc_bind_rel) == n,
length(mc_agg_rel) == n
)
# --- Output folder ---
out_dir <- "output/sense_check_includes_nondemo"
dir.create(out_dir, showWarnings = FALSE)
# Optional: annotation text per plot (or just keep single string)
ann_labels <- rep("Target waiting list for 92%\nat 18 weeks 2028/29", n)
# --- Helpers for rounded y-scale ---
round_up <- function(x, to) ceiling(x / to) * to
choose_step <- function(ymax) {
# Choose a “nice” step in 100s or 1000s based on magnitude
if (is.na(ymax) || ymax <= 0) return(100)
if (ymax <= 1000) return(100)
else if (ymax <= 2000) return(250)
else if (ymax <= 6000) return(500)
else if (ymax <= 12000) return(1000)
else if (ymax <= 30000) return(2000)
else if (ymax <= 100000) return(5000)
else return(10000)
}
# --- Plotting function ---
make_plot <- function(
i,
target_row = NULL,
target_date = NULL,
target_date_fmt = "%d/%m/%Y", # set to NULL if target_date is already Date
ann_label = ann_labels[i]
) {
df <- df_list[[i]]
cur_bind <- mc_bind_cur[[i]]
cur_agg <- mc_agg_cur[[i]]
rel_bind <- mc_bind_rel[[i]]
rel_agg <- mc_agg_rel[[i]]
# --- Coerce x variables to Date (robust to character/POSIXct) ---
coerce_date <- function(x, fmt = NULL) {
if (inherits(x, "Date")) return(x)
if (inherits(x, "POSIXt")) return(as.Date(x))
if (is.character(x)) {
if (!is.null(fmt)) return(as.Date(x, fmt))
# Fallback: try ISO then UK
out <- suppressWarnings(as.Date(x)) # ISO
if (any(is.na(out))) out <- suppressWarnings(as.Date(x, "%d/%m/%Y")) # UK
return(out)
}
# As last resort
suppressWarnings(as.Date(x))
}
df$start_date <- coerce_date(df$start_date)
df$end_date <- coerce_date(df$end_date)
cur_bind$dates <- coerce_date(cur_bind$dates)
cur_agg$dates <- coerce_date(cur_agg$dates)
rel_bind$dates <- coerce_date(rel_bind$dates)
rel_agg$dates <- coerce_date(rel_agg$dates)
# Ensure aggregated frames are sorted (ribbon likes ordered x)
cur_agg <- dplyr::arrange(cur_agg, dates)
rel_agg <- dplyr::arrange(rel_agg, dates)
# --- Target row/date selection ---
idx <- NA_integer_
if (!is.null(target_row)) {
idx <- target_row
} else if (!is.null(target_date)) {
td <- if (inherits(target_date, "Date")) target_date
else coerce_date(target_date, fmt = target_date_fmt)
idx <- match(as.Date(td), as.Date(df$start_date))
}
if (is.na(idx) || idx < 1 || idx > nrow(df)) {
warning(glue::glue("Plot {i}: target row/date not found; defaulting to row 30."))
idx <- min(30L, nrow(df))
}
hline <- suppressWarnings(as.numeric(df$target_wl[idx]))
cutoff_date <- as.Date('2026-07-01', "%Y-%m-%d")
#if (!is.null(target_date)) coerce_date(target_date, fmt = target_date_fmt)
#else df$start_date[idx]
ann_x <- if (!is.null(target_date)) coerce_date(target_date, fmt = target_date_fmt)
else as.Date("2025-01-01")
# --- Dynamic y-axis ---
round_up <- function(x, to) ceiling(x / to) * to
choose_step <- function(ymax) {
if (is.na(ymax) || ymax <= 0) return(100)
if (ymax <= 100) 20 else
if (ymax <= 500) 50 else
if (ymax <= 1500) 100 else
if (ymax <= 6000) 500 else
if (ymax <= 12000) 1000 else
if (ymax <= 30000) 2000 else
if (ymax <= 100000) 5000 else 10000
}
ymax_raw <- suppressWarnings(max(
df$Waiting.list.size,
as.numeric(cur_bind$queue_size),
as.numeric(rel_bind$queue_size),
as.numeric(cur_agg$upper_95CI),
as.numeric(rel_agg$upper_95CI),
hline,
na.rm = TRUE
))
step <- choose_step(ymax_raw)
y_top <- round_up(ymax_raw, step)
ann_y <- pmin(hline * 0.6, y_top * 0.9)
# Title with Specialty
spec <- dplyr::coalesce(
df$Specialty[1] %||% NA
#df$specialty[1] %||% NA
)
if(substr(group_name,1,2) == "bc"){
title_base <- "Simulated BC waiting list"
} else {
title_base <- "Simulated BSOL waiting list"
}
title_txt <- if (!is.na(spec)) paste0(title_base, " for treatment specialty ", spec) else title_base
ggplot() +
geom_line(
aes(x = end_date, y = Waiting.list.size),
col = "black",
data = dplyr::filter(df, start_date < cutoff_date)
) +
geom_line(
aes(x = dates, y = queue_size, group = run_id),
alpha = 0.4, col = "#A6CEE3", data = as.data.frame(cur_bind)
) +
geom_ribbon(
aes(x = dates, y = mean_q, ymin = lower_95CI, ymax = upper_95CI),
alpha = 0.5, data = cur_agg, fill = "#1F78B4"
) +
geom_line(aes(x = dates, y = mean_q), data = cur_agg, col = "#1F78B4") +
geom_line(
aes(x = dates, y = queue_size, group = run_id),
alpha = 0.3, col = "#B2DF8A", data = as.data.frame(rel_bind)
) +
geom_ribbon(
aes(x = dates, y = mean_q, ymin = lower_95CI, ymax = upper_95CI),
alpha = 0.5, data = rel_agg, fill = "#33A02C"
) +
geom_line(aes(x = dates, y = mean_q), data = rel_agg, col = "#33A02C") +
geom_hline(yintercept = hline, col = "#FF7F00") +
annotate("text", x = ann_x, y = ann_y, label = ann_label,
col = "#FF7F00", hjust = 0.1, vjust = 0.1) +
scale_y_continuous(
labels = scales::comma,
breaks = seq(0, y_top, by = step),
limits = c(0, y_top),
expand = c(0, 0)
) +
# Keep your overall limits; all x's are now Date
scale_x_date(
date_breaks = "6 months", date_labels = "%b-%y",
date_minor_breaks = "3 months",
limits = as.Date(c("2025-04-01", "2031-04-01")),
expand = c(0, 0)
) +
guides(x = guide_axis(check.overlap = TRUE, n.dodge = 2)) +
labs(
y = "Queue Size", x = "Date",
title = title_txt,
subtitle = "Average WL over 50 runs, with 95% point-wise confidence interval"
) +
theme(
axis.text.x = element_text(angle = 0),
axis.line = element_line(color = "grey"),
axis.ticks = element_line(color = "grey"),
plot.margin = unit(c(2, 5, 2, 2), "mm")
)
}