The bml package implements Bayesian
Multiple-Membership Multilevel Models with Parameterizable Weight
Functions. These models are designed for situations where
higher-level outcomes depend on the combined influence of multiple
lower-level units. Instead of aggregating lower-level data to higher
levels (risking aggregation bias) or disaggregating outcomes to lower
levels (artificially inflating sample size), bml explicitly
models how lower-level units jointly shape higher-level outcomes.
Install the stable version from CRAN:
You’ll also need JAGS installed on your system. See the installation vignette for details.
Let’s start with a simple example using coalition government data. Each coalition (higher-level unit) is composed of multiple political parties (lower-level units), and we want to model how party characteristics influence coalition outcomes.
library(bml)
data(coalgov)
# Examine the data structure
head(coalgov[, c("gid", "pid", "pname", "n", "finance", "dur_wkb", "event_wkb")])The coalgov dataset is in long format
where each row represents a party’s participation in a coalition:
gid: Government (coalition) identifierpid: Party identifiern: Number of parties in the coalitionfinance: Party’s financial dependence on member
contributions (standardized)dur_wkb: Coalition duration in daysevent_wkb: Early termination indicator (1 = terminated
early, 0 = censored)Our first model examines coalition duration as a function of party-level characteristics, aggregated using equal weights:
mod1 <- bml(
Surv(dur_wkb, event_wkb) ~ 1 + majority +
mm(
id = id(pid, gid),
vars = vars(finance),
fn = fn(w ~ 1/n, c = TRUE),
RE = TRUE
),
family = "Weibull",
data = coalgov,
seed = 1
)
summary(mod1)Breaking down the formula:
Surv(dur_wkb, event_wkb): Survival outcome (duration
and event indicator)majority: Government-level covariate (binary
indicator)mm(): Multiple-membership block containing:
id = id(pid, gid): Party ID (member) and government ID
(group)vars = vars(finance): Party-level covariate to
aggregatefn = fn(w ~ 1/n, c = TRUE): Weight function (equal
weights, constrained to sum to 1)RE = TRUE: Include party-specific random effectsA key feature of bml is the ability to model aggregation
weights as a function of covariates. Instead of assuming all parties
have equal influence, we can test whether seat share affects a party’s
weight in the coalition:
mod2 <- bml(
Surv(dur_wkb, event_wkb) ~ 1 +
majority +
mm(
id = id(pid, gid),
vars = vars(finance),
fn = fn(w ~ 1 / (1 + (n - 1) * exp(-(b0 + b1 * pseat))), c = TRUE),
RE = TRUE
),
family = "Weibull",
priors = c("b.w ~ dnorm(0,1)"), # weakly informative prior on the weight parameters
data = coalgov,
seed = 1,
monitor = TRUE
)
summary(mod2)New elements:
pseat (party seat
share) with parameter b1priors: Specify prior for the weight function
parametermonitor = TRUE: Save MCMC chains for diagnosticsInterpretation:
w ~ 1/n)w ~ 1/n^exp(b1*x))c:
c = TRUE: Weights sum to 1 within each groupc = FALSE: No constraint on weight sumsRE = TRUE in
mm()) or nesting level (type = "RE" in
hm())