pyINLA · learn

Random Effects

Latent Gaussian effects for what the covariates do not explain: groups, time, space.

Specifying a random effect

Each random effect is a dict in random=[...]: id, the column that indexes it, and model, its structure.

# 6 groups of 20 observations
import numpy as np
import pandas as pd
from pyinla import pyinla

rng = np.random.default_rng(42)
n = 120
group_id = np.repeat(np.arange(6), n // 6)
x1 = rng.standard_normal(n); x2 = rng.standard_normal(n)
df = pd.DataFrame({'y': 3.0 + 1.5 * x1 - 0.7 * x2 + rng.normal(0, 0.3, n),
                   'x1': x1, 'x2': x2, 'group_id': group_id})

model = {'response': 'y',
         'fixed': ['x1', 'x2'],                              # the intercept is included
         'random': [{'id': 'group_id', 'model': 'iid'}]}     # one effect per group
result = pyinla(model=model, family='gaussian', data=df)

The prior of each hyperparameter is set with hyper, one dict per hyperparameter:

{'id': 'group_id', 'model': 'iid',
 'hyper': [{'prior': 'pc.prec', 'param': [1, 0.01],    # P(sd > 1) = 0.01
            'initial': 4, 'fixed': False}]}             # start at log precision 4; estimate it

How an effect enters the predictor: anatomy.

Models

Groups

Independent effects with one precision: group intercepts, overdispersion.

Correlated effects in k dimensions with a Wishart prior: random intercepts and slopes.

Time and other orderings

First-order random walk: a trend in time or along a covariate.

Second-order random walk: a smoother trend, which penalises changes of slope.

First-order autoregressive process, with correlation $\rho$ between neighbouring times.

Autoregressive process of order $p$ (1 to 10), parametrised by partial autocorrelations.

Periodic effect: sums over $m$ consecutive values are independent Gaussian.

fbesag in timepyinla.gmrfs

A random walk whose step size differs between periods, such as an epidemic season and the rest of the year.

Space

Areas on a map: each area is pulled towards its neighbours, by the same amount everywhere.

Areas on a map: a spatial part plus noise of each area, with one parameter for the split.

Continuous space: a Matérn field on a triangular mesh.

fbesagpyinla.gmrfs

besag with its own precision in each sub-region of the map.

fbym2pyinla.gmrfs

bym2 whose spatial part has its own smoothness in each sub-region.

Covariates

A covariate with its own normal prior on the coefficient; by default the same as a fixed effect.

A covariate effect whose coefficient is restricted to a range $[a, b]$.

Your own structure

Your own structure matrix $C$, with one precision: $Q = \tau C$.

The same random effect again in the predictor, with its own weight.

Choosing a model

datamodel
Groups with no orderiid
Correlated effects of several kinds per groupiidkd
A smooth trend in time or along a covariaterw1, rw2
Time series with correlation that decays with lagar1, ar
A periodic pattern (weekly, monthly, …)seasonal
A trend that is rougher in some periods than in othersfbesag on a time line
Areas on a mapbym2; besag without noise of each area
Areas on a map, smoother in some parts than in othersfbym2, fbesag
Points in continuous spacespde
A covariate with its own prior on the coefficientlinear
A coefficient that must stay in a range $[a, b]$clinear
A structure none of these hasgeneric0

All latent models

keywordmodel
iidindependent and identically distributed
iidkdk-dimensional correlated iid
rw1, rw2first- and second-order random walk
ar1, arautoregressive, order 1 and order p (1 to 10)
seasonalseasonal effect
besagintrinsic CAR (besag)
bymthe original BYM model; prefer bym2
bym2BYM reparametrised, with PC priors
spde objectMatérn field on a mesh
pyinla.gmrfs.fbesag(…), pyinla.gmrfs.fbym2(…)functions that return the random effect, not model= keywords
linear, clineara slope with a Gaussian prior; a slope bounded by a reparametrisation
generic0 (or generic)$Q = \tau C$ with your $C$