Vignette for package bayesmsm

Xiao Yan, Kuan Liu

1. Introduction

devtools::install_github("Kuan-Liu-Lab/bayesmsm")
library(bayesmsm)

2. Simulated observational data with a time-dependent treatment

library(DiagrammeR)
grViz("
    digraph causal {
    # Nodes
    node [shape=plaintext]
    W [label = 'w1, w2']
    L1 [label = 'L11, L21']
    Z1 [label = 'Z1']
    L2 [label = 'L12, L22']
    Z2 [label = 'Z2']
    Y [label = 'Y']
    
    # Edges
    edge [color=black, arrowhead=vee]
    rankdir = LR
    W->L1
    W->Z1
    W->L2
    W->Z2
    W->Y
    L1->Z1
    L1->L2
    L1->Z2
    L1->Y
    Z1->L2
    Z1->Z2
    Z1->Y
    L2->Z2
    L2->Y
    Z2->Y
    
    # Graph
    graph [overlap=true, fontsize=14]
    }")
library(DT)
options(scipen = 999)

testdata <- read.csv(system.file("extdata", "continuous_outcome_data.csv", package = "bayesmsm"))

# look at the data;
datatable(testdata,
          rownames = FALSE,
          options = list(dom = 't')) %>%
  formatRound(columns=c('w2', 'L2_1', 'L2_2', 'y'), digits=2)
# frequency counts by treatment combinations;
table(testdata$a_1, testdata$a_2)
#>    
#>       0   1
#>   0 520 201
#>   1 111 168

\[ ATE = E(Y \mid Z_1 = 1, Z_2 = 1) - E(Y \mid Z_1 = 0, Z_2 = 0) \]

3. Bayesian treatment effect weight estimation using bayesweight

weights <- bayesweight(trtmodel.list = list(a_1 ~ w1 + w2 + L1_1 + L2_1,
                                             a_2 ~ w1 + w2 + L1_1 + L2_1 + L1_2 + L2_2 + a_1),
                        data = testdata,
                        n.iter = 10000,
                        n.burnin = 5000,
                        n.thin = 5,
                        n.chains = 4,
                        seed = 890123,
                        parallel = TRUE)
str(weights)
#>  num [1:1000] 1.233 1.138 1.016 0.83 0.794 ...

4. Bayesian non-parametric bootstrap to maximize the utility function with respect to the causal effect using bayesmsm

The function bayesmsm estimates causal effect of time-varying treatments. It uses subject-specific treatment assignmennt weights weights calculated using bayesweight, and performs Bayesian non-parametric bootstrap to estimate the causal parameters.

model <- bayesmsm(ymodel = y ~ a_1+a_2,
                           nvisit = 2,
                           reference = c(rep(0,2)),
                           comparator = c(rep(1,2)),
                           family = "gaussian",
                           data = testdata,
                           wmean = rep(1, 1000),
                           nboot = 1000,
                           optim_method = "BFGS",
                           parallel = TRUE,
                           seed = 890123,
                           ncore = 4)
str(model)
#> List of 6
#>  $ RD_mean    : num -3.16
#>  $ RD_sd      : num 0.0947
#>  $ RD_quantile: Named num [1:2] -3.34 -2.98
#>   ..- attr(*, "names")= chr [1:2] "2.5%" "97.5%"
#>  $ bootdata   :'data.frame': 1000 obs. of  3 variables:
#>   ..$ effect_reference : num [1:1000] 2.36 2.29 2.24 2.33 2.3 ...
#>   ..$ effect_comparator: num [1:1000] -0.951 -0.825 -0.77 -0.811 -0.75 ...
#>   ..$ RD               : num [1:1000] -3.31 -3.11 -3.01 -3.14 -3.05 ...
#>  $ reference  : num [1:2] 0 0
#>  $ comparator : num [1:2] 1 1

5. Visualization functions: plot_ATE, plot_APO, plot_est_box

The bayesmsm package also provides several other functions for visualizing the above results: plot_ATE, plot_APO, and plot_est_box. These functions help the user better interpret the estimated causal effects.

plot_ATE(model)

plot_APO(model, "effect_reference")

plot_APO(model, "effect_comparator")

plot_est_box(model)

Reference