Lecture 1

Introduction to inlabru

Sara Martino

Dept. of Mathematical Science, NTNU

Janine Illian

University of Glasgow

Jafet Belmont

University of Glasgow

December 15, 2025

Outline

  • What are INLA and inlabru?
  • Why the Bayesian framework?
  • Which model are inlabru-friendly?
  • What are Latent Gaussian Models?
  • How are they implemented in inlabru?

What is INLA? What is inlabru?

The short answer:

INLA is a fast method to do Bayesian inference with latent Gaussian models and inlabru is an R-package that implements this method with a flexible and simple interface.

The (much) longer answer:

  • Rue, H., Martino, S. and Chopin, N. (2009), Approximate Bayesian inference for latent Gaussian models by using integrated nested Laplace approximations. Journal of the Royal Statistical Society: Series B (Statistical Methodology), 71: 319-392.
  • Van Niekerk, J., Krainski, E., Rustand, D., & Rue, H. (2023). A new avenue for Bayesian inference with INLA. Computational Statistics & Data Analysis, 181, 107692.
  • Lindgren, F., Bachl, F., Illian, J., Suen, M. H., Rue, H., & Seaton, A. E. (2024). inlabru: software for fitting latent Gaussian models with non-linear predictors. arXiv preprint arXiv:2407.00791.
  • Lindgren, F., Bolin, D., & Rue, H. (2022). The SPDE approach for Gaussian and non-Gaussian fields: 10 years and still running. Spatial Statistics, 50, 100599.

Where?

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.

So… Why should you use inlabru?

  • What type of problems can we solve?
  • What type of models can we use?
  • When can we use it?

So… Why should you use inlabru?

  • What type of problems can we solve?
  • What type of models can we use?
  • When can we use it?

To give proper answers to these questions, we need to start at the very beginning ..

The core

  • We have observed something.

The core

  • We have observed something.
  • We have questions.

The core

  • We have observed something.
  • We have questions.
  • We want answers!

How do we find answers?

We need to make choices:

  • Bayesian or frequentist?
  • How do we model the data?
  • How do we compute the answer?

How do we find answers?

We need to make choices:

  • Bayesian or frequentist?
  • How do we model the data?
  • How do we compute the answer?

These questions are not independent.

Bayesian or frequentist?

In this course we embrace the Bayesian perspective

  • There are no “true but unknown” parameters !

Bayesian or frequentist?

In this course we embrace the Bayesian perspective

  • There are no “true but unknown” parameters !
  • Every parameter is described by a probability distribution!

Bayesian or frequentist?

In this course we embrace the Bayesian perspective

  • There are no “true but unknown” parameters !
  • Every parameter is described by a probability distribution!
  • Evidence from the data is used to update the belief we had before observing the data!

Some more details 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 = \beta_0 + \beta_1 x_i\)

Some more details 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 !

Some more details II

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!

Some more details II

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) \]

Let’s formalize this a bit…

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.

Let’s formalize this a bit…

The elements of a inlabru friendly statistical model are:

  1. The observational model \(y_i|\eta_i,\sigma^2\sim\mathcal{N}(\eta_i,\sigma^2),\qquad i = 1,\dots,n\)

  2. A model for the linear predictor \[ E(y_i|\eta_i,\sigma^2) = \eta_i = \beta_0 + \beta_1x_i \]

Let’s formalize this a bit…

The elements of a inlabru friendly statistical model are:

  1. The observational model \(y_i|\eta_i,\sigma^2\sim\mathcal{N}(\eta_i,\sigma^2),\qquad i = 1,\dots,n\)

  2. 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.

Let’s formalize this a bit…

The elements of a inlabru friendly statistical model are:

  1. The observational model \(y_i|\eta_i,\sigma^2\sim\mathcal{N}(\eta_i,\sigma^2),\qquad i = 1,\dots,n\)

  2. A model for the linear predictor \(\eta_i = \color{red}{\boxed{\beta_0}} + \color{red}{\boxed{\beta_1x_i} }\)

  3. 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!

