-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathone row per variable.R
378 lines (316 loc) · 10.2 KB
/
one row per variable.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
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
## ==========================================================================##
# Project: DACHA
# Team: Improvement Analytics Unit (IAU) at the Health Foundation
# Script: one row per varaible.R
# Corresponding author: Freya Tracey ([email protected])
# Descripton: generate tables for paper which have one row per variable summary
# Inputs:
# MDS
# Outputs:
# Series of tables for reporting
# Notes: To use, need to adjust locations of R scripts and csv files
## ==========================================================================##
library (tidyverse)
library (dplyr)
library (aws.s3)
library (gtsummary)
library (ggplot2)
project_bucket <- '' # assign project directory
# Import data sets
MDS <- s3read_using(read.csv, object = "MDS.csv", bucket = project_bucket, na.strings = c("", "NA")) %>%
select (-X) %>%
mutate (
#barthel needs to be multiplied by 5 to be from 0-20 score to 0-100 score
barthel_score = ifelse (!is.na (barthel_score), barthel_score*5, NA)
)
MDS_variables_filters <- s3read_using(read.csv, object = "MDS_variables.csv", bucket = project_bucket, na.strings = c("", "NA"))
variables_to_exclude <- MDS_variables_filters %>%
filter (is.na(Filter.1)) %>%
select (
-Filter.1, -Filter.2, -Filter.3
) %>%
as.list()
frailty_conditions <- c ("f_anxdep_h36",
"f_dependence_h36",
"f_fallsfract_h36",
"f_incont_h36",
"f_mobprob_h36",
"f_pulcers_h36",
"f_senility_h36",
"cogimp_sus"
)
MDS_for_table <- MDS %>%
# select (-(any_of (variables_to_exclude[[1]]))) %>%
# need to change NAs to be missing for categorical variables so they are included in %
# remove NAs for all continuous variables - want everything to have the same denominator, but not for diagnoses
mutate (IMD_quintile = coalesce(IMD_quintile, "Missing"),
DiscussedPreferredDeathLocation_Indicator = coalesce(DiscussedPreferredDeathLocation_Indicator, "Missing"),
DeathLocationPreferred_Type = coalesce(DeathLocationPreferred_Type, "Missing"),
Client_Funding_Status_ASC = coalesce(Client_Funding_Status_ASC, "Missing"),
Der_CQC_Service_Type_CHR = coalesce(Der_CQC_Service_Type_CHR, "Missing"),
cogimp_sus = ifelse(
rowSums(select (., c(f_delirium_h36, f_dementia_h36, f_senility_h36)))>0, 1,0)) %>%
mutate (
n_frailty_conditions = rowSums (select(., all_of(frailty_conditions)))
)
#conditions
conditions_n <- MDS_for_table %>%
select (
starts_with ("c_"), # charlson variables
starts_with ("e_"), # elixhauser variables
starts_with ("ec_"), # variables in charlson and elixhauser
starts_with ("f_"), # frailty syndromes
) %>%
tbl_summary(
statistic = list(
all_categorical() ~ "{n}" # need small n suppression
),
digits = all_continuous() ~2
) %>%
add_n() %>%
modify_header (label ~ "Variable") %>%
as.data.frame() %>%
filter (!grepl( "Unknown", Variable)) %>%
select (- "**N**") %>%
rename (n= "**N = 727**")
conditions_pct <- MDS_for_table %>%
select (
starts_with ("c_"), # charlson variables
starts_with ("e_"), # elixhauser variables
starts_with ("ec_"), # variables in charlson and elixhauser
starts_with ("f_"), # frailty syndromes
) %>%
tbl_summary(
statistic = list(
all_categorical() ~ "{p}%" # need small n suppression
),
digits = all_continuous() ~2
) %>%
add_n() %>%
modify_header (label ~ "Variable") %>%
as.data.frame() %>%
filter (!grepl( "Unknown", Variable))
conditions <- conditions_n %>%
left_join(conditions_pct, by= "Variable") %>%
rename (pct= "**N = 727**")
# have removed unknowns - still need to have top elixhauser bit
conditions_overview_1 <- MDS_for_table %>%
select (
nr_elix_h36,
) %>%
tbl_summary(
statistic = list(
all_continuous() ~ "{mean} ({sd})"
),
digits = all_continuous() ~2
) %>%
add_n() %>%
modify_header (label ~ "Variable") %>%
as.data.frame()
conditions_overview_2 <- MDS_for_table %>%
select (
nr_elix_2_h36,
) %>%
tbl_summary(
statistic = list(
all_continuous() ~ "{mean} ({sd})"
),
digits = all_continuous() ~2
) %>%
add_n() %>%
modify_header (label ~ "Variable") %>%
as.data.frame() %>%
filter (!grepl( "Unknown", Variable))
conditions_final <- conditions_overview_1 %>%
bind_rows(
conditions_overview_2
) %>%
bind_rows(
conditions
)
### all continuous numbers
#to report a table which shows n with value of 1 or more, N with non missing value and % with an event need to turn into a categorical variable
MDS_for_table_binary <- MDS_for_table %>%
select (
"adm_em_12h",
"adm_el_12h",
"adm_avoid_12h",
"adm_lrti_12h",
"adm_urti_12h",
"ngproc_combined",
starts_with("n_")
) %>%
mutate (across (everything (),
~case_when(
. >=1 ~1,
TRUE ~0
),
.names = "{.col}_binary")) %>%
select (
ends_with("_binary")
)
# so now need to use this data set to report 3 columns - CHANGE HERE
percent <- MDS_for_table_binary %>%
pivot_longer(
cols=everything(),
names_to = "category",
values_to = "counts"
) %>%
group_by(category) %>%
na.omit() %>%
summarise(
total_n = n(),
happened = sum(counts)
) %>%
mutate(
percent =(happened/ total_n)
)
# now second part of table which is total activity, total N and mean and sd
all_counts_means <- MDS_for_table %>%
select (
"adm_em_12h",
"adm_el_12h",
"adm_avoid_12h",
"adm_lrti_12h",
"adm_urti_12h",
"ngproc_combined",
starts_with("n_")
) %>%
mutate_all(~ifelse (is.na(.),0,.)) %>% # want everything to be a 0 if its a NA
tbl_summary(
type = c(
"adm_em_12h",
"adm_el_12h",
"adm_avoid_12h",
"adm_lrti_12h",
"adm_urti_12h",
"ngproc_combined",
starts_with("n_")) ~ "continuous",
statistic = list(
all_continuous() ~ "{mean} ({sd})"
),
digits = all_continuous() ~2
) %>%
add_n()%>%
modify_header (label ~ "Variable") %>%
as.data.frame()
# need to add in having two columns, one with n/N and one with mean
col_sums <- MDS_for_table %>%
summarise (across (where(is.numeric), ~sum(., na.rm = TRUE), .names = "{.col}_colsum")) %>%
select (ends_with("_colsum")) %>%
pivot_longer(cols = everything (), names_to = "Variable", values_to = "Value") %>%
mutate (Value = round (Value)) %>%
mutate (Variable = str_remove (Variable, "_colsum"))
all_counts_activity_means_final <- all_counts_means%>%
left_join (col_sums, by = "Variable") %>%
filter (Variable != "Unknown") %>%
relocate (
Variable, Value, "**N**", "**N = 727**"
) %>%
rename ("Total activity" = "Value")
# now have to do the other parts of the table which have been missed
categorical_variables_n <- MDS_for_table %>%
select (
-adm_em_12h,
-adm_el_12h,
-adm_avoid_12h,
-starts_with("n_"),
-starts_with ("c_"),
-starts_with ("e_"),
-starts_with ("ec_"),
-starts_with ("f_"),
-nr_elix_2_h36,
-nr_elix_h36
)%>%
tbl_summary(
statistic = list(
all_categorical() ~ "{n}"
),
digits = all_continuous() ~2
) %>%
as.data.frame()
categorical_variables_N <- MDS_for_table %>%
select (
-adm_em_12h,
-adm_el_12h,
-adm_avoid_12h,
-starts_with("n_"),
-starts_with ("c_"),
-starts_with ("e_"),
-starts_with ("ec_"),
-starts_with ("f_"),
-nr_elix_2_h36,
-nr_elix_h36
)%>%
tbl_summary(
statistic = list(
all_categorical() ~ "{N}"
),
digits = all_continuous() ~2
) %>%
as.data.frame()
categorical_variables_pct <- MDS_for_table %>%
select (
-adm_em_12h,
-adm_el_12h,
-adm_avoid_12h,
-starts_with("n_"),
-starts_with ("c_"),
-starts_with ("e_"),
-starts_with ("ec_"),
-starts_with ("f_"),
-nr_elix_2_h36,
-nr_elix_h36
)%>%
tbl_summary(
statistic = list(
all_categorical() ~ "{p}%"
),
digits = all_continuous() ~2
) %>%
as.data.frame()
# continuous variables from care home record
continuous_chvariables <- MDS_for_table %>%
select (
waterlow_recent,
braden_recent,
iaged_score,
barthel_score,
ascot_scrqol,
icecap_qol,
eq5d_vas,
UK_crosswalk,
ascot_mascore_p
) %>%
tbl_summary(
type = everything () ~"continuous",
statistic = list(
all_continuous() ~ "{mean} ({sd})"
),
digits = all_continuous() ~2
) %>%
add_n()%>%
modify_header (label ~ "Variable") %>%
as.data.frame()
los <- MDS%>%
select (pseudonhsno, lengthofstay)%>%
mutate (lengthofstay = ifelse (lengthofstay >14000, NA, lengthofstay)) %>% #change record w >14000 to be missing
summarise (
mean = (mean (lengthofstay, na.rm = TRUE)),
sd = (sd(lengthofstay, na.rm = TRUE)))
CH_level_data <- MDS_for_table %>%
select (starts_with("Q3_"))%>%
summarise (across (everything (), list (
mean = ~round(mean (.x, na.rm = TRUE),2),
sd = ~round(sd(.x, na.rm = TRUE),2),
na_count= ~sum (is.na (.))
)))
s3write_using(CH_level_data, FUN=write.csv, object = "Linked data sets/CH_level_data.csv", bucket = project_bucket)
s3write_using(continuous_chvariables, FUN=write.csv, object = "Linked data sets/continuous_chvariables.csv", bucket = project_bucket)
s3write_using(all_counts_activity_means_final, FUN=write.csv, object = "Linked data sets/all_counts_activity_means_final.csv", bucket = project_bucket)
s3write_using(percent, FUN=write.csv, object = "Linked data sets/all_counts_percent_final.csv", bucket = project_bucket)
s3write_using(conditions_final, FUN=write.csv, object = "Linked data sets/conditions_final.csv", bucket = project_bucket)
#cant join these together as >1 row with label unknown
s3write_using(categorical_variables_n, FUN=write.csv, object = "Linked data sets/categorical_variables_n.csv", bucket = project_bucket)
s3write_using(categorical_variables_N, FUN=write.csv, object = "Linked data sets/categorical_variables_N.csv", bucket = project_bucket)
s3write_using(categorical_variables_pct, FUN=write.csv, object = "Linked data sets/categorical_variables_pct.csv", bucket = project_bucket)