Last updated: 2023-06-17

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 fdd1a68. 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/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/fitted_eGFR_slopes.csv
    Untracked:  output/flagged_episodes.csv
    Untracked:  output/follow_up_report_by_patient.pdf
    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/01flagAKI.Rmd) and HTML (docs/01flagAKI.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 fdd1a68 Charlotte Boys 2023-06-17 Add report and eGFR slope table
html 391f074 Charlotte Boys 2023-06-16 Build site.
Rmd 7de25c8 Charlotte Boys 2023-06-16 Publish main parts of framework

Introduction

We flag AKI episodes from serum creatinine measurements.

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
library(zoo)

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

Load support functions:

source("code/flagAKI.R")

Read in the synthetic longitudinal data:

longitudinal_data <- read.csv("data/simulated_longitudinal_data.csv")

Test AKI critera

For each SCr measurement, we test if one of the following criteria are met [@hapca2021relationship]:

  1. SCr \(\geq\) 1.5$ times higher than median of all creatinine measures in the 8-365 days before index (day the measurement was taken)

  2. SCr \(\geq\) 1.5 times higher than the lowest creatinine in the 7 days before or post-index

  3. SCr \(>\) 26 umol/L higher than the lowest creatinine in the 2 days before or post-index

  4. SCr \(\geq\) 1.5 times higher than median of all creatinine measures in the 8-365 days post-index

The functions in which these tests are implemented can be found in the script code/flagAKI.R.

scr_values <- testAKIcriteria(longitudinal_data)
Testing criterion 1...
Testing criterion 2...
Testing criterion 3...
Testing criterion 4...
Done!
knitr::kable(head(scr_values))
patient_id years_from_biopsy measurement type units days_from_biopsy AKIcrit1 AKIcrit2 AKIcrit3 AKIcrit4
1 0.0054757 107.02514 SCr umol/L 2 NA FALSE FALSE FALSE
1 0.0191650 107.14072 SCr umol/L 7 NA FALSE FALSE FALSE
1 0.0273785 101.41421 SCr umol/L 10 FALSE FALSE FALSE FALSE
1 0.0301164 106.18664 SCr umol/L 11 FALSE FALSE FALSE FALSE
1 0.7091034 95.95474 SCr umol/L 259 FALSE FALSE FALSE NA
1 0.9637235 95.74575 SCr umol/L 352 FALSE FALSE FALSE NA

Group observations into episodes of care

We identify “episodes of care”, that is, periods where multiple observations are made in a short space of time. This will allow us to later identify potential hospital admissions or in-patient events.

We define an “episode of care” by linking together any observation made within seven days of another into one group of observations. To give a concrete example: observations made on days 1, 8, and 14 post-biopsy belong to one “episode of care” because they all occur within seven days of one another. If the next observation were made on day 22 (eight days after day 14), then it would belong in a separate episode of care. The code below assigns a unique numeric group_id to each observation by which its episode of care can be identified.

scr_episodes <- scr_values %>%
  dplyr::group_by(patient_id) %>%
  dplyr::mutate(lag_day = abs(days_from_biopsy - lag(days_from_biopsy)) <= 7,
         lead_day = abs(days_from_biopsy - lead(days_from_biopsy)) <= 7) %>%
  dplyr::mutate_at(vars(lag_day, lead_day), ~ ifelse(., ., NA))%>%
  dplyr::mutate(episode_of_care = ifelse(is.na(lead_day), NA, data.table::rleid(lead_day))) %>%
  dplyr::mutate(episode_of_care = ifelse(!is.na(lag_day), zoo::na.locf(episode_of_care, na.rm = FALSE), episode_of_care)) %>%
  dplyr::mutate(group_id = coalesce(
      case_when(is.na(episode_of_care) ~ nrow(.) + row_number()), # number of rows in df plus row number ensures unique ID
      episode_of_care)) %>%
  dplyr::ungroup()

Flag episodes of care as potential AKI and in-patient events

Now we have our observations grouped into episodes of care, we use the above AKI criteria to flag for potential AKI events. Specifically, an episode of care is given the flag AKI if and only if one or more observations belonging to the episode meets one or more of the AKI criteria. We also take note of the number of serum creatinine observations within each episode, which we will use to flag for potential in-patient events (hospital admissions). Specifically, if one episode of care contains five or more observations we give it the in-patient flag.

summarised_episodes <- scr_episodes %>%
  dplyr::group_by(patient_id, group_id) %>%
  dplyr::summarise(
    AKI = any(AKIcrit1, AKIcrit2, AKIcrit3, AKIcrit4, na.rm = TRUE), 
    n_obs = n(),
    start = min(days_from_biopsy),
    end = max(days_from_biopsy))
`summarise()` has grouped output by 'patient_id'. You can override using the
`.groups` argument.
flagged_episodes <- summarised_episodes %>%
  dplyr::filter(AKI | n_obs >= 5) %>%
  dplyr::mutate(in_patient = ifelse(n_obs >= 5, TRUE, FALSE)) %>%
  dplyr::mutate(flag = ifelse(AKI & in_patient, "AKI and in-patient",
                                  ifelse(AKI, "AKI", "in-patient"))) %>%
  dplyr::mutate(flag = as.factor(flag)) %>%
  dplyr::select(start, end, patient_id, n_obs, flag, group_id)
knitr::kable(head(flagged_episodes))
start end patient_id n_obs flag group_id
1 5 2 4 AKI 1
2759 2759 2 1 AKI 2945
3893 3893 2 1 AKI 2954
6192 6192 2 1 AKI 2980
6234 6234 2 1 AKI 2982
0 13 3 5 in-patient 1

Save data

Save the flagged episodes, so that if desired we can remove the outlying observations from the data when calculating the long-term eGFR slopes:

write.csv(flagged_episodes, "output/flagged_episodes.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] zoo_1.8-12      lubridate_1.9.2 forcats_1.0.0   stringr_1.5.0  
 [5] dplyr_1.1.2     purrr_1.0.1     readr_2.1.4     tidyr_1.3.0    
 [9] tibble_3.2.1    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    lattice_0.21-8   
 [5] stringi_1.7.12    hms_1.1.3         digest_0.6.31     magrittr_2.0.3   
 [9] timechange_0.2.0  evaluate_0.21     grid_4.3.0        fastmap_1.1.1    
[13] rprojroot_2.0.3   jsonlite_1.8.5    processx_3.8.1    whisker_0.4.1    
[17] ps_1.7.5          promises_1.2.0.1  httr_1.4.6        fansi_1.0.4      
[21] scales_1.2.1      jquerylib_0.1.4   cli_3.6.1         rlang_1.1.1      
[25] munsell_0.5.0     withr_2.5.0       cachem_1.0.8      yaml_2.3.7       
[29] tools_4.3.0       tzdb_0.4.0        colorspace_2.1-0  httpuv_1.6.11    
[33] vctrs_0.6.2       R6_2.5.1          lifecycle_1.0.3   git2r_0.32.0     
[37] fs_1.6.2          pkgconfig_2.0.3   callr_3.7.3       pillar_1.9.0     
[41] bslib_0.4.2       later_1.3.1       gtable_0.3.3      data.table_1.14.8
[45] glue_1.6.2        Rcpp_1.0.10       xfun_0.39         tidyselect_1.2.0 
[49] rstudioapi_0.14   knitr_1.43        htmltools_0.5.5   rmarkdown_2.22   
[53] compiler_4.3.0    getPass_0.2-2