simulate / bootMer

Parametric simulation and bootstrap for fitted mixed models. simulate() draws response vectors from the conditional model; bootMer() repeats that process to build a bootstrap distribution over any user-defined statistic.

simulate

simulate(result, nsim=1, seed=None)[source]

Draw response vectors from the fitted conditional model.

Simulates from:

\[y^* = X\beta + Z u^* + \varepsilon^*\]

where \(u^* \sim N(0,\, \sigma^2 \Lambda\Lambda^\top)\) and \(\varepsilon^* \sim N(0,\, \sigma^2 I_n)\).

Equivalently: \(u^* = \sigma \Lambda z_u\) with \(z_u \sim N(0, I_q)\).

Parameters:
  • result – Fitted CrossedLMEResult.

  • nsim – Number of response vectors to simulate.

  • seed – Seed for the random number generator (int, Generator, or None).

Return type:

ndarray

Returns:

  • np.ndarray of shape (n, nsim) — each column is one simulated

  • response vector.

Parameters:

Examples

>>> y_sim = interlace.simulate(result, nsim=10, seed=42)
>>> y_sim.shape  # (n, 10)

bootMer

bootMer(result, statistic=None, B=500, seed=None, show_progress=False)[source]

Parametric bootstrap for a fitted mixed model.

For each of B iterations:

  1. Simulate a new response vector y* from the fitted model.

  2. Refit the model on (X, Z, y*).

  3. Collect statistic(refit) into estimates.

Parameters:
  • result – Fitted CrossedLMEResult.

  • statistic – Callable (CrossedLMEResult) np.ndarray (1-D). Defaults to _default_statistic() which collects [fe_params..., sqrt(sigma^2), theta...].

  • B – Number of bootstrap replicates.

  • seed – Seed for the random number generator.

  • show_progress – If True and tqdm is installed, display a progress bar.

Return type:

BootResult

Returns:

BootResult.estimates has shape (B, p).

Parameters:
  • result (CrossedLMEResult)

  • statistic (Callable[[...], ndarray] | None)

  • B (int)

  • seed (int | Generator | None)

  • show_progress (bool)

Examples

>>> boot = interlace.bootMer(result, B=200, seed=42)
>>> boot.ci(level=0.95)

BootResult

class BootResult(estimates)[source]

Container for parametric bootstrap estimates.

Parameters:

estimates (ndarray)

estimates

Array of shape (B, p) where B is the number of bootstrap replicates and p is the length of the statistic vector.

Examples

>>> boot = result.bootMer(B=100, seed=42)
>>> boot.estimates.shape  # (100, p + 1 + n_theta)
>>> boot.ci(level=0.95)
estimates: ndarray
ci(method='perc', level=0.95)[source]

Compute bootstrap confidence intervals.

Parameters:
  • method"perc" (percentile) is currently supported.

  • level – Confidence level, e.g. 0.95 for a 95% CI.

Return type:

ndarray

Returns:

np.ndarray of shape (p, 2) with columns [lower, upper].

Parameters:
  • method (str)

  • level (float)

Example

import interlace

result = interlace.fit("rt ~ condition", data=df, groups=["subject", "item"])

# Draw 100 response vectors from the fitted model
sims = interlace.simulate(result, n=100, seed=42)
print(sims.shape)  # (n_obs, 100)

# Parametric bootstrap: distribution of fixed-effect coefficients
boot = interlace.bootMer(result, B=500, seed=0)
print(boot.estimates.shape)          # (500, n_params)
ci = boot.ci(method="perc", level=0.95)
print(ci)                            # (n_params, 2) lower/upper bounds

See also