Last updated: 2023-06-16
Checks: 7 0
Knit directory: eGFRslopes/
This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20230613)
was run prior to running
the code in the R Markdown file. Setting a seed ensures that any results
that rely on randomness, e.g. subsampling or permutations, are
reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 7de25c8. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for
the analysis have been committed to Git prior to generating the results
(you can use wflow_publish
or
wflow_git_commit
). workflowr only checks the R Markdown
file, but you know if there are other scripts or data files that it
depends on. Below is the status of the Git repository when the results
were generated:
Ignored files:
Ignored: .RData
Ignored: .Rhistory
Ignored: .Rproj.user/
Untracked files:
Untracked: analysis/04longitudinal.Rmd
Untracked: analysis/05survival.Rmd
Untracked: analysis/07extractSlopes.Rmd
Untracked: analysis/misc_performanceChecks.Rmd
Untracked: code/calculateEGFR.R
Untracked: code/extractSlopes.R
Untracked: code/flagAKI.R
Untracked: code/syntheticData.R
Untracked: data/simulated_longitudinal_data.csv
Untracked: data/simulated_metadata.csv
Untracked: output/eGFR_meta.csv
Untracked: output/eGFR_minimal.csv
Untracked: output/eGFR_minimal_surv.csv
Untracked: output/fitJMbayes.RDS
Untracked: output/flagged_episodes.csv
Untracked: output/longitudinal_data.csv
Untracked: output/pred_long.csv
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were
made to the R Markdown (analysis/03prepareData.Rmd
) and
HTML (docs/03prepareData.html
) files. If you’ve configured
a remote Git repository (see ?wflow_git_remote
), click on
the hyperlinks in the table below to view the files as they were in that
past version.
File | Version | Author | Date | Message |
---|---|---|---|---|
Rmd | 7de25c8 | Charlotte Boys | 2023-06-16 | Publish main parts of framework |
Next, we prepare the data so it is ready for joint modelling. Since we are interested in the chronic eGFR trajectory, we filter out the episodes flagged as AKI and in-patient events.
Load necessary libraries:
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.2 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.0
✔ ggplot2 3.4.2 ✔ tibble 3.2.1
✔ lubridate 1.9.2 ✔ tidyr 1.3.0
✔ purrr 1.0.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Load data:
patient_metadata <- read.csv("data/simulated_metadata.csv")
flagged_episodes <- read.csv("output/flagged_episodes.csv")
longitudinal_data <- read.csv("output/longitudinal_data.csv")
First, let’s remind ourselves of how the flagged episodes data looked:
knitr::kable(head(flagged_episodes))
start | end | patient_id | n_obs | flag |
---|---|---|---|---|
1 | 5 | 2 | 4 | AKI |
2759 | 2759 | 2 | 1 | AKI |
3893 | 3893 | 2 | 1 | AKI |
6192 | 6192 | 2 | 1 | AKI |
6234 | 6234 | 2 | 1 | AKI |
0 | 13 | 3 | 5 | in-patient |
There were three types of flags:
unique(flagged_episodes$flag)
[1] "AKI" "in-patient" "AKI and in-patient"
We define a function to filter out the flagged episodes:
filter_flagged_episodes <- function(longitudinal_data,
flagged_episodes,
flags = c("AKI and in-patient", "AKI")){
to_remove <- flagged_episodes %>%
dplyr::filter(flag %in% flags)
patients_without_flags <- longitudinal_data %>%
dplyr::filter(!(patient_id %in% to_remove$patient_id)) %>%
dplyr::distinct()
patients_with_flags <- longitudinal_data %>%
dplyr::filter(patient_id %in% to_remove$patient_id)
check_observations <- dplyr::left_join(patients_with_flags,
to_remove,
by = "patient_id",
multiple = "all",
relationship = "many-to-many") %>%
dplyr::mutate(flagged = ifelse(days_from_biopsy >= start & days_from_biopsy <= end, TRUE, FALSE))
patients_with_flags_keep <- check_observations %>%
dplyr::group_by(patient_id, days_from_biopsy) %>%
dplyr::filter(!any(flagged == TRUE)) %>%
dplyr::select(-c(start, end, n_obs, flag, flagged)) %>%
dplyr::distinct()
patients_with_flags_remove <- check_observations %>%
dplyr::group_by(patient_id, days_from_biopsy) %>%
dplyr::filter(any(flagged == TRUE)) %>%
dplyr::select(-c(start, end, n_obs, flag, flagged)) %>%
dplyr::distinct()
stopifnot("Sum of rows to keep and rows to remove does not equal original number of rows" = nrow(patients_with_flags_keep) + nrow(patients_with_flags_remove) == nrow(patients_with_flags))
measurements_filtered <- rbind(patients_without_flags, patients_with_flags_keep)
return(measurements_filtered)
}
We filter the eGFR measurements, removing those taken during episodes of care flagged as meeting both the AKI and in-patient criteria:
eGFR_filtered <- filter_flagged_episodes(longitudinal_data,
flagged_episodes,
flags = c("AKI and in-patient")) %>%
dplyr::filter(type == "eGFR")
In our simulated longitudinal data, we only have one clinical endpoint event of interest: dialysis. We gather information about which patients reached the clinical endpoint, at which event time:
endpoints <- c("Dialysis")
event_time <- longitudinal_data %>%
dplyr::filter(type %in% endpoints) %>%
dplyr::transmute(patient_id, event_time = days_from_biopsy)
reached_endpoint <- event_time$patient_id
Add information about endpoints and final measurement time to the patient metadata:
final_measurement <- longitudinal_data %>%
dplyr::group_by(patient_id) %>%
dplyr::slice_max(days_from_biopsy, n = 1, with_ties = F) %>%
dplyr::transmute(patient_id, last_measurement = days_from_biopsy)
patient_metadata <- patient_metadata %>%
dplyr::full_join(final_measurement,
by = "patient_id" ,
multiple = "all") %>%
dplyr::full_join(event_time, by = "patient_id", multiple = "all") %>%
dplyr::distinct() %>%
dplyr::group_by(patient_id) %>%
dplyr::slice_head(n = 1) # Keep only one entry per patient_id
We add the patient metadata to the filtered longitudinal eGFR data,
and also rescale measurements from days to years. We add the columns
start
, stop
and endpoint
as they
are needed for the joint modelling:
eGFR_plus_metadata <- dplyr::full_join(eGFR_filtered,
patient_metadata,
by = c('patient_id')) %>%
dplyr::mutate(last_measurement_y = last_measurement/365.25,
event_time_y = event_time/365.25) %>%
dplyr::distinct() %>%
tidyr::drop_na(years_from_biopsy) %>%
dplyr::group_by(patient_id) %>%
dplyr::arrange(years_from_biopsy) %>%
dplyr::mutate(start = years_from_biopsy) %>%
dplyr::mutate(stop = dplyr::lead(start,
order_by = years_from_biopsy,
default = unique(last_measurement_y))) %>%
dplyr::mutate(event = ifelse(is.na(event_time_y),
0,
ifelse(abs(stop-event_time_y) < 1e-10,
1,
0))) %>%
dplyr::mutate(endpoint = ifelse(patient_id %in% reached_endpoint,
1,
0))
We have just a few more changes to make to be able to run the joint modelling.
Surv()
requires the start time to be different from the
stop time of the interval. However, when an observation is the last
available measurement for that patient, the start and the stop time are
equal. As a work around, we omit the final observations from the
model:
to_omit <- eGFR_plus_metadata %>% dplyr::filter(stop <= start)
eGFR_plus_metadata <- setdiff(eGFR_plus_metadata, to_omit)
JM does not accept start times of zero. We have to set them to a
small positive number. Also, create a minimal dataframe of only the
variables we will use in the modelling process, check that
patient_id
is numeric, and order the data according to
patient_id
and start
time:
eGFR_minimal <- eGFR_plus_metadata %>%
dplyr::select(patient_id, last_measurement_y, endpoint, measurement, disease, age_at_biopsy, sex, start, stop, event) %>%
dplyr::mutate(start = ifelse(start < 1/365.25, 1e-5, start))
stopifnot("`patient_id` is not numeric" = all(is.numeric(eGFR_minimal$patient_id)))
# Order the longitudinal data according to the patient_id, and order the time points within each subject with respect to time
eGFR_minimal <- eGFR_minimal[order(eGFR_minimal$patient_id, eGFR_minimal$start),]
knitr::kable(head(eGFR_minimal))
patient_id | last_measurement_y | endpoint | measurement | disease | age_at_biopsy | sex | start | stop | event |
---|---|---|---|---|---|---|---|---|---|
1 | 9.514031 | 0 | 83.08316 | B | 29 | M | 0.0054757 | 0.0191650 | 0 |
1 | 9.514031 | 0 | 82.96855 | B | 29 | M | 0.0191650 | 0.0273785 | 0 |
1 | 9.514031 | 0 | 88.61724 | B | 29 | M | 0.0273785 | 0.0301164 | 0 |
1 | 9.514031 | 0 | 83.85820 | B | 29 | M | 0.0301164 | 0.7091034 | 0 |
1 | 9.514031 | 0 | 94.30087 | B | 29 | M | 0.7091034 | 0.9637235 | 0 |
1 | 9.514031 | 0 | 94.39833 | B | 29 | M | 0.9637235 | 1.4510609 | 0 |
write.csv(eGFR_minimal, "output/eGFR_minimal.csv", row.names = FALSE)
sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.4
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: Europe/Rome
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lubridate_1.9.2 forcats_1.0.0 stringr_1.5.0 dplyr_1.1.2
[5] purrr_1.0.1 readr_2.1.4 tidyr_1.3.0 tibble_3.2.1
[9] ggplot2_3.4.2 tidyverse_2.0.0 workflowr_1.7.0
loaded via a namespace (and not attached):
[1] sass_0.4.6 utf8_1.2.3 generics_0.1.3 stringi_1.7.12
[5] hms_1.1.3 digest_0.6.31 magrittr_2.0.3 timechange_0.2.0
[9] evaluate_0.21 grid_4.3.0 fastmap_1.1.1 rprojroot_2.0.3
[13] jsonlite_1.8.5 processx_3.8.1 whisker_0.4.1 ps_1.7.5
[17] promises_1.2.0.1 httr_1.4.6 fansi_1.0.4 scales_1.2.1
[21] jquerylib_0.1.4 cli_3.6.1 rlang_1.1.1 munsell_0.5.0
[25] withr_2.5.0 cachem_1.0.8 yaml_2.3.7 tools_4.3.0
[29] tzdb_0.4.0 colorspace_2.1-0 httpuv_1.6.11 vctrs_0.6.2
[33] R6_2.5.1 lifecycle_1.0.3 git2r_0.32.0 fs_1.6.2
[37] pkgconfig_2.0.3 callr_3.7.3 pillar_1.9.0 bslib_0.4.2
[41] later_1.3.1 gtable_0.3.3 glue_1.6.2 Rcpp_1.0.10
[45] xfun_0.39 tidyselect_1.2.0 rstudioapi_0.14 knitr_1.43
[49] htmltools_0.5.5 rmarkdown_2.22 compiler_4.3.0 getPass_0.2-2