eth-mds / ricu

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

`admittime` stored as `difftime`, not `datetime` #41

Closed mlondschien closed 9 months ago

mlondschien commented 1 year ago

I'd like to write a concept retrieving the day of the week at admission.

    "adm_dow": {
        "description": "Admission day of the week",
        "target": "id_tbl",
        "type": "fct_cncpt",
        "levels": [
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
            "Sunday"
        ],
        "sources": {
            "mimic": [
                {
                    "table": "admissions",
                    "val_var": "admittime",
                    "class": "col_itm",
                    "callback": "transform_fun(weekdays)"
                }
            ],
            "miiv": [
                {
                    "table": "admissions",
                    "val_var": "admittime",
                    "class": "col_itm",
                    "callback": "transform_fun(weekdays)"
                }
            ],
            "hirid": [
                {
                    "table": "general",
                    "val_var": "admissiontime",
                    "class": "col_itm",
                    "callback": "transform_fun(weekdays)"
                }
            ]
        }
    },

However,

concepts = load_concepts("adm_dow", "mimic")

fails with

Error in UseMethod("weekdays") : 
  no applicable method for 'weekdays' applied to an object of class "difftime"
In addition: Warning message:
Ignoring argument passed as `...` 

Some monkey-debugging yields that admittime (and admissiontime in hirid) is internally transformed to a difftime (relative to hospital admission?) somewhere. Why? How can I implement the above concept?

nbenn commented 1 year ago

Sorry, this response might be a bit late. Also not sure whether it is helpful, as what I'm going to suggest is not used anywhere and is therefore untested.

What is happening in

https://github.com/eth-mds/ricu/blob/9979eb1014c7a17eb21af03ff2d0e02dc712e18c/R/data-load.R#L336-L342

is that settings like id_var (and more interesting for you time_vars) are extracted from the "item" definition and used to override the table defaults. Only columns that are considered time_vars are converted to difftime, so if you make sure the column you'll be passing to weekday() is not among time_vars, you should be fine.

As to why we are doing this: for our work, we were never interested in absolute timestamps, but only times relative to certain events (icu admission, death, etc.). Additionally some datasets do not offer absolute timestamps (AUMC I believe) for something as central as timestamps (for time-series data) we needed to have something that works for all datasets.

I seem to remember that weekdays have been preserved in the MIMIC time-stamp-randomization procedure. If you're unsure, this'd be something to check if you want to use this data. Not sure about HiRID.

mlondschien commented 1 year ago

Thanks! So you're suggesting to pass another variable explicitly as time_vars? Can this be empty?

nbenn commented 1 year ago

One (slightly hacky) thing that could work is setting in your concept definition the field time_vars to some column that (could be considered a "time var" but) is not the variable you're interested in using for other purposes (for mimic "admissions" that could be dischtime).

Another thing you could try is setting this to a value like NULL (or in JSON maybe null). Not sure if this makes it through to load_id(). If it does, you have more cleanly conveyed your intention of "not time vars". If it does not, it might be straightforward to fix this and I feel, this could potentially be useful.

mlondschien commented 10 months ago

Thanks. Late reply: null does not work (gets ignored I think), but passing any other column works. This doesn't need to be a time variable, e.g., sex in hirid works.

dplecko commented 9 months ago

Hey @mlondschien, you are correct, I usually pass something that is a dummy variable (such as sex or age). It is a hacky way, but the abuse does not seem too bad.