Let’s formalize this a bit…

The elements of a inlabru friendly statistical model are:

  1. The observational model \(y_i|\eta_i,\sigma^2\sim\mathcal{N}(\eta_i,\sigma^2),\qquad i = 1,\dots,n\)

  2. A model for the linear predictor \(\eta_i = \color{red}{\boxed{\beta_0}} + \color{red}{\boxed{\beta_1x_i} }\)

  3. A prior for the model components \(\mathbf{u} = \{\beta_0, \beta_1\}\sim\mathcal{N}(0,\mathbf{Q}^{-1})\)

  4. A prior for the non-gaussian parameters \(\theta\) \[ \theta = \sigma^2 \]

Latent Gaussian Models (LGM)

  1. The observation model: \[ \pi(\mathbf{y}|\eta,\theta) = \prod_{i=1}^{n}\pi(y_i|\eta_i,\theta) \]

  2. Linear predictor \(\eta_i = \beta_0 + \beta_1 x_i\)

  3. Latent Gaussian field \(\pi(\mathbf{u}|\theta)\)

  4. The hyperparameters: \(\pi(\theta)\)

Stage 1 The data generating process

Latent Gaussian Models (LGM)

  1. The observation model: \[ \pi(\mathbf{y}|\eta,\theta) = \prod_{i=1}^{n}\pi(y_i|\eta_i,\theta) \]

  2. Linear predictor \(\eta_i = \beta_0 + \beta_1 x_i\)

  3. Latent Gaussian field \(\pi(\mathbf{u}|\theta)\)

  4. The hyperparameters: \(\pi(\theta)\)

Stage 1 The data generating process

Stage 2 The dependence structure

Latent Gaussian Models (LGM)

  1. The observation model: \[ \pi(\mathbf{y}|\eta,\theta) = \prod_{i=1}^{n}\pi(y_i|\eta_i,\theta) \]

  2. Linear predictor \(\eta_i = \beta_0 + \beta_1 x_i\)

  3. Latent Gaussian field \(\pi(\mathbf{u}|\theta)\)

  4. The hyperparameters: \(\pi(\theta)\)

Stage 1 The data generating process

Stage 2 The dependence structure

Stage 3 The hyperparameters

Latent Gaussian Models (LGM)

  1. The observation model: \[ \pi(\mathbf{y}|\eta,\theta) = \prod_{i=1}^{n}\pi(y_i|\eta_i,\theta) \]

  2. Linear predictor \(\eta_i = \beta_0 + \beta_1 x_i\)

  3. Latent Gaussian field \(\pi(\mathbf{u}|\theta)\)

  4. 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?

The posterior distribution

posterior A Prior belief C Bayes Theorem & Bayesian Computations A->C B Observation model B->C D Posterior distribution C->D

\[ \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)} \] ##

The posterior distribution

posterior C Bayes Theorem & Bayesian Computations D Posterior distribution C->D E Bayesian Computation are hard!! Here is where INLA comes in!!! E->C A Prior belief A->C B Observation model B->C

inlabru for linear regression

The 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} \]

library(inlabru)
Pallid[1:3,c("w","tl")]
      w    tl
1 2.239  95.9
2 2.948  95.0
3 3.402 108.0

The code

# define model components
cmp =  ~ -1 + beta0(1) + beta1(tl, model = "linear")

# define model predictor
eta = w ~ beta0 + beta1

# build the observation model
lik = bru_obs(formula = eta,
              family = "gaussian",
              data = Pallid)
# fit the model
fit = bru(cmp, lik)

inlabru for linear regression

The 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} \]

library(inlabru)
Pallid[1:3,c("w","tl")]
      w    tl
1 2.239  95.9
2 2.948  95.0
3 3.402 108.0

The code

# define model components
cmp =  ~ -1 + beta0(1) + beta1(tl, model = "linear")

# define model predictor
eta = w ~ beta0 + beta1

