Overview
This page walks you through fitting an SPDE model in pyINLA, from mesh creation to extracting results. For the theory behind SPDEs (Matérn covariance, FEM matrices, precision matrix Q), see Understanding SPDEs.
The SPDE Workflow
Here's the step-by-step process for fitting an SPDE model with pyINLA:
Step 1: Create Mesh
Build a triangular mesh covering your spatial domain using the fmesher package.
Step 2: Create SPDE Model
Create the SPDE model object with PC priors on range and sigma.
The FEM matrices (C, G) are computed internally using fm_fem.
prior_range=[r0, p] means P(range < r0) = p, and
prior_sigma=[s0, p] means P(sigma > s0) = p.
Choose r0 based on your domain size and s0 based on expected variability.
Step 3: Build Projector Matrix
Link mesh vertices to observation locations using barycentric interpolation.
Step 4: Create Data Stack
Use inla.stack() to organize data, projector matrices, and effects together.
This handles the different dimensions (n observations vs m mesh vertices).
Step 5: Fit Model
Call INLA with the SPDE random effect. The formula removes the default intercept and adds it as a covariate so all terms use projector matrices.
inla.stack()- Combines data, projector matrices, and effectsinla.stack.data()- Extracts data from stack for inla()inla.stack.A()- Extracts combined projector matrixf(i, model=spde)- Specifies the SPDE random effect in the formula
What You Get
After fitting, you can extract these results from the pyINLA result object:
| Result | pyINLA Access | Description |
|---|---|---|
| Fixed effects | result.summary_fixed |
Posterior of \(\beta_0\) and other covariates (mean, sd, quantiles) |
| Spatial field | result.summary_random['i'] |
Posterior mean/sd at each mesh vertex |
| Hyperparameters | result.summary_hyperpar |
DataFrame with range, sigma, nugget posteriors |
| Marginal likelihood | result.mlik |
Log marginal likelihood for model comparison |
| Fitted values | result.summary_linear_predictor |
Posterior of linear predictor at each observation |
Example: Extracting Results
Next Steps
- Understanding SPDEs - Learn the theory behind SPDE models
- Creating Meshes - Detailed guide on mesh construction
- Priors - Setting PC priors for range and sigma