--- title: "Calculating probabilistic excursion sets and related quantities using excursions" author: "David Bolin and Finn Lindgren" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Calculating probabilistic excursion sets and related quantities using excursions} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} bibliography: ref.bib --- \DeclareMathOperator*{\argmax}{arg\,max} \newcommand{\mapset}{G} \newcommand{\mv}[1]{{\boldsymbol{\mathrm{#1}}}} \newcommand{\exset}[2]{E_{{#1}}^{{#2}}} \newcommand{\md}{\,\mathrm{d}} ```{r setup, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", dev = "png", dev.args = list(type = "cairo-png") ) library(excursions) library(fields) library(RColorBrewer) library(sp) library(fmesher) library(excursions) seed <- 1 exc.seed <- 123 set.seed(seed) ``` ```{r inla_link, include = FALSE} inla_link <- function() { sprintf("[%s](%s)", "`R-INLA`", "https://www.r-inla.org") } ``` The functions in `excursions` can be divided into four main categories depending on what they compute: (1) Excursion sets and credible regions for contour curves, (2) Quality measures for contour maps, (3) Simultaneous confidence bands, and (4) Utility such as Gaussian integrals and continuous domain mappings. The main functions come in at least three different versions taking different input: (1) The parameters of a Gaussian process, (2) results from an analysis using the `INLA` software package, and (3) Monte Carlo simulations of the process. These different categories are described in further detail below. As an example that will be used to illustrate the methods in later sections, we generate data $Y_i \sim N(X(\mv{s}_i),\sigma^2)$ at some locations $\mv{s}_1, \ldots, \mv{s}_{100}$ where $X(\mv{s})$ is a Gaussian random field specified using a stationary SPDE model [@lindgren10]. ```{r} x <- seq(from = 0, to = 10, length.out = 20) mesh <- fm_rcdt_2d_inla(lattice = fm_lattice_2d(x = x, y = x), extend = FALSE, refine = FALSE) Q <- fm_matern_precision(mesh, alpha = 2, rho = 3, sigma = 1) x <- fm_sample(n = 1, Q = Q) obs.loc <- 10 * cbind(runif(100), runif(100)) ``` Based on the observations, we calculate the posterior distribution of the latent field, which is Gaussian with mean `mu.post` and precision matrix `Q.post`, these are computed as follows. We refer to [@lindgren2015software] for details about the `INLA` related details in the code. ```{r} A <- fm_basis(mesh, loc = obs.loc) sigma2.e = 0.01 Y <- as.vector(A %*% x + rnorm(100) * sqrt(sigma2.e)) Q.post <- (Q + (t(A) %*% A) / sigma2.e) mu.post <- as.vector(solve(Q.post, (t(A) %*% Y) / sigma2.e)) ``` The following figures show the posterior mean and the posterior standard deviations. ```{r, fig.width=7, fig.height=4, fig.align = "center"} proj <- fm_evaluator(mesh, dims = c(100, 100)) cmap <- colorRampPalette(brewer.pal(9, "YlGnBu"))(100) sd.post <- excursions.variances(Q = Q.post, max.threads = 2)^0.5 cmap.sd <- colorRampPalette(brewer.pal(9, "Reds"))(100) par(mfrow=c(1,2)) image.plot(proj$x, proj$y, fm_evaluate(proj, field = mu.post), col = cmap, axes = FALSE, xlab = "", ylab = "", asp = 1) points(obs.loc[, 1], obs.loc[, 2], pch = 20) image.plot(proj$x, proj$y, fm_evaluate(proj, field = sd.post), col = cmap.sd, axes = FALSE, xlab = "", ylab = "", asp = 1) points(obs.loc[, 1], obs.loc[, 2], pch = 20) ``` ## Excursion sets and contour credible regions The main function for computing excursion sets and contour credible regions is `excursions`. A typical call to the function looks like ```{r} res.exc <- excursions(mu = mu.post, Q = Q.post, alpha = 0.1, type = '>', u = 0, F.limit = 1) ``` Here, `mu` and `Q` are the mean vector and precision matrix for the joint distribution of the Gaussian vector `x`. The arguments `alpha`, `u`, and `type` are used to specify what type of excursion set that should be computed: `alpha` is the error probability, `u` is the excursion or contour level, and `type` determines what type of region that is considered: '>' for positive excursion regions, '<' for negative excursion regions, '!=' for contour avoiding regions, and '=' for contour credibility regions. Thus, the call above computes the excursion set $\exset{0,0.1}{+}$ as introduced in [Definitions and computational methodology](theory.html). The argument `F.limit` is used to specify when to stop the computation of the excursion function. In this case with `F.limit=1`, all values of $\mv{F}_u^+$ are computed, but the computation time can be reduced by decreasing the value of `F.limit`. The function has the EB method as default strategy for handling the possible latent Gaussian structure. In the simulated example, the likelihood is Gaussian and the parameters are assumed to be known, so the EB method is exact. The QC method can be used instead by specifying `method='QC'`. In this case, the argument `rho` should be used to also provide a vector with point-wise marginal probabilities: $P(x_i>u)$ for positive excursions and contour regions, and $P(x_i