pyINLA · random effects

linear

A covariate times a coefficient with its own normal prior: a fixed effect, written as a random effect.

Idea

{'id': 'x', 'model': 'linear'} adds $\beta\, x_i$ to the predictor, as 'fixed': ['x'] does, but the prior of $\beta$ is set in the effect. Use it to give one covariate its own prior. The estimate is reported in summary_fixed, with the fixed effects. clinear keeps $\beta$ in a range $[a, b]$.

Model

$$\eta_i = \dots + \beta\, x_i, \qquad \beta \sim \mathcal N\big(\mu,\ 1/\tau_\beta\big)$$
meaningset withdefault
$x_i$the covariate of observation $i$id
$\mu$prior mean of $\beta$mean.linear0
$\tau_\beta$prior precision of $\beta$prec.linear0.001 (sd 31.6)

The model has no hyperparameters.

Example

Simulated data with slope 2: as a fixed effect, as linear, and as linear with the prior $\beta \sim \mathcal N(1.5,\ 0.1^2)$.

import numpy as np
import pandas as pd
from pyinla import pyinla

rng = np.random.default_rng(1)
n = 200
x = rng.standard_normal(n)
df = pd.DataFrame({"y": 1 + 2 * x + rng.normal(0, 0.5, n), "x": x})   # true slope 2

def fit(model):
    return pyinla(model=model, family="gaussian", data=df)

linear = {"id": "x", "model": "linear"}
prior  = {"id": "x", "model": "linear", "mean.linear": 1.5, "prec.linear": 100}   # mean 1.5, sd 0.1

fit_fixed  = fit({"response": "y", "fixed": ["1", "x"]})                 # x as a fixed effect
fit_linear = fit({"response": "y", "fixed": ["1"], "random": [linear]})  # the same, written as linear
fit_prior  = fit({"response": "y", "fixed": ["1"], "random": [prior]})   # with its own prior

print(pd.DataFrame({name: f.summary_fixed.loc["x", ["mean", "sd"]]
                    for name, f in [("fixed", fit_fixed), ("linear", fit_linear),
                                    ("linear, prior", fit_prior)]}).T.round(3))
                mean     sd
fixed          2.020  0.034
linear         2.020  0.034
linear, prior  1.964  0.033

With the default prior, linear gives the same as the fixed effect. With the prior, the estimate, 1.96, lies between the data (2.02) and the prior mean (1.5), weighted by their precisions: $1/0.034^2 \approx 865$ for the data, 100 for the prior.

Reference

Options

entry, for exampledefaultmeaning
'id': 'x'column with the covariate; also its name in summary_fixed
'model': 'linear'
'covariate': 'x'the id columnthe covariate, as a column name or a vector
'mean.linear': 1.50prior mean of $\beta$
'prec.linear': 1000.001prior precision of $\beta$
'diagonal': 1e-40constant added to the prior precision

No other key is accepted. There is no hyper: the prior is set with mean.linear and prec.linear. There are no weights: multiply the covariate by them instead.