pyINLA · Random Effects

Generic1 Model

Type 1 generic specification with correlation parameter controlling the influence of structure matrix C.

Overview

The generic1 model extends the generic0 model by introducing a correlation parameter \(\beta\) that controls how much the structure matrix \(\mathbf{C}\) influences the precision. This allows the data to determine the strength of the structured dependence.

Key features:

Mathematical Formulation

The generic1 model defines the precision matrix as:

\[\mathbf{Q} = \tau \left( \mathbf{I} - \frac{\beta}{\lambda_{\max}} \mathbf{C} \right)\]

where:

Interpretation of Parameters

  • \(\beta \to 0\): The model approaches IID (\(\mathbf{Q} \approx \tau \mathbf{I}\))
  • \(\beta \to 1\): Maximum influence of the structure matrix C

When to Use Generic1

Unknown Correlation Strength

When you have a structure matrix but want to estimate how strongly it influences the random effects.

Network/Graph Effects

When using adjacency matrices where the strength of neighbor influence should be estimated.

Spatial Proximity

When nearby locations should be similar but the degree of similarity is unknown.

Model Flexibility

When you want the data to determine if structured dependence is present.

Specification in pyINLA

Allowed Parameters

Parameter Required Description
id Yes Column name for the grouping/index variable
model Yes Must be 'generic1'
Cmatrix Yes The \(n \times n\) structure matrix (symmetric, eigenvalues typically scaled to [-1, 1])
hyper No List of two prior specifications: [precision, beta]
constr No Sum-to-zero constraint. Default: False
extraconstr No Dict with keys 'A' and 'e' for custom linear constraints
n No Dimension of the random effect (auto-detected from Cmatrix if omitted)
diagonal No Small value added to precision diagonal for numerical stability (default: 1e-4)

Basic Syntax

Shared setup. Every snippet on this page assumes this block has been run first; it builds a small synthetic df with the columns the examples use.

# shared setup for every snippet on this page (run this first)
import numpy as np
import pandas as pd
from pyinla import pyinla, inla_surv

rng = np.random.default_rng(42)
n = 80
n_regions = 20
neighbors = [[(i - 1) % n_regions, (i + 1) % n_regions] for i in range(n_regions)]   # a ring
df = pd.DataFrame({'x': rng.standard_normal(n), 'idx': rng.integers(1, n_regions + 1, size=n)})
df['y'] = 0.5 * df['x'] + rng.normal(0, 0.5, n)
df['region'] = rng.integers(1, 11, size=len(df))     # index column used by a snippet below
import numpy as np
from pyinla import pyinla

# Create adjacency-based structure matrix
n = 20
# Simple chain: each node connected to neighbors
C = np.zeros((n, n))
for i in range(n - 1):
    C[i, i + 1] = 1
    C[i + 1, i] = 1

# Model specification (intercept included by default)
formula = {
    'response': 'y',
    'fixed': ['x'],
    'random': [
        {
            'id': 'idx',
            'model': 'generic1',
            'Cmatrix': C
        }
    ]
}

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

Hyperparameters

The generic1 model has two hyperparameters:

Parameter Internal Scale Range Description
\(\tau\) \(\log(\tau)\) \((0, \infty)\) Precision (overall scale)
\(\beta\) \(\text{logit}(\beta)\) \((0, 1)\) Correlation parameter (C influence)

Prior Specification

'random': [{
    'id': 'idx',
    'model': 'generic1',
    'Cmatrix': C,
    'hyper': [
        {  # theta1: log(tau) - precision
            'prior': 'pc.prec',
            'param': [1, 0.01],
            'initial': 4
        },
        {  # theta2: logit(beta) - correlation
            'prior': 'gaussian',
            'param': [0, 1],       # mean=0, precision=1 on logit scale
            'initial': 0            # logit(0.5) = 0 (beta = 0.5)
        }
    ]
}]

Fixed Beta

Fix the correlation parameter at a specific value:

import math
# scipy.special.logit(0.8) or manual calculation
beta_fixed = 0.8
logit_beta = math.log(beta_fixed / (1 - beta_fixed))

'hyper': [
    {'prior': 'pc.prec', 'param': [1, 0.01]},  # Estimate precision
    {'initial': logit_beta, 'fixed': True}    # Fix beta = 0.8
]

Example: Spatial Adjacency

Use generic1 with a row-standardized adjacency matrix:

# Build adjacency matrix from neighbors
# Assume 'neighbors' is a list of neighbor indices for each region
n_regions = 20
W = np.zeros((n_regions, n_regions))
for i, neighs in enumerate(neighbors):
    for j in neighs:
        W[i, j] = 1

# Row-standardize
row_sums = W.sum(axis=1, keepdims=True)
row_sums[row_sums == 0] = 1  # Avoid division by zero
C = W / row_sums

formula = {
    'response': 'y',
    'fixed': ['x'],
    'random': [{
        'id': 'region',
        'model': 'generic1',
        'Cmatrix': C
    }]
}

Relationship to Other Models