`bayesmsm` for longitudinal data without right-censoring"

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.align = 'center',
  fig.width = 9,
  fig.height = 6,
  warning = F,
  message = F
)

Introduction

if (!requireNamespace("bayesmsm", quietly = TRUE)) {
  stop("The package 'bayesmsm' is required to run this vignette. Please install it manually using:
       devtools::install_github('Kuan-Liu-Lab/bayesmsm')")
} else {
  library(bayesmsm)
}

Simulated longitudinal observational data without right-censoring

In this vignette, we demonstrate how to simulate a clean longitudinal dataset without censoring using simData(). We generate data for 200 individuals observed over 2 visits. At each visit, two normally distributed covariates (L1_j, L2_j) and a binary treatment assignment (A_j) are created. No censoring is applied, so all covariates, treatments, and the final outcome Y are fully observed. The outcome Y is continuous, generated from a linear model on the covariate and treatment history.

| Variable | Description | | --------------- | --------------------------------------- | | L1_1, L2_1 | Baseline covariates (continuous) | | L1_2, L2_2 | Time-varying covariates at visit 2 | | A1, A2 | Binary treatments at visits 1 and 2 | | Y | Continuous end-of-study outcome |

# 1) Define coefficient lists for 2 visits
amodel <- list(
  # Visit 1: logit P(A1=1) = -0.3 + 0.4*L1_1 - 0.2*L2_1
  c("(Intercept)" = -0.3, "L1_1" = 0.4, "L2_1" = -0.2),
  # Visit 2: logit P(A2=1) = -0.1 + 0.3*L1_2 - 0.1*L2_2 + 0.5*A_prev
  c("(Intercept)" = -0.1, "L1_2" = 0.3, "L2_2" = -0.1, "A_prev" = 0.5)
)

# 2) Define binary outcome model: logistic on treatments and last covariates
ymodel <- c(
  "(Intercept)" = -0.8,
  "A1"          = 0.2,
  "A2"          = 0.4,
  "L1_2"        = 0.3,
  "L2_2"        = -0.3
)

# 3) Load package and simulate data without censoring
simdat <- simData(
  n                = 100,
  n_visits         = 2,
  covariate_counts = c(2, 2),
  amodel           = amodel,
  ymodel           = ymodel,
  y_type           = "binary",
  right_censor     = FALSE,
  seed             = 123
)

# 4) Inspect first rows
head(simdat)

$$ ATE = E(Y \mid A_1 = 1, A_2 = 1) - E(Y \mid A_1 = 0, A_2 = 0) $$

Bayesian treatment effect weight estimation using bayesweight

weights <- bayesweight(
  trtmodel.list = list(
    A1 ~ L1_1 + L2_1,
    A2 ~ L1_2 + L2_2 + A1),
  data = simdat,
  n.chains = 1,
  n.iter = 200,
  n.burnin = 100,
  n.thin = 1,
  seed = 890123,
  parallel = FALSE)

summary(weights)
cat(weights$model_string)

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 assignment weights weights calculated using bayesweight, and performs Bayesian non-parametric bootstrap to estimate the causal parameters.

model <- bayesmsm(ymodel = Y ~ A1 + A2,
  nvisit = 2,
  reference = c(rep(0,2)),
  comparator = c(rep(1,2)),
  family = "binomial",
  data = simdat,
  wmean = weights$weights,
  nboot = 50,
  optim_method = "BFGS",
  parallel = TRUE,
  seed = 890123,
  ncore = 2)
str(model)
summary_bayesmsm(model)

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



Try the bayesmsm package in your browser

Any scripts or data that you put into this service are public.

bayesmsm documentation built on June 17, 2025, 9:08 a.m.