Data from RT CGM Randomized Clinical Trial

Background

Treatment group was given CGM for six months, Control group was not. Then both groups had CGM for the following 6 months. Primary outcome: Change in HbA1c from study enrollment to end of phase 1 (six months later).

Study design: https://pubmed.ncbi.nlm.nih.gov/18828243/

More info: https://clinicaltrials.gov/ct2/show/NCT00406133

Paper with results: https://pubmed.ncbi.nlm.nih.gov/18779236/

# You only need to run this one time to install the package
# install.packages("dtplyr")

library(data.table)
library(dtplyr)
library(dplyr, warn.conflicts = FALSE)
library(ggplot2)

data_full = fread("cgm_sample_patients.csv") # Full dataset: "cgm_all_patients.csv"

# Add hour, and indicators (inrange, hyper, hypo1, hypo2)
data_full = data_full %>% mutate(
  hour = hour(datetime),
  inrange = ifelse(glucose >= 70 & glucose <= 180, 1, 0),
  hyper = ifelse(glucose > 180, 1, 0),
  hypo1 = ifelse(glucose <= 70, 1, 0),
  hypo2 = ifelse(glucose <= 54, 1, 0))

# Shows the first few rows
data_full %>% head() %>% collect()
## # A tibble: 6 × 9
##   datetime            glucose    id datetime_edit  hour inrange hyper hypo1
##   <dttm>                <int> <int> <date>        <int>   <dbl> <dbl> <dbl>
## 1 2000-06-15 23:52:00     124     1 2000-06-15       23       1     0     0
## 2 2000-06-15 23:47:00     122     1 2000-06-15       23       1     0     0
## 3 2000-06-15 21:42:00      78     1 2000-06-15       21       1     0     0
## 4 2000-06-15 21:37:00      80     1 2000-06-15       21       1     0     0
## 5 2000-06-15 21:32:00      82     1 2000-06-15       21       1     0     0
## 6 2000-06-15 21:27:00      82     1 2000-06-15       21       1     0     0
## # … with 1 more variable: hypo2 <dbl>

Aggregate measurements by patient-hour

data_agg = data_full %>% group_by(id, hour) %>%
      summarise(TIR = mean(inrange, na.rm = TRUE),
                hyper = mean(hyper, na.rm = TRUE),
                hypo1 = mean(hypo1, na.rm = TRUE),
                hypo2 = mean(hypo2, na.rm = TRUE),
                mean_glucose = mean(glucose, na.rm = TRUE))

# Show a few rows
data_agg %>% head() %>% collect()
## # A tibble: 6 × 7
## # Groups:   id [1]
##      id  hour   TIR hyper   hypo1   hypo2 mean_glucose
##   <int> <int> <dbl> <dbl>   <dbl>   <dbl>        <dbl>
## 1     1     0 0.667 0.287 0.0491  0.0211          148.
## 2     1     1 0.613 0.334 0.0591  0.0209          159.
## 3     1     2 0.498 0.451 0.0585  0.00234         173.
## 4     1     3 0.465 0.534 0.00572 0               185.
## 5     1     4 0.447 0.550 0.00571 0               190.
## 6     1     5 0.447 0.539 0.0138  0               187.

Plot TIR by hour for one patient (ID=1)

ggplot(data_agg %>% filter(id==1) %>% collect(), aes(x=hour, y=TIR)) +
  geom_line() + geom_point() +
  theme_bw() +
  ggtitle("TIR by hour of day") +
  guides(color=guide_legend(ncol=2))

Plot hypo2 (% time with glucose < 54) by hour for one patient (ID=1)

ggplot(data_agg %>% filter(id==1) %>% collect(), aes(x=hour, y=hypo2)) +
  geom_line() + geom_point() +
  theme_bw() +
  ggtitle("Extreme hypo by hour of day") +
  guides(color=guide_legend(ncol=2))

Plot TIR by hour for all patients (one line per patient)

ggplot(collect(data_agg), aes(x=hour, y=TIR, color=factor(id))) +
  geom_line() +
  theme_bw() +
  ggtitle("TIR by hour of day (one line per patient)") +
  guides(color=guide_legend(ncol=2))