---
title: Flight Data Matching Supplement
date: last-modified
bibliography: references.bib
csl: critical-care-medicine.csl
number-sections: false
---

```{r setup} 
# Load dependencies 
#| output: false
#| echo: false
#| warning: false
library(tidyverse)
library(lubridate)
library(brms)
library(DBI)
library(RSQLite)
library(flowchart)
library(gtsummary)
library(ggplot2)
library(bayestestR)
library(marginaleffects)
library(rstan)
library(flowchart)
library("devtools")
library(rcmswe)

# Set gtsummart theme
theme_gtsummary_compact()

# Load data
d <- read_delim(file = "~/PhD/nsicu-transfers/data/pre-processed-data/patient-df-2025-05-25 22:12:51.563534.csv", delim = ";")[ , -1]

# Prune data
d_pruned <- d %>%
  filter(sir_dsc_time >= "2019-10-01" & sir_dsc_time <= "2024-05-15") %>% # 2019-10-01 -- 2024-05-15
  filter(!is.na(TERTIARY_HADM_ID)) %>%                  # Keep only rows with a tertiary HADM match
  filter(sir_total_time <= 720) %>%                     # Keep only rows with a primary ICU stay 12 hours or shorter
  filter(SIR_PAR_OFFSET_TIGHT %in% (c(-1,0,1))) %>%     # Keep only rows with a recent PAR admit
  filter(road_distance >= 45) %>%                       # Keep only rows with transfers 45 km or longer
  filter(DX_GROUP %in% c("ASAH", "ICH", "AIS", "TBI")) %>% #Keep only admits with ASAH, ICH, AIS or TBI
  filter(min_rank(DX_RANK) == 1, .by = VtfId_LopNr) %>% # Within each unique VtfId_LopNr, keep only the lowest ranking diagnostic PAR admit
  slice_max(sir_dsc_time, by = LopNr, n = 1, with_ties = FALSE) # This gives the latest date by default (if ties, may give more than one)

```


```{r timediff, fig.popup=TRUE, fig.cap="Histogram of Time Difference Between Helicopter Leaving Hospital and SIR Discharge Time"}
# Create transfers plot

#| label: fig-timediff
#| fig-cap: "Distribution of time differences between helicopter departure from the sending hospital and the recorded ICU discharge time. Positive values indicate that the helicopter departed after ICU discharge; negative values indicate departure before the documented discharge time. The ±120-minute window was used to define a matched helicopter transfer as the tails of the distribution reached a uniform plateu."
#| echo: false
#| output: false
#| warning: false
#| fig-popup: true

dsc_time <- ymd_hms(d_pruned$sir_dsc_time_UTC, tz="UTC")
out_time <-  ymd_hms(d_pruned$UTC_out_sending_hems, tz="UTC")
timediff_minutes <- data.frame((out_time - dsc_time) / 60)
names(timediff_minutes) <- c("SIR_discharge_time_relative_helicopter_out_time")

plot_timediff <- ggplot(timediff_minutes, aes(x = SIR_discharge_time_relative_helicopter_out_time)) +
  geom_histogram(binwidth = 5, fill = "maroon", color = "black") +
  labs(title = "Distribution of Time Difference Between Helicopter Departure and Discharge Time",
       x = "Minutes", y = "Frequency") +
  scale_x_continuous(
    limits = c(-120, 120),
    breaks = c(-120, -90, -60, -30, 0, 30, 60, 90, 120)
  ) +
  theme_minimal()
```

```{r fig-timediff, fig.popup=TRUE, fig.cap="Histogram of Time Difference Between Helicopter Departing Hospital and SIR Discharge Time"}
#| label: fig-timediff
#| fig-cap: "Distribution of time differences between helicopter departure from the sending hospital and the recorded ICU discharge time. Positive values indicate that the helicopter departed after ICU discharge; negative values indicate departure before the documented discharge time. The ±120-minute window was used to define a matched helicopter transfer as the tails of the distribution reached a uniform plateau."
#| echo: false
#| warning: false
#| fig-popup: true

plot_timediff
```

@fig-timediff shows the distribution of time differences between ICU discharge and matched flight departures. Transfers without a corresponding flight in the ADS-B dataset were classified as using another transport modality.


