Learn R Programming

bivarhr (version 0.1.5)

contrafactual_ATE: Contrafactual Average Treatment Effects (ATE) for the Bivariate Hurdle Model

Description

Computes time-varying contrafactual Average Treatment Effects (ATE) for both series (I and C) from a fitted bivariate hurdle negative binomial model. For each time point and posterior draw, the function compares the expected outcome under the observed design matrix with a contrafactual scenario where cross-lag terms and transition covariates are set to zero.

Usage

contrafactual_ATE(fit_obj, compute_intervals = TRUE, ndraws = 1200, seed = 42)

Value

A tibble with one row per effective time index (length des$idx). If compute_intervals = TRUE, the columns are:

  • t: time index (from des$idx).

  • ATE_I_mean, ATE_I_low, ATE_I_high: posterior mean and 95\

  • ATE_C_mean, ATE_C_low, ATE_C_high: posterior mean and 95\

If compute_intervals = FALSE, only ATE_I_mean and ATE_C_mean are returned (plus t).

Arguments

fit_obj

A list returned by fit_one() (or an equivalent fitting function), containing at least:

  • $fit: a CmdStanR fit object.

  • $des: a list with design matrices X_pi_I, X_mu_I, X_pi_C, X_mu_C, a vector log_exposure50, and an index vector idx.

compute_intervals

Logical; if TRUE, returns posterior means and 95\ FALSE, only posterior means are returned.

ndraws

Integer; maximum number of posterior draws to use. If ndraws exceeds the number of available draws, it is truncated.

seed

Integer; random seed used to subsample posterior draws.

Details

The function identifies in the design matrices:

  • Cross-lag terms via column names containing "zC_L" / "C_L" (for I) and "zI_L" / "I_L" (for C).

  • Transition covariates via column names starting with "trans_".

For each time point t and posterior draw s, the expected value under the observed design (\(E[Y \mid X]\)) is contrasted with a contrafactual design where these cross-lag and transition columns are set to zero (\(E[Y \mid X_{cf}]\)). The ATE at time t is defined as the posterior distribution of \(E[Y \mid X] - E[Y \mid X_{cf}]\), computed separately for I and C.

Examples

Run this code
# \donttest{
if (interactive() && requireNamespace("cmdstanr", quietly = TRUE)) {
  n <- 120
  DT <- data.table::data.table(
    I = rpois(n, 5), C = rpois(n, 3),
    Regime = factor(sample(c("A","B","C"), n, TRUE)),
    trans_PS = c(rep(1,5), rep(0,n-5)),
    trans_SF = c(rep(0,60), rep(1,5), rep(0,n-65)),
    trans_FC = rep(0, n),
    log_exposure50 = log(runif(n, 40, 60))
  )
  fit_obj <- fit_one(DT, k = 1, spec = "C")
  ate_tab <- contrafactual_ATE(fit_obj, compute_intervals = TRUE)
  head(ate_tab)
}
# }

Run the code above in your browser using DataLab