Introduction to inlabru
December 15, 2025
inlabru?inlabru-friendly?inlabru?inlabru?The short answer:
INLA is a fast method to do Bayesian inference with latent Gaussian models and
inlabruis anR-package that implements this method with a flexible and simple interface.
The (much) longer answer:
Website-tutorials
Discussion forums
Books
Blangiardo, M., & Cameletti, M. (2015). Spatial and spatio-temporal Bayesian models with R-INLA. John Wiley & Sons.
Gómez-Rubio, V. (2020). Bayesian inference with INLA. Chapman and Hall/CRC.
Krainski, E., Gómez-Rubio, V., Bakka, H., Lenzi, A., Castro-Camilo, D., Simpson, D., … & Rue, H. (2018). Advanced spatial modeling with stochastic partial differential equations using R and INLA. Chapman and Hall/CRC.
Wang, X., Yue, Y. R., & Faraway, J. J. (2018). Bayesian regression modeling with INLA. Chapman and Hall/CRC.
inlabru?inlabru?To give proper answers to these questions, we need to start at the very beginning ..
We need to make choices:
- Bayesian or frequentist?
- How do we model the data?
- How do we compute the answer?
We need to make choices:
These questions are not independent.
In this course we embrace the Bayesian perspective
In this course we embrace the Bayesian perspective
In this course we embrace the Bayesian perspective
We define linear predictor the mean (or a function of the mean) of our observations given the model components.
In this case \(E(y_i|\beta_0, \beta_i) = \eta_i = \beta_0 + \beta_1 x_i\)
We define linear predictor the mean (or a function of the mean) of our observations given the model components.
In this case \(E(y_i|\beta_0, \beta_i) =\eta_i = \color{red}{\boxed{\beta_0}} + \color{red}{\boxed{\beta_1 x_i}}\)
This model as two components !
Given the linear predictor \(\eta\) the observations a independent on each other!
This means that all dependencies in the observations are accounted for by the components!
Given the linear predictor \(\eta\) the observations a independent on each other!
The observation model (likelihood) can be written as: \[ \pi(\mathbf{y}|\eta,\sigma^2) = \prod_{i = 1}^n\pi(y_i|\eta_i,\sigma^2) \]
The elements of a inlabru friendly statistical model are:
The observational model \[ \begin{aligned} y_i|\eta_i, \sigma^2 & \sim\mathcal{N}(\eta_i,\sigma^2),\qquad i = 1,\dots,n\\ E(y_i|\eta_i, \sigma^2) & = \eta_i \end{aligned} \]
Note
We assume that, given the linear predictor \(\eta\), the data are independent on each other! Data dependence is expressed through the components if the linear predictor.
The elements of a inlabru friendly statistical model are:
The observational model \(y_i|\eta_i,\sigma^2\sim\mathcal{N}(\eta_i,\sigma^2),\qquad i = 1,\dots,n\)
A model for the linear predictor \[ E(y_i|\eta_i,\sigma^2) = \eta_i = \beta_0 + \beta_1x_i \]
The elements of a inlabru friendly statistical model are:
The observational model \(y_i|\eta_i,\sigma^2\sim\mathcal{N}(\eta_i,\sigma^2),\qquad i = 1,\dots,n\)
A model for the linear predictor
\[ E(y_i|\eta_i,\sigma^2) = \eta_i = \color{red}{\boxed{\beta_0}} + \color{red}{\boxed{\beta_1x_i} } \]
Note 1: These are the components of our model! These explain the dependence structure of the data.
The elements of a inlabru friendly statistical model are:
The observational model \(y_i|\eta_i,\sigma^2\sim\mathcal{N}(\eta_i,\sigma^2),\qquad i = 1,\dots,n\)
A model for the linear predictor \(\eta_i = \color{red}{\boxed{\beta_0}} + \color{red}{\boxed{\beta_1x_i} }\)
A prior for the model components \(\textbf{u}\) \[ \mathbf{u} = \{\beta_0, \beta_1\}\sim\mathcal{N}(0,\mathbf{Q}^{-1}) \] Note: These have always a Gaussian prior and are use to explain the dependencies among data!
The elements of a inlabru friendly statistical model are:
The observational model \(y_i|\eta_i,\sigma^2\sim\mathcal{N}(\eta_i,\sigma^2),\qquad i = 1,\dots,n\)
A model for the linear predictor \(\eta_i = \color{red}{\boxed{\beta_0}} + \color{red}{\boxed{\beta_1x_i} }\)
A prior for the model components \(\mathbf{u} = \{\beta_0, \beta_1\}\sim\mathcal{N}(0,\mathbf{Q}^{-1})\)
A prior for the non-gaussian parameters \(\theta\) \[ \theta = \sigma^2 \]
The observation model: \[ \pi(\mathbf{y}|\eta,\theta) = \prod_{i=1}^{n}\pi(y_i|\eta_i,\theta) \]
Linear predictor \(\eta_i = \beta_0 + \beta_1 x_i\)
Latent Gaussian field \(\pi(\mathbf{u}|\theta)\)
The hyperparameters: \(\pi(\theta)\)
Stage 1 The data generating process
The observation model: \[ \pi(\mathbf{y}|\eta,\theta) = \prod_{i=1}^{n}\pi(y_i|\eta_i,\theta) \]
Linear predictor \(\eta_i = \beta_0 + \beta_1 x_i\)
Latent Gaussian field \(\pi(\mathbf{u}|\theta)\)
The hyperparameters: \(\pi(\theta)\)
Stage 1 The data generating process
Stage 2 The dependence structure
The observation model: \[ \pi(\mathbf{y}|\eta,\theta) = \prod_{i=1}^{n}\pi(y_i|\eta_i,\theta) \]
Linear predictor \(\eta_i = \beta_0 + \beta_1 x_i\)
Latent Gaussian field \(\pi(\mathbf{u}|\theta)\)
The hyperparameters: \(\pi(\theta)\)
Stage 1 The data generating process
Stage 2 The dependence structure
Stage 3 The hyperparameters
The observation model: \[ \pi(\mathbf{y}|\eta,\theta) = \prod_{i=1}^{n}\pi(y_i|\eta_i,\theta) \]
Linear predictor \(\eta_i = \beta_0 + \beta_1 x_i\)
Latent Gaussian field \(\pi(\mathbf{u}|\theta)\)
The hyperparameters: \(\pi(\theta)\)
Stage 1 The data generating process
Stage 2 The dependence structure
Stage 3 The hyperparameters
Q: What are we interested in?
\[ \color{purple}{\pi(\mathbf{u},\theta|\mathbf{y})}\propto \color{#FF6B6B}{\pi(\mathbf{y}|\mathbf{u},\theta)}\color{#0066CC}{\pi(\mathbf{u}|\theta)\pi(\theta)} \] ##
inlabru for linear regressionThe Model
\[ \begin{aligned} y_i|\eta_i, \sigma^2 & \sim \mathcal{N}(\eta_i,\sigma^2)\\ \eta_i & = \beta_0 + \beta_i x_i \end{aligned} \]
The code
inlabru for linear regressionThe Model
\[ \begin{aligned} y_i|\eta_i, \sigma^2 & \sim \mathcal{N}(\eta_i,\sigma^2)\\ \eta_i & = \color{#FF6B6B}{\boxed{\beta_0}} + \color{#FF6B6B}{\boxed{\beta_i x_i}} \end{aligned} \]
The code
inlabru for linear regressionThe Model
\[ \begin{aligned} y_i|\eta_i, \sigma^2 & \sim \mathcal{N}(\eta_i,\sigma^2)\\ \eta_i & = \color{#FF6B6B}{\boxed{\beta_0 + \beta_i x_i}} \end{aligned} \]
The code
inlabru for linear regressionThe Model
\[ \begin{aligned} \color{#FF6B6B}{\boxed{y_i|\eta_i, \sigma^2}} & \color{#FF6B6B}{\boxed{\sim \mathcal{N}(\eta_i,\sigma^2)}}\\ \eta_i & = \beta_0 + \beta_i x_i \end{aligned} \]
The code
inlabru for linear regressionThe Model
\[ \begin{aligned} y_i|\eta_i, \sigma^2 & \sim \mathcal{N}(\eta_i,\sigma^2)\\ \eta_i & = \beta_0 + \beta_i x_i \end{aligned} \]
The code
inlabru for linear regressionData can have several dependence structures: temporal, spatial,…
Using a Bayesian framework:
Build (hierarchical) models to account for potentially complicated dependency structures in the data.
Attribute uncertainty to model parameters and latent variables using priors.
Two main challenges:
In many cases complicated spatio-temporal models are just special cases of the same model structure!! 😃
Stage 1: What is the distribution of the responses?
Stage 2: What are the model components? and what is their distribution?
Stage 3: What are our prior beliefs about the parameters controlling the components in the model?
In many cases complicated spatio-temporal models are just special cases of the same model structure!! 😃
Stage 1: What is the distribution of the responses?
Stage 2: What are the model components? and what is their distribution?
Stage 3: What are our prior beliefs about the parameters controlling the components in the model?
In many cases complicated spatio-temporal models are just special cases of the same model structure!! 😃
Stage 1: What is the distribution of the responses?
Stage 2: What are the model components? and what is their distribution?
Stage 3: What are our prior beliefs about the parameters controlling the components in the model?
In many cases complicated spatio-temporal models are just special cases of the same model structure!! 😃
Stage 1: What is the distribution of the responses?
Stage 2: What are the model components? and what is their distribution?
Here we can have:
These are linked to the responses in the likelihood through linear predictors.
In many cases complicated spatio-temporal models are just special cases of the same model structure!! 😃
Stage 1: What is the distribution of the responses?
Stage 2: What are the model components? and what is their distribution?
Stage 3: What are our prior beliefs about the parameters controlling the components in the model?
The likelihood and the latent model typically have hyperparameters that control their behavior.
They can include:
No matter how complicated is your model, the inlabru workflow is always the same 😃
No matter how complicated is your model, the inlabru workflow is always the same 😃
NOTE inlabru allows non-linear functions to be specified
One example with time series: Rainfall over 1 mm in the Tokyo area for each calendar day during two years (1983-84) are registered.
Stage 1 The observation model
\[ y_t|\eta_t\sim\text{Bin}(n_t, p_t),\qquad \eta_t = \text{logit}(p_t),\qquad i = 1,\dots,366 \]
\[ n_t = \left\{ \begin{array}{lr} 1, & \text{for}\; 29\; \text{February}\\ 2, & \text{other days} \end{array}\right. \] \[ y_t = \begin{cases} \{0,1\}, & \text{for}\; 29\; \text{February}\\ \{0,1,2\}, & \text{other days} \end{cases} \]
Stage 1 The observation model
\[ y_t|\eta_t\sim\text{Bin}(n_t, p_t),\qquad \eta_t = \text{logit}(p_t),\qquad i = 1,\dots,366 \]
Stage 2 The latent field
\[ \eta_t = \beta_0 + f(\text{time}_t) \]
probability of rain depends on on the day of the year \(t\)
\(\beta_0\) is an intercept
\(f(\text{time}_t)\) is a RW2 model (this is just a smoother). The smoothness is controlled by a hyperparameter \(\tau_f\)
Stage 1 The observation model
\[ y_t|\eta_t\sim\text{Bin}(n_t, p_t),\qquad \eta_t = \text{logit}(p_t),\qquad i = 1,\dots,366 \]
Stage 2 The latent field
\[ \eta_t = \beta_0 + f(\text{time}_t) \]
Stage 3 The hyperparameters
The structured time effect is controlled by one parameter \(\tau_f\).
We assign a prior to \(\tau_f\) to finalize the model.
inlabru for time seriesThe Model
\[ \begin{aligned} y_t|\eta_t & \sim \text{Binomial}(n_t,p_t)\\ \text{logit}(p_t) = \eta_i & = \color{#FF6B6B}{\boxed{\beta_0}} + \color{#FF6B6B}{\boxed{f(\text{time}_t)}} \end{aligned} \]
The code
inlabru for time seriesThe Model
\[ \begin{aligned} y_t|\eta_t & \sim \text{Binomial}(n_t,p_t)\\ \text{logit}(p_t) = \color{#FF6B6B}{\boxed{\eta_i}} & = \color{#FF6B6B}{\boxed{\beta_0 + f(\text{time}_t)}} \end{aligned} \]
The code
inlabru for time seriesThe Model
\[ \begin{aligned} \color{#FF6B6B}{\boxed{y_t|\eta_t}} & \color{#FF6B6B}{\boxed{\sim \text{Binomial}(n_t,p_t)}}\\ \text{logit}(p_t) = \eta_i & = \beta_0 + f(\text{time}_t) \end{aligned} \]
The code
inlabru for time seriesThe Model
\[ \begin{aligned} y_t|\eta_t & \sim \text{Binomial}(n_t,p_t)\\ \text{logit}(p_t) = \eta_i & = \beta_0 + f(\text{time}_t) \end{aligned} \]
The code
We observed larynx cancer mortality counts for males in 544 district of Germany from 1986 to 1990 and want to make a model.
\(y_i\): The count at location \(i\).
\(E_i\): An offset; expected number of cases in district \(i\).
\(c_i\): A covariate (level of smoking consumption) at \(i\)
\(\boldsymbol{s}_i\): spatial location \(i\) .
- Stage 1: We assume the responses are Poisson distributed: \[ y_i \mid \eta_i \sim \text{Poisson}(E_i\exp(\eta_i))) \]
- Stage 2: \(\eta_i\) is a linear function of three components: an intercept, a covariate \(c_i\), a spatially structured effect \(\omega\) likelihood by \[ \eta_i = \beta_0 + \beta_1\ c_i + \omega_i \]
- Stage 3:
- \(\tau_{\omega}\): Precisions parameter for the random effects
The latent field is \(\boldsymbol{u} = (\beta_0, \beta_1, \omega_1, \omega_2,\ldots, \omega_n)\), the hyperparameters are \(\boldsymbol{\theta} = (\tau_{\omega})\), and must be given a prior.
inlabru for disease mappingThe Model
\[ \begin{aligned} y_i|\eta_t & \sim \text{Poisson}(E_i\lambda_i)\\ \text{log}(\lambda_i) = \eta_i & = \color{#FF6B6B}{\boxed{\beta_0}} + \color{#FF6B6B}{\boxed{\beta_1\ c_i}} + \color{#FF6B6B}{\boxed{\omega_i}} \end{aligned} \]
The code
# define model component
cmp = ~ -1 + beta0(1) + beta1(x, model = "linear") +
space(region, model = "besag", graph = g)
# define model predictor
eta = Y ~ beta0 + beta1 + space
# build the observation model
lik = bru_obs(formula = eta,
family = "poisson",
E = E,
data = Germany)
# fit the model
fit = bru(cmp, lik)inlabru for disease mappingThe Model
\[ \begin{aligned} y_i|\eta_t & \sim \text{Poisson}(E_i\lambda_i)\\ \text{log}(\lambda_i) = \color{#FF6B6B}{\boxed{\eta_i}} & = \color{#FF6B6B}{\boxed{\beta_0 + \beta_1\ c_i + \omega_i}} \end{aligned} \]
The code
# define model component
cmp = ~ -1 + beta0(1) + beta1(x, model = "linear") +
space(region, model = "bym2", graph = g)
# define model predictor
eta = Y ~ beta0 + beta1 + space
# build the observation model
lik = bru_obs(formula = eta,
family = "poisson",
E = E,
data = Germany)
# fit the model
fit = bru(cmp, lik)inlabru for disease mappingThe Model
\[ \begin{aligned} \color{#FF6B6B}{\boxed{y_i|\eta_t}} & \color{#FF6B6B}{\boxed{\sim \text{Poisson}(E_i\lambda_i)}}\\ \text{log}(\lambda_i) = \eta_i & = \beta_0 + \beta_1\ c_i + \omega_i \end{aligned} \]
The code
# define model component
cmp = ~ -1 + beta0(1) + beta1(x, model = "linear") +
space(region, model = "bym2", graph = g)
# define model predictor
eta = Y ~ beta0 + beta1 + space
# build the observation model
lik = bru_obs(formula = eta,
family = "poisson",
E = E,
data = Germany)
# fit the model
fit = bru(cmp, lik)inlabru for disease mappingEncounter probability of Pacific Cod (Gadus macrocephalus) from a trawl survey.
\(y(s)\) Presence of absence in location \(s\)
Stage 1 Model for the response \[ y(s)|\eta(s)\sim\text{Binom}(1, p(s)) \]
Stage 2 Latent field model \[ \eta(s) = \text{logit}(p(s)) = \beta_0 + f( x(s)) + \omega(s) \]
Stage 3 Hyperparameters
Stage 1 Model for the response \[ y(s)|\eta(s)\sim\text{Binom}(1, p(s)) \]
Stage 2 Latent field model \[ \eta(s) = \text{logit}(p(s)) = \beta_0 + \beta_1 x(s) + \omega(s) \]
Stage 3 Hyperparameters
inlabru for geostatisticsThe Model
\[ \begin{aligned} y(s)|\eta(s) & \sim \text{Binom}(1, p(s))\\ \eta(s) & = \color{#FF6B6B}{\boxed{\beta_0 }}+ \color{#FF6B6B}{\boxed{f(x(s))}} + \color{#FF6B6B}{\boxed{\omega(s)}}\\ \end{aligned} \]
Simple feature collection with 2143 features and 2 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 343.0617 ymin: 5635.893 xmax: 579.3681 ymax: 5839.019
Projected CRS: +proj=utm +zone=9 +datum=WGS84 +no_defs +type=crs +units=km
# A tibble: 2,143 × 3
depth present geometry
<dbl> <dbl> <POINT [km]>
1 201 1 (446.4752 5793.426)
2 212 1 (446.4594 5800.136)
3 220 0 (448.5987 5801.687)
# ℹ 2,140 more rows
The code
# define model component
cmp = ~ -1 + Intercept(1) + depth_smooth(log(depth), model='rw2') +
space(geometry, model = spde_model)
# define model predictor
eta = present ~ Intercept + depth_smooth + space
# build the observation model
lik = bru_obs(formula = eta,
data = df,
family = "binomial")
# fit the model
fit = bru(cmp, lik)inlabru for geostatisticsThe Model
\[ \begin{aligned} y(s)|\eta(s) & \sim \text{Binom}(1, p(s))\\ \color{#FF6B6B}{\boxed{\eta(s)}} & = \color{#FF6B6B}{\boxed{\beta_0 + f(x(s)) + \omega(s)}}\\ \end{aligned} \]
Simple feature collection with 2143 features and 2 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 343.0617 ymin: 5635.893 xmax: 579.3681 ymax: 5839.019
Projected CRS: +proj=utm +zone=9 +datum=WGS84 +no_defs +type=crs +units=km
# A tibble: 2,143 × 3
depth present geometry
<dbl> <dbl> <POINT [km]>
1 201 1 (446.4752 5793.426)
2 212 1 (446.4594 5800.136)
3 220 0 (448.5987 5801.687)
# ℹ 2,140 more rows
The code
# define model component
cmp = ~ -1 + Intercept(1) + depth_smooth(log(depth), model='rw2') +
space(geometry, model = spde_model)
# define model predictor
eta = present ~ Intercept + depth_smooth + space
# build the observation model
lik = bru_obs(formula = eta,
data = df,
family = "binomial")
# fit the model
fit = bru(cmp, lik)inlabru for geostatisticsThe Model
\[ \begin{aligned} \color{#FF6B6B}{\boxed{y(s)|\eta(s)}} & \sim \color{#FF6B6B}{\boxed{\text{Binom}(1, p(s))}}\\ \eta(s) & = \beta_0 + f(x(s)) + \omega(s)\\ \end{aligned} \]
Simple feature collection with 2143 features and 2 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 343.0617 ymin: 5635.893 xmax: 579.3681 ymax: 5839.019
Projected CRS: +proj=utm +zone=9 +datum=WGS84 +no_defs +type=crs +units=km
# A tibble: 2,143 × 3
depth present geometry
<dbl> <dbl> <POINT [km]>
1 201 1 (446.4752 5793.426)
2 212 1 (446.4594 5800.136)
3 220 0 (448.5987 5801.687)
# ℹ 2,140 more rows
The code
# define model component
cmp = ~ -1 + Intercept(1) + depth_smooth(log(depth), model='rw2') +
space(geometry, model = spde_model)
# define model predictor
eta = present ~ Intercept + depth_smooth + space
# build the observation model
lik = bru_obs(formula = eta,
data = df,
family = "binomial")
# fit the model
fit = bru(cmp, lik)inlabru for geostatisticsMany of the model you have used (and some you have never used but will learn about) are just special cases of the large class of Latent Gaussian models
inlabru provides an efficient and unified way to fit all these models!