forked from nhs-r-community/NHSRplotthedots
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHigh Intensity Service Users - sample dataset.Rmd
382 lines (289 loc) · 13.9 KB
/
High Intensity Service Users - sample dataset.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
---
title: "High Intensity Users - Dummy Dataset"
author: "DPT Informatics"
date: "13/09/2023"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning=FALSE)
library(dplyr)
library(wakefield)
library(openxlsx)
library(stringr)
library(lubridate)
```
SELECT ClientID, COUNT(*) AS [Number of Referrals in Last 6 Months],
sum(COUNT(*)) over(order by COUNT(*) DESC rows unbounded preceding) as runningtotal
FROM *table*
WHERE Date >= DATEADD(month, -6, GETDATE())
GROUP BY ClientID
ORDER BY COUNT(*) DESC
```{r}
# ClientID
# [Number of Referrals in Last 6 Months]
# runningtotal
attendances_per_client_df <- wakefield::r_data_frame(n = 472,
ClientID = wakefield::id(),
`Number of Referrals in Last 6 Months` = rlnorm(meanlog=2, sdlog=1.2)
) %>%
mutate(`Number of Referrals in Last 6 Months` = ceiling(`Number of Referrals in Last 6 Months`)) %>%
arrange(desc(`Number of Referrals in Last 6 Months`)) %>%
mutate(runningtotal = cumsum(`Number of Referrals in Last 6 Months`)) %>%
mutate(ClientID = glue::glue("1000{ClientID}"))
attendances_per_client_df %>%
write.xlsx(file="attendances_per_client_dummy_data.xlsx")
```
```{r}
high_intensity_users <- attendances_per_client_df %>%
slice_head(n=30) %>%
select(-runningtotal) %>%
rename(`FRS Calls in Last 12 Months` = `Number of Referrals in Last 6 Months`)
high_intensity_users %>%
write.xlsx("high_intensity_users_dummy_data.xlsx")
client_ids <- high_intensity_users %>% select(ClientID) %>% pull()
```
# Begin by creating dataset for type of interest (in this case FRS) thaat will match the specified number of calls
```{r}
list_frs_dfs <- list()
for (client in (1:length(client_ids))) {
client_row <- high_intensity_users[client,]
this_client_id <- client_row %>% select(ClientID) %>% pull() %>% as.character()
n <- client_row %>% select(`FRS Calls in Last 12 Months`) %>% pull()
list_frs_dfs[[client]] <- wakefield::r_data_frame(
n = n,
ClientID = this_client_id,
Type = "Crisis Line Call",
Label = "Crisis Line Call",
Date = wakefield::dob(start=Sys.Date() - 365 * 7, k = 365 * 7),
Time = wakefield::time_stamp(random=TRUE, x=seq(0, 23, by = 0.1)),
Triage = wakefield::r_sample_factor(c("A - Emergency", "B - 4 Hour Response", "C - 24 Hour Response", "D - 48 Hour Response", "E - Requires Specialist Mental Health Assessment but Stable", "F - Not requiring face to face response, specify ongoing support", "G - Advice or information only"),
prob=c(0.02, 0.04, 0.05, 0.07, 0.15, 0.25, 0.42))
)
}
frs <- bind_rows(list_frs_dfs) %>%
mutate(EndDate = NULL) %>%
mutate(`Additional Tooltip` = glue::glue("Initial Triage Rating: {Triage} </br> Call Time: {substr(Time, 1, 5)}")) %>%
select(-c(Time, Triage))
```
### Create list of place of safety attendances
```{r}
list_pos_dfs <- list()
for (client in (1:length(client_ids))) {
client_row <- high_intensity_users[client,]
this_client_id <- client_row %>% select(ClientID) %>% pull() %>% as.character()
n <- (rnorm(n=1, mean=7, sd=20) %>% round(0) %>% abs()) + 1
list_pos_dfs[[client]] <- wakefield::r_data_frame(
n = n,
ClientID = this_client_id,
Type = "Place of Safety (POS)",
Label = "Place of Safety (POS)",
Date = wakefield::dob(start=Sys.Date() - 365 * 7, k = 365 * 7),
Time = wakefield::time_stamp(random=TRUE, x=seq(0, 23, by = 0.1)),
Transport = wakefield::r_sample_factor(c("Ambulance", "Police", "Other", "Not Recorded"),
prob=c(0.1, 0.6, 0.2, 0.1))
)
}
pos <- bind_rows(list_pos_dfs) %>%
mutate(EndDate = NULL) %>%
mutate(`Additional Tooltip` = glue::glue("Arrival Method: {Transport} </br> Arrival Time: {substr(Time, 1, 5)}")) %>%
select(-c(Time, Transport))
```
### Create list of liaison psychiatry attendances
```{r}
list_liaison_dfs <- list()
for (client in (1:length(client_ids))) {
client_row <- high_intensity_users[client,]
this_client_id <- client_row %>% select(ClientID) %>% pull() %>% as.character()
n <- (rnorm(n=1, mean=7, sd=20) %>% round(0) %>% abs()) + 1
list_liaison_dfs[[client]] <- wakefield::r_data_frame(
n = n,
ClientID = this_client_id,
Type = "Liaison Psychiatry",
Label = "Liaison Psychiatry",
Date = wakefield::dob(start=Sys.Date() - 365 * 7, k = 365 * 7),
Time = wakefield::time_stamp(random=TRUE, x=seq(0, 23, by = 0.1)),
Location = wakefield::r_sample_factor(c("AMU", "ED", "DGH", "Other"),
prob=c(0.1, 0.8, 0.1, 0.1))
)
}
liaison <- bind_rows(list_liaison_dfs) %>%
mutate(EndDate = NULL) %>%
mutate(`Response Time Target` = case_when(Location == "ED" | Location == "AMU" ~ "1 hour", TRUE ~ "24 hours")) %>%
rowwise() %>%
mutate(`Response Time` = case_when(`Response Time Target` == "1 hour" ~ rnorm(1, 80, 13), TRUE ~ (rnorm(1, 1440, 140))/24)) %>%
ungroup() %>%
mutate(`Response Time` = `Response Time` %>% round(0)) %>%
mutate(`Response Time` = case_when(`Response Time Target` == "1 hour" ~ paste(`Response Time`, "minutes"), TRUE ~ paste(`Response Time`, "hours"))) %>%
mutate(`Additional Tooltip` = glue::glue("Client Location: {Location} </br>Arrival Time: {substr(Time, 1, 5)}</br>Response Time: {`Response Time`} (target: {`Response Time Target`})")) %>%
select(-c(Time, Location, `Response Time Target`, `Response Time`))
```
### Next create a list of referrals for these clients
```{r}
list_referral_dfs <- list()
for (client in (1:length(client_ids))) {
client_row <- high_intensity_users[client,]
this_client_id <- client_row %>% select(ClientID) %>% pull() %>% as.character()
n <- (rnorm(n=1, mean=9, sd=5) %>% round(0) %>% abs()) + 1
list_referral_dfs[[client]] <- wakefield::r_data_frame(
n = n,
ClientID = this_client_id,
Type = "Referral",
Label = wakefield::r_sample_factor(c("Core Adult Mental Health Team",
"Crisis Home Treatment Team",
"Psychology",
"Support Into Employment",
"Personality Disorders",
"Adult ADHD",
"Adult Autistic Spectrum Service",
"Mental Health Assessment Team",
"STEP Team"),
prob=c(0.5, 0.2, 0.1, 0.05, 0.05, 0.05, 0.02, 0.02, 0.01 )),
Date = wakefield::dob(start=Sys.Date() - 365 * 7, k = 365 * 7),
ReferralLengthDays = wakefield::normal(365, 365),
Reason = wakefield::r_sample_factor(c("Anxiety", "Depression", "Personality Disorders"), prob=c(0.45, 0.45, 0.1))
)
}
referrals <- bind_rows(list_referral_dfs) %>%
rowwise() %>%
mutate(ReferralLengthDays = case_when(Label == "Crisis Home Treatment Team" ~ rnorm(1, 12, 3), TRUE ~ ReferralLengthDays )) %>%
ungroup() %>%
# Order each client's referrals to a particular team by the order in which they tool place
group_by(ClientID, Label) %>%
arrange(ClientID, Label, Date) %>%
mutate(RowNumber = row_number()) %>%
mutate(EndDate = Date + abs(ReferralLengthDays)) %>%
mutate(Date = case_when(RowNumber > 1 ~ as.Date(NA), TRUE ~ Date),
EndDate = case_when(RowNumber > 1 ~ as.Date(NA), TRUE ~ EndDate)) %>%
mutate(Date = case_when(RowNumber == 2 ~ lag(EndDate, 1) + abs(rnorm(1, 365, 120)), TRUE ~ Date) ) %>%
mutate(EndDate = Date + abs(ReferralLengthDays)) %>%
mutate(Date = case_when(RowNumber == 3 ~ lag(EndDate, 1) + abs(rnorm(1, 365, 120)), TRUE ~ Date) ) %>%
mutate(EndDate = Date + abs(ReferralLengthDays)) %>%
mutate(Date = case_when(RowNumber == 4 ~ lag(EndDate, 1) + abs(rnorm(1, 365, 120)), TRUE ~ Date) ) %>%
mutate(EndDate = Date + abs(ReferralLengthDays)) %>%
ungroup() %>%
filter(RowNumber <= 4) %>%
filter(Date <= Sys.Date()) %>%
mutate(EndDate = case_when(EndDate > Sys.Date() ~ as.Date(NA), TRUE ~ EndDate)) %>%
mutate(Reason = case_when(Label == "Crisis Home Treatment Team" ~ "Mental Health Crisis",
Label == "STEP Team" ~ "First Episode Psychosis",
Label == "Adult ADHD" & RowNumber == 1 ~ "ADHD Diagnosis",
Label == "Adult ADHD" & RowNumber > 1 ~ "ADHD Support",
Label == "Adult Autistic Spectrum Service" & RowNumber == 1 ~ "ASC Diagnosis",
Label == "Adult Autistic Spectrum Service" & RowNumber > 1 ~ "ASC Support",
TRUE ~ as.character(Reason))
) %>%
mutate(`Additional Tooltip` = glue::glue("Reason: {Reason}")) %>%
select(-c(Reason, RowNumber))
```
### Create individual contacts that took place
```{r}
contacts_df_lists <- list()
for (row in 1:nrow(referrals)) {
client_row <- referrals[row,]
this_client_id <- client_row %>% select(ClientID) %>% pull() %>% as.character()
n_contacts <- (rnorm(n=1, mean=12, sd=15) %>% round(0) %>% abs())
if (n_contacts > 1) {
contacts_df_lists[[row]] <- wakefield::r_data_frame(
n = n_contacts,
ClientID = this_client_id,
Type = "Contact",
Label = client_row %>% select(Label) %>% pull() %>% as.character(),
Date = wakefield::dob(start = client_row %>% select(Date) %>% pull(),
k = client_row %>% select(ReferralLengthDays) %>% pull() %>% abs()),
ReferralLengthDays = wakefield::normal(365, 365),
ContactMedium = wakefield::r_sample_factor(c("Face to Face",
"Telephone",
"Video Call",
"Text Message",
"Online Chat",
"Other"),
prob=c(0.5, 0.2, 0.15, 0.05, 0.05, 0.05)),
Attended = wakefield::r_sample_factor(c("Attended on Time",
"Attended Late but Seen",
"Attended Late - Not Possible to See",
"Did not Attend"),
prob=c(0.7, 0.1, 0.1, 0.1))
)
}
}
contacts <- bind_rows(contacts_df_lists) %>%
mutate(`Additional Tooltip` = glue::glue("Contact Medium: {ContactMedium} </br>{Attended}")) %>%
select(-c(Attended, ContactMedium, ReferralLengthDays) )
```
```{r}
referrals <- referrals %>%
select(-ReferralLengthDays)
```
### Create ward stays
```{r}
list_inpatient_dfs <- list()
for (client in (1:length(client_ids))) {
client_row <- high_intensity_users[client,]
this_client_id <- client_row %>% select(ClientID) %>% pull() %>% as.character()
n <- (rnorm(n=1, mean=2, sd=1) %>% round(0) %>% abs()) + 1
list_inpatient_dfs[[client]] <- wakefield::r_data_frame(
n = n,
ClientID = this_client_id,
Type = "Inpatient",
Label = wakefield::r_sample_factor(c("Zinc Ward",
"Copper Ward",
"Titanium House",
"Magnesium Heights",
"Tin Ward",
"Steel House",
"Strontium View",
"Cobalt Ward",
"Beryllium Crisis House")#,
#prob=c(0.5, 0.2, 0.1, 0.05, 0.05, 0.05, 0.02, 0.02, 0.01 )
)
,
Date = wakefield::dob(start=Sys.Date() - 365 * 7, k = 365 * 7),
StayLengthDays = wakefield::normal(30, 120)#,
#Reason = wakefield::r_sample_factor(c("Anxiety", "Depression", "Personality Disorders"), prob=c(0.45, 0.45, 0.1))
)
}
inpatient_stays <- bind_rows(list_inpatient_dfs) %>%
rowwise() %>%
mutate(StayLengthDays = case_when(Label == "Beryllium Crisis House" ~ rnorm(1, 12, 3), TRUE ~ StayLengthDays )) %>%
ungroup() %>%
# Order each client's referrals to a particular team by the order in which they tool place
group_by(ClientID) %>%
arrange(ClientID, Date) %>%
mutate(RowNumber = row_number()) %>%
mutate(EndDate = Date + abs(StayLengthDays)) %>%
mutate(Date = case_when(RowNumber > 1 ~ as.Date(NA), TRUE ~ Date),
EndDate = case_when(RowNumber > 1 ~ as.Date(NA), TRUE ~ EndDate)) %>%
mutate(Date = case_when(RowNumber == 2 ~ lag(EndDate, 1) + abs(rnorm(1, 30, 120)), TRUE ~ Date) ) %>%
mutate(EndDate = Date + abs(StayLengthDays)) %>%
mutate(Date = case_when(RowNumber == 3 ~ lag(EndDate, 1) + abs(rnorm(1, 30, 120)), TRUE ~ Date) ) %>%
mutate(EndDate = Date + abs(StayLengthDays)) %>%
mutate(Date = case_when(RowNumber == 4 ~ lag(EndDate, 1) + abs(rnorm(1, 30, 120)), TRUE ~ Date) ) %>%
mutate(EndDate = Date + abs(StayLengthDays)) %>%
ungroup() %>%
filter(RowNumber <= 4) %>%
filter(Date <= Sys.Date()) %>%
mutate(EndDate = case_when(EndDate > Sys.Date() ~ as.Date(NA), TRUE ~ EndDate)) %>%
mutate(`Additional Tooltip` = "") %>%
select(-c(RowNumber, StayLengthDays))
```
```{r}
inpatient_stays %>%
rename(AdmissionDate = Date,
DischargeDate = EndDate,
Specialty = Type,
Ward = Label) %>%
write.xlsx("inpatient_dummy_data.xlsx")
```
### Write to file
```{r}
referrals %>%
union_all(contacts) %>%
union_all(frs) %>%
union_all(pos) %>%
union_all(liaison) %>%
union_all(inpatient_stays) %>%
write.xlsx("community_simplified_dummy_data.xlsx")
```
```{r}
print("done")
```