# build the observation model
lik = bru_obs(formula = eta,
              family = "gaussian",
              data = Pallid)
# fit the model
fit = bru(cmp, lik)

inlabru for linear regression

The 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} \]

library(inlabru)
Pallid[1:3,c("w","tl")]
      w    tl
1 2.239  95.9
2 2.948  95.0
3 3.402 108.0

The code

# define model components
cmp =  ~ -1 + beta0(1) + beta1(tl, model = "linear")

# define model predictor
eta = w ~ beta0 + beta1

# build the observation model
lik = bru_obs(formula = eta,
              family = "gaussian",
              data = Pallid)
# fit the model
fit = bru(cmp, lik)

inlabru for linear regression

The 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} \]

library(inlabru)
Pallid[1:3,c("w","tl")]
      w    tl
1 2.239  95.9
2 2.948  95.0
3 3.402 108.0

The code

# define model components
cmp =  ~ -1 + beta0(1) + beta1(tl, model = "linear")

# define model predictor
eta = w ~ beta0 + beta1

# build the observation model
lik = bru_obs(formula = eta,
              family = "gaussian",
              data = Pallid)
# fit the model
fit = bru(cmp, lik)

inlabru for linear regression

The 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} \]

library(inlabru)
Pallid[1:3,c("w","tl")]
      w    tl
1 2.239  95.9
2 2.948  95.0
3 3.402 108.0

The code

# define model components
cmp =  ~ -1 + beta0(1) + beta1(tl, model = "linear")

# define model predictor
eta = w ~ beta0 + beta1

# build the observation model
lik = bru_obs(formula = eta,
              family = "gaussian",
              data = Pallid)
# fit the model
fit = bru(cmp, lik)

inlabru for linear regression

Real datasets are more complicated!

Data 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:

  • Need computationally efficient methods to calculate posteriors (this is where INLA helps!).
  • Select priors in a sensible way (we’ll talk about this)

The good news!!

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 good news!!

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?

    • Gaussian response? (temperature, rainfall, fish weight …)
    • Count data? (people infected with a disease in each area)
    • Point pattern? (locations of trees in a forest)
    • Binary data? (yes/no response, binary image)
    • Survival data? (recovery time, time to death)
    • … (many more examples!!)
  • 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 good news!!

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?

    • We assume data to be conditionally independent given the model components and some hyperparameters
    • This means that all dependencies in data are explained in Stage
  • 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 good news!!

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:

  • Fixed effects for covariates
  • Unstructured random effects (individual effects, group effects)
  • Structured random effects (AR(1), regional effects, )

These are linked to the responses in the likelihood through linear predictors.

  • Stage 3: What are our prior beliefs about the parameters controlling the components in the model?

The good news!!

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:

    • Variance of observation noise
    • Dispersion parameter in the negative binomial model
    • Variance of unstructured effects

The second good news!

No matter how complicated is your model, the inlabru workflow is always the same 😃

# Define model components
comps <- component_1(...) + 
  component_2(...) + ...

# Define the model predictor
pred <- linear_function(component_1, 
                            component_2, ...)

# Build the observation model
lik <- bru_obs(formula = pred,
               family = ... ,
               data = ... ,
                ...)

# Fit the model
fit <- bru(comps, lik, ...)

The second good news!

No matter how complicated is your model, the inlabru workflow is always the same 😃

# Define model components
comps <- component_1(...) + 
  component_2(...) + ...

# Define the model predictor
pred <- linear_function(component_1, 
                            component_2, ...)

# Build the observation model
lik <- bru_obs(formula = pred,
               family = ... ,
               data = ... ,
                ...)

# Fit the model
fit <- bru(comps, lik, ...)

NOTE inlabru allows non-linear functions to be specified

Examples

The Tokyo rainfall data

One example with time series: Rainfall over 1 mm in the Tokyo area for each calendar day during two years (1983-84) are registered.

The model

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} \]

  • the likelihood has no hyperparameters

The model

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\)

The model

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 series

