eth-mds / ricu

🏥 ICU data with R 🏥
https://eth-mds.github.io/ricu/
GNU General Public License v3.0
37 stars 10 forks source link

[AUMC] Error in difftimes for second ICU stays #20

Open prockenschaub opened 2 years ago

prockenschaub commented 2 years ago

AUMCdb, like eICU, does not provide dates but instead reports times since admission. For this reason, both databases are loaded in ricu via that load_eiau function.

Problem

The calculation of times -- while similar -- is not equal between eICU and AUMCdb. While in eICU times denote the number of minutes from unit admit time, in AUMCdb they represent milliseconds since the first ICU admission. That is, while eICU provides the time since current admission, AUMCdb gives the time since the first admission. load_eiau however treats them both the same, which leads to incorrect times for AUMCdb

Example

library(ricu)

# Two admissions for one patient. Note that the first admittedat is always 0, 
# whereas the second is relative to the first
aumc$admissions[aumc$admissions$patientid == 38, c("admissionid", "admittedat", "dischargedat")]
#>    admissionid admittedat dischargedat
#> 1:          40          0     56820000
#> 2:          41  164280000    325620000

# For heart rates during the first admission, everything works correctly
aumc$numericitems[aumc$numericitems$admissionid == 40 & aumc$numericitems$itemid == 6640, c("admissionid", "measuredat", "value")][1]
#>    admissionid measuredat value
#> 1:          40     300000    86
load_concepts("hr", "aumc", patient_ids = 40, interval = mins(1L))[1]
#>   admissionid measuredat    hr
#>         <int> <drtn>     <dbl>
#> 1          40 5 mins        86
ricu:::ms_as_mins(300000 - 0) # <-- correct
#> Time difference of 5 mins

# For heart rates during the second admission, ricu calculates difftimes since *first* admission 
aumc$numericitems[aumc$numericitems$admissionid == 41 & aumc$numericitems$itemid == 6640, c("admissionid", "measuredat", "value")][1]
#>    admissionid measuredat value
#> 1:          41  167280000   118
load_concepts("hr", "aumc", patient_ids = 41, interval = mins(1L))[1]
#>   admissionid measuredat    hr
#>         <int> <drtn>     <dbl>
#> 1          41 2788 mins    118
ricu:::ms_as_mins(167280000 - 0) # <-- incorrect
#> Time difference of 2788 mins
ricu:::ms_as_mins(167280000 - 164280000) # <-- should be this
#> Time difference of 50 mins

Note that the output above has been been shortened a little for readability.

dplecko commented 11 months ago

Very good catch, thanks @prockenschaub. The PR currently seems to have a lot going on, unrelated to this issue (if I am not mistaken).

Could you open a simple pull request building off the current main which bumped ricu to 0.6.0? It would simplify things considerably for me. Thanks for the help!

prockenschaub commented 8 months ago

Sorry for the delay! Finally got to look at this and I've provided a cleaned PR here #51