generated from opensafely/research-template
/
report_seqtrialcox.R
113 lines (85 loc) · 3.27 KB
/
report_seqtrialcox.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
# # # # # # # # # # # # # # # # # # # # #
# This script:
# imports fitted cox models
# outputs time-dependent effect estimates for booster
#
# The script must be accompanied by two arguments,
# `treatment` - the exposure variable in the regression model
# `outcome` - the dependent variable in the regression model
# # # # # # # # # # # # # # # # # # # # #
# Preliminaries ----
# import command-line arguments ----
args <- commandArgs(trailingOnly=TRUE)
if(length(args)==0){
# use for interactive testing
removeobs <- FALSE
treatment <- "pfizer"
outcome <- "admitted"
} else {
removeobs <- TRUE
treatment <- args[[1]]
outcome <- args[[2]]
}
## Import libraries ----
library('tidyverse')
library('here')
library('glue')
library('survival')
## Import custom user functi
source(here("lib", "functions", "utility.R"))
source(here("lib", "functions", "survival.R"))
output_dir <- here("output", "models", "seqtrialcox", treatment, outcome)
## import globally defined study dates and convert to "Date"
study_dates <-
jsonlite::read_json(path=here("lib", "design", "study-dates.json")) %>%
map(as.Date)
postvaxcuts <- seq(0,7*12, 7)
model_tidy <- read_csv(fs::path(output_dir, "model_tidy.csv"))
model_effects <- model_tidy %>%
filter(str_detect(term, fixed("treated"))) %>%
mutate(
term=str_replace(term, pattern=fixed("treated:strata(recruit_dayssincepw)"), ""),
) %>%
mutate(
term_left = as.numeric(str_extract(term, "^\\d+")),
term_right = as.numeric(str_extract(term, "\\d+$")),
term_midpoint = (term_right+term_left)/2
)
write_csv(model_effects, path = fs::path(output_dir, "report_effects.csv"))
plot_effects <-
ggplot(data = model_effects) +
geom_point(aes(y=exp(estimate), x=term_midpoint, colour=model_descr), position = position_dodge(width = 1.8))+
geom_linerange(aes(ymin=exp(conf.low), ymax=exp(conf.high), x=term_midpoint, colour=model_descr), position = position_dodge(width = 1.8))+
geom_hline(aes(yintercept=1), colour='grey')+
scale_y_log10(
breaks=c(0.25, 0.33, 0.5, 0.67, 0.80, 1, 1.25, 1.5, 2, 3, 4),
sec.axis = dup_axis(name="<-- favours not boosting / favours boosting -->", breaks = NULL)
)+
scale_x_continuous(breaks=unique(model_effects$term_left), limits=c(min(model_effects$term_left), max(model_effects$term_right)+1), expand = c(0, 0))+
scale_colour_brewer(type="qual", palette="Set2", guide=guide_legend(ncol=1))+
labs(
y="Hazard ratio",
x="Days since booster dose",
colour=NULL
) +
theme_bw()+
theme(
panel.border = element_blank(),
axis.line.y = element_line(colour = "black"),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
strip.background = element_blank(),
strip.placement = "outside",
strip.text.y.left = element_text(angle = 0),
panel.spacing = unit(0.8, "lines"),
plot.title = element_text(hjust = 0),
plot.title.position = "plot",
plot.caption.position = "plot",
plot.caption = element_text(hjust = 0, face= "italic"),
legend.position = "bottom"
) +
NULL
plot_effects
## save plot
ggsave(filename=fs::path(output_dir, "report_effectsplot.svg"), plot_effects, width=20, height=15, units="cm")
ggsave(filename=fs::path(output_dir, "report_effectsplot.png"), plot_effects, width=20, height=15, units="cm")