The 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} \]

Tokyo[1:3,]
  y n time
1 0 2    1
2 0 2    2
3 1 2    3

The code

# define model component
cmp =  ~ -1 + beta0(1) + time_effect(time, model = "rw2", cyclic = TRUE)

# define model predictor
eta = y ~ beta0 + time_effect

# build the observation model
lik = bru_obs(formula = eta,
              family = "binomial",
              Ntrials = n,
              data = Tokyo)
# fit the model
fit = bru(cmp, lik)

inlabru for time series

The 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} \]

Tokyo[1:3,]
  y n time
1 0 2    1
2 0 2    2
3 1 2    3

The code

# define model component
cmp =  ~ -1 + beta0(1) + time_effect(time, model = "rw2", cyclic = TRUE)

# define model predictor
eta = y ~ beta0 + time_effect

# build the observation model
lik = bru_obs(formula = eta,
              family = "binomial",
              Ntrials = n,
              data = Tokyo)
# fit the model
fit = bru(cmp, lik)

inlabru for time series

The 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} \]

Tokyo[1:3,]
  y n time
1 0 2    1
2 0 2    2
3 1 2    3

The code

# define model component
cmp =  ~ -1 + beta0(1) + time_effect(time, model = "rw2", cyclic = TRUE)

# define model predictor
eta = y ~ beta0 + time_effect

# build the observation model
lik = bru_obs(formula = eta,
              family = "binomial",
              Ntrials = n,
              data = Tokyo)
# fit the model
fit = bru(cmp, lik)

inlabru for time series

The 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} \]

Tokyo[1:3,]
  y n time
1 0 2    1
2 0 2    2
3 1 2    3

The code

# define model component
cmp =  ~ -1 + beta0(1) + time_effect(time, model = "rw2", cyclic = TRUE)

# define model predictor
eta = y ~ beta0 + time_effect

# build the observation model
lik = bru_obs(formula = eta,
              family = "binomial",
              Ntrials = n,
              data = Tokyo)
# fit the model
fit = bru(cmp, lik)

Example: disease mapping

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\) .

Bayesian disease mapping

  • 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 mapping

The 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} \]

g = system.file("demodata/germany.graph",
                package="INLA")
Germany[1:3,]
  region         E  Y  x region.struct
1      1  7.965008  8 56             1
2      2 22.836219 22 65             2
3      3 22.094716 19 50             3

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 mapping

The 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} \]

g = system.file("demodata/germany.graph",
                package="INLA")
Germany[1:3,]
  region         E  Y  x region.struct
1      1  7.965008  8 56             1
2      2 22.836219 22 65             2
3      3 22.094716 19 50             3

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 mapping

The 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} \]

g = system.file("demodata/germany.graph",
                package="INLA")
Germany[1:3,]
  region         E  Y  x region.struct
1      1  7.965008  8 56             1
2      2 22.836219 22 65             2
3      3 22.094716 19 50             3

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 mapping

Bayesian Geostatistics

Encounter probability of Pacific Cod (Gadus macrocephalus) from a trawl survey.

\(y(s)\) Presence of absence in location \(s\)

Bayesian Geostatistics

  • 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

Bayesian Geostatistics

  • 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) \]
    • A global intercept \(\beta_0\)
    • A smooth effect of covariate \(x(s)\) (depth)
    • A Gaussian field \(\omega(s)\) (will discuss this later..)
  • Stage 3 Hyperparameters

Bayesian Geostatistics

  • 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

    • Precision for the smooth function \(f(\cdot)\)
    • Range and sd in the Gaussian field \(\sigma_{\omega}, \tau_{\omega}\)

inlabru for geostatistics

The 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} \]

df %>% select(depth, present) %>% print(n = 3)
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 geostatistics

The 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} \]

df %>% select(depth, present) %>% print(n = 3)
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 geostatistics

The 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} \]

df %>% select(depth, present) %>% print(n = 3)
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 geostatistics

Take home message!

  • Many 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!