In this vignette we will present the `R-INLA`

implementation of the rational SPDE approach. For theoretical details we refer the reader to the Rational approximation with the `rSPDE`

package vignette and to (Xiong, Simas, and Bolin 2022)(https://arxiv.org/abs/2209.04670).

We begin by providing a step-by-step illustration on how to use our implementation. To this end we will consider a real world data set that consists of precipitation measurements from the Paraná region in Brazil.

After the initial model fitting, we will show how to change some parameters of the model. In the end, we will also provide an example in which we have replicates.

It is important to mention that one can improve the performance by using the PARDISO solver. Please, go to https://www.pardiso-project.org/r-inla/#license to apply for a license. Also, use `inla.pardiso()`

for instructions on how to enable the PARDISO sparse library.

To illustrate our implementation of `rSPDE`

in `R-INLA`

we will consider a dataset available in `R-INLA`

. This data has also been used to illustrate the SPDE approach, see for instance the book Advanced Spatial Modeling with Stochastic Partial Differential Equations Using R and INLA and also the vignette Spatial Statistics using R-INLA and Gaussian Markov random fields. See also (Lindgren, Rue, and Lindström 2011)(https://rss.onlinelibrary.wiley.com/doi/full/10.1111/j.1467-9868.2011.00777.x) for theoretical details on the standard SPDE approach.

The data consist of precipitation measurements from the Paraná region in Brazil and were provided by the Brazilian National Water Agency. The data were collected at 616 gauge stations in Paraná state, south of Brazil, for each day in 2011.

We will follow the vignette Spatial Statistics using R-INLA and Gaussian Markov random fields. As precipitation data are always positive, we will assume it is Gamma distributed. `R-INLA`

uses the following parameterization of the Gamma distribution, \[\Gamma(\mu, \phi): \pi (y) = \frac{1}{\Gamma(\phi)} \left(\frac{\phi}{\mu}\right)^{\phi} y^{\phi - 1} \exp\left(-\frac{\phi y}{\mu}\right) .\] In this parameterization, the distribution has expected value \(E(x) = \mu\) and variance \(V(x) = \mu^2/(\phi)\), where\(1/\phi\) is a dispersion parameter.

In this example \(\mu\) will be modeled using a stochastic model that includes both covariates and spatial structure, resulting in the latent Gaussian model for the precipitation measurements \[\begin{align} y_i\mid \mu(s_i), \theta &\sim \Gamma(\mu(s_i),c\phi)\\ \log (\mu(s)) &= \eta(s) = \sum_k f_k(c_k(s))+u(s)\\ \theta &\sim \pi(\theta) \end{align},\]

where \(y_i\) denotes the measurement taken at location \(s_i\), \(c_k(s)\) are covariates, \(u(s)\) is a mean-zero Gaussian Matérn field, and \(\theta\) is a vector containing all parameters of the model, including smoothness of the field. That is, by using the `rSPDE`

model we will also be able to estimate the smoothness of the latent field.

We will be using `R-INLA`

. To install `R-INLA`

go to R-INLA Project.

We begin by loading some libraries we need to get the data and build the plots.

```
library(gridExtra)
library(ggplot2)
library(lattice)
library(INLA)
library(splancs)
library(fields)
```

Let us load the data and the border of the region

```
data(PRprec)
data(PRborder)
```

The data frame contains daily measurements at 616 stations for the year 2011, as well as coordinates and altitude information for the measurement stations. We will not analyze the full spatio-temporal data set, but instead look at the total precipitation in January, which we calculate as

`<- rowMeans(PRprec[, 3 + 1:31]) Y `

In the next snippet of code, we extract the coordinates and altitudes and remove the locations with missing values.

```
<- !is.na(Y)
ind <- Y[ind]
Y <- as.matrix(PRprec[ind, 1:2])
coords <- PRprec$Altitude[ind] alt
```

Let us build plot the precipitation observations using `ggplot`

:

```
ggplot() +
geom_point(aes(
x = coords[, 1], y = coords[, 2],
colour = Y
size = 2, alpha = 1) +
), scale_colour_gradientn(colours = tim.colors(100)) +
geom_path(aes(x = PRborder[, 1], y = PRborder[, 2])) +
geom_path(aes(x = PRborder[1034:1078, 1], y = PRborder[
1034:1078,
2
colour = "red") ]),
```

The red line in the figure shows the coast line, and we expect the distance to the coast to be a good covariate for precipitation. This covariate is not available, so let us calculate it for each observation location:

```
<- apply(spDists(coords, PRborder[1034:1078, ],
seaDist longlat = TRUE
1, min) ),
```

Now, let us plot the precipitation as a function of the possible covariates:

```
par(mfrow = c(2, 2))
plot(coords[, 1], Y, cex = 0.5, xlab = "Longitude")
plot(coords[, 2], Y, cex = 0.5, xlab = "Latitude")
plot(seaDist, Y, cex = 0.5, xlab = "Distance to sea")
plot(alt, Y, cex = 0.5, xlab = "Altitude")
```

`par(mfrow = c(1, 1))`

To use the `R-INLA`

implementation of the `rSPDE`

model we need to load the functions:

`library(rSPDE)`

The `rSPDE`

-`INLA`

implementation is very reminiscent of `R-INLA`

, so its usage should be straightforward for `R-INLA`

users. For instance, to create a `rSPDE`

model, one would use `rspde.matern()`

in place of `inla.spde2.matern()`

. To create an index, one should use `rspde.make.index()`

in place of `inla.spde.make.index()`

. To create the `A`

matrix, one should use `rspde.make.A()`

in place of `inla.spde.make.A()`

, and so on.

The main differences when comparing the arguments between the `rSPDE`

-`INLA`

implementation and the standard SPDE implementation in `R-INLA`

, are the `nu`

and `rspde_order`

arguments, which are present in `rSPDE`

-`INLA`

implementation. We will see below how use these arguments.

We can use `R-INLA`

for creating the mesh. Let us create a mesh which is based on a non-convex hull to avoid adding many small triangles outside the domain of interest:

```
<- inla.nonconvex.hull(coords, -0.03, -0.05, resolution = c(100, 100))
prdomain <- inla.mesh.2d(boundary = prdomain, max.edge = c(0.45, 1), cutoff = 0.2)
prmesh plot(prmesh, asp = 1, main = "")
lines(PRborder, col = 3)
points(coords[, 1], coords[, 2], pch = 19, cex = 0.5, col = "red")
```

We now create the \(A\) matrix, that connects the mesh to the observation locations and then create the `rSPDE`

model.

For this task, as we mentioned earlier, we need to use an `rSPDE`

specific function, whose name is very reminiscent to `R-INLA`

’s standard SPDE approach, namely `rspde.make.A()`

(in place of `R-INLA`

’s `inla.spde.make.A()`

). The reason for the need of this specific function is that the size of the \(A\) matrix depends on the order of the rational approximation. The details can be found in the introduction of the Rational approximation with the `rSPDE`

package vignette.

The default order is 2 for our covariance-based rational approximation. As mentioned in the introduction of the Rational approximation with the `rSPDE`

package vignette, an approximation of order 2 in the covariance-based rational approximation has approximately the same computational cost as the operator-based rational approximation of order 1.

Recall that the latent process \(u\) is a solution of \[(\kappa^2 I-\Delta)^{\alpha/2}(\tau u) = \mathcal{W},\] where \(\alpha = \nu + d/2\). We want to estimate all three parameters \(\tau,\kappa\) and \(\nu\), which is the default option of

the `rSPDE`

-`INLA`

implementation. However, there is also an option to fix the smoothness parameter \(\nu\) to some predefined value and only estimate \(\tau\) and \(\kappa\). This will be discussed later.

In this first example we will assume we want a rational approximation of order 2. To this end we can use the `rspde.make.A()`

function. Since we will assume order 2 and that we want to estimate smoothness, which are the default options in this function, the required parameters are simply the mesh and the locations:

`<- rspde.make.A(mesh = prmesh, loc = coords) Abar `

To set up an `rSPDE`

model, all we need is the mesh. By default it will assume that we want to estimate the smoothness parameter \(\nu\) and to do a covariance-based rational approximation of order 2.

Later in this vignette we will also see other options for setting up `rSPDE`

models such as keeping the smoothness parameter fixed and/or increasing the order of the covariance-based rational approximation.

Therefore, to set up a model all we have to do is use the `rspde.matern()`

function:

`<- rspde.matern(mesh = prmesh) rspde_model `

Note that this function is very reminiscent of `R-INLA`

’s `inla.spde2.matern()`

function. This is a pattern we have tried to keep consistent in the package: All the `rSPDE`

versions of some `R-INLA`

function will either replace `inla`

or `inla.spde`

or `inla.spde2`

by `rspde`

.

`inla.stack`

Since the covariates are already evaluated at the observation locations, we only want to apply the \(A\) matrix to the spatial effect and not the fixed effects. We can use the `inla.stack()`

function.

The difference, however, is that we need to use the function `rspde.make.index()`

(in place of the standard `inla.spde.make.index()`

) to create the index.

If one is using the default options, that is, to estimate the smoothness parameter \(\nu\) and to do a rational approximation of order 2, the usage of `rspde.make.index()`

is identical to the usage of `inla.spde.make.index()`

:

`<- rspde.make.index(name = "field", mesh = prmesh) mesh.index `

We can then create the stack in a standard manner:

```
<- inla.stack(
stk.dat data = list(y = Y), A = list(Abar, 1), tag = "est",
effects = list(
c(
mesh.index,list(Intercept = 1)
),list(
seaDist = inla.group(seaDist)
)
) )
```

Here the observation matrix \(A\) is applied to the spatial effect and the intercept while an identity observation matrix, denoted by \(1\), is applied to the covariates. This means the covariates are unaffected by the observation matrix.

The observation matrices in \(A=list(Abar,1)\) are used to link the corresponding elements in the effects-list to the observations. Thus in our model the latent spatial field `mesh.index`

and the intercept are linked to the log-expectation of the observations, i.e. \(\eta(s)\), through the \(A\)-matrix. The covariates, on the other hand, are linked directly to \(\eta(s)\). The `stk.dat`

object defined above implies the following principal linkage between model components and observations \[\eta(s) \sim A x(s) + A \text{ Intercept} + \text{seaDist}.\] \(\eta(s)\) will then be used in the observation-likelihood, \[y_i\mid \eta(s_i),\theta \sim \Gamma(\exp(\eta (s_i)), c\phi).\]

We will build a model using the distance to the sea \(x_i\) as a covariate through an improper CAR(1) model with \(\beta_{ij}=1(i\sim j)\), which `R-INLA`

calls a random walk of order 1.

```
<- y ~ -1 + Intercept + f(seaDist, model = "rw1") +
f.s f(field, model = rspde_model)
```

Here `-1`

is added to remove R’s implicit intercept, which is replaced by the explicit `+Intercept`

from when we created the stack.

To fit the model we proceed as in the standard SPDE approach and we simply call `inla()`

. It is noteworthy that we can have a performance enhancement by using the argument `inla.mode = "experimental"`

. As argued in `R-INLA`

’s man page, not all features are available in `inla.mode="experimental"`

, and we will need to use the `"classic"`

mode (the default mode) instead of `"experimental"`

whenever we are doing predictions.

```
<- inla(f.s,
rspde_fit family = "Gamma", data = inla.stack.data(stk.dat),
verbose = FALSE,
control.inla = list(int.strategy = "eb"),
control.predictor = list(A = inla.stack.A(stk.dat), compute = TRUE),
inla.mode = "experimental"
)
```

We can look at some summaries of the posterior distributions for the parameters, for example the fixed effects (i.e. the intercept) and the hyper-parameters (i.e. dispersion in the gamma likelihood, the precision of the RW1, and the parameters of the spatial field):

`summary(rspde_fit)`

```
##
## Call:
## c("inla.core(formula = formula, family = family, contrasts = contrasts,
## ", " data = data, quantiles = quantiles, E = E, offset = offset, ", "
## scale = scale, weights = weights, Ntrials = Ntrials, strata = strata,
## ", " lp.scale = lp.scale, link.covariates = link.covariates, verbose =
## verbose, ", " lincomb = lincomb, selection = selection, control.compute
## = control.compute, ", " control.predictor = control.predictor,
## control.family = control.family, ", " control.inla = control.inla,
## control.fixed = control.fixed, ", " control.mode = control.mode,
## control.expert = control.expert, ", " control.hazard = control.hazard,
## control.lincomb = control.lincomb, ", " control.update =
## control.update, control.lp.scale = control.lp.scale, ", "
## control.pardiso = control.pardiso, only.hyperparam = only.hyperparam,
## ", " inla.call = inla.call, inla.arg = inla.arg, num.threads =
## num.threads, ", " blas.num.threads = blas.num.threads, keep = keep,
## working.directory = working.directory, ", " silent = silent, inla.mode
## = inla.mode, safe = FALSE, debug = debug, ", " .parent.frame =
## .parent.frame)")
## Time used:
## Pre = 4.57, Running = 42.3, Post = 0.148, Total = 47
## Fixed effects:
## mean sd 0.025quant 0.5quant 0.975quant mode kld
## Intercept 0.648 0.018 0.612 0.648 0.683 0.648 0
##
## Random effects:
## Name Model
## seaDist RW1 model
## field RGeneric2
##
## Model hyperparameters:
## mean sd 0.025quant
## Precision parameter for the Gamma observations 13.269 0.723 11.883
## Precision for seaDist 10454.553 8206.525 2501.405
## Theta1 for field -1.319 0.352 -2.161
## Theta2 for field 1.164 0.109 0.915
## Theta3 for field -0.821 0.194 -1.282
## 0.5quant 0.975quant mode
## Precision parameter for the Gamma observations 13.254 14.740 13.23
## Precision for seaDist 8149.181 32250.879 5317.94
## Theta1 for field -1.316 -0.476 -1.32
## Theta2 for field 1.164 1.413 1.17
## Theta3 for field -0.822 -0.361 -0.82
##
## Marginal log-Likelihood: -1263.34
## 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)')
```

Let \(\theta_1 = \textrm{Theta1}\), \(\theta_2=\textrm{Theta2}\) and \(\theta_3=\textrm{Theta3}\). In terms of the SPDE \[(\kappa^2 I - \Delta)^{\alpha/2}(\tau u) = \mathcal{W},\] where \(\alpha = \nu + d/2\), we have that \[\tau = \exp(\theta_1),\quad \kappa = \exp(\theta_2), \] and by default \[\nu = 4\Big(\frac{\exp(\theta_3)}{1+\exp(\theta_3)}\Big).\] The number 4 comes from the upper bound for \(\nu\), which will be discussed later in this vignette.

In general, we have \[\nu = \nu_{UB}\Big(\frac{\exp(\theta_3)}{1+\exp(\theta_3)}\Big),\] where \(\nu_{UB}\) is the value of the upper bound for the smoothness parameter \(\nu\).

Another choice for prior for \(\nu\) is a truncated lognormal distribution and will also be discussed later in this vignette.

`rSPDE`

-`INLA`

resultsWe can obtain outputs with respect to parameters in the original scale by using the function `rspde.result()`

:

```
<- rspde.result(rspde_fit, "field", rspde_model)
result_fit summary(result_fit)
```

```
## mean sd 0.025quant 0.5quant 0.975quant mode
## tau 0.284389 0.102555 0.134655 0.267417 0.532882 0.23609
## kappa 3.222250 0.350369 2.587780 3.203780 3.962280 3.16699
## nu 1.228130 0.162816 0.926028 1.222310 1.563440 1.20998
```

We can also plot the posterior densities:

```
par(mfrow = c(1, 3))
plot(result_fit, caption = c("tau", "kappa", "nu"))
```

This function is reminiscent to the `inla.spde.result()`

function with the main difference that it has the `summary()`

and `plot()`

methods implemented.

Let us now obtain predictions (i.e. do kriging) of the expected precipitation on a dense grid in the region.

We begin by creating the grid in which we want to do the predictions. To this end, we can use the `rspde.mesh.projector()`

function. This function has the same arguments as the function `inla.mesh.projector()`

, with the only difference being that the `rSPDE`

version also has an argument `nu`

and an argument `rspde_order`

. Thus, we proceed in the same fashion as we would in `R-INLA`

’s standard SPDE implementation:

```
<- c(150, 100)
nxy <- rspde.mesh.projector(prmesh,
projgrid xlim = range(PRborder[, 1]),
ylim = range(PRborder[, 2]), dims = nxy
)
```

This lattice contains 150 × 100 locations. One can easily change the resolution of the kriging prediction by changing `nxy`

. Let us find the cells that are outside the region of interest so that we do not plot the estimates there.

`<- inout(projgrid$lattice$loc, cbind(PRborder[, 1], PRborder[, 2])) xy.in `

Let us plot the locations that we will do prediction:

```
<- projgrid$lattice$loc[xy.in, ]
coord.prd plot(coord.prd, type = "p", cex = 0.1)
lines(PRborder)
points(coords[, 1], coords[, 2], pch = 19, cex = 0.5, col = "red")
```

Now, there are a few ways we could calculate the kriging prediction. The simplest way is to evaluate the mean of all individual random effects in the linear predictor and then to calculate the exponential of their sum (since \(\mu(s)=\exp(\eta(s))\) ). A more accurate way is to calculate the prediction jointly with the estimation, which unfortunately is quite computationally expensive if we do prediction on a fine grid. However, in this illustration, we proceed with this option to show how one can do it.

To this end, first, link the prediction coordinates to the mesh nodes through an \(A\) matrix

`<- projgrid$proj$A[xy.in, ] A.prd `

Since we are using distance to the sea as a covariate, we also have to calculate this covariate for the prediction locations.

```
<- apply(spDists(coord.prd,
seaDist.prd 1034:1078, ],
PRborder[longlat = TRUE
1, min) ),
```

We now make a stack for the prediction locations. We have no data at the prediction locations, so we set `y= NA`

. We then join this stack with the estimation stack.

```
<- list(
ef.prd c(mesh.index, list(Intercept = 1)),
list(
long = inla.group(coord.prd[
,1
lat = inla.group(coord.prd[, 2]),
]), seaDist = inla.group(seaDist.prd)
)
)<- inla.stack(
stk.prd data = list(y = NA),
A = list(A.prd, 1), tag = "prd",
effects = ef.prd
)<- inla.stack(stk.dat, stk.prd) stk.all
```

Doing the joint estimation takes a while, and we therefore turn off the computation of certain things that we are not interested in, such as the marginals for the random effect. We will also use a simplified integration strategy (actually only using the posterior mode of the hyper-parameters) through the command `control.inla = list(int.strategy = "eb")`

, i.e. empirical Bayes. Since we are doing predictions, we should not use `R-INLA`

’s experimental mode.

```
<- inla(f.s,
rspde_fitprd family = "Gamma",
data = inla.stack.data(stk.all),
control.predictor = list(
A = inla.stack.A(stk.all),
compute = TRUE, link = 1
),control.compute = list(
return.marginals = FALSE,
return.marginals.predictor = FALSE
),control.inla = list(int.strategy = "eb")
)
```

We then extract the indices to the prediction nodes and then extract the mean and the standard deviation of the response:

```
<- inla.stack.index(stk.all, "prd")$data
id.prd <- m.prd <- matrix(NA, nxy[1], nxy[2])
sd.prd <- rspde_fitprd$summary.fitted.values$mean[id.prd]
m.prd[xy.in] <- rspde_fitprd$summary.fitted.values$sd[id.prd] sd.prd[xy.in]
```

Finally, we plot the results:

```
# Plot the predictions
grid.arrange(
levelplot(m.prd,
col.regions = tim.colors(99),
xlab = "", ylab = "", main = "mean",
scales = list(draw = FALSE)
),levelplot(sd.prd,
col.regions = topo.colors(99),
xlab = "", ylab = "", scales = list(draw = FALSE),
main = "standard deviation"
) )
```

For this example we will simulate a data with replicates. We will use the same example considered in the Rational approximation with the `rSPDE`

package vignette (the only difference is the way the data is organized). We also refer the reader to this vignette for a description of the function `matern.operators()`

, along with its methods (for instance, the `simulate()`

method).

Let us consider a simple Gaussian linear model with 30 independent replicates of a latent spatial field \(x(\mathbf{s})\), observed at the same \(m\) locations, \(\{\mathbf{s}_1 , \ldots , \mathbf{s}_m \}\), for each replicate. For each \(i = 1,\ldots,m,\) we have

\[\begin{align} y_i &= x_1(\mathbf{s}_i)+\varepsilon_i,\\ \vdots &= \vdots\\ y_{i+29m} &= x_{30}(\mathbf{s}_i) + \varepsilon_{i+29m}, \end{align}\]

where \(\varepsilon_1,\ldots,\varepsilon_{30m}\) are iid normally distributed with mean 0 and standard deviation 0.1.

We use the basis function representation of \(x(\cdot)\) to define the \(A\) matrix linking the point locations to the mesh. We also need to account for the fact that we have 30 replicates at the same locations. To this end, the \(A\) matrix we need can be generated by `inla.spde.make.A()`

function. The reason being that we are sampling \(x(\cdot)\) directly and not the latent vector described in the introduction of the Rational approximation with the `rSPDE`

package vignette.

We begin by creating the mesh:

```
<- 200
m <- matrix(runif(m * 2), m, 2)
loc_2d_mesh <- inla.mesh.2d(
mesh_2d loc = loc_2d_mesh,
cutoff = 0.05,
offset = c(0.1, 0.4),
max.edge = c(0.05, 0.5)
)plot(mesh_2d, main = "")
points(loc_2d_mesh[, 1], loc_2d_mesh[, 2])
```

We then compute the \(A\) matrix, which is needed for simulation, and connects the observation locations to the mesh:

```
<- 30
n.rep <- inla.spde.make.A(
A mesh = mesh_2d,
loc = loc_2d_mesh,
index = rep(1:m, times = n.rep),
repl = rep(1:n.rep, each = m)
)
```

Notice that for the simulated data, we should use the \(A\) matrix from `inla.spde.make.A()`

function.

We will now simulate a latent process with standard deviation \(\sigma=1\) and range \(0.1\). We will use \(\nu=0.5\) so that the model has an exponential covariance function. To this end we create a model object with the `matern.operators()`

function:

```
<- 0.5
nu <- 1
sigma <- 0.1
range <- sqrt(8 * nu) / range
kappa <- sqrt(gamma(nu) / (sigma^2 * kappa^(2 * nu) * (4 * pi) * gamma(nu + 1)))
tau <- 2
d <- matern.operators(
operator_information mesh = mesh_2d,
nu = nu,
kappa = kappa,
sigma = sigma,
m = 2
)
```

More details on this function can be found at the Rational approximation with the rSPDE package vignette.

To simulate the latent process all we need to do is to use the `simulate()`

method on the `operator_information`

object. We then obtain the simulated data \(y\) by connecting with the \(A\) matrix and adding the gaussian noise.

```
set.seed(1)
<- simulate(operator_information, nsim = n.rep)
u <- as.vector(A %*% as.vector(u)) +
y rnorm(m * n.rep) * 0.1
```

The first replicate of the simulated random field as well as the observation locations are shown in the following figure.

```
<- par(mfrow = c(1, 2), mgp = c(1.2, 0.5, 0),
opar mar = c(2, 2, 0.5, 0.5) + 0.1)
<- inla.mesh.projector(mesh_2d, dims = c(100, 100))
proj image(inla.mesh.project(proj, field = as.vector(u[, 1])),
xlab = "", ylab = "",
cex.main = 0.8, cex.axis = 0.8, cex.lab = 0.8
)plot(loc_2d_mesh[, 1], loc_2d_mesh[, 2],
cex = 0.2, pch = 16, xlab = "", ylab = "",
cex.main = 0.8, cex.axis = 0.8, cex.lab = 0.8
)par(opar)
```

Let us then use the rational SPDE approach to fit the data.

We begin by creating the \(A\) matrix and index with replicates, and the `inla.stack`

object. It is important to notice that since we have replicates we should provide the `index`

and `repl`

arguments for `rspde.make.A()`

function, and also the argument `n.repl`

in `rspde.make.index()`

function. They behave identically as in their `R-INLA`

’s counterparts, namely, `inla.spde.make.A()`

and `inla.make.index()`

.

```
<- rspde.make.A(
Abar.rep mesh = mesh_2d, loc = loc_2d_mesh, index = rep(1:m, times = n.rep),
repl = rep(1:n.rep, each = m)
)<- rspde.make.index(
mesh.index.rep name = "field", mesh = mesh_2d,
n.repl = n.rep
)
<- inla.stack(
st.dat.rep data = list(y = y),
A = Abar.rep,
effects = mesh.index.rep
)
```

We now create the model object.

`<- rspde.matern(mesh = mesh_2d) rspde_model.rep `

Finally, we create the formula and fit. It is extremely important not to forget the `replicate`

argument when building the formula as `inla()`

function will not produce warning and might fit some meaningless model.

```
<-
f.rep ~ -1 + f(field,
y model = rspde_model.rep,
replicate = field.repl
)<-
rspde_fit.rep inla(f.rep,
data = inla.stack.data(st.dat.rep),
family = "gaussian",
control.predictor =
list(A = inla.stack.A(st.dat.rep)),
inla.mode = "experimental"
)
```

We can get the summary:

`summary(rspde_fit.rep)`

```
##
## Call:
## c("inla.core(formula = formula, family = family, contrasts = contrasts,
## ", " data = data, quantiles = quantiles, E = E, offset = offset, ", "
## scale = scale, weights = weights, Ntrials = Ntrials, strata = strata,
## ", " lp.scale = lp.scale, link.covariates = link.covariates, verbose =
## verbose, ", " lincomb = lincomb, selection = selection, control.compute
## = control.compute, ", " control.predictor = control.predictor,
## control.family = control.family, ", " control.inla = control.inla,
## control.fixed = control.fixed, ", " control.mode = control.mode,
## control.expert = control.expert, ", " control.hazard = control.hazard,
## control.lincomb = control.lincomb, ", " control.update =
## control.update, control.lp.scale = control.lp.scale, ", "
## control.pardiso = control.pardiso, only.hyperparam = only.hyperparam,
## ", " inla.call = inla.call, inla.arg = inla.arg, num.threads =
## num.threads, ", " blas.num.threads = blas.num.threads, keep = keep,
## working.directory = working.directory, ", " silent = silent, inla.mode
## = inla.mode, safe = FALSE, debug = debug, ", " .parent.frame =
## .parent.frame)")
## Time used:
## Pre = 4.11, Running = 259, Post = 1.42, Total = 265
## Random effects:
## Name Model
## field RGeneric2
##
## Model hyperparameters:
## mean sd 0.025quant 0.5quant
## Precision for the Gaussian observations 91.83 4.677 83.75 91.47
## Theta1 for field -3.04 0.099 -3.23 -3.04
## Theta2 for field 3.06 0.034 2.99 3.06
## Theta3 for field -1.60 0.045 -1.69 -1.60
## 0.975quant mode
## Precision for the Gaussian observations 102.19 90.16
## Theta1 for field -2.84 -3.05
## Theta2 for field 3.12 3.06
## Theta3 for field -1.52 -1.60
##
## Marginal log-Likelihood: -4400.97
## 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)')
```

and the summary in the user’s scale:

```
<- rspde.result(rspde_fit.rep, "field", rspde_model.rep)
result_fit_rep summary(result_fit_rep)
```

```
## mean sd 0.025quant 0.5quant 0.975quant mode
## tau 0.0481594 0.0047997 0.0397772 0.047759 0.058562 0.046833
## kappa 21.2811000 0.7095750 19.8889000 21.283700 22.673900 21.306300
## nu 0.6713700 0.0251117 0.6215060 0.671698 0.720070 0.673368
```

```
<- data.frame(
result_df parameter = c("tau", "kappa", "nu"),
true = c(tau, kappa, nu),
mean = c(
$summary.tau$mean,
result_fit_rep$summary.kappa$mean,
result_fit_rep$summary.nu$mean
result_fit_rep
),mode = c(
$summary.tau$mode,
result_fit_rep$summary.kappa$mode,
result_fit_rep$summary.nu$mode
result_fit_rep
)
)print(result_df)
```

```
## parameter true mean mode
## 1 tau 0.08920621 0.04815936 0.04683297
## 2 kappa 20.00000000 21.28112714 21.30628437
## 3 nu 0.50000000 0.67136966 0.67336818
```

`rSPDE`

-`INLA`

implementationWe will now discuss some of the arguments that were introduced in our `R-INLA`

implementation of the rational approximation that are not present in `R-INLA`

’s standard SPDE implementation.

In each case we will provide an illustrative example.

When we fit a `rspde.matern()`

model we need to provide an upper bound for the smoothness parameter \(\nu\). The reason for that is that the sparsity of the precision matrix should be kept fixed during `R-INLA`

’s estimation and the higher the value of \(\nu\) the denser the precision matrix gets.

This means that the higher the value of \(\nu\), the higher the computational cost to fit the model. Therefore, ideally, want to choose an upper bound for \(\nu\) as small as possible.

To change the value of the upper bound for the smoothness parameter, we must change the argument `nu_upper_bound`

. The default value for `nu_upper_bound`

is 4. Other common choices for `nu_upper_bound`

are 2 and 1.

It is clear from the discussion above that the smaller the value of `nu_upper_bound`

the faster the estimation procedure will be.

However, if we choose a value of `nu_upper_bound`

which is too low, the “correct” value of \(\nu\) might not belong to the interval \((0,\nu_{UB})\), where \(\nu_{UB}\) is the value of `nu_upper_bound`

. Hence, one might be forced to increase `nu_upper_bound`

and estimate again, which, obviously will increase the computational cost as we will need to do more than one estimation.

Let us illustrate by considering the same model we considered above for the precipitation in Paraná region in Brazil and consider `nu_upper_bound`

equal to 2, which is generally a good choice for `nu_upper_bound`

.

We simply use the function `rspde.matern()`

with the argument `nu_upper_bound`

set to 2:

`<- rspde.matern(mesh = prmesh, nu_upper_bound = 2) rspde_model_2 `

Since we are considering the default `rspde_order`

, the \(A\) matrix and the mesh index objects are the same as the previous ones. Let us then update the formula and fit the model:

```
.2 <- y ~ -1 + Intercept + f(seaDist, model = "rw1") +
f.sf(field, model = rspde_model_2)
<- inla(f.s.2,
rspde_fit_2 family = "Gamma", data = inla.stack.data(stk.dat),
verbose = FALSE,
control.inla = list(int.strategy = "eb"),
control.predictor = list(A = inla.stack.A(stk.dat), compute = TRUE),
inla.mode = "experimental"
)
```

Let us see the summary of the fit:

`summary(rspde_fit_2)`

```
##
## Call:
## c("inla.core(formula = formula, family = family, contrasts = contrasts,
## ", " data = data, quantiles = quantiles, E = E, offset = offset, ", "
## scale = scale, weights = weights, Ntrials = Ntrials, strata = strata,
## ", " lp.scale = lp.scale, link.covariates = link.covariates, verbose =
## verbose, ", " lincomb = lincomb, selection = selection, control.compute
## = control.compute, ", " control.predictor = control.predictor,
## control.family = control.family, ", " control.inla = control.inla,
## control.fixed = control.fixed, ", " control.mode = control.mode,
## control.expert = control.expert, ", " control.hazard = control.hazard,
## control.lincomb = control.lincomb, ", " control.update =
## control.update, control.lp.scale = control.lp.scale, ", "
## control.pardiso = control.pardiso, only.hyperparam = only.hyperparam,
## ", " inla.call = inla.call, inla.arg = inla.arg, num.threads =
## num.threads, ", " blas.num.threads = blas.num.threads, keep = keep,
## working.directory = working.directory, ", " silent = silent, inla.mode
## = inla.mode, safe = FALSE, debug = debug, ", " .parent.frame =
## .parent.frame)")
## Time used:
## Pre = 3.57, Running = 10.4, Post = 0.0593, Total = 14
## Fixed effects:
## mean sd 0.025quant 0.5quant 0.975quant mode kld
## Intercept 0.647 0.019 0.61 0.647 0.684 0.647 0
##
## Random effects:
## Name Model
## seaDist RW1 model
## field RGeneric2
##
## Model hyperparameters:
## mean sd 0.025quant
## Precision parameter for the Gamma observations 13.172 9.02e-01 11.472
## Precision for seaDist 12440.553 1.20e+04 2166.210
## Theta1 for field -1.382 7.69e-01 -2.910
## Theta2 for field 1.123 3.30e-01 0.465
## Theta3 for field 0.606 8.19e-01 -0.949
## 0.5quant 0.975quant mode
## Precision parameter for the Gamma observations 13.146 15.03 13.101
## Precision for seaDist 8924.049 44045.21 5039.461
## Theta1 for field -1.374 0.11 -1.347
## Theta2 for field 1.127 1.76 1.140
## Theta3 for field 0.584 2.27 0.507
##
## Marginal log-Likelihood: -1263.71
## 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)')
```

Let us compare with the cost from the previous fit, with the default value of `nu_upper_bound`

of 4:

```
# nu_upper_bound = 4
$cpu.used rspde_fit
```

```
## Pre Running Post Total
## 4.5738280 42.2833989 0.1479502 47.0051770
```

```
# nu_upper_bound = 2
$cpu.used rspde_fit_2
```

```
## Pre Running Post Total
## 3.56895685 10.35541916 0.05929184 13.98366785
```

We can see that the fit for `nu_upper_bound`

equal to 2 was considerably faster.

Finally, let us get the result results for the field and see the estimate of \(\nu\):

```
<- rspde.result(rspde_fit_2, "field", rspde_model_2)
result_fit_2 summary(result_fit_2)
```

```
## mean sd 0.025quant 0.5quant 0.975quant mode
## tau 0.334864 0.285205 0.0550375 0.253202 1.10612 0.13919
## kappa 3.242650 1.078310 1.6015800 3.086190 5.79699 2.78157
## nu 1.257240 0.335209 0.5619250 1.283550 1.81026 1.37528
```

To change the order of the rational approximation all we have to do is set the argument `rspde_order`

to the desired value. The current available possibilities are `1,2,3`

,…, up to `8`

.

The higher the order of the rational approximation, the more accurate the results will be, however, the higher the computational cost will be.

The default `rspde_order`

of 2 is generally a good choice and reasonably accurate. See the vignette Rational approximation with the rSPDE package for further details on the order of the rational approximation and some comparison with the Matérn covariance.

Let us fit the above model with the covariance-based rational approximation of order `3`

. Since we are changing the order of the rational approximation, that is, we are changing the `rspde_order`

argument, we need to recompute the \(A\) matrix and the mesh index. Therefore, we proceed as follows:

- We build a new model:

```
<- rspde.matern(mesh = prmesh,
rspde_model_order_3 rspde_order = 3,
nu_upper_bound = 2
)
```

- We create a new \(A\) matrix:

`<- rspde.make.A(mesh = prmesh, loc = coords, rspde_order = 3) Abar_3 `

- We create a new index:

```
.3 <- rspde.make.index(
mesh.indexname = "field", mesh = prmesh,
rspde_order = 3
)
```

Now the remaining steps are the same as before:

```
.3 <- inla.stack(
stk.datdata = list(y = Y), A = list(Abar_3, 1), tag = "est",
effects = list(
c(
.3,
mesh.indexlist(Intercept = 1)
),list(
long = inla.group(coords[, 1]),
lat = inla.group(coords[, 2]),
seaDist = inla.group(seaDist)
)
)
)
.3 <- y ~ -1 + Intercept + f(seaDist, model = "rw1") +
f.sf(field, model = rspde_model_order_3)
<- inla(f.s.3,
rspde_fit_order_3 family = "Gamma", data = inla.stack.data(stk.dat.3),
verbose = FALSE,
control.inla = list(int.strategy = "eb"),
control.predictor = list(A = inla.stack.A(stk.dat.3), compute = TRUE),
inla.mode = "experimental"
)
```

Let us see the summary:

`summary(rspde_fit_order_3)`

```
##
## Call:
## c("inla.core(formula = formula, family = family, contrasts = contrasts,
## ", " data = data, quantiles = quantiles, E = E, offset = offset, ", "
## scale = scale, weights = weights, Ntrials = Ntrials, strata = strata,
## ", " lp.scale = lp.scale, link.covariates = link.covariates, verbose =
## verbose, ", " lincomb = lincomb, selection = selection, control.compute
## = control.compute, ", " control.predictor = control.predictor,
## control.family = control.family, ", " control.inla = control.inla,
## control.fixed = control.fixed, ", " control.mode = control.mode,
## control.expert = control.expert, ", " control.hazard = control.hazard,
## control.lincomb = control.lincomb, ", " control.update =
## control.update, control.lp.scale = control.lp.scale, ", "
## control.pardiso = control.pardiso, only.hyperparam = only.hyperparam,
## ", " inla.call = inla.call, inla.arg = inla.arg, num.threads =
## num.threads, ", " blas.num.threads = blas.num.threads, keep = keep,
## working.directory = working.directory, ", " silent = silent, inla.mode
## = inla.mode, safe = FALSE, debug = debug, ", " .parent.frame =
## .parent.frame)")
## Time used:
## Pre = 3.6, Running = 19.7, Post = 0.0797, Total = 23.4
## Fixed effects:
## mean sd 0.025quant 0.5quant 0.975quant mode kld
## Intercept 0.486 0.013 0.46 0.486 0.512 0.486 0
##
## Random effects:
## Name Model
## seaDist RW1 model
## field RGeneric2
##
## Model hyperparameters:
## mean sd 0.025quant
## Precision parameter for the Gamma observations 13.232 0.863 11.619
## Precision for seaDist 9411.566 7078.883 2457.716
## Theta1 for field -1.324 0.402 -2.382
## Theta2 for field 1.186 0.111 0.866
## Theta3 for field 0.419 0.383 -0.717
## 0.5quant 0.975quant mode
## Precision parameter for the Gamma observations 13.201 15.022 13.134
## Precision for seaDist 7432.733 28225.479 4990.775
## Theta1 for field -0.868 -0.132 -1.381
## Theta2 for field 1.075 1.473 1.203
## Theta3 for field -0.015 1.427 0.473
##
## Marginal log-Likelihood: -1262.18
## 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)')
```

We can see in the above summary that the computational cost significantly increased. Let us compare the cost of having `rspde_order=3`

and `nu_upper_bound=2`

with the cost of having `rspde_order=2`

and `nu_upper_bound=4`

:

```
# nu_upper_bound = 4
$cpu.used rspde_fit
```

```
## Pre Running Post Total
## 4.5738280 42.2833989 0.1479502 47.0051770
```

```
# nu_upper_bound = 2
$cpu.used rspde_fit_order_3
```

```
## Pre Running Post Total
## 3.59550214 19.68542695 0.07968807 23.36061716
```

Let us now see the summary of the fit in the original scale:

```
<- rspde.result(rspde_fit_order_3,
result_fit_order_3 "field", rspde_model_order_3)
summary(result_fit_order_3)
```

```
## mean sd 0.025quant 0.5quant 0.975quant mode
## tau 0.288771 0.126731 0.128261 0.260836 0.616475 0.219414
## kappa 3.295390 0.359952 2.594540 3.293260 4.007540 3.289130
## nu 1.200340 0.178529 0.811056 1.215290 1.505860 1.249040
```

The following figure shows the posterior marginal densities of the three parameters:

```
par(mfrow = c(1, 3))
plot(result_fit_order_3, caption = c("tau", "kappa", "nu"))
```

One can check the order of the rational approximation by using the `rational.order()`

function. It also allows another way to change the order of the rational order, by using the corresponding `rational.order<-()`

function.

The `rational.order()`

and `rational.order<-()`

functions can be applied to the `inla.rspde`

object, to the `A`

matrix and to the `index`

objects.

Here to check the models:

`rational.order(rspde_model)`

`## [1] 2`

`rational.order(rspde_model_order_3)`

`## [1] 3`

Here to check the `A`

matrices:

`rational.order(Abar)`

`## [1] 2`

`rational.order(Abar_3)`

`## [1] 3`

Here to check the indexes:

`rational.order(mesh.index)`

`## [1] 2`

`rational.order(mesh.index.3)`

`## [1] 3`

Let us now change the order of the `rspde_model`

object to be 1:

```
rational.order(rspde_model) <- 1
rational.order(Abar) <- 1
rational.order(mesh.index) <- 1
```

Let us fit this new model:

```
.1 <- y ~ -1 + Intercept + f(seaDist, model = "rw1") +
f.sf(field, model = rspde_model)
.1 <- inla.stack(
stk.datdata = list(y = Y), A = list(Abar, 1), tag = "est",
effects = list(
c(
mesh.index,list(Intercept = 1)
),list(
long = inla.group(coords[, 1]),
lat = inla.group(coords[, 2]),
seaDist = inla.group(seaDist)
)
)
)
<- inla(f.s.1,
rspde_fit_order_1 family = "Gamma", data = inla.stack.data(stk.dat.1),
verbose = FALSE,
control.inla = list(int.strategy = "eb"),
control.predictor = list(A = inla.stack.A(stk.dat.1), compute = TRUE),
inla.mode = "experimental"
)
```

Here is the summary:

`summary(rspde_fit_order_1)`

```
##
## Call:
## c("inla.core(formula = formula, family = family, contrasts = contrasts,
## ", " data = data, quantiles = quantiles, E = E, offset = offset, ", "
## scale = scale, weights = weights, Ntrials = Ntrials, strata = strata,
## ", " lp.scale = lp.scale, link.covariates = link.covariates, verbose =
## verbose, ", " lincomb = lincomb, selection = selection, control.compute
## = control.compute, ", " control.predictor = control.predictor,
## control.family = control.family, ", " control.inla = control.inla,
## control.fixed = control.fixed, ", " control.mode = control.mode,
## control.expert = control.expert, ", " control.hazard = control.hazard,
## control.lincomb = control.lincomb, ", " control.update =
## control.update, control.lp.scale = control.lp.scale, ", "
## control.pardiso = control.pardiso, only.hyperparam = only.hyperparam,
## ", " inla.call = inla.call, inla.arg = inla.arg, num.threads =
## num.threads, ", " blas.num.threads = blas.num.threads, keep = keep,
## working.directory = working.directory, ", " silent = silent, inla.mode
## = inla.mode, safe = FALSE, debug = debug, ", " .parent.frame =
## .parent.frame)")
## Time used:
## Pre = 3.95, Running = 15.2, Post = 0.0629, Total = 19.2
## Fixed effects:
## mean sd 0.025quant 0.5quant 0.975quant mode kld
## Intercept 0.972 0.028 0.917 0.972 1.026 0.972 0
##
## Random effects:
## Name Model
## seaDist RW1 model
## field RGeneric2
##
## Model hyperparameters:
## mean sd 0.025quant
## Precision parameter for the Gamma observations 13.213 0.909 11.505
## Precision for seaDist 10322.059 8482.729 2245.040
## Theta1 for field -0.976 0.549 -2.053
## Theta2 for field 1.071 0.311 0.452
## Theta3 for field -1.022 0.362 -1.731
## 0.5quant 0.975quant mode
## Precision parameter for the Gamma observations 13.185 15.083 13.136
## Precision for seaDist 7923.584 32855.962 4987.165
## Theta1 for field -0.978 0.111 -0.986
## Theta2 for field 1.074 1.679 1.082
## Theta3 for field -1.024 -0.307 -1.028
##
## Marginal log-Likelihood: -1264.18
## 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)')
```

We can fix the smoothness, say \(\nu\), of the model by providing a non-NULL positive value for `nu`

.

When the smoothness, \(\nu\), is fixed, we can have two possibilities:

\(\alpha = \nu + d/2\) is integer;

\(\alpha = \nu + d/2\) is not integer.

The first case, i.e., when \(\alpha\) is integer, has less computational cost. Furthermore, the \(A\) matrix is different than the \(A\) matrix for the non-integer \(\alpha\).

The \(A\) matrix is the same for all values of \(\nu\) such that \(\alpha\) is integer. So, the \(A\) matrix for these cases only need to be computed once. The same holds for the `index`

obtained from the `rspde.make.index()`

function.

In the second case the \(A\) matrix only depends on the order of the rational approximation and not on \(\nu\). Therefore, if the matrix \(A\) has already been computed for some `rspde_order`

, then the \(A\) matrix will be same for all the values of \(\nu\) such that \(\alpha\) is non-integer for that `rspde_order`

. The same holds for the `index`

obtained from the `rspde.make.index()`

function.

If \(\nu\) is fixed, we have that the parameters returned by `R-INLA`

are \[\kappa = \exp(\theta_1)\quad\hbox{and}\quad\tau = \exp(\theta_2).\] We will now provide illustrations for both scenarios. It is also noteworthy that just as for the case in which we estimate \(\nu\), we can also change the order of the rational approximation by changing the value of `rspde_order`

. For both illustrations with fixed \(\nu\) below, we will only consider the order of the rational approximation of 2, that is, the default order.

Recall that: \[\nu = \nu_{UB}\Big(\frac{\exp(\theta_3)}{1+\exp(\theta_3)}\Big).\] Thus, to illustrate, let us consider a fixed \(\nu\) given by the mean of \(\nu\) obtained from the first model we considered in this vignette, namely, the fit given by `rspde_fit`

, which is approximately \(\nu = 1.21\).

Notice that for this \(\nu\), the value of \(\alpha\) is non-integer, so we can use the \(A\) matrix and the index of the first fitted model, which is also of order 2.

Therefore, all we have to do is build a new model in which we set `nu`

to `1.21`

:

```
<- rspde.matern(mesh = prmesh, rspde_order = 2,
rspde_model_fix nu = 1.21
)
```

Let us now fit the model:

```
<- y ~ -1 + Intercept + f(seaDist, model = "rw1") +
f.s.fix f(field, model = rspde_model_fix)
<- inla(f.s.fix,
rspde_fix family = "Gamma", data = inla.stack.data(stk.dat),
verbose = FALSE,
control.inla = list(int.strategy = "eb"),
control.predictor = list(A = inla.stack.A(stk.dat), compute = TRUE),,
inla.mode = "experimental"
)
```

Here we have the summary:

`summary(rspde_fix)`

```
##
## Call:
## c("inla.core(formula = formula, family = family, contrasts = contrasts,
## ", " data = data, quantiles = quantiles, E = E, offset = offset, ", "
## scale = scale, weights = weights, Ntrials = Ntrials, strata = strata,
## ", " lp.scale = lp.scale, link.covariates = link.covariates, verbose =
## verbose, ", " lincomb = lincomb, selection = selection, control.compute
## = control.compute, ", " control.predictor = control.predictor,
## control.family = control.family, ", " control.inla = control.inla,
## control.fixed = control.fixed, ", " control.mode = control.mode,
## control.expert = control.expert, ", " control.hazard = control.hazard,
## control.lincomb = control.lincomb, ", " control.update =
## control.update, control.lp.scale = control.lp.scale, ", "
## control.pardiso = control.pardiso, only.hyperparam = only.hyperparam,
## ", " inla.call = inla.call, inla.arg = inla.arg, num.threads =
## num.threads, ", " blas.num.threads = blas.num.threads, keep = keep,
## working.directory = working.directory, ", " silent = silent, inla.mode
## = inla.mode, safe = FALSE, debug = debug, ", " .parent.frame =
## .parent.frame)")
## Time used:
## Pre = 3.69, Running = 7.77, Post = 0.0601, Total = 11.5
## Fixed effects:
## mean sd 0.025quant 0.5quant 0.975quant mode kld
## Intercept 0.648 0.017 0.614 0.648 0.682 0.648 0
##
## Random effects:
## Name Model
## seaDist RW1 model
## field RGeneric2
##
## Model hyperparameters:
## mean sd 0.025quant
## Precision parameter for the Gamma observations 13.23 0.906 11.534
## Precision for seaDist 9788.58 8061.121 2457.151
## Theta1 for field -1.34 0.335 -1.995
## Theta2 for field 1.19 0.310 0.571
## 0.5quant 0.975quant mode
## Precision parameter for the Gamma observations 13.20 15.101 13.13
## Precision for seaDist 7451.41 31190.405 4817.41
## Theta1 for field -1.34 -0.674 -1.35
## Theta2 for field 1.20 1.792 1.21
##
## Marginal log-Likelihood: -1263.56
## 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)')
```

Now, the summary in the original scale:

```
<- rspde.result(rspde_fix, "field", rspde_model_fix)
result_fix summary(result_fix)
```

```
## mean sd 0.025quant 0.5quant 0.975quant mode
## tau 0.276386 0.0950577 0.136918 0.260582 0.506509 0.231598
## kappa 3.453120 1.0759500 1.780750 3.308540 5.971600 3.031500
```

Since we are in dimension \(d=2\), and \(\nu>0\), the smallest value of \(\nu\) that makes \(\alpha = \nu + 1\) an integer is \(\nu=1\). This value is also close to the estimated mean of the first model we fitted and enclosed by the posterior marginal density of \(\nu\) for the first fit. Therefore, let us fit the model with \(\nu=1\).

To this end we need to compute a new \(A\) matrix:

```
<- rspde.make.A(
Abar.int mesh = prmesh, loc = coords,
nu = 1
)
```

a new index:

```
<- rspde.make.index(
mesh.index.int name = "field", mesh = prmesh,
nu = 1
)
```

create a new model (remember to set `nu=1`

):

```
<- rspde.matern(mesh = prmesh,
rspde_model_fix_int1 nu = 1
)
```

The remaining is standard:

```
<- inla.stack(
stk.dat.int data = list(y = Y), A = list(Abar.int, 1), tag = "est",
effects = list(
c(
mesh.index.int,list(Intercept = 1)
),list(
long = inla.group(coords[, 1]),
lat = inla.group(coords[, 2]),
seaDist = inla.group(seaDist)
)
)
)
.1 <- y ~ -1 + Intercept + f(seaDist, model = "rw1") +
f.s.fix.intf(field, model = rspde_model_fix_int1)
<- inla(f.s.fix.int.1,
rspde_fix_int_1 family = "Gamma",
data = inla.stack.data(stk.dat.int), verbose = FALSE,
control.inla = list(int.strategy = "eb"),
control.predictor = list(
A = inla.stack.A(stk.dat.int),
compute = TRUE
),inla.mode = "experimental"
)
```

Let us check the summary:

`summary(rspde_fix_int_1)`

```
##
## Call:
## c("inla.core(formula = formula, family = family, contrasts = contrasts,
## ", " data = data, quantiles = quantiles, E = E, offset = offset, ", "
## scale = scale, weights = weights, Ntrials = Ntrials, strata = strata,
## ", " lp.scale = lp.scale, link.covariates = link.covariates, verbose =
## verbose, ", " lincomb = lincomb, selection = selection, control.compute
## = control.compute, ", " control.predictor = control.predictor,
## control.family = control.family, ", " control.inla = control.inla,
## control.fixed = control.fixed, ", " control.mode = control.mode,
## control.expert = control.expert, ", " control.hazard = control.hazard,
## control.lincomb = control.lincomb, ", " control.update =
## control.update, control.lp.scale = control.lp.scale, ", "
## control.pardiso = control.pardiso, only.hyperparam = only.hyperparam,
## ", " inla.call = inla.call, inla.arg = inla.arg, num.threads =
## num.threads, ", " blas.num.threads = blas.num.threads, keep = keep,
## working.directory = working.directory, ", " silent = silent, inla.mode
## = inla.mode, safe = FALSE, debug = debug, ", " .parent.frame =
## .parent.frame)")
## Time used:
## Pre = 3.36, Running = 3.57, Post = 0.0476, Total = 6.98
## Fixed effects:
## mean sd 0.025quant 0.5quant 0.975quant mode kld
## Intercept 1.945 0.054 1.839 1.945 2.051 1.945 0
##
## Random effects:
## Name Model
## seaDist RW1 model
## field RGeneric2
##
## Model hyperparameters:
## mean sd 0.025quant
## Precision parameter for the Gamma observations 13.251 0.908 11.536
## Precision for seaDist 10563.969 9591.953 2569.202
## Theta1 for field -0.862 0.309 -1.451
## Theta2 for field 1.033 0.352 0.311
## 0.5quant 0.975quant mode
## Precision parameter for the Gamma observations 13.23 15.111 13.188
## Precision for seaDist 7710.02 35839.749 4794.549
## Theta1 for field -0.87 -0.233 -0.899
## Theta2 for field 1.04 1.697 1.087
##
## Marginal log-Likelihood: -1262.65
## 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)')
```

and check the summary in the user’s scale:

```
<- rspde.result(rspde_fix_int_1, "field", rspde_model_fix_int1)
rspde_result_int summary(rspde_result_int)
```

```
## mean sd 0.025quant 0.5quant 0.975quant mode
## tau 0.443021 0.141887 0.235715 0.418704 0.787673 0.374686
## kappa 2.984190 1.042990 1.374150 2.845300 5.423080 2.563830
```

We begin by recalling that the fitted `rSPDE`

model in `R-INLA`

contains the parameters \(\textrm{Theta1}\), \(\textrm{Theta2}\) and \(\textrm{Theta3}\). Let, again, \(\theta_1 = \textrm{Theta1}\), \(\theta_2=\textrm{Theta2}\) and \(\theta_3=\textrm{Theta3}\). In terms of the SPDE \[(\kappa^2 I - \Delta)^{\alpha/2}(\tau u) = \mathcal{W},\] where \(\alpha = \nu + d/2\).

We begin by dealing with \(\tau\) and \(\kappa\).

We have that \[\tau = \exp(\theta_1),\quad \kappa = \exp(\theta_2).\] The `rspde.matern()`

function assumes a lognormal prior distribution for \(\tau\) and \(\kappa\). This prior distribution is obtained by assuming that \(\theta_1\) and \(\theta_2\) follow normal distributions. By default we assume \(\theta_1\) and \(\theta_2\) to be independent and to follow normal distributions \(\theta_1\sim N(\log(\tau_0), 10)\) and \(\theta_2\sim N(\log(\kappa_0), 10)\).

\(\kappa_0\) is suitably defined in terms of the mesh and \(\tau_0\) is defined in terms of \(\kappa_0\) and on the prior of the smoothness parameter.

If one wants to define the prior \[\theta_1 \sim N(\text{mean_theta_1}, \text{sd_theta_1}),\] one can simply set the argument `prior.tau = list(meanlog=mean_theta_1, sdlog=sd_theta_1)`

. Analogously, to define the prior \[\theta_2 \sim N(\text{mean_theta_2}, \text{sd_theta_2}),\] one can set the argument `prior.kappa = list(meanlog=mean_theta_2, sdlog=sd_theta_2)`

.

It is important to mention that, by default, the initial values of \(\tau\) and \(\kappa\) are \(\exp(\text{mean_theta_1})\) and \(\exp(\text{mean_theta_2})\), respectively. So, if the user does not change these parameters, and also does not change the initial values, the initial values of \(\tau\) and \(\kappa\) will be, respectively, \(\tau_0\) and \(\kappa_0\).

If one sets `prior.tau = list(meanlog=mean_theta_1)`

, the prior for \(\theta_1\) will be \[\theta_1 \sim N(\text{mean_theta_1}, 1),\] whereas, if one sets, `prior.tau = list(sdlog=sd_theta_1)`

, the prior will be \[\theta_1 \sim N(\log(\tau_0), \text{sd_theta_1}).\] Analogously, if one sets `prior.kappa = list(meanlog=mean_theta_2)`

, the prior for \(\theta_2\) will be \[\theta_2 \sim N(\text{mean_theta_2}, 1),\] whereas, if one sets, `prior.kappa = list(sdlog=sd_theta_2)`

, the prior will be \[\theta_2 \sim N(\log(\kappa_0), \text{sd_theta_2}).\]

Finally, let us consider the smoothness parameter \(\nu\).

By default, we assume that \(\nu\) follows a beta distribution on the interval \((0,\nu_{UB})\), where \(\nu_{UB}\) is the upper bound for \(\nu\), with mean \(\nu_0=\min\{1, \nu_{UB}/2\}\) and variance \(\frac{\nu_0(\nu_{UB}-\nu_0)}{1+\phi_0}\), and we call \(\phi_0\) the precision parameter, whose default value is \[\phi_0 = \max\Big\{\frac{\nu_{UB}}{\nu_0}, \frac{\nu_{UB}}{\nu_{UB}-\nu_0}\Big\} + \phi_{inc}.\] The parameter \(\phi_{inc}\) is an increment to ensure that the prior beta density has boundary values equal to zero (where the boundary values are defined either by continuity or by limits). The default value of \(\phi_{inc}\) is 1. The value of \(\phi_{inc}\) can be changed by changing the argument `nu.prec.inc`

in the `rspde.matern()`

function. The higher the value of \(\phi_{inc}\) (that is, the value of `nu.prec.inc`

) the more informative the prior distribution becomes.

Let us denote a beta distribution with support on \((0,\nu_{UB})\), mean \(\mu\) and precision parameter \(\phi\) by \(\mathcal{B}_{\nu_{UB}}(\mu,\phi)\).

If we want \(\nu\) to have a prior \[\nu \sim \mathcal{B}_{\nu_{UB}}(\text{nu_1},\text{prec_1}),\] one simply needs to set `prior.nu = list(mean=nu_1, prec=prec_1)`

. If one sets `prior.nu = list(mean=nu_1)`

, then \(\nu\) will have prior \[\nu \sim \mathcal{B}_{\nu_{UB}}(\text{nu_1},\phi_1),\] where \[\phi_1 = \max\Big\{\frac{\nu_{UB}}{\text{nu_1}}, \frac{\nu_{UB}}{\nu_{UB}-\text{nu_1}}\Big\} + \text{nu.prec.inc}.\]

Of one sets `prior.nu = list(prec=prec_1)`

, then \(\nu\) will have prior \[\nu\sim \mathcal{B}_{\nu_{UB}}(\nu_0, \text{prec_1}).\] It is also noteworthy that we have that, in terms of `R-INLA`

’s parameters,

\[\nu = \nu_{UB}\Big(\frac{\exp(\theta_3)}{1+\exp(\theta_3)}\Big).\]

It is important to mention that, by default, if a beta prior distribution is chosen for the smoothness parameter \(\nu\), then the initial value of \(\nu\) is the mean of the prior beta distribution. So, if the user does not change this parameter, and also does not change the initial value, the initial value of \(\nu\) will be \(\min\{1,\nu_{UB}/2\}\).

We can have another possibility of prior distribution for \(\nu\), namely, a truncated lognormal distribution. The truncated lognormal distribution is defined in the following sense. We assume that \(\log(\nu)\) has prior distribution given by a truncated normal distribution with support \((-\infty,\log(\nu_{UB}))\), where \(\nu_{UB}\) is the upper bound for \(\nu\), with location parameter \(\mu_0 =\log(\nu_0)= \log\Big(\min\{1,\nu_{UB}/2\}\Big)\) and scale parameter \(\sigma_0 = 1\). More precisely, let \(\Phi(\cdot; \mu,\sigma)\) stand for the cumulative distribution function (CDF) of a normal distribution with mean \(\mu\) and standard deviation \(\sigma\). Then, \(\log(\nu)\) has cumulative distribution function given by \[F_{\log(\nu)}(x) = \frac{\Phi(x;\mu_0,\sigma_0)}{\Phi(\nu_{UB})},\quad x\leq \nu_{UB},\] and \(F_{\log(\nu)}(x) = 1\) if \(x>\nu_{UB}\). We will call \(\mu_0\) and \(\sigma_0\) the log-location and log-scale parameters of \(\nu\), respectively, and we say that \(\log(\nu)\) follows a truncated normal distribution with location parameter \(\mu_0\) and scale parameter \(\sigma_0\).

We also assume that, in terms of `R-INLA`

’s parameters, \[\nu = \nu_{UB}\Big(\frac{\exp(\theta_3)}{1+\exp(\theta_3)}\Big).\]

To change the prior distribution of \(\nu\) to the truncated lognormal distribution, we need to set the argument `prior.nu.dist="lognormal"`

.

To change these parameters in the prior distribution to, say, `log_nu_1`

and `log_sigma_1`

, one can simply set `prior.nu = list(loglocation=log_nu_1, logscale=sigma_1)`

.

If one sets `prior.nu = list(loglocation=log_nu_1)`

, the prior for \(\theta_3\) will be a truncated normal normal distribution with location parameter `log_nu_1`

and scale parameter `1`

. Analogously, if one sets, `prior.nu = list(logscale=sigma_1)`

, the prior for \(\theta_3\) will be a truncated normal distribution with location parameter \(\log(\nu_0)= \log\Big(\min\{1,\nu_{UB}/2\}\Big)\) and scale parameter `sigma_1`

.

It is important to mention that, by default, if a truncated lognormal prior distribution is chosen for the smoothness parameter \(\nu\), then the initial value of \(\nu\) is the exponential of the log-location parameter of \(\nu\). So, if the user does not change this parameter, and also does not change the initial value, the initial value of \(\nu\) will be \(\min\{1,\nu_{UB}/2\}\).

Let us consider an example with the same dataset used in the first model of this vignette where we change the prior distribution of \(\nu\) from beta to lognormal. Let us also set `nu_upper_bound`

to 2.

```
<- rspde.matern(mesh = prmesh, prior.nu.dist = "lognormal",
rspde_model_ln nu_upper_bound = 2
)
```

Since we did not change `rspde_order`

and are not fixing \(\nu\), we can use the same \(A\) matrix and same index from the first example.

Therefore, all we have to do is update the formula and fit the model:

```
<- y ~ -1 + Intercept + f(seaDist, model = "rw1") +
f.s.ln f(field, model = rspde_model_ln)
<- inla(f.s.ln,
rspde_fit_ln family = "Gamma", data = inla.stack.data(stk.dat),
verbose = FALSE,
control.inla = list(int.strategy = "eb"),
control.predictor = list(A = inla.stack.A(stk.dat), compute = TRUE),
inla.mode = "experimental"
)
```

We have the summary:

`summary(rspde_fit_ln)`

```
##
## Call:
## c("inla.core(formula = formula, family = family, contrasts = contrasts,
## ", " data = data, quantiles = quantiles, E = E, offset = offset, ", "
## scale = scale, weights = weights, Ntrials = Ntrials, strata = strata,
## ", " lp.scale = lp.scale, link.covariates = link.covariates, verbose =
## verbose, ", " lincomb = lincomb, selection = selection, control.compute
## = control.compute, ", " control.predictor = control.predictor,
## control.family = control.family, ", " control.inla = control.inla,
## control.fixed = control.fixed, ", " control.mode = control.mode,
## control.expert = control.expert, ", " control.hazard = control.hazard,
## control.lincomb = control.lincomb, ", " control.update =
## control.update, control.lp.scale = control.lp.scale, ", "
## control.pardiso = control.pardiso, only.hyperparam = only.hyperparam,
## ", " inla.call = inla.call, inla.arg = inla.arg, num.threads =
## num.threads, ", " blas.num.threads = blas.num.threads, keep = keep,
## working.directory = working.directory, ", " silent = silent, inla.mode
## = inla.mode, safe = FALSE, debug = debug, ", " .parent.frame =
## .parent.frame)")
## Time used:
## Pre = 3.56, Running = 10.6, Post = 0.0634, Total = 14.2
## Fixed effects:
## mean sd 0.025quant 0.5quant 0.975quant mode kld
## Intercept 0.646 0.017 0.612 0.646 0.681 0.646 0
##
## Random effects:
## Name Model
## seaDist RW1 model
## field RGeneric2
##
## Model hyperparameters:
## mean sd 0.025quant
## Precision parameter for the Gamma observations 13.203 9.05e-01 11.515
## Precision for seaDist 12616.037 1.02e+04 2305.533
## Theta1 for field -1.443 1.10e+00 -3.444
## Theta2 for field 1.223 3.22e-01 0.558
## Theta3 for field 0.773 1.05e+00 -1.306
## 0.5quant 0.975quant mode
## Precision parameter for the Gamma observations 13.170 1.51e+01 13.102
## Precision for seaDist 9831.302 3.96e+04 5811.553
## Theta1 for field -1.503 8.42e-01 -1.717
## Theta2 for field 1.235 1.82e+00 1.282
## Theta3 for field 0.776 2.83e+00 0.789
##
## Marginal log-Likelihood: -1264.23
## 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)')
```

Also, we can have the summary in the user’s scale:

```
<- rspde.result(rspde_fit_ln, "field", rspde_model_ln)
result_fit_ln summary(result_fit_ln)
```

```
## mean sd 0.025quant 0.5quant 0.975quant mode
## tau 0.447157 0.726613 0.032274 0.222036 2.29310 0.0768696
## kappa 3.571820 1.133390 1.758030 3.443310 6.16229 3.1826700
## nu 1.305790 0.400785 0.430142 1.369770 1.88777 1.6254500
```

and the plot of the posterior marginal densities

```
par(mfrow = c(1, 3))
plot(result_fit_ln, caption = c("tau", "kappa", "nu"))
```

The starting values to be used by `R-INLA`

’s optimization algorithm can be changed by setting the arguments `start.ltau`

, `start.lkappa`

and `start.nu`

.

`start.ltau`

will be the initial value for \(\log(\tau)\), that is, the logarithm of \(\tau\).`start.lkappa`

will be the inital value for \(\log(\kappa)\), that is, the logarithm of \(\kappa\).`start.nu`

will be the initial value for \(\nu\). Notice that here the initial value is*not*on the log scale.

One can change the initial value of one or more parameters.

For instance, let us consider the example with precipitation data, `rspde_order=3`

, but change the initial values to the ones close to the fitted value when considering the default `rspde_order`

(which is 2):

```
<- rspde.matern(mesh = prmesh, rspde_order = 3,
rspde_model_order_3_start nu_upper_bound = 2,
start.lkappa = result_fit$summary.log.kappa$mean,
start.ltau = result_fit$summary.log.tau$mean,
start.nu = result_fit$summary.nu$mean
)
```

Since we already computed the \(A\) matrix and the index for `rspde_order=3`

, all we have to do is to update the formula and fit:

```
3.start <- y ~ -1 + Intercept + f(seaDist, model = "rw1") +
f.s.f(field, model = rspde_model_order_3_start)
<- inla(f.s.3.start,
rspde_fit_order_3_start family = "Gamma",
data = inla.stack.data(stk.dat.3),
verbose = FALSE,
control.inla = list(int.strategy = "eb"),
control.predictor = list(
A = inla.stack.A(stk.dat.3),
compute = TRUE
),inla.mode = "experimental"
)
```

We have the summary:

`summary(rspde_fit_order_3_start)`

```
##
## Call:
## c("inla.core(formula = formula, family = family, contrasts = contrasts,
## ", " data = data, quantiles = quantiles, E = E, offset = offset, ", "
## scale = scale, weights = weights, Ntrials = Ntrials, strata = strata,
## ", " lp.scale = lp.scale, link.covariates = link.covariates, verbose =
## verbose, ", " lincomb = lincomb, selection = selection, control.compute
## = control.compute, ", " control.predictor = control.predictor,
## control.family = control.family, ", " control.inla = control.inla,
## control.fixed = control.fixed, ", " control.mode = control.mode,
## control.expert = control.expert, ", " control.hazard = control.hazard,
## control.lincomb = control.lincomb, ", " control.update =
## control.update, control.lp.scale = control.lp.scale, ", "
## control.pardiso = control.pardiso, only.hyperparam = only.hyperparam,
## ", " inla.call = inla.call, inla.arg = inla.arg, num.threads =
## num.threads, ", " blas.num.threads = blas.num.threads, keep = keep,
## working.directory = working.directory, ", " silent = silent, inla.mode
## = inla.mode, safe = FALSE, debug = debug, ", " .parent.frame =
## .parent.frame)")
## Time used:
## Pre = 3.48, Running = 13.3, Post = 0.0681, Total = 16.9
## Fixed effects:
## mean sd 0.025quant 0.5quant 0.975quant mode kld
## Intercept 0.486 0.013 0.46 0.486 0.512 0.486 0
##
## Random effects:
## Name Model
## seaDist RW1 model
## field RGeneric2
##
## Model hyperparameters:
## mean sd 0.025quant
## Precision parameter for the Gamma observations 13.196 0.893 11.511
## Precision for seaDist 9938.655 8461.530 2436.085
## Theta1 for field -1.478 0.437 -2.428
## Theta2 for field 1.234 0.147 0.954
## Theta3 for field 0.572 0.418 -0.233
## 0.5quant 0.975quant mode
## Precision parameter for the Gamma observations 13.171 15.027 13.13
## Precision for seaDist 7466.163 32376.380 4756.05
## Theta1 for field -1.480 -0.635 -1.42
## Theta2 for field 1.233 1.557 1.22
## Theta3 for field 0.574 1.481 0.52
##
## Marginal log-Likelihood: -1262.89
## 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)')
```

We have three rational approximations available. The BRASIL algorithm (Hofreither 2021)(https://doi.org/10.1007/s11075-020-01042-0), and two “versions” of the Clenshaw-Lord Chebyshev-Pade algorithm, one with lower bound zero and another with the lower bound given in (Xiong, Simas, and Bolin 2022)(https://arxiv.org/abs/2209.04670).

The type of rational approximation can be chosen by setting the `type.rational.approx`

argument in the `rspde.matern`

function. The BRASIL algorithm corresponds to the choice `brasil`

, the Clenshaw-Lord Chebyshev pade with zero lower bound and non-zero lower bounds are given, respectively, by the choices `chebfun`

and `chebfunLB`

.

Let us fit a model assigning a `brasil`

rational approximation. We will consider a model with the order of the rational approximation being 1:

```
<- rspde.matern(prmesh, rspde_order = 1,
rspde_model_brasil type.rational.approx = "brasil")
1.brasil <- y ~ -1 + Intercept + f(seaDist, model = "rw1") +
f.s.f(field, model = rspde_model_brasil)
<- inla(f.s.1.brasil,
rspde_fit_order_1_brasil family = "Gamma", data = inla.stack.data(stk.dat.1),
verbose = FALSE,
control.inla = list(int.strategy = "eb"),
control.predictor = list(A = inla.stack.A(stk.dat.1), compute = TRUE),
inla.mode = "experimental"
)
```

Let us get the summary:

`summary(rspde_fit_order_1_brasil)`

```
##
## Call:
## c("inla.core(formula = formula, family = family, contrasts = contrasts,
## ", " data = data, quantiles = quantiles, E = E, offset = offset, ", "
## scale = scale, weights = weights, Ntrials = Ntrials, strata = strata,
## ", " lp.scale = lp.scale, link.covariates = link.covariates, verbose =
## verbose, ", " lincomb = lincomb, selection = selection, control.compute
## = control.compute, ", " control.predictor = control.predictor,
## control.family = control.family, ", " control.inla = control.inla,
## control.fixed = control.fixed, ", " control.mode = control.mode,
## control.expert = control.expert, ", " control.hazard = control.hazard,
## control.lincomb = control.lincomb, ", " control.update =
## control.update, control.lp.scale = control.lp.scale, ", "
## control.pardiso = control.pardiso, only.hyperparam = only.hyperparam,
## ", " inla.call = inla.call, inla.arg = inla.arg, num.threads =
## num.threads, ", " blas.num.threads = blas.num.threads, keep = keep,
## working.directory = working.directory, ", " silent = silent, inla.mode
## = inla.mode, safe = FALSE, debug = debug, ", " .parent.frame =
## .parent.frame)")
## Time used:
## Pre = 3.53, Running = 10.9, Post = 0.0542, Total = 14.5
## Fixed effects:
## mean sd 0.025quant 0.5quant 0.975quant mode kld
## Intercept 0.971 0.029 0.915 0.971 1.028 0.971 0
##
## Random effects:
## Name Model
## seaDist RW1 model
## field RGeneric2
##
## Model hyperparameters:
## mean sd 0.025quant
## Precision parameter for the Gamma observations 13.191 0.854 11.576
## Precision for seaDist 8423.690 2043.989 5256.632
## Theta1 for field -0.887 0.288 -1.537
## Theta2 for field 1.009 0.133 0.719
## Theta3 for field -1.064 0.182 -1.469
## 0.5quant 0.975quant mode
## Precision parameter for the Gamma observations 13.168 14.942 13.130
## Precision for seaDist 8178.281 13686.509 7541.411
## Theta1 for field -0.854 -0.240 -0.917
## Theta2 for field 0.999 1.299 1.023
## Theta3 for field -1.082 -0.656 -1.045
##
## Marginal log-Likelihood: -1262.88
## 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)')
```

Finally, similarly to the order of the rational approximation, one can check the order with the `rational.type()`

function, and assign a new type with the `rational.type<-()`

function.

`rational.type(rspde_model)`

`## [1] "chebfun"`

`rational.type(rspde_model_brasil)`

`## [1] "brasil"`

Let us change the type of the rational approximation on the model with rational approximation of order 3:

```
rational.type(rspde_model_order_3) <- "brasil"
.3 <- y ~ -1 + Intercept + f(seaDist, model = "rw1") +
f.sf(field, model = rspde_model_order_3)
<- inla(f.s.3,
rspde_fit_order_3_brasil family = "Gamma", data = inla.stack.data(stk.dat.3),
verbose = FALSE,
control.inla = list(int.strategy = "eb"),
control.predictor = list(A = inla.stack.A(stk.dat.3), compute = TRUE),
inla.mode = "experimental"
)
```

Let us get the summary:

`summary(rspde_fit_order_3_brasil)`

```
##
## Call:
## c("inla.core(formula = formula, family = family, contrasts = contrasts,
## ", " data = data, quantiles = quantiles, E = E, offset = offset, ", "
## scale = scale, weights = weights, Ntrials = Ntrials, strata = strata,
## ", " lp.scale = lp.scale, link.covariates = link.covariates, verbose =
## verbose, ", " lincomb = lincomb, selection = selection, control.compute
## = control.compute, ", " control.predictor = control.predictor,
## control.family = control.family, ", " control.inla = control.inla,
## control.fixed = control.fixed, ", " control.mode = control.mode,
## control.expert = control.expert, ", " control.hazard = control.hazard,
## control.lincomb = control.lincomb, ", " control.update =
## control.update, control.lp.scale = control.lp.scale, ", "
## control.pardiso = control.pardiso, only.hyperparam = only.hyperparam,
## ", " inla.call = inla.call, inla.arg = inla.arg, num.threads =
## num.threads, ", " blas.num.threads = blas.num.threads, keep = keep,
## working.directory = working.directory, ", " silent = silent, inla.mode
## = inla.mode, safe = FALSE, debug = debug, ", " .parent.frame =
## .parent.frame)")
## Time used:
## Pre = 3.42, Running = 23.1, Post = 0.0695, Total = 26.6
## Fixed effects:
## mean sd 0.025quant 0.5quant 0.975quant mode kld
## Intercept 0.486 0.013 0.46 0.486 0.512 0.486 0
##
## Random effects:
## Name Model
## seaDist RW1 model
## field RGeneric2
##
## Model hyperparameters:
## mean sd 0.025quant
## Precision parameter for the Gamma observations 13.174 0.903 11.508
## Precision for seaDist 9588.188 7419.754 2439.397
## Theta1 for field -1.735 0.606 -2.981
## Theta2 for field 1.290 0.233 0.835
## Theta3 for field 0.792 0.565 -0.323
## 0.5quant 0.975quant mode
## Precision parameter for the Gamma observations 13.134 15.065 13.040
## Precision for seaDist 7497.709 29340.135 4963.094
## Theta1 for field -1.727 -0.538 -1.687
## Theta2 for field 1.287 1.764 1.273
## Theta3 for field 0.785 1.954 0.747
##
## Marginal log-Likelihood: -1262.95
## 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)')
```

The `rspde.matern()`

function has an optimized version, which is the default and a non-optimized version.

It will take less time to fit the optimized object than to fit the non-optimized one. However it requires a sparsity analysis when creating the model object. If the size of the data is small it might happen that the time to analyze the sparsity plus the time to fit the model is larger than the time to fit non-optimized model, which does not require a sparsity analysis when creating the model object.

There is also another option for the optimized model, which is the `sharp`

argument. The `sharp`

argument is only used if `optimize`

is `TRUE`

. If `sharp=TRUE`

the time to fit the model is less than the time to fit a model in which we have `sharp=FALSE`

. However, the sparsity analysis takes more time when `sharp=TRUE`

than when `sharp=FALSE`

.

Let us then create the three model objects with `rspde_order=1`

for the precipitation data and compare the times.

We begin by creating the \(A\) matrix, index and `inla.stack`

object that all the three models will use:

```
.1 <- rspde.make.A(mesh = prmesh, loc = coords, rspde_order = 1)
Abar.1 <- rspde.make.index(name = "field", mesh = prmesh, rspde_order = 1)
mesh.index.1 <- inla.stack(
stk.datdata = list(y = Y), A = list(Abar.1, 1), tag = "est",
effects = list(
c(
.1,
mesh.indexlist(Intercept = 1)
),list(
long = inla.group(coords[, 1]),
lat = inla.group(coords[, 2]),
seaDist = inla.group(seaDist)
)
) )
```

Now, let us create the three models and compute the time it took to create the model objects:

```
# Creating the optimized and sharp model
<- Sys.time()
start_time <- rspde.matern(mesh = prmesh,
rspde_model_opt_sharp rspde_order = 1
)<- Sys.time()
end_time <- end_time - start_time
time_opt_sharp
# Creating the optimized non-sharp model
<- Sys.time()
start_time <- rspde.matern(mesh = prmesh,
rspde_model_opt_notsharp rspde_order = 1,
sharp = FALSE
)<- Sys.time()
end_time <- end_time - start_time
time_opt_notsharp
# Creating the non-optimized model
<- Sys.time()
start_time <- rspde.matern(mesh = prmesh,
rspde_model_nonopt rspde_order = 1,
optimize = FALSE
)<- Sys.time()
end_time <- end_time - start_time time_nonopt
```

Now, let us fit the models:

```
<- y ~ -1 + Intercept + f(seaDist, model = "rw1") +
f.s.opt.sharp f(field, model = rspde_model_opt_sharp)
<- inla(f.s.opt.sharp,
rspde_fit_opt_sharp family = "Gamma",
data = inla.stack.data(stk.dat.1),
verbose = FALSE,
control.inla = list(int.strategy = "eb"),
control.predictor = list(
A = inla.stack.A(stk.dat.1),
compute = FALSE
inla.mode = "experimental"
),
)
<- y ~ -1 + Intercept + f(seaDist, model = "rw1") +
f.s.opt.notsharp f(field, model = rspde_model_opt_notsharp)
<- inla(f.s.opt.notsharp,
rspde_fit_opt_notsharp family = "Gamma",
data = inla.stack.data(stk.dat.1),
verbose = FALSE,
control.inla = list(int.strategy = "eb"),
control.predictor = list(
A = inla.stack.A(stk.dat.1),
compute = FALSE
),inla.mode = "experimental"
)
<- y ~ -1 + Intercept + f(seaDist, model = "rw1") +
f.s.nonopt f(field, model = rspde_model_nonopt)
<- inla(f.s.nonopt,
rspde_fit_nonopt family = "Gamma",
data = inla.stack.data(stk.dat.1),
verbose = FALSE,
control.inla = list(int.strategy = "eb"),
control.predictor = list(
A = inla.stack.A(stk.dat.1),
compute = FALSE
),inla.mode = "experimental"
)
```

Finally, let us compare the times:

```
`Time to create model` <- c(time_opt_sharp, time_opt_notsharp, time_nonopt)
`Time to fit the model` <- c(
$cpu.used[[2]],
rspde_fit_opt_sharp$cpu.used[[2]],
rspde_fit_opt_notsharp$cpu.used[[2]]
rspde_fit_nonopt
)`Total time` <- `Time to create model` + `Time to fit the model`
<- c("Opt & sharp", "Opt & not sharp", "Non-opt")
Model <- data.frame(
result_table `Time to create model`,
Model, `Time to fit the model`,
`Total time`
) result_table
```

```
## Model Time.to.create.model Time.to.fit.the.model Total.time
## 1 Opt & sharp 1.452124 secs 14.63862 16.09074 secs
## 2 Opt & not sharp 2.534471 secs 17.61556 20.15003 secs
## 3 Non-opt 0.959187 secs 25.96482 26.92401 secs
```

Therefore, we see that for this example one benefits from using the optimized version.

Hofreither, Clemens. 2021. “An Algorithm for Best Rational Approximation Based on Barycentric Rational Interpolation.” *Numerical Algorithms* 88 (1): 365–88.

Lindgren, Finn, Håvard Rue, and Johan Lindström. 2011. “An Explicit Link Between Gaussian Fields and Gaussian Markov Random Fields: The Stochastic Partial Differential Equation Approach.” *Journal of the Royal Statistical Society. Series B. Statistical Methodology* 73 (4): 423–98.

Xiong, Zhen, Alexandre B. Simas, and David Bolin. 2022. “Covariance-Based Rational Approximations of Fractional SPDEs for Computationally Efficient Bayesian Inference.” *arXiv:2209.04670*.