The goal of this demo is to conduct a Bayesian Dynamic Borrowing (BDB) analysis using the hierarchical commensurate prior on a dataset.
# Load packages ----
library(psborrow2)
# Survival analysis
library(survival)
library(ggsurvfit)
library(flexsurv)
# Additional tools for draws objects
library(bayesplot)
library(posterior)
# Comparing populations
library(table1)
## id ext trt cov4 cov3 cov2 cov1 time status cnsr resp
## [1,] 1 0 0 1 1 1 0 2.4226411 1 0 1
## [2,] 2 0 0 1 1 0 1 50.0000000 0 1 1
## [3,] 3 0 0 0 0 0 1 0.9674372 1 0 1
## [4,] 4 0 0 1 1 0 1 14.5774738 1 0 1
## [5,] 5 0 0 1 1 0 0 50.0000000 0 1 0
## [6,] 6 0 0 1 1 0 1 50.0000000 0 1 0
?example_matrix # for more details
## trt
## ext 0 1
## 0 50 100
## 1 350 0
km_fit <- survfit(Surv(time = time, event = 1 - cnsr) ~ trt + ext,
data = example_dataframe
)
ggsurvfit(km_fit)The internal and external control arms look quite different…
cox_fit <- coxph(Surv(time = time, event = 1 - cnsr) ~ trt,
data = example_dataframe,
subset = ext == 0
)
cox_fit## Call:
## coxph(formula = Surv(time = time, event = 1 - cnsr) ~ trt, data = example_dataframe,
## subset = ext == 0)
##
## coef exp(coef) se(coef) z p
## trt -0.1097 0.8961 0.1976 -0.555 0.579
##
## Likelihood ratio test=0.3 on 1 df, p=0.5809
## n= 150, number of events= 114
## 2.5 % 97.5 %
## trt 0.6083595 1.319842
Let’s start by demonstrating the utility of BDB by trying to borrow data from the external control arm which we know experiences worse survival.
The end goal is to create an Analysis object with:
?create_analysis_obj
psborrow2 allows the user to specify priors with the following functions:
?prior_bernoulli
?prior_beta
?prior_cauchy
?prior_exponential
?prior_gamma
?prior_normal
?prior_poisson
?prior_uniform
Prior distributions can be plotted with the plot() method
psborrow2 currently supports 4 outcomes:
?outcome_surv_weibull_ph # Weibull survival w/ proportional hazards
?outcome_surv_exponential # Exponential survival
?outcome_bin_logistic # Logistic binary outcome
?outocme_cont_normal # Normal continuous outcome
Borrowing objects are created with:
?borrowing_hierarchical_commensurate # Hierarchical commensurate borrowing
?borrowing_none # No borrowing
?borrowing_full # Full borrowing
Treatment objects are created with:
?treatment_details
Combine everything and create object of class Analysis:
analysis_object <- create_analysis_obj(
data_matrix = example_matrix,
outcome = exp_outcome,
borrowing = bdb_borrowing,
treatment = trt_details
)## Inputs look good.
## Stan program compiled successfully!
## Ready to go! Now call `mcmc_sample()`.
## Analysis Object
##
## Outcome model: OutcomeSurvExponential
## Outcome variables: time cnsr
##
## Borrowing method: Bayesian dynamic borrowing with the hierarchical commensurate prior
## External flag: ext
##
## Treatment variable: trt
##
## Data: Matrix with 500 observations
## - 50 internal controls
## - 350 external controls
## - 100 internal experimental
##
## Stan model compiled and ready to sample.
## Call mcmc_sample() next.
Conduct MCMC sampling with:
?mcmc_sample
results <- mcmc_sample(
x = analysis_object,
iter_warmup = 1000,
iter_sampling = 5000,
chains = 1
)
## [1] "CmdStanMCMC" "CmdStanFit" "R6"
## variable mean median sd mad q5 q95 rhat ess_bulk ess_tail
## lp__ -1617.94 -1617.65 1.46 1.26 -1620.73 -1616.19 1.00 2005 2457
## beta_trt -0.15 -0.16 0.20 0.20 -0.47 0.18 1.00 1933 2381
## alpha[1] -3.36 -3.36 0.16 0.17 -3.64 -3.11 1.00 1943 2545
## alpha[2] -2.40 -2.40 0.06 0.06 -2.49 -2.31 1.00 2486 2637
## tau 1.16 0.48 1.82 0.66 0.01 4.64 1.00 2408 1617
## HR_trt 0.88 0.86 0.18 0.17 0.63 1.20 1.00 1933 2381
Dictionary to interpret parameters:
## Stan_variable Description
## 1 tau commensurability parameter
## 2 alpha[1] baseline log hazard rate, internal
## 3 alpha[2] baseline log hazard rate, external
## 4 beta_trt treatment log HR
## 5 HR_trt treatment HR
Create a draws object
Rename draws object parameters
Get 95% credible intervals with posterior package
## # A tibble: 6 × 4
## variable `2.5%` `50%` `97.5%`
## <chr> <dbl> <dbl> <dbl>
## 1 lp__ -1622. -1618. -1616.
## 2 treatment log HR -0.531 -0.156 0.254
## 3 baseline log hazard rate, internal -3.70 -3.36 -3.06
## 4 baseline log hazard rate, external -2.51 -2.40 -2.29
## 5 commensurability parameter 0.00123 0.481 6.02
## 6 treatment HR 0.588 0.856 1.29
Look at histogram of draws with bayesplot package
## Registered S3 method overwritten by 'plyr':
## method from
## [.indexed table1
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.
Our model does not borrow much from the external arm! This is the desired outcome given how different the control arms were.
Check balance between arms
table1(
~ cov1 + cov2 + cov3 + cov4 |
factor(ext, labels = c("Internal RCT", "External data")) +
factor(trt, labels = c("Not treated", "Treated")),
data = example_dataframe
)Internal RCT |
External data |
Overall |
|||
|---|---|---|---|---|---|
| Not treated (N=50) |
Treated (N=100) |
Not treated (N=350) |
Not treated (N=400) |
Treated (N=100) |
|
| cov1 | |||||
| Mean (SD) | 0.540 (0.503) | 0.630 (0.485) | 0.740 (0.439) | 0.715 (0.452) | 0.630 (0.485) |
| Median [Min, Max] | 1.00 [0, 1.00] | 1.00 [0, 1.00] | 1.00 [0, 1.00] | 1.00 [0, 1.00] | 1.00 [0, 1.00] |
| cov2 | |||||
| Mean (SD) | 0.200 (0.404) | 0.370 (0.485) | 0.500 (0.501) | 0.463 (0.499) | 0.370 (0.485) |
| Median [Min, Max] | 0 [0, 1.00] | 0 [0, 1.00] | 0.500 [0, 1.00] | 0 [0, 1.00] | 0 [0, 1.00] |
| cov3 | |||||
| Mean (SD) | 0.760 (0.431) | 0.760 (0.429) | 0.403 (0.491) | 0.448 (0.498) | 0.760 (0.429) |
| Median [Min, Max] | 1.00 [0, 1.00] | 1.00 [0, 1.00] | 0 [0, 1.00] | 0 [0, 1.00] | 1.00 [0, 1.00] |
| cov4 | |||||
| Mean (SD) | 0.420 (0.499) | 0.460 (0.501) | 0.197 (0.398) | 0.225 (0.418) | 0.460 (0.501) |
| Median [Min, Max] | 0 [0, 1.00] | 0 [0, 1.00] | 0 [0, 1.00] | 0 [0, 1.00] | 0 [0, 1.00] |
Because the imbalance may be conditional on observed covariates, let’s adjust for propensity scores in our analysis
Create a propensity score model
ps_model <- glm(ext ~ cov1 + cov2 + cov3 + cov4,
data = example_dataframe,
family = binomial
)
ps <- predict(ps_model, type = "response")
example_dataframe$ps <- ps
example_dataframe$ps_cat_ <- cut(
example_dataframe$ps,
breaks = 5,
include.lowest = TRUE
)
levels(example_dataframe$ps_cat_) <- c(
"ref", "low",
"low_med", "high_med", "high"
)Convert the data back to a matrix with dummy variables for
ps_cat_ levels
example_matrix_ps <- create_data_matrix(
example_dataframe,
outcome = c("time", "cnsr"),
trt_flag_col = "trt",
ext_flag_col = "ext",
covariates = ~ps_cat_
)## Call `add_covariates()` with `covariates = c("ps_cat_low", "ps_cat_low_med", "ps_cat_high_med", "ps_cat_high" ) `
Propensity score analysis without borrowing
anls_ps_no_borrow <- create_analysis_obj(
data_matrix = example_matrix_ps,
covariates = add_covariates(
c("ps_cat_low", "ps_cat_low_med", "ps_cat_high_med", "ps_cat_high"),
prior_normal(0, 10000)
),
outcome = outcome_surv_exponential("time", "cnsr", prior_normal(0, 10000)),
borrowing = borrowing_none("ext"),
treatment = treatment_details("trt", prior_normal(0, 10000))
)
res_ps_no_borrow <- mcmc_sample(
x = anls_ps_no_borrow,
iter_warmup = 1000,
iter_sampling = 5000,
chains = 1
)
draws_ps_no_borrow <- rename_draws_covariates(
res_ps_no_borrow$draws(),
anls_ps_no_borrow
)
summarize_draws(draws_ps_no_borrow, ~ quantile(.x, probs = c(0.025, 0.50, 0.975)))## # A tibble: 8 × 4
## variable `2.5%` `50%` `97.5%`
## <chr> <dbl> <dbl> <dbl>
## 1 lp__ -466. -462. -460.
## 2 treatment log HR -0.736 -0.342 0.0575
## 3 baseline log hazard rate -4.76 -4.20 -3.71
## 4 ps_cat_low -0.342 0.403 1.16
## 5 ps_cat_low_med 0.454 1.01 1.60
## 6 ps_cat_high_med 1.42 2.10 2.78
## 7 ps_cat_high 2.41 3.01 3.65
## 8 treatment HR 0.479 0.710 1.06
anls_ps_bdb <- create_analysis_obj(
data_matrix = example_matrix_ps,
covariates = add_covariates(
c("ps_cat_low", "ps_cat_low_med", "ps_cat_high_med", "ps_cat_high"),
prior_normal(0, 10000)
),
outcome = outcome_surv_exponential("time", "cnsr", prior_normal(0, 10000)),
borrowing = borrowing_hierarchical_commensurate("ext", prior_gamma(0.001, 0.001)),
treatment = treatment_details("trt", prior_normal(0, 10000))
)
res_ps_bdb <- mcmc_sample(
x = anls_ps_bdb,
iter_warmup = 1000,
iter_sampling = 5000,
chains = 1
)
draws_ps_bdb <- rename_draws_covariates(
res_ps_bdb$draws(),
anls_ps_bdb
)
summarize_draws(draws_ps_bdb, ~ quantile(.x, probs = c(0.025, 0.50, 0.975)))## # A tibble: 10 × 4
## variable `2.5%` `50%` `97.5%`
## <chr> <dbl> <dbl> <dbl>
## 1 lp__ -1426. -1421. -1418.
## 2 treatment log HR -0.687 -0.344 -0.0294
## 3 baseline log hazard rate, internal -4.62 -4.19 -3.77
## 4 baseline log hazard rate, external -4.64 -4.19 -3.80
## 5 commensurability parameter 0.0759 56.2 1653.
## 6 ps_cat_low -0.305 0.259 0.830
## 7 ps_cat_low_med 0.626 1.06 1.52
## 8 ps_cat_high_med 1.64 2.09 2.57
## 9 ps_cat_high 2.53 2.94 3.39
## 10 treatment HR 0.503 0.709 0.971
It looks like PS + BDB allowed us to most accurately recover the true hazard ratio of 0.70.