FRK | R Documentation |
The Spatial Random Effects (SRE) model is the central object in FRK. The function FRK()
provides a wrapper for the construction and estimation of the SRE object from data, using the functions SRE()
(the object constructor) and SRE.fit()
(for fitting it to the data). Please see SRE-class
for more details on the SRE object's properties and methods.
FRK(
f,
data,
basis = NULL,
BAUs = NULL,
est_error = TRUE,
average_in_BAU = TRUE,
sum_variables = NULL,
normalise_wts = TRUE,
fs_model = "ind",
vgm_model = NULL,
K_type = c("block-exponential", "precision", "unstructured"),
n_EM = 100,
tol = 0.01,
method = c("EM", "TMB"),
lambda = 0,
print_lik = FALSE,
response = c("gaussian", "poisson", "gamma", "inverse-gaussian", "negative-binomial",
"binomial"),
link = c("identity", "log", "sqrt", "logit", "probit", "cloglog", "inverse",
"inverse-squared"),
optimiser = nlminb,
fs_by_spatial_BAU = FALSE,
known_sigma2fs = NULL,
taper = NULL,
simple_kriging_fixed = FALSE,
...
)
SRE(
f,
data,
basis,
BAUs,
est_error = TRUE,
average_in_BAU = TRUE,
sum_variables = NULL,
normalise_wts = TRUE,
fs_model = "ind",
vgm_model = NULL,
K_type = c("block-exponential", "precision", "unstructured"),
normalise_basis = TRUE,
response = c("gaussian", "poisson", "gamma", "inverse-gaussian", "negative-binomial",
"binomial"),
link = c("identity", "log", "sqrt", "logit", "probit", "cloglog", "inverse",
"inverse-squared"),
include_fs = TRUE,
fs_by_spatial_BAU = FALSE,
...
)
SRE.fit(
object,
n_EM = 100L,
tol = 0.01,
method = c("EM", "TMB"),
lambda = 0,
print_lik = FALSE,
optimiser = nlminb,
known_sigma2fs = NULL,
taper = NULL,
simple_kriging_fixed = FALSE,
...
)
## S4 method for signature 'SRE'
predict(
object,
newdata = NULL,
obs_fs = FALSE,
pred_time = NULL,
covariances = FALSE,
nsim = 400,
type = "mean",
k = NULL,
percentiles = c(5, 95),
kriging = "simple"
)
## S4 method for signature 'SRE'
logLik(object)
## S4 method for signature 'SRE'
nobs(object, ...)
## S4 method for signature 'SRE'
coef(object, ...)
## S4 method for signature 'SRE'
coef_uncertainty(
object,
percentiles = c(5, 95),
nsim = 400,
random_effects = FALSE
)
simulate(object, newdata = NULL, nsim = 400, conditional_fs = FALSE, ...)
## S4 method for signature 'SRE'
fitted(object, ...)
## S4 method for signature 'SRE'
residuals(object, type = "pearson")
## S4 method for signature 'SRE'
AIC(object, k = 2)
## S4 method for signature 'SRE'
BIC(object)
f |
|
data |
list of objects of class |
basis |
object of class |
BAUs |
object of class |
est_error |
(applicable only if |
average_in_BAU |
if |
sum_variables |
if |
normalise_wts |
if |
fs_model |
if "ind" then the fine-scale variation is independent at the BAU level. Only the independent model is allowed for now, future implementation will include CAR/ICAR (in development) |
vgm_model |
(applicable only if |
K_type |
the parameterisation used for the basis-function covariance matrix, |
n_EM |
(applicable only if |
tol |
(applicable only if |
method |
parameter estimation method to employ. Currently "EM" and "TMB" are supported |
lambda |
(applicable only if |
print_lik |
(applicable only if |
response |
string indicating the assumed distribution of the response variable. It can be "gaussian", "poisson", "negative-binomial", "binomial", "gamma", or "inverse-gaussian". If |
link |
string indicating the desired link function. Can be "log", "identity", "logit", "probit", "cloglog", "reciprocal", or "reciprocal-squared". Note that only sensible link-function and response-distribution combinations are permitted. If |
optimiser |
(applicable only if |
fs_by_spatial_BAU |
(applicable only in a spatio-temporal setting and if |
known_sigma2fs |
known value of the fine-scale variance parameter. If |
taper |
positive numeric indicating the strength of the covariance/partial-correlation tapering. Only applicable if |
simple_kriging_fixed |
commit to simple kriging at the fitting stage? If |
... |
other parameters passed on to |
normalise_basis |
flag indicating whether to normalise the basis functions so that they reproduce a stochastic process with approximately constant variance spatially |
include_fs |
(applicable only if |
object |
object of class |
newdata |
object of class |
obs_fs |
flag indicating whether the fine-scale variation sits in the observation model (systematic error; indicated by |
pred_time |
vector of time indices at which prediction will be carried out. All time points are used if this option is not specified |
covariances |
(applicable only for |
nsim |
number of i) MC samples at each location when using |
type |
(applicable only if |
k |
(applicable only if |
percentiles |
(applicable only if |
kriging |
(applicable only if |
random_effects |
logical; if set to true, confidence intervals will also be provided for the random effects random effects γ (see '?SRE' for details on these random effects) |
conditional_fs |
condition on the fitted fine-scale random effects? |
The following details provide a summary of the model and basic workflow used in FRK. See Zammit-Mangion and Cressie (2021) and Sainsbury-Dale, Zammit-Mangion and Cressie (2023) for further details.
Model description
The hierarchical model implemented in FRK is a spatial generalised linear mixed model (GLMM), which may be summarised as
where Zj denotes a datum, EF
corresponds to a probability
distribution in the exponential family with dispersion parameter \psi
,
μZ is the vector containing the conditional expectations of each datum,
CZ is a matrix which aggregates the BAU-level mean process over the observation supports,
μ is the mean process evaluated over the BAUs, g
is a link function,
Y is a latent Gaussian process evaluated over the BAUs,
the matrix T contains regression covariates at the BAU level associated with the fixed effects α,
the matrix G is a design matrix at the BAU level associated with random effects γ,
the matrix S contains basis-function evaluations over the BAUs associated with basis-function random effects η, and ξ is a vector containing fine-scale variation at the BAU level.
The prior distribution of the random effects, γ, is a mean-zero multivariate Gaussian with diagonal covariance matrix, with each group of random effects associated with its own variance parameter. These variance parameters are estimated during model fitting.
The prior distribution of the basis-function coefficients, η, is formulated
using either a covariance matrix K or precision matrix Q, depending on the argument
K_type
. The parameters of these matrices are estimated during model fitting.
The prior distribution of the fine-scale random effects, ξ, is a mean-zero multivariate Gaussian with diagonal covariance matrix,
Σξ.
By default, Σξ = σ2ξV, where V is a
known, positive-definite diagonal matrix whose elements are provided in the
field fs
in the BAUs. In the absence of problem
specific fine-scale information, fs
can simply be set to 1, so that
V = I.
In a spatio-temporal setting, another model for Σξ
can be used by setting fs_by_spatial_BAU = TRUE
, in which case each
spatial BAU is associated with its own fine-scale variance parameter
(see Sainsbury-Dale et al., 2023, Sec. 2.6).
In either case, the fine-scale variance parameter(s) are either estimated during model fitting, or provided by
the user via the argument known_sigma2fs
.
Gaussian data model with an identity link function
When the data is Gaussian, and an identity link function is used, the preceding model simplifies considerably: Specifically,
where Z is the data vector, δ is systematic error at the BAU level, and e represents independent measurement error.
Distributions with size parameters
Two distributions considered in this framework, namely the binomial distribution and the negative-binomial distribution, have an assumed-known ‘size’ parameter and a ‘probability of success’ parameter. Given the vector of size parameters associated with the data, kZ, the parameterisation used in FRK assumes that Zj represents either the number of ‘successes’ from kZj trials (binomial data model) or that it represents the number of failures before kZj successes (negative-binomial data model).
When model fitting, the BAU-level size parameters
k are needed.
The user must supply these size parameters either through the data or though
the BAUs. How this is done depends on whether the data are areal or
point-referenced, and whether they overlap common BAUs or not.
The simplest case is when each observation is associated with a single BAU
only and each BAU is associated with at most one observation support; then,
it is straightforward to assign elements from
kZ to elements of
k and vice-versa, and so the user may provide either
k or
kZ.
If each observation is associated with
exactly one BAU, but some BAUs are associated with multiple observations,
the user must provide kZ, which is used to infer
k ; in
particular,
ki = Σj∈ai kZj ,
i = 1, \dots, N
, where
ai
denotes the indices of the observations associated with BAU
Ai.
If one or more observations encompass multiple BAUs,
k
must be provided with the BAUs, as we cannot meaningfully
distribute
kZj
over multiple BAUs associated with datum
Zj.
In this case, we infer
kZ using
kZj = Σi∈cj ki ,
j = 1, \dots, m
, where
cj
denotes the indices of the BAUs associated with observation
Zj.
Set-up
SRE()
constructs a spatial random effects model from the user-defined formula, data object (a list
of spatially-referenced data), basis functions and a set of Basic Areal Units (BAUs).
It first takes each object in the list data
and maps it to the BAUs – this
entails binning point-referenced data into the BAUs (and averaging within the
BAU if average_in_BAU = TRUE
), and finding which BAUs are associated
with observations. Following this, the incidence matrix, CZ, is
constructed.
All required matrices (S, T, CZ, etc.)
are constructed within SRE()
and returned as part of the SRE
object.
SRE()
also intitialises the parameters and random effects using
sensible defaults. Please see
SRE-class
for more details.
The functions observed_BAUs()
and unobserved_BAUs()
return the
indices of the observed and unobserved BAUs, respectively.
To include random effects in FRK please follow the notation as used in lme4.
For example, to add a random effect according to a variable fct
, simply add
'(1 | fct)
' to the formula used when calling FRK()
or SRE()
.
Note that FRK only supports simple, uncorrelated random effects and
that a formula term such as '(1 + x | fct)
' will throw an error
(since in lme4 parlance this implies that the random effect corresponding to
the intercept and the slope are correlated). If one wishes to model a an intercept and linear trend
for each level in fct
,
then one can force the intercept and slope terms to be uncorrelated by using
the notation "(x || fct)
", which is shorthand for
"(1 | fct) + (x - 1 | x2)
".
Model fitting
SRE.fit()
takes an object of class SRE
and estimates all unknown
parameters, namely the covariance matrix K, the fine scale variance
(σ2ξ or σ2δ, depending on whether Case 1
or Case 2 is chosen; see the vignette "FRK_intro") and the regression parameters α.
There are two methods of model fitting currently implemented, both of which
implement maximum likelihood estimation (MLE).
This method is implemented only
for Gaussian data and an identity link function.
The log-likelihood (given in Section 2.2 of the vignette) is evaluated at each
iteration at the current parameter estimate. Optimation continues until
convergence is reached (when the log-likelihood stops changing by more than
tol
), or when the number of EM iterations reaches n_EM
.
The actual computations for the E-step and M-step are relatively straightforward.
The E-step contains an inverse of an r \times r
matrix, where r
is the number of basis functions which should not exceed 2000. The M-step
first updates the matrix K, which only depends on the sufficient
statistics of the basis-function coefficients η. Then, the regression
parameters α are updated and a simple optimisation routine
(a line search) is used to update the fine-scale variance
σ2δ or σ2ξ. If the fine-scale errors and
measurement random errors are homoscedastic, then a closed-form solution is
available for the update of σ2ξ or σ2δ.
Irrespectively, since the updates of α, and σ2δ
or σ2ξ, are dependent, these two updates are iterated until
the change in σ2. is no more than 0.1%.
TMB
. This method is implemented for
all available data models and link functions offered by FRK. Furthermore,
this method facilitates the inclusion of many more basis function than possible
with the EM algorithm (in excess of 10,000). TMB
applies
the Laplace approximation to integrate out the latent random effects from the
complete-data likelihood. The resulting approximation of the marginal
log-likelihood, and its derivatives with respect to the parameters, are then
called from within R
using the optimising function optimiser
(default nlminb()
).
Wrapper for set-up and model fitting
The function FRK()
acts as a wrapper for the functions SRE()
and
SRE.fit()
. An added advantage of using FRK()
directly is that it
automatically generates BAUs and basis functions based on the data. Hence
FRK()
can be called using only a list of data objects and an R
formula, although the R
formula can only contain space or time as
covariates when BAUs are not explicitly supplied with the covariate data.
Prediction
Once the parameters are estimated, the SRE
object is passed onto the
function predict()
in order to carry out optimal predictions over the
same BAUs used to construct the SRE model with SRE()
. The first part
of the prediction process is to construct the matrix S over the
prediction polygons. This is made computationally efficient by treating the
prediction over polygons as that of the prediction over a combination of BAUs.
This will yield valid results only if the BAUs are relatively small. Once the
matrix S is found, a standard Gaussian inversion (through conditioning)
using the estimated parameters is used for prediction.
predict()
returns the BAUs (or an object specified in newdata
),
which are of class SpatialPixelsDataFrame
, SpatialPolygonsDataFrame
,
or STFDF
, with predictions and
uncertainty quantification added.
If method
= "TMB", the returned object is a list, containing the
previously described predictions, and a list of Monte Carlo samples.
The predictions and uncertainties can be easily plotted using plot
or spplot
from the package sp
.
Zammit-Mangion, A. and Cressie, N. (2021). FRK: An R package for spatial and spatio-temporal prediction with large datasets. Journal of Statistical Software, 98(4), 1-48. doi:10.18637/jss.v098.i04.
Sainsbury-Dale, M. and Zammit-Mangion, A. and Cressie, N. (2024) Modelling Big, Heterogeneous, Non-Gaussian Spatial and Spatio-Temporal Data using FRK. Journal of Statistical Software, 108(10), 1–39. doi:10.18637/jss.v108.i10.
SRE-class
for details on the SRE object internals,
auto_basis
for automatically constructing basis functions, and
auto_BAUs
for automatically constructing BAUs.
library("FRK")
library("sp")
## Generate process and data
m <- 250 # Sample size
zdf <- data.frame(x = runif(m), y= runif(m)) # Generate random locs
zdf$Y <- 3 + sin(7 * zdf$x) + cos(9 * zdf$y) # Latent process
zdf$z <- rnorm(m, mean = zdf$Y) # Simulate data
coordinates(zdf) = ~x+y # Turn into sp object
## Construct BAUs and basis functions
BAUs <- auto_BAUs(manifold = plane(), data = zdf,
nonconvex_hull = FALSE, cellsize = c(0.03, 0.03), type="grid")
BAUs$fs <- 1 # scalar fine-scale covariance matrix
basis <- auto_basis(manifold = plane(), data = zdf, nres = 2)
## Construct the SRE model
S <- SRE(f = z ~ 1, list(zdf), basis = basis, BAUs = BAUs)
## Fit with 2 EM iterations so to take as little time as possible
S <- SRE.fit(S, n_EM = 2, tol = 0.01, print_lik = TRUE)
## Check fit info, final log-likelihood, and estimated regression coefficients
info_fit(S)
logLik(S)
coef(S)
## Predict over BAUs
pred <- predict(S)
## Plot
## Not run:
plotlist <- plot(S, pred)
ggpubr::ggarrange(plotlist = plotlist, nrow = 1, align = "hv", legend = "top")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.