-
Notifications
You must be signed in to change notification settings - Fork 0
/
13_excess_costs.R
131 lines (109 loc) · 4.13 KB
/
13_excess_costs.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
# Load packages ----
library(tidyverse)
library(finalfit)
# Source parameters ----
source("timestamp.R")
source("resource_parameters.R")
source("myfunctions.R")
# Create folder for LCMM model outputs -----
dir.create(here::here("02_outputs", "09_excess_cost"), recursive = TRUE,
showWarnings = FALSE)
# Bootstrap samples
n_bootstrap = 500
# Load datasets ----
survivor_index = read_rds(
here::here("01_data", paste0("survivor_index_", timestamp, ".rds")))
cluster_labeled = read_rds(
here::here("01_data", paste0("cluster_labeled_", timestamp, ".rds")))
resource_emerg_2yr_prior = read_rds(
here::here("01_data", paste0("resource_emerg_2yr_prior_", timestamp, ".rds")))
resource = read_rds(
here::here("01_data", paste0("resource_", timestamp, ".rds")))
# Join to cohort cluster and prior emergency data ----
survivor_index = survivor_index %>%
left_join(cluster_labeled, by = "PatientID") %>%
left_join(resource_emerg_2yr_prior, by = "PatientID") %>%
mutate(overall = "Overall" %>%
ff_label(" "))
# Determine annualised baseline (year -2 to -0.5) and followup (first 6 months) costs -----
resource_prepost = resource %>%
filter(measurement == "cost") %>%
group_by(PatientID, period) %>%
summarise(
n_days = first(n_days),
cost = sum(value)
) %>%
ungroup() %>%
mutate(
interval = case_when(
(period >= -24) & (period < -6) ~ "baseline",
(period > 0) & (period <= 6) ~ "followup",
TRUE ~ NA_character_
)
) %>%
drop_na(interval) %>%
group_by(PatientID, interval) %>%
summarise(
days = sum(n_days),
cost = sum(cost)
) %>%
pivot_wider(names_from = interval, values_from = c(days, cost)) %>%
mutate(
# Calucluate annualised costs
annualised_baseline = cost_baseline/days_baseline*365.25,
annualised_followup = cost_followup/days_followup*365.25,
annualised_excess = annualised_followup - annualised_baseline
)
# Filter for patients with at least 6 months potential follow-up -------
resource_prepost = resource_prepost %>%
left_join(
survivor_index %>%
mutate(
potential_followup_6months = if_else((discharge_date + days(180)) <= date_censor,
"Yes", NA_character_)
) %>%
select(PatientID, potential_followup_6months),
by = "PatientID"
) %>%
filter(potential_followup_6months == "Yes")
## Stratifying variables ----
var_statifaction = c(
"overall", "age.factor", "sex",
"n_comorb_charl.factor", "any_icu", "wave", "vacc_status_index",
"class_factor", "prior_emergency_beddays_factor"
)
# Extract labels
vlabel = extract_variable_label(survivor_index)
# Table of stratified baseline followup and excess costs
tbl_excess_cost = var_statifaction %>%
map(function(group_var){
resource_prepost %>%
left_join(survivor_index %>%
select(PatientID, grouping = all_of(group_var)), by = "PatientID") %>%
group_by(grouping) %>%
summarise(
statistic = c("mean", "ci_lower", "ci_upper"),
baseline = Hmisc::smean.cl.boot(annualised_baseline, B = n_bootstrap),
followup = Hmisc::smean.cl.boot(annualised_followup, B = n_bootstrap),
excess = Hmisc::smean.cl.boot(annualised_excess, B = n_bootstrap)
) %>%
pivot_longer(cols = c(baseline, followup, excess), names_to = "cost_type") %>%
pivot_wider(names_from = statistic) %>%
group_by(grouping, cost_type) %>%
summarise(
output = paste0(scales::comma_format()(mean), " (",
scales::comma_format()(ci_lower), "; ",
scales::comma_format()(ci_upper), ")")
) %>%
pivot_wider(names_from = "cost_type", values_from = "output") %>%
mutate(group_var = group_var,
group_label = vlabel[group_var]) %>%
relocate(group_var, group_label, group_level = grouping,
`Baseline (£/yr)` = baseline,
`Follow-up (£/yr)` = followup,
`Excess (£/yr)` = excess)
}) %>%
bind_rows()
## Save table ----
write_csv(tbl_excess_cost,
here::here("02_outputs", "09_excess_cost", "tbl_excess_cost.csv"))