← Back to Learn
Learn PyINLA

Random Effects

Model unobserved heterogeneity, correlation structures, and hierarchical dependencies using latent Gaussian components via random=[...].

What are Random Effects?

Random effects capture variation that cannot be explained by fixed covariates alone. They are essential for:

  • Hierarchical data - students within schools, patients within hospitals
  • Repeated measurements - multiple observations per subject over time
  • Spatial correlation - nearby locations share similar characteristics
  • Overdispersion - extra variability beyond what the likelihood explains

In pyINLA, random effects are specified using the random key in your model dictionary, with each effect defined by an id (the grouping variable) and a model (the correlation structure).

Quick Syntax

formula = {
    'response': 'y',
    'fixed': ['x1', 'x2'],  # Intercept included by default
    'random': [
        {'id': 'group_id', 'model': 'iid'},  # Group-level random intercepts
    ]
}

result = pyinla(formula=formula, family='gaussian', data=df)

Available Models

Exchangeable

IID (Independent)

Independent and identically distributed random effects. Perfect for group-level intercepts, overdispersion, and simple hierarchical structures.

Learn about IID
Multivariate

IIDKD (k-dimensional)

Correlated k-dimensional random effects with Wishart prior. For random intercept-slope models and multivariate outcomes.

Learn about IIDKD
Temporal

RW1 (Random Walk)

First-order random walk for smooth temporal trends. Ideal for time series with local dependence.

Learn about RW1
Temporal

RW2 (Second-order)

Second-order random walk for smoother temporal trends. Penalizes curvature for gradual, smooth changes over time.

Learn about RW2
Temporal

AR1 (Autoregressive)

First-order autoregressive process with correlation parameter. Classic time series dependence structure with exponentially decaying correlation.

Learn about AR1
Temporal

AR(p) (Higher-order)

Autoregressive process of order p (1-10) with PACF parameterization. For complex temporal dependence beyond simple exponential decay.

Learn about AR(p)
Temporal

Seasonal

Periodic seasonal effects where sums over m consecutive values are IID Gaussian. For monthly, quarterly, or other cyclic patterns.

Learn about Seasonal
Spatial

Besag (ICAR)

Intrinsic Conditional Autoregressive model for areal spatial data. Captures spatial dependence where neighboring regions have similar values.

Learn about Besag
Spatial

BYM2

Besag-York-Mollie reparameterization with PC priors. Combines structured (ICAR) and unstructured (IID) spatial effects with interpretable hyperparameters.

Learn about BYM2
Spatial

SPDE (Matérn)

Stochastic Partial Differential Equation approach for continuous spatial fields with Matérn covariance. Uses triangular meshes and PC priors.

Learn about SPDE
Adaptive Spatial · cgeneric

fbesag (Partitioned)

Adaptive Besag where the graph is split into P partitions and each partition has its own log-precision. Useful when different regions need different smoothness. Accessed via the pyinla.gmrfs wrapper.

Learn about fbesag
Custom Precision

Generic0 (User-supplied Q)

Bring your own structure matrix C; pyinla scales it by a precision hyperparameter to give Q = τ · C. The most flexible building block; use when Besag/RW1/etc. don't capture the dependence you have in mind.

Learn about Generic0

Hyperparameter Customization

Each random effect has hyperparameters (e.g., precision for IID) that control the variance structure. You can customize these using the hyper key:

'random': [
    {
        'id': 'group_id',
        'model': 'iid',
        'hyper': [{
            'prior': 'pc.prec',      # PC prior for precision
            'param': [1, 0.01],     # P(sigma > 1) = 0.01
            'initial': 4,           # Starting value for log(precision)
            'fixed': False          # Estimate (don't fix)
        }]
    }
]

Choosing the Right Model

Supported Latent Model Reference

The latent model types recognised by pyINLA, grouped by use case.

Exchangeable / IID Models

Keyword Description
iidIndependent and identically distributed random effects
iidkdk-dimensional correlated IID

Temporal Models

Keyword Description
rw1First-order random walk
rw2Second-order random walk (smoother)
ar1First-order autoregressive
arHigher-order AR(p), p=1-10
seasonalSeasonal/periodic effects

Spatial Models

Keyword Description
besagIntrinsic CAR (ICAR) model
bymOriginal BYM (structured + unstructured spatial). Prefer bym2.
bym2Reparameterized BYM with PC priors
SPDE objectSPDE approach for Matérn fields (limited safety-layer surface)
pyinla.gmrfs.fbesag(…)Partitioned Besag with one log-precision per partition (cgeneric wrapper, not a model='…' keyword)

Design Matrix / Linear Models

Keyword Description
linearLatent single-slope with explicit Gaussian prior
clinearConstrained linear effect (slope bounded via reparameterization)

Custom Precision Models

User-supplied precision matrix C; pyinla wraps it with one precision hyperparameter. Both model="generic" and model="generic0" are accepted and resolve to the same precision structure.

Keyword Precision matrix Q Hyperparameters
generic0Q = τ · Clog τ
visual ref Anatomy: how does u attach to y?