LGCPs - Multiple Likelihoods
Fabian E. Bachl
Generated on 2024-11-18
Source:vignettes/articles/2d_lgcp_multilikelihood.Rmd
2d_lgcp_multilikelihood.Rmd
Introduction
For this vignette we are going to be working with the inlabru’s
´gorillas_sf´ dataset which was originally obtained from the
R
package spatstat
. The data set contains two
types of gorillas nests which are marked as either major or minor. We
will set up a multi-likelihood model for these nests which creates two
spatial LGCPs that share a common intercept but have employ different
spatial smoothers.
Get the data
For the next few practicals we are going to be working with a dataset
obtained from the R
package spatstat
, which
contains the locations of 647 gorilla nests. We load the dataset like
this:
data(gorillas_sf, package = "inlabru")
Plot the nests and visualize the group membership (major/minor) by color:
Fiting the model
First, we define all components that enter the joint model. That is, the intercept that is common to both LGCPs and the two different spatial smoothers, one for each nest group.
matern <- inla.spde2.pcmatern(gorillas_sf$mesh,
prior.range = c(0.1, 0.01),
prior.sigma = c(1, 0.01)
)
cmp <- ~
Common(geometry, model = matern) +
Difference(geometry, model = matern) +
Intercept(1)
Given these components we define the linear predictor for each of the
likelihoods. (Using “.” indicates a pure additive model, and one can use
include/exclude options for like()
to indicate which
components are actively involved in each model.)
fml.major <- geometry ~ Intercept + Common + Difference / 2
fml.minor <- geometry ~ Intercept + Common - Difference / 2
Setting up the Cox process likelihoods is easy in this example. Both nest types were observed within the same window:
lik_minor <- like("cp",
formula = fml.major,
data = gorillas_sf$nests[gorillas_sf$nests$group == "major", ],
samplers = gorillas_sf$boundary,
domain = list(geometry = gorillas_sf$mesh)
)
lik_major <- like("cp",
formula = fml.minor,
data = gorillas_sf$nests[gorillas_sf$nests$group == "minor", ],
samplers = gorillas_sf$boundary,
domain = list(geometry = gorillas_sf$mesh)
)
… which we provide to the ´bru´ function.
jfit <- bru(cmp, lik_major, lik_minor,
options = list(
control.inla = list(
int.strategy = "eb"
),
bru_max_iter = 1
)
)
library(patchwork)
pl.major <- ggplot() +
gg(gorillas_sf$mesh,
mask = gorillas_sf$boundary,
col = exp(jfit$summary.random$Common$mean)
)
pl.minor <- ggplot() +
gg(gorillas_sf$mesh,
mask = gorillas_sf$boundary,
col = exp(jfit$summary.random$Difference$mean)
)
(pl.major + scale_fill_continuous(trans = "log")) +
(pl.minor + scale_fill_continuous(trans = "log")) &
theme(legend.position = "right")
Rerunning
Rerunning with the previous estimate as starting point sometimes improves the accuracy of the posterior distribution estimation.
jfit0 <- jfit
jfit <- bru_rerun(jfit)
pl.major <- ggplot() +
gg(gorillas_sf$mesh,
mask = gorillas_sf$boundary,
col = exp(jfit$summary.random$Common$mean)
)
pl.minor <- ggplot() +
gg(gorillas_sf$mesh,
mask = gorillas_sf$boundary,
col = exp(jfit$summary.random$Difference$mean)
)
(pl.major + scale_fill_continuous(trans = "log")) +
(pl.minor + scale_fill_continuous(trans = "log")) &
theme(legend.position = "right")
summary(jfit0)
#> inlabru version: 2.11.1.9025
#> INLA version: 24.11.17
#> Components:
#> Common: main = spde(geometry), group = exchangeable(1L), replicate = iid(1L), NULL
#> Difference: main = spde(geometry), group = exchangeable(1L), replicate = iid(1L), NULL
#> Intercept: main = linear(1), group = exchangeable(1L), replicate = iid(1L), NULL
#> Likelihoods:
#> Family: 'cp'
#> Tag: ''
#> Data class: 'sf', 'data.frame'
#> Response class: 'numeric'
#> Predictor: geometry ~ Intercept + Common - Difference/2
#> Used components: effects[Common, Difference, Intercept], latent[]
#> Family: 'cp'
#> Tag: ''
#> Data class: 'sf', 'data.frame'
#> Response class: 'numeric'
#> Predictor: geometry ~ Intercept + Common + Difference/2
#> Used components: effects[Common, Difference, Intercept], latent[]
#> Time used:
#> Pre = 0.652, Running = 23.3, Post = 0.164, Total = 24.1
#> Fixed effects:
#> mean sd 0.025quant 0.5quant 0.975quant mode kld
#> Intercept -0.346 1.375 -3.041 -0.346 2.349 -0.346 0
#>
#> Random effects:
#> Name Model
#> Common SPDE2 model
#> Difference SPDE2 model
#>
#> Model hyperparameters:
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> Range for Common 2.935 0.572 1.994 2.872 4.239 2.739
#> Stdev for Common 2.047 0.333 1.483 2.016 2.789 1.949
#> Range for Difference 2.067 2.943 0.128 1.176 9.477 0.337
#> Stdev for Difference 0.158 0.096 0.034 0.137 0.397 0.093
#>
#> Deviance Information Criterion (DIC) ...............: 589.78
#> Deviance Information Criterion (DIC, saturated) ....: 588.14
#> Effective number of parameters .....................: -773.97
#>
#> Watanabe-Akaike information criterion (WAIC) ...: 1628.66
#> Effective number of parameters .................: 104.96
#>
#> Marginal log-Likelihood: -1203.99
#> is computed
#> Posterior summaries for the linear predictor and the fitted values are computed
#> (Posterior marginals needs also 'control.compute=list(return.marginals.predictor=TRUE)')
summary(jfit)
#> inlabru version: 2.11.1.9025
#> INLA version: 24.11.17
#> Components:
#> Common: main = spde(geometry), group = exchangeable(1L), replicate = iid(1L), NULL
#> Difference: main = spde(geometry), group = exchangeable(1L), replicate = iid(1L), NULL
#> Intercept: main = linear(1), group = exchangeable(1L), replicate = iid(1L), NULL
#> Likelihoods:
#> Family: 'cp'
#> Tag: ''
#> Data class: 'sf', 'data.frame'
#> Response class: 'numeric'
#> Predictor: geometry ~ Intercept + Common - Difference/2
#> Used components: effects[Common, Difference, Intercept], latent[]
#> Family: 'cp'
#> Tag: ''
#> Data class: 'sf', 'data.frame'
#> Response class: 'numeric'
#> Predictor: geometry ~ Intercept + Common + Difference/2
#> Used components: effects[Common, Difference, Intercept], latent[]
#> Time used:
#> Pre = 0.509, Running = 10.2, Post = 0.13, Total = 10.9
#> Fixed effects:
#> mean sd 0.025quant 0.5quant 0.975quant mode kld
#> Intercept -0.342 1.365 -3.018 -0.342 2.334 -0.342 0
#>
#> Random effects:
#> Name Model
#> Common SPDE2 model
#> Difference SPDE2 model
#>
#> Model hyperparameters:
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> Range for Common 2.936 0.573 2.001 2.872 4.246 2.732
#> Stdev for Common 2.046 0.332 1.486 2.015 2.791 1.944
#> Range for Difference 2.034 2.772 0.142 1.195 9.077 0.377
#> Stdev for Difference 0.158 0.093 0.036 0.139 0.386 0.097
#>
#> Deviance Information Criterion (DIC) ...............: 590.34
#> Deviance Information Criterion (DIC, saturated) ....: 588.70
#> Effective number of parameters .....................: -773.55
#>
#> Watanabe-Akaike information criterion (WAIC) ...: 1629.18
#> Effective number of parameters .................: 105.26
#>
#> Marginal log-Likelihood: -1204.06
#> is computed
#> Posterior summaries for the linear predictor and the fitted values are computed
#> (Posterior marginals needs also 'control.compute=list(return.marginals.predictor=TRUE)')
Single-likelihood version
In this particular model, we can also rewrite the problem as a single
point process over a product domain over space and group
.
In versions <= 2.7.0
, the integration domain had to be
numeric, so we convert the group variable to a 0/1 variable,
group_major <- group == "major"
, which is also useful in
the predictor expression:
fml.joint <-
geometry + group_major ~ Intercept + Common + (group_major - 0.5) * Difference
gorillas_sf$nests$group_major <- gorillas_sf$nests$group == "major"
lik_joint <- like("cp",
formula = fml.joint,
data = gorillas_sf$nests,
samplers = gorillas_sf$boundary,
domain = list(
geometry = gorillas_sf$mesh,
group_major = c(0, 1)
)
)
# Approximate with "eb" for faster vignette
jfit_joint <- bru(cmp, lik_joint,
options = list(
control.inla = list(
int.strategy = "eb"
),
bru_max_iter = 1
)
)
Plotting the ratios of exp(Common)
and
exp(Difference)
between the new fit and the old confirms
that the results are the same up to small numerical differences.
library(patchwork)
pl.major <- ggplot() +
gg(gorillas_sf$mesh,
mask = gorillas_sf$boundary,
col = exp(jfit_joint$summary.random$Common$mean -
jfit$summary.random$Common$mean)
)
pl.minor <- ggplot() +
gg(gorillas_sf$mesh,
mask = gorillas_sf$boundary,
col = exp(jfit_joint$summary.random$Difference$mean -
jfit$summary.random$Difference$mean)
)
(pl.major + scale_fill_continuous(trans = "log")) +
(pl.minor + scale_fill_continuous(trans = "log")) &
theme(legend.position = "right")