generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frequency_tables_routine.R
127 lines (99 loc) · 4.75 KB
/
frequency_tables_routine.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
###############################################################
# This script creates summary statistics for all key variables
# for all people with a closed RTT pathway (May21-Apr22)
###############################################################
# For running locally only #
#setwd("C:/Users/aschaffer/OneDrive - Nexus365/Documents/GitHub/waiting-list")
#getwd()
## Import libraries
library('tidyverse')
library('lubridate')
library('here')
library('dplyr')
library('ggplot2')
library('zoo')
library('reshape2')
library('fs')
## Rounding function
source(here("analysis", "custom_functions.R"))
## Create directories if needed
dir_create(here::here("output", "clockstops"), showWarnings = FALSE, recurse = TRUE)
dir_create(here::here("output", "data"), showWarnings = FALSE, recurse = TRUE)
## Load data - orthopaedic routine only ##
ortho_routine_final <- read_csv(here::here("output", "data", "cohort_ortho_routine_clockstops.csv.gz"),
col_types = cols(rtt_start_date = col_date(format="%Y-%m-%d"),
rtt_end_date = col_date(format="%Y-%m-%d"),
reg_end_date = col_date(format="%Y-%m-%d"),
dod = col_date(format="%Y-%m-%d"),
end_date = col_date(format="%Y-%m-%d"),
rtt_start_month = col_date(format="%Y-%m-%d"),
rtt_end_month = col_date(format="%Y-%m-%d"))) %>%
mutate(gaba_any = (gabapentinoid_pre_count >= 1),
gaba_3plus = (gabapentinoid_pre_count >= 3),
nsaid_any = (nsaid_pre_count >= 1),
nsaid_3plus = (nsaid_pre_count >= 3),
tca_any = (tca_pre_count >= 1),
tca_3plus = (tca_pre_count >= 3),
ad_any = (antidepressant_pre_count >= 1),
ad_3plus = (antidepressant_pre_count >= 3))
############ Mean/median age ###############
quantile <- scales::percent(c(.25,.5,.75))
age_stats <- ortho_routine_final %>%
summarise(p25 = quantile(age, .25, na.rm=TRUE),
p50 = quantile(age, .5, na.rm=TRUE),
p75 = quantile(age, .75, na.rm=TRUE)) %>%
mutate(variable = "Age summary statistics",
prior_opioid_rx = "Full cohort",
cohort = "Orthopaedic - Routine/Admitted")
age_stats_prior <- ortho_routine_final %>%
group_by(prior_opioid_rx) %>%
summarise(p25 = quantile(age, .25, na.rm=TRUE),
p50 = quantile(age, .5, na.rm=TRUE),
p75 = quantile(age, .75, na.rm=TRUE)) %>%
mutate(variable = "Age summary statistics",
cohort = "Orthopaedic - Routine/Admitted")
age_stats_combined <- rbind(age_stats, age_stats_prior)
write.csv(age_stats_combined, here::here("output", "clockstops", "age_stats.csv"),
row.names = FALSE)
########### Categorical variable relative frequency distributions #############
############ By prior opioid rx ###############################################
# Prior opioid rx only
dat <- ortho_routine_final %>%
subset(prior_opioid_rx == TRUE)
prior_yes <- cat_dist_combined() %>%
rbind(cat_dist(hip_hrg, "Hip procedure"),
cat_dist(knee_hrg, "Knee procedure"),
cat_dist(gaba_any, "Gabapentinoids (any)"),
cat_dist(gaba_3plus, "Gabapentinoids (>=3)"),
cat_dist(nsaid_any, "NSAID (any)"),
cat_dist(nsaid_3plus, "NSAID (>=3)"),
cat_dist(tca_any, "TCA (any)"),
cat_dist(tca_3plus, "TCA (>=3)"),
cat_dist(ad_any, "Antidepressant (any)"),
cat_dist(ad_3plus, "Antidepressant (>=3)")) %>%
rename(count_prior_opioid = count, total_prior_opioid = total)
# No prior opioid rx only
dat <- ortho_routine_final %>%
subset(prior_opioid_rx == FALSE)
prior_no <- cat_dist_combined() %>%
rbind(cat_dist(hip_hrg, "Hip procedure"),
cat_dist(knee_hrg, "Knee procedure"),
cat_dist(gaba_any, "Gabapentinoids (any)"),
cat_dist(gaba_3plus, "Gabapentinoids (>=3)"),
cat_dist(nsaid_any, "NSAID (any)"),
cat_dist(nsaid_3plus, "NSAID (>=3)"),
cat_dist(tca_any, "TCA (any)"),
cat_dist(tca_3plus, "TCA (>=3)"),
cat_dist(ad_any, "Antidepressant (any)"),
cat_dist(ad_3plus, "Antidepressant (>=3)")) %>%
rename(count_opioid_naive = count, total_opioid_naive = total)
# Merge
cat_dist <- list(prior_yes, prior_no) %>%
reduce(full_join, by=c("category","var","cohort")) %>%
filter(category != FALSE) %>%
arrange(var, category)
cat_dist <- cat_dist[,c("cohort", "var", "category",
"count_prior_opioid", "total_prior_opioid",
"count_opioid_naive", "total_opioid_naive")]
write.csv(cat_dist, here::here("output", "clockstops", "cat_var_dist_prior.csv"),
row.names = FALSE)