generated from opensafely/research-template
/
waves_kaplan_meier.R
83 lines (76 loc) · 2.98 KB
/
waves_kaplan_meier.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
## ###########################################################
## This script:
## - Imports data of the three waves
## - Plots Kaplan Meiers
## - Saves Kaplan Meiers to ./output/figures
## linda.nab@thedatalab.com - 20220412
## ###########################################################
# Load libraries & functions ---
library(here)
library(readr)
library(purrr)
library(dplyr)
library(jsonlite)
library(survival)
library(survminer)
library(stringr)
## Load json file listing demographics, comorbidities and start dates waves
config <- fromJSON(here("analysis", "config.json"))
# create vector containing subgroups
subgroups_vctr <- c(config$demographics, config$comorbidities)
# source functions 'km_fit' and 'km_plot'
source(here("analysis", "utils", "plot_kaplan_meier.R"))
# vector with waves
waves_vctr <- c("wave1", "wave2", "wave3")
# Import data extracts of waves ---
input_files_processed <-
Sys.glob(here("output", "processed", "input_wave*.rds"))
data_processed <-
map(.x = input_files_processed,
.f = ~ readRDS(.x))
names(data_processed) <- waves_vctr
# Kaplan-Meier plotting ---
# Creates list containing 2 survfit objects ('Female' and 'Male') for each wave.
# This is a list of a list, first level = waves, second level is
# 'Females' and 'Males' and for each level a survfit object is saved
sfit_list_waves_list <-
map(.x = data_processed,
.f = ~ km_fit(.x))
# Creates list containing the plots of the survfit objects in
# 'sfit_list_waves_list'. It is a list of a list, first level = waves, second
# level is 'Females' and 'Males' and for each level a plot of the survfit
# objects is saved
plots_waves_list <-
map(.x = waves_vctr,
.f = ~ km_plot(sfit_list_waves_list,
data_processed,
.x))
# Name list before unlisting --> 'wave1', 'wave2', 'wave3'
names(plots_waves_list) <- waves_vctr
# Unlist (only first level), plot_waves_list is now a list with one level, with
# names 'wave1.Female', 'wave1.Male', etc...
plots_waves_list <- unlist(plots_waves_list, recursive = FALSE)
# Save output --
output_dir <- here("output", "figures", "kaplan_meier")
ifelse(!dir.exists(here("output", "figures")),
dir.create(here("output", "figures")),
FALSE) # create ./output/figures if not already there
ifelse(!dir.exists(output_dir),
dir.create(output_dir),
FALSE)
# change names of list of plots to the ones that will be used to save the file
# --> i.e., filename = 'wave1.Female' = 'wave1_F'
names(plots_waves_list) <-
names(plots_waves_list) %>%
str_replace("[.]", "_") %>%
substr(1, 7)
# save plots
walk(.x = names(plots_waves_list),
.f = ~ ggsave(plot = plots_waves_list[[.x]]$plot,
# need $plot as per
# https://github.com/kassambara/survminer/issues/152
filename = paste0(output_dir,
"/",
.x,
".png"),
device = "png"))