Untitled

Anonymous
plain_text
05/01/2026 11:30 AM
2.2 KB
9
Indexable
```{r}
library(EGM)
library(patchwork)

data_ECG <- load_case(tname = 'SNUADC/ECG_II', caseid = cases_withMonitoring$caseid[4])

# Use only pre-induction ECG — post-induction signal contains artefacts
ecg_pre <- data_ECG |>
  filter(Time >= anestart_derived - 60, Time < anestart_derived)

# Detect R peaks (500 Hz) and refine to true peak within ±15 samples
R_spikes <- detect_QRS(ecg_pre$SNUADC.ECG_II, 500)
shift <- sapply(R_spikes, function(i) {
  end_i <- min(i + 15, nrow(ecg_pre))
  which.max(ecg_pre$SNUADC.ECG_II[i:end_i]) - 1
})
R_spikes <- R_spikes + shift

# Build tachogram — filter out physiologically implausible RR intervals
R_spikes_df <- tibble(
  Time     = ecg_pre$Time[R_spikes],
  ECG      = ecg_pre$SNUADC.ECG_II[R_spikes],
  diffTime = c(NA, diff(Time))
) |>
  filter(is.na(diffTime) | (diffTime > 0.3 & diffTime < 2.0))

# Rolling RMSSD (10-beat window) as a measure of vagal tone
window_size <- 10
rmssd_roll <- map_dfr(window_size:nrow(R_spikes_df), function(i) {
  rr <- R_spikes_df$diffTime[(i - window_size + 1):i]
  tibble(Time = R_spikes_df$Time[i], RMSSD = sqrt(mean(diff(rr)^2, na.rm = TRUE)))
})

# Tachogram
p_tacho <- ggplot(R_spikes_df |> filter(!is.na(diffTime)), aes(x = Time, y = diffTime * 1000)) +
  geom_line() +
  geom_vline(xintercept = anestart_derived, linetype = "dashed", colour = "red") +
  labs(title = "Tachogram (pre-induction)", x = NULL, y = "RR interval (ms)")

# Rolling RMSSD
p_rmssd <- ggplot(rmssd_roll, aes(x = Time, y = RMSSD * 1000)) +
  geom_line() +
  geom_vline(xintercept = anestart_derived, linetype = "dashed", colour = "red") +
  labs(title = "Rolling RMSSD — 10-beat window", x = "Time (s)", y = "RMSSD (ms)")

p_tacho / p_rmssd
```
Editor is loading...
Leave a Comment