INLA Course Introduction for R
INLA Course Introduction for R
Goal
Notes
This is not about how INLA works internally. This is not for
discussing whether the model you fit is sensible.
Good news
Notes
Getting INLA
Notes
[Link]()
##
##
## INLA version ............: 0.0-1475838132
## INLA date ...............: Fri 7 Oct 13:02:12 CEST 2016
## INLA hgid ...............: hgid: 1a142f78d436 date: Fri Oct 07 12:55:12 2016 +0200
## INLA-program hgid .......: hgid: 1a142f78d436 date: Fri Oct 07 12:55:12 2016 +0200
## Maintainers .............: Havard Rue <hrue@[Link]>
## : Finn Lindgren <[Link]@[Link]>
## : Daniel Simpson <[Link]@[Link]>
## : Andrea Riebler <[Link]@[Link]>
## : Elias Teixeira Krainski <[Link]@[Link]>
## : Geir-Arne Fuglstad <fulgstad@[Link]>
## Web-page ................: [Link]
## Email support ...........: help@[Link]
## : r-inla-discussion-group@[Link]
## Source-code .............: [Link]/hrue/r-inla
Produces:
Input • Input files Output
• .ini files
Collect
results
The first two are not particularly user-friendly. They are used in the
background by the INLA package.
Implementing INLA
Notes
All procedures required to perform INLA need to be carefully
implemented to achieve a good speed; easier to implement a slow
version of INLA.
I The GMRFLib-library
I Basic library written in C, user friendly for programmers
I The inla-program
I Define latent Gaussian models and interface with the
GMRFLib-library
I Avoids the need for C-programming
I Models are defined using .ini-files
I Requires to write input files in a special format
I inla-program write all the results (E/Var/marginals) to files
I The INLA package for R
I R-interface to the inla-program.
I Convert “formula”-statements into “.ini”-file definitions
The first two are not particularly user-friendly. They are used in the
background by the INLA package.
Call:
"inla(formula = formula, family = \"gaussian\", data = data)"
Time used:
Pre-processing Running inla Post-processing Total
0.0581 0.0161 0.0181 0.0924
Fixed effects:
mean sd 0.025quant 0.5quant 0.975quant mode kld
(Intercept) 137.0288 1.3929 134.2798 137.0288 139.7741 137.0288 0
x 2.1259 0.0526 2.0221 2.1259 2.2295 2.1259 0
...
Data organization
Notes
# Option 1
data = list(y = y, x1 = x1, x2 = x2, t = t)
# Option 2
data = [Link](y = y, x1 = x1, x2 = x2, t = t)
result = inla(
## This is all that is needed for a basic call
# Description of the model
formula,
# Likelihood
family = "gaussian",
# List or data frame with response, covariates, etc.
data = data,
## ...
# 'control statements'
# [Link]
)
Likelihood functions
Notes
I "gaussian"
I "T"
I "poisson"
I "nbinomial"
I "binomial"
I "exponential"
I "weibull"
I "coxph"
I See list at [Link]
or
names([Link]()$likelihood)
Posterior inference
Notes
Main functions:
I summary(result)
I plot(result)
I result2 = [Link](result)
yi | ηi ∼ N (ηi , σo2 )
ηi = β0 + β1 xi
# Generate data
x = runif(10)
y = 1 + 2*x + rnorm(n = 100, sd = 0.1)
# Run inla
formula = y ~ 1 + x
result = inla(formula,
data = [Link](x = x, y = y),
family = "gaussian")
# Get summary
summary(result)
##
## Call: Notes
## c("inla(formula = formula, family = \"gaussian\", data = [Link](x = x, ", " y = y))")
##
## Time used:
## Pre-processing Running inla Post-processing Total
## 0.5021 0.1717 0.0597 0.7335
##
## Fixed effects:
## mean sd 0.025quant 0.5quant 0.975quant mode kld
## (Intercept) 0.9839 0.0260 0.9327 0.9839 1.0349 0.9839 0
## x 2.0394 0.0392 1.9622 2.0394 2.1165 2.0394 0
##
## The model has no random effects
##
## Model hyperparameters:
## mean sd 0.025quant 0.5quant
## Precision for the Gaussian observations 91.72 12.42 68.69 91.24
## 0.975quant mode
## Precision for the Gaussian observations 117.24 90.10
##
## Expected number of effective parameters(std dev): 2.007(0.0011)
## Number of equivalent replicates : 49.81
##
## Marginal log-Likelihood: 67.39
Summary for the fixed effects
Notes
result$[Link]
## mean sd 0.025quant 0.5quant 0.975quant mode
## (Intercept) 0.9838558 0.02598130 0.9327357 0.983855 1.034922 0.9838557
## x 2.0393672 0.03922258 1.9621939 2.039366 2.116458 2.0393673
## kld
## (Intercept) 2.014763e-12
## x 1.732002e-12
●
● ●●
● ●
● ●
● ●
● ●
● ●
● ●
● ●
● ●
10
● ●
● ●
● ●
y
● ●
● ●
5
● ●
● ●
● ●
● ●
● ● ● ● ● ● ●● ●●● ●●● ●● ● ● ● ● ● ●
0
x
Marginal posterior densities
Notes
15
●●
● ●
●●
●
● ●
●
●
●● ●●
●
● ●
●
●
●
●
● ●
●
●
● ●
●
●
● ●
●
●
● ●
●
●
● ●
●
●
●
● ●
●
[Link](m)$y
●
●
● ●
●
●
● ●
●
●
●
●
● ●
●
●
● ●●
●
● ●
10
●
●
● ●
●
●
●
● ●
●
●
● ●●
●
● ●
●
●
● ●
●
●
● ●
●
●● ●
●
●
● ●
●
●
● ●
●
●
● ●
●
●● ●
●
●
● ●
●
● ●
●
●
● ●
●
●● ●●
5
●
● ●
●
●
● ●
●
●● ●
●
●
● ●
●
●
● ●
●
●● ●●
●
● ●
●
●● ●
●●
●
●
● ●
●
●● ●
●●
●
● ●
●
●●
●
● ●
●
●
●
●
●●
●
● ●
●
●
●●
●
●
●●
●
●●
● ●
●●
●
●●
●
●
●
●
●●
●●
●
●●
●
●●
●
●●
●
●
●●
●
●●
●
●
●●
●
●●
●●
●
●●
●
●●
●
●
●●
●
●●
●
●●
●
●
●●
●
●●
●
●
●●
●
●●
●
●
●●
●
●●
●
●●
●
●●
●
●
●●
●
●●
●
●
●●
●
●
●●
●
●●
●
●
●●
●
●●
●
●
●●
●
●● ●●
●
●●
●
●
●●
●
●●
●
●
●●
●
●●
●
●
●●
●
●●
●
●●
●
●
●●
●
●●
●
●●
●
●
●●
●
●●
●
●
●●
●
●●
●
●
●●
●
●●
●
●●
●
●●
●
●
●●
●
●●
●
●●
●●
●
●●
●
●●
●
●
●●
●
●●
●
●●
●
●●
●
●●
●
●●
●
0
[Link](m)$x
# Extract quantiles
[Link](0.05, m)
## [1] 0.9409562
# Distribution function
[Link](0.975, m)
## [1] 0.3686119
# Density function
[Link](1, m)
## [1] 12.68609
# Generate realizations
[Link](4, m)
## [1] 1.0411516 1.0310270 1.0137149 0.9876703
Marginal posterior densities
Notes
For example:
result$[Link]
## mean sd 0.025quant 0.5quant 0.975quant mode kld
## (Intercept) 0.9838558 0.02598130 0.9327357 0.983855 1.034922 0.9838557 2.014763e-12
## x 2.0393672 0.03922258 1.9621939 2.039366 2.116458 2.0393673 1.732002e-12
Organisation of the returned inla-object
Notes
# formula used
result$.args$formula
## y ~ 1 + x
## NULL
# data used
head(result$.args$data)
## x y
## 1 0.59185320 2.2577575
## 2 0.63805949 2.3618730
## 3 0.87653695 2.8205119
## 4 0.91609803 2.9861276
## 5 0.01756631 0.9930623
## 6 0.32969977 1.3996612
# log-file including information of INLA approximations
result$logfile
Get estimates for variance not precision
Notes
1
Goal: Posterior mean and standard deviation of σ02 = τ0 .
names(result$[Link])
## [1] "Precision for the Gaussian observations"
# get the marginal for the precision
tau0 = result$[Link][[1]]
# Calculate sd
mysd = sqrt(E[2] - E[1]^2)
print(c(mean=E[1], sd=mysd))
## mean sd
## 0.011092890 0.001593518
I name – the index of the effect (each f-function needs its own!)
I model – the type of latent model. E.g. "iid", "rw2", "ar1",
"besag", and so on
I hyper – specify the prior on the hyperparameters
I constr – sum-to-zero constraint?
I cyclic – are you cyclic?
I ...
ηi = β0 + β1 xi + ai
# Run inla
formula = y ~ 1 + x + f(t, model="ar1")
result = inla(formula,
data = [Link](x = x, y = y, t = t),
family = "gaussian")
summary(result)
## Notes
## Call:
## c("inla(formula = formula, family = \"gaussian\", data = [Link](x = x, ", " y = y, t = t))")
##
## Time used:
## Pre-processing Running inla Post-processing Total
## 0.6199 0.2186 0.0952 0.9336
##
## Fixed effects:
## mean sd 0.025quant 0.5quant 0.975quant mode kld
## (Intercept) 1.0023 0.0621 0.8783 1.0026 1.1246 1.0031 0
## x 2.0071 0.0837 1.8427 2.0070 2.1719 2.0069 0
##
## Random effects:
## Name Model
## t AR1 model
##
## Model hyperparameters:
## mean sd 0.025quant 0.5quant
## Precision for the Gaussian observations 24.4008 5.4772 15.4221 23.8007
## Precision for t 67.6345 46.1766 16.1276 55.9859
## Rho for t 0.7214 0.1674 0.2877 0.7618
## 0.975quant mode
## Precision for the Gaussian observations 36.8175 22.6452
## Precision for t 187.6038 37.9032
## Rho for t 0.9286 0.8343
##
## Expected number of effective parameters(std dev): 22.25(12.21)
## Number of equivalent replicates : 4.494
##
## Marginal log-Likelihood: -21.02
The interpretation of NA
Notes
Prediction
Notes
# Re-compute
[Link] = inla(formula,
data = [Link](x = x, t = t, y = y),
family="gaussian",
[Link] = list([Link] = "grid"),
[Link] = list(config = TRUE),
[Link] = list(compute = TRUE))
Prediction
Notes
m = [Link]$[Link][[101]]
round([Link]$[Link][101,], 3)
## mean sd 0.025quant 0.5quant 0.975quant mode kld
## predictor.101 1.573 0.122 1.321 1.576 1.819 1.587 0
par(mar=c(5,5,0.5,0.5))
plot([Link](m), type="l")
4
3
[Link](m)$y
2
1
0
[Link](m)$x
Prediction
Notes
x101 = rep(NA, n)
[Link]= rep(NA, n)
for(i in 1:n){
x101[i] = x[[i]]$latent["Predictor.101",]
[Link][i] = x[[i]]$hyperpar[1]
}
Illustration of samples
Notes
library(MASS)
par(mfrow=c(1,2), mar=c(5,5,1,1))
truehist(x101, xlab="Linear predictor 101")
lines([Link]$[Link][[101]], col=2, lwd=2)
truehist([Link], xlab=expression(sqrt(1/sigma^2)))
lines([Link]$[Link][[1]], col=2, lwd=2)
0.08
3
0.06
2
0.04
1
0.02
0.00
0
predDist
Example: Smoothing binary time series
Notes
The data set Tokyo is available in the INLA package and consists of
the number of days in Tokyo with rainfall above 1 mm in
1983–1984.
2 | | | | | | || || || |||| ||| | | || | | || || | |
1 |||| ||||| || || | ||| || ||||||| ||||| |||||||||| ||| || ||| ||| | || |||| ||| || ||||||| |||| | || |||||||| || |||| | ||||||||| | |||||||||| | |||| |||| | | ||| | ||
0 ||||||||||||||||||||||||||||||||||||||||||||||||||||| ||| |||| || ||||||||||||||||| ||||| |||||||||||| | | |||| ||| ||||||||||||||||||| | |||||||||||| ||| | | ||||||||| | | |||||||||||||||||||||||||||||||||||||||||| ||||||||||
Observations
Notes
Each observation consists of
t: Day of year; t ∈ {1, 2, . . . , 366}
nt : Number of observations for day t in 1983–1984; nt ∈ {1, 2}
yt : Number of days with rain out of nt days for day t;
yt ∈ {0, 1, 2}
data(Tokyo)
head(Tokyo,4)
## y n time
## 1 0 2 1
## 2 0 2 2
## 3 1 2 3
## 4 1 2 4
Tokyo[60,]
## y n time
## 60 0 1 60
Hierarchical model
Notes
yt ∼ Binomial(nt , pt )
exp (ηt )
pt = with linear predictor ηt = CRW2t
1 + exp(ηt )
Computations
Notes
# Read data
data(Tokyo)
# Run model
result = inla(formula,
family = "binomial",
Ntrials = n,
data = Tokyo)
Marginal posterior of CRW2
Notes
par(mar=c(5,5,1,0.5))
toplot = c("mean", "0.025quant", "0.5quant", "0.975quant")
matplot(result$[Link]$t[, toplot],
lty =c(1,3,2,3), type="l", col=1, ylab="")
0.5
0.0
−2.5 −2.0 −1.5 −1.0 −0.5
par(mar=c(5,5,1.5,0.5))
plot([Link](result$[Link][[1]]), xlim=c(0, 10^6),
xlab=expression(tau), ylab="Density", type="l")
4e−05
Density
2e−05
0e+00
τ
Transform to probability
Notes
1.0
0.8
0.6
0.4
0.2
0.0
names([Link]()$latent)
## [1] "linear" "iid" "mec"
## [4] "meb" "rgeneric" "rw1"
## [7] "rw2" "crw2" "seasonal"
## [10] "besag" "besag2" "bym"
## [13] "bym2" "besagproper" "besagproper2"
## [16] "fgn" "ar1" "ar"
## [19] "ou" "generic" "generic0"
## [22] "generic1" "generic2" "generic3"
## [25] "spde" "spde2" "spde3"
## [28] "iid1d" "iid2d" "iid3d"
## [31] "iid4d" "iid5d" "2diid"
## [34] "z" "rw2d" "rw2diid"
## [37] "slm" "matern2d" "copy"
## [40] "clinear" "sigm" "revsigm"
## [43] "log1exp" "logdist"
Add weight to components of a random effect
Notes
θ1 = log(τ )
1+ρ
θ2 = log
1−ρ
I The prior must be set on the parameter in internal scale
I Initial values for the mode-search must be set in internal scale
I The functions [Link] and [Link] can be used to map
back and forth.
Changing the prior: Code
Notes
EPIL example
Notes
1 > result = inla ( formula , family = " poisson " , data = Epil ,
2 control . fixed = list ( prec . intercept = 0.001 ,
3 prec = 0.001) )
Bayesian hierarchical models
Notes
Stage 1
Notes
Stage 3
Notes
Examples likelihood:
I Variance of observation noise
I Dispersion parameter in the negative binomial model
I Probability of a zero (zero-inflated models)
1.0
4
Density
Density
3
0.5
2
1
0.0
0
1.4 1.5 1.6 1.7 1.8 1.9 −0.5 0.0 0.5 1.0 1.5
a0 [Link]
log([Link]) log([Link])
1.5
1.5
1.0
Density
Density
1.0
0.5
0.5
0.0
0.0
log(tau.b1) log(tau.b)
1.2
5
4
0.8
Density
Density
3
0.4
2
1
0.0
0
1.4 1.5 1.6 1.7 1.8 1.9 −0.5 0.0 0.5 1.0 1.5
a0 [Link]
log([Link]) log([Link])
1.5
1.5
1.0
Density
Density
1.0
0.5
0.5
0.0
0.0
0.5 1.0 1.5 2.0 2.5 1.5 2.0 2.5 3.0
log(tau.b1) log(tau.b)
0.8
Density
Density
3
0.4
2
1
0.0
0
1.3 1.4 1.5 1.6 1.7 1.8 1.9 −0.5 0.0 0.5 1.0 1.5 2.0
a0 [Link]
log([Link]) log([Link])
1.5
1.5
1.0
Density
Density
1.0
0.5
0.5
0.0
0.0
log(tau.b1) log(tau.b)
1.2
5
4
0.8
Density
Density
3
2
0.4
1
0.0
0
1.3 1.4 1.5 1.6 1.7 1.8 1.9 −0.5 0.0 0.5 1.0 1.5 2.0
a0 [Link]
log([Link]) log([Link])
1.5
1.5
1.0
Density
Density
1.0
0.5
0.5
0.0
0.0
0.5 1.0 1.5 2.0 2.5 1.5 2.0 2.5 3.0
log(tau.b1) log(tau.b)
0.8
Density
Density
3
2
0.4
1
0.0
0
1.3 1.4 1.5 1.6 1.7 1.8 1.9 −1.0 −0.5 0.0 0.5 1.0 1.5 2.0
a0 [Link]
log([Link]) log([Link])
1.5
1.5
1.0
Density
Density
1.0
0.5
0.5
0.0
0.0
log(tau.b1) log(tau.b)
1.2
5
4
0.8
Density
Density
3
2
0.4
1
0.0
0
1.3 1.4 1.5 1.6 1.7 1.8 1.9 −1.0 0.0 0.5 1.0 1.5 2.0
a0 [Link]
log([Link]) log([Link])
1.5
1.5
1.0
1.0
Density
Density
0.5
0.5
0.0
0.0
0.5 1.0 1.5 2.0 2.5 3.0 1.5 2.0 2.5 3.0
log(tau.b1) log(tau.b)
Density
3
2
1
0
1.3 1.4 1.5 1.6 1.7 1.8 1.9 −1.0 0.0 0.5 1.0 1.5 2.0
a0 [Link]
log([Link]) log([Link])
1.5
1.5
1.0
1.0
Density
Density
0.5
0.5
0.0
0.0
0.5 1.0 1.5 2.0 2.5 3.0 1.5 2.0 2.5 3.0
log(tau.b1) log(tau.b)
Exercise
Notes