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.
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.
besag with its own precision in each sub-region of the map.
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
Choosing a model
| data | model |
|---|---|
| Groups with no order | iid |
| Correlated effects of several kinds per group | iidkd |
| A smooth trend in time or along a covariate | rw1, rw2 |
| Time series with correlation that decays with lag | ar1, ar |
| A periodic pattern (weekly, monthly, …) | seasonal |
| A trend that is rougher in some periods than in others | fbesag on a time line |
| Areas on a map | bym2; besag without noise of each area |
| Areas on a map, smoother in some parts than in others | fbym2, fbesag |
| Points in continuous space | spde |
| A covariate with its own prior on the coefficient | linear |
| A coefficient that must stay in a range $[a, b]$ | clinear |
| A structure none of these has | generic0 |
All latent models
| keyword | model |
|---|---|
iid | independent and identically distributed |
iidkd | k-dimensional correlated iid |
rw1, rw2 | first- and second-order random walk |
ar1, ar | autoregressive, order 1 and order p (1 to 10) |
seasonal | seasonal effect |
besag | intrinsic CAR (besag) |
bym | the original BYM model; prefer bym2 |
bym2 | BYM reparametrised, with PC priors |
spde object | Matérn field on a mesh |
pyinla.gmrfs.fbesag(…), pyinla.gmrfs.fbym2(…) | functions that return the random effect, not model= keywords |
linear, clinear | a slope with a Gaussian prior; a slope bounded by a reparametrisation |
generic0 (or generic) | $Q = \tau C$ with your $C$ |