Title: | Spatial and Spatio-Temporal Models using 'INLA' |
---|---|
Description: | Prepare objects to implement models over spatial and spacetime domains with the 'INLA' package (<https://www.r-inla.org>). These objects contain data to for the 'cgeneric' interface in 'INLA', enabling fast parallel computations. We implemented the spatial barrier model, see Bakka et. al. (2019) <doi:10.1016/j.spasta.2019.01.002>, and some of the spatio-temporal models proposed in Lindgren et. al. (2023) <https://www.idescat.cat/sort/sort481/48.1.1.Lindgren-etal.pdf>. Details are provided in the available vignettes and from the URL bellow. |
Authors: | Elias Teixeira Krainski [cre, aut, cph] , Finn Lindgren [aut] , Haavard Rue [aut] |
Maintainer: | Elias Teixeira Krainski <[email protected]> |
License: | GPL (>=2) |
Version: | 0.1.10 |
Built: | 2024-11-05 05:44:05 UTC |
Source: | https://github.com/eliaskrainski/INLAspacetime |
Computes the auto-covariance for given coefficients.
ar2cov(a1, a2, k = 30, useC = FALSE)
ar2cov(a1, a2, k = 30, useC = FALSE)
a1 |
the first auto-regression coefficient. |
a2 |
the second auto-regression coefficient. |
k |
maximum lag for evaluating the auto-correlation. |
useC |
just a test (to use C code). |
the autocorrelation as a vector or matrix, whenever a1
or a2
are
scalar or vector.
Let the second order auto-regression model defined as
x_t + a_1 x_{t-1} + a_2 x_{t-2} = w_t
where w_t ~ N(0, 1)
.
ar2cov(c(-1.7, -1.8), 0.963, k = 5) plot(ar2cov(-1.7, 0.963), type = "o")
ar2cov(c(-1.7, -1.8), 0.963, k = 5) plot(ar2cov(-1.7, 0.963), type = "o")
Creates a precision matrix as a sparse matrix object considering the specification stated in Details.
ar2precision(n, a)
ar2precision(n, a)
n |
the size of the model. |
a |
a length three vector with the coefficients. See details. |
the precision matrix as a sparse matrix object with edge correction.
Let the second order auto-regression model be defined as
The n times n symmetric precision matrix Q for x_1, x_2, ..., x_n has the following non-zero elements:
ar2precision(7, c(1, -1.5, 0.9))
ar2precision(7, c(1, -1.5, 0.9))
f()
call.Define a spacetime model object for the f()
call.
barrierModel.define( mesh, barrier.triangles, prior.range, prior.sigma, range.fraction = 0.1, constr = FALSE, debug = FALSE, verbose = FALSE, useINLAprecomp = TRUE, libpath = NULL )
barrierModel.define( mesh, barrier.triangles, prior.range, prior.sigma, range.fraction = 0.1, constr = FALSE, debug = FALSE, verbose = FALSE, useINLAprecomp = TRUE, libpath = NULL )
mesh |
a spatial mesh |
barrier.triangles |
a integer vector to specify which triangles centers are in the barrier domain, or a list with integer vector if more than one. |
prior.range |
numeric vector containing U and a to define the probability statements P(range < U) = a used to setup the PC-prior for range. If a = 0 then U is taken to be the fixed value for the range. |
prior.sigma |
numeric vector containing U and a to define the probability statements P(range > U) = a used to setup the PC-prior for sigma. If a = 0 then U is taken to be the fixed value for sigma. |
range.fraction |
numeric to specify the fraction of the range for the barrier domain. Default value is 0.1. This has to be specified with care in order to have it small enough to make it act as barrier but not too small in order to prevent numerical issues. |
constr |
logical to indicate if the integral of the field over the domain is to be constrained to zero. Default value is FALSE. |
debug |
logical indicating if to run in debug mode. |
verbose |
logical indicating if to print parameter values. |
useINLAprecomp |
logical indicating if is to be used
shared object pre-compiled by INLA.
This will not be considered if the argument
|
libpath |
string to the shared object. Default is NULL. |
See the paper.
objects to be used in the f() formula term in INLA.
Return an inlabru
bru_mapper
object that can be used for computing
model matrices for the space-time model components. The bru_get_mapper()
function is called by the inlabru
methods to automatically obtain the
needed mapper object (from inlabru
2.7.0.9001
; before that, use
mapper = bru_get_mapper(model)
explicitly).
bru_get_mapper.stModel_cgeneric(model, ...)
bru_get_mapper.stModel_cgeneric(model, ...)
model |
The model object (of class |
... |
Unused. |
A bru_mapper
object of class bru_mapper_multi
with
sub-mappers space
and time
based on the model smesh
and tmesh
or mesh
objects.
This computes the correlation function as derived in Matern model, see Matern (1960) eq. (2.4.7). For nu=1, see Whittle (1954) eq. (68). For the limiting case of nu=0, see Besag (1981) eq. (14-15).
cWhittleMatern(x, range, nu, kappa = sqrt(8 * nu)/range)
cWhittleMatern(x, range, nu, kappa = sqrt(8 * nu)/range)
x |
distance. |
range |
practical range (our prefered parametrization) given as range = sqrt(8 * nu) / kappa, where kappa is the scale parameter in the specialized references. |
nu |
process smoothness parameter. |
kappa |
scale parameter, commonly considered in the specialized literature. |
the correlation.
Whittle, P. (1954) On Stationary Processes in the Plane. Biometrika, Vol. 41, No. 3/4, pp. 434-449. http://www.jstor.org/stable/2332724
Matern, B. (1960) Spatial Variation: Stochastic models and their application to some problems in forest surveys and other sampling investigations. PhD Thesis.
Besag, J. (1981) On a System of Two-Dimensional Recurrence Equations. JRSS-B, Vol. 43 No. 3, pp. 302-309. https://www.jstor.org/stable/2984940
plot(function(x) cWhittleMatern(x, 1, 5), bty = "n", las = 1, xlab = "Distance", ylab = "Correlation" ) plot(function(x) cWhittleMatern(x, 1, 1), add = TRUE, lty = 2) plot(function(x) cWhittleMatern(x, 1, 0.5), add = TRUE, lty = 3) abline(h = 0.139, lty = 3, col = gray(0.5, 0.5))
plot(function(x) cWhittleMatern(x, 1, 5), bty = "n", las = 1, xlab = "Distance", ylab = "Correlation" ) plot(function(x) cWhittleMatern(x, 1, 1), add = TRUE, lty = 2) plot(function(x) cWhittleMatern(x, 1, 0.5), add = TRUE, lty = 3) abline(h = 0.139, lty = 3, col = gray(0.5, 0.5))
Download files from the NOAA's GHCN daily data
downloadUtilFiles(data.dir, year = 2022, force = FALSE)
downloadUtilFiles(data.dir, year = 2022, force = FALSE)
data.dir |
the folder to store the files. |
year |
the year of the daily weather data. |
force |
logical indicating if it is to force the download. If FALSE each file will be downloaded if it does not exists locally yet. |
a named character vector with the local file names: daily.data, stations.all, elevation.
Function to define the boundary Earch polygon in longlat projection for a given resolution.
Earth_poly(resol = 300, crs = "+proj=moll +units=km")
Earth_poly(resol = 300, crs = "+proj=moll +units=km")
resol |
is the number of subdivisions along the latitude coordinates and half the number of subdivisions along the longitude coordinates. |
crs |
a string with the projection. Default is the Mollweide projection with units in kilometers. |
a 'st_sfc' object with the Earth polygon.
Select data from the daily dataset
ghcndSelect( gzfile, variable = c("TMIN", "TAVG", "TMAX"), station = NULL, qflag = "", verbose = TRUE, astype = as.integer )
ghcndSelect( gzfile, variable = c("TMIN", "TAVG", "TMAX"), station = NULL, qflag = "", verbose = TRUE, astype = as.integer )
gzfile |
the local filename for the daily data file file. E.g. 2023.csv.gz from the daily GHCN data repository at NCEI-NOAA, at "https://www.ncei.noaa.gov/pub/data/ghcn/daily/by_year/". Please see the references bellow. |
variable |
string with the variable name(s) to be selected |
station |
string (vector) with the station(s) to be selected |
qflag |
a string with quality control flag(s) |
verbose |
logical indicating if progress is to be printed |
astype |
function to convert data to a class, default is set to convert the data to integer. |
if more than one variable, it returns an array whose dimentions are days, stations, variables. If one variable, then it returns a matrix whose dimentions are days, stations.
The default selects TMIN, TAVG and TMAX and return it as integer because the original data is also integer with units in 10 Celcius degrees.
It can take time to execute if, for example, the data.table package is not available.
Menne, M., Durre, I., Vose, R., Gleason, B. and Houston, T. (2012) An overview of the global historical climatology network-daily database. Journal of Atmospheric and Oceanic Technology, 897–910.
This computes the area of a triangle given its three coordinates.
Heron(x, y) Area(x, y) s2trArea(tr, R = 1) flatArea(tr) Stiffness(tr)
Heron(x, y) Area(x, y) s2trArea(tr, R = 1) flatArea(tr) Stiffness(tr)
x , y
|
coordinate vectors. |
tr |
the triangle coordinates |
R |
the radius of the spherical domain |
Function used internally to compute the area of a triangle.
the area of a 2d triangle
the area of a 2d polygon
the area of a triangle in S2
the area of a triangle
the stiffness matrix for a triangle
Internal functions, not exported.
This package main purpose is to provide user friendly functions to fit temporal, spatial and space-time models using the INLA software available at www.r-inla.org as well the inlabru package available
INLAspacetime()
INLAspacetime()
opens the Vignettes directory on a browser
The 2nd order temporal matrices with boundary correction
Jmatrices(tmesh)
Jmatrices(tmesh)
tmesh |
Temporal mesh |
Temporal GMRF representation with stationary boundary conditions as in Appendix E of the paper.
return a list of temporal finite element method matrices for the supplied mesh.
Extracts the dual of a mesh object.
mesh.dual( mesh, returnclass = c("list", "sf", "sv", "SP"), mc.cores = getOption("mc.cores", 2L) )
mesh.dual( mesh, returnclass = c("list", "sf", "sv", "SP"), mc.cores = getOption("mc.cores", 2L) )
mesh |
a 2d mesh object. |
returnclass |
if 'list' return a list of polygon coordinates, if "sf" return a 'sf' sfc_multipolygon object, if "sv" return a 'terra', SpatVector object, if "SP" return a 'sp' SpatialPolygons object. |
mc.cores |
number of threads to be used. |
one of the three in 'returnclass'
Creates a mesh object. This is just a test code. For efficient, reliable and general code use the fmesher package.
mesh2d(loc, domain, max.edge, offset, SP = TRUE)
mesh2d(loc, domain, max.edge, offset, SP = TRUE)
loc |
a two column matrix with location coordinates. |
domain |
a two column matrix defining the domain. |
max.edge |
the maximum edge length. |
offset |
the length of the outer extension. |
SP |
logical indicating if the output will include the SpatialPolygons. |
a mesh object.
This is just for illustration purpose and one should consider the efficient function available a the INLA package.
Illustrative code for Finite Element matrices of a mesh in 2d domain.
Illustrative code for Finite Element matrices when some triangles are in a barrier domain.
mesh2fem(mesh, order = 2, barrier.triangles = NULL) mesh2fem.barrier(mesh, barrier.triangles = NULL)
mesh2fem(mesh, order = 2, barrier.triangles = NULL) mesh2fem.barrier(mesh, barrier.triangles = NULL)
mesh |
a 2d mesh object. |
order |
the desired order. |
barrier.triangles |
integer index to specify the triangles in the barrier domain |
a list object containing the FE matrices.
a list object containing the FE matrices for the barrier problem.
Creates a projector matrix object.
mesh2projector( mesh, loc = NULL, lattice = NULL, xlim = NULL, ylim = NULL, dims = c(100, 100) )
mesh2projector( mesh, loc = NULL, lattice = NULL, xlim = NULL, ylim = NULL, dims = c(100, 100) )
mesh |
a 2d mesh object. |
loc |
a two columns matrix with the locations to project for. |
lattice |
Unused; feature not supported by this illustration. |
xlim , ylim
|
vector with the boundary limits. |
dims |
the number of subdivisions over each boundary limits. |
the projector matrix as a list with sparse matrix object at x$proj$A
..
This is just for illustration purpose and one should consider the
efficient functions available in the INLA and inlabru packages,
e.g. inlabru::fm_evaluator
.
Detect outliers in a time series considering the raw data and a smoothed version of it.
outDetect(x, weights = NULL, ff = c(7, 7))
outDetect(x, weights = NULL, ff = c(7, 7))
x |
numeric vector |
weights |
non-increasing numeric vector used as weights for
computing a smoothed vector as a rooling window average.
Default is null and then |
ff |
numeric length two vector with the factors used to consider how many times the standard deviation one data point is out to be considered as an outlier. |
logical vector indicating if the data is an outlier with attributes as detailed bellow.
attr(, 'm') is the mean of x.
attr(, 's') is the standard devation of x.
attr(, 'ss') is the standard deviation for
the smoothed data that is defined as
Both s
and ss
are used to define outliers if
or
attr(, 'xs') the smoothed time series
Functions to help converting from/to user/internal parametrization. The internal parameters are 'gamma_s, 'gamma_t', 'gamma_E' The user parameters are 'r_s', 'r_t', 'sigma'
Convert from user parameters to SPDE parameters
Convert from SPDE parameters to user parameters
lgsConstant(lg.s, alpha, smanifold) params2gammas( lparams, alpha.t, alpha.s, alpha.e, smanifold = "R2", verbose = FALSE ) gammas2params(lgammas, alpha.t, alpha.s, alpha.e, smanifold = "R2")
lgsConstant(lg.s, alpha, smanifold) params2gammas( lparams, alpha.t, alpha.s, alpha.e, smanifold = "R2", verbose = FALSE ) gammas2params(lgammas, alpha.t, alpha.s, alpha.e, smanifold = "R2")
lg.s |
the logarithm of the SPDE parameter |
alpha |
the resulting spatial order. |
smanifold |
spatial domain manifold, which could be "S1", "S2", "R1", "R2" and "R3". |
lparams |
log(spatial range, temporal range, sigma) |
alpha.t |
temporal order of the SPDE |
alpha.s |
spatial order of the spatial differential operator in the non-separable part. |
alpha.e |
spatial order of the spatial differential operator in the separable part. |
verbose |
logical if it is to print internal variables |
lgammas |
numeric of length 3 with
|
See equation (23) in the paper.
See equations (19), (20) and (21) in the paper.
See equations (19), (20) and (21) in the paper.
the part of sigma
from the spatial constant and \gamma_s
.
log(gamma.s, gamma.t, gamma.e)
log(spatial range, temporal range, sigma)
params2gammas(log(c(1, 1, 1)), 1, 2, 1, "R2") gammas2params(log(c(0, 0, 0)), 1, 2, 1, "R2")
params2gammas(log(c(1, 1, 1)), 1, 2, 1, "R2") gammas2params(log(c(0, 0, 0)), 1, 2, 1, "R2")
Creates a precision matrix as a sparse matrix object. For general code look at the functions in the INLA package.
spde2precision(kappa, fem, alpha)
spde2precision(kappa, fem, alpha)
kappa |
the scale parameter. |
fem |
a list containing the Finite Element matrices. |
alpha |
the smoothness parameter. |
the precision matrix as a sparse matrix object.
This is just for illustration purpose and one should consider the efficient function available a the INLA package.
Extracts dic, waic and log-cpo from an output returned by the inla function from the INLA package or by the bru function from the inlabru package, and computes log-po, mse, mae, crps and scrps for a given input. A summary is applied considering the user imputed function, which by default is the mean.
stats.inla(m, i = NULL, y, fsummarize = mean)
stats.inla(m, i = NULL, y, fsummarize = mean)
m |
an inla output object. |
i |
an index to subset the estimated values. |
y |
observed to compare against. |
fsummarize |
the summary function,
the default is |
A named numeric vector with the extracted statistics.
It assumes Gaussian posterior predictive distributions!
Considering the defaults, for n observations,
, we have
. dic
where is the dic computed for observation i.
. waic
where is the waic computed for observation i.
. lcpo
where is the cpo computed for observation i.
For the log-po, crps, and scrps scores it assumes a
Gaussian predictive distribution for each observation
which the following definitions:
,
is the posterior mean for the linear predictor,
,
is the observation posterior mean,
is the posterior variance of the
linear predictor for
.
Then we consider the density of a standard
Gaussian variable and
the corresponding
Cumulative Probability Distribution.
. lpo
. crps
where
. scrps
where
All the scores are negatively oriented which means that smaller scores are better.
Held, L. and Schrödle, B. and Rue, H. (2009). Posterior and Cross-validatory Predictive Checks: A Comparison of MCMC and INLA. Statistical Modelling and Regression Structures pp 91–110. https://link.springer.com/chapter/10.1007/978-3-7908-2413-1_6.
Bolin, D. and Wallin, J. (2022) Local scale invariance and robustness of proper scoring rules. Statistical Science. doi:10.1214/22-STS864.
To check unusual low/high variance segments
stdSubs(x, nsub = 12, fs = 15)
stdSubs(x, nsub = 12, fs = 15)
x |
numeric vector |
nsub |
number for the segments length |
fs |
numeric to use for detecting too hight or too low local standard deviations. |
logical indicating if any of the
st
are fs
times lower/higher the average
of st
, where is returned as an attribute:
attr(, 'st') numeric vector with the standard deviation at each segment of the data.
To visualize time series over space.
stlines( stdata, spatial, group = NULL, nmax.group = NULL, xscale = 1, yscale = 1, colour = NULL, ... ) stpoints( stdata, spatial, group = NULL, nmax.group = NULL, xscale = 1, yscale = 1, colour = NULL, ... )
stlines( stdata, spatial, group = NULL, nmax.group = NULL, xscale = 1, yscale = 1, colour = NULL, ... ) stpoints( stdata, spatial, group = NULL, nmax.group = NULL, xscale = 1, yscale = 1, colour = NULL, ... )
stdata |
matrix with the data, each column is a location. |
spatial |
an object with one of class defined in the sp package. |
group |
an integer vector indicating to which spatial unit each time series belongs to. Default is NULL and them it is assumed that each time series belongs o each spatial unit. |
nmax.group |
an integer indicating the maximum number of time series to be plotted over each spatial unit. Default is NULL, so all will be drawn. |
xscale |
numeric to define a scaling factor in the horizontal direction. |
yscale |
numeric to define a scaling factor in the vertical direction. |
colour |
color (may be a vector, one for each time series).
Default is NULL and it will generate colors considering the
average of each time series.
These automatic colors are defined using the |
... |
further arguments to be passed for the lines function. |
Scaling the times series is needed before drawing it over the map.
The area of the bounding box for the spatial object
divided by the number of locations is the standard scaling factor.
This is further multiplied by the user given xcale
and yscale
.
add lines to an existing plot
stlines()
: each time series over the map centered at the location.
stpoints()
: each time series over the map centered at the location.
if there are too many geographical locations, it will not look good
f()
call.Define a spacetime model object for the f()
call.
stModel.define( smesh, tmesh, model, control.priors, constr = FALSE, debug = FALSE, verbose = FALSE, useINLAprecomp = TRUE, libpath = NULL )
stModel.define( smesh, tmesh, model, control.priors, constr = FALSE, debug = FALSE, verbose = FALSE, useINLAprecomp = TRUE, libpath = NULL )
smesh |
a spatial mesh |
tmesh |
a temporal mesh |
model |
a three characters string to specify the
smoothness alpha (each one as integer) parameters.
Currently it considers the |
control.priors |
a named list with parameter priors. E.g. prior.rs, prior.rt and prior.sigma as vectors with length two (U, a) to define the corresponding PC-prior such that P(r_s<U)=a, P(r_t<U)=a or P(sigma>U)=a. If a=0 then U is taken to be the fixed value of the parameter. |
constr |
logical to indicate if the integral of the field over the domain is to be constrained to zero. Default value is FALSE. |
debug |
logical indicating if to run in debug mode. |
verbose |
logical indicating if to print parameter values. |
useINLAprecomp |
logical indicating if is to be used shared object pre-compiled by INLA. Not considered if libpath is provided. |
libpath |
string to the shared object. Default is NULL. |
See the paper.
objects to be used in the f() formula term in INLA.
This function computes all the matrices needed to build the precision matrix for spatio-temporal model, as in Lindgren et. al. (2023)
stModel.matrices(smesh, tmesh, model, constr = FALSE)
stModel.matrices(smesh, tmesh, model, constr = FALSE)
smesh |
a mesh object over the spatial domain. |
tmesh |
a mesh object over the time domain. |
model |
a string identifying the model. So far we have the following models: '102', '121', '202' and '220' models. |
constr |
logical to indicate if the integral of the field over the domain is to be constrained to zero. Default value is FALSE. |
See the paper for details.
a list containing needed objects for model definition.
'manifold' to spedify the a string with the model identification
a length three vector with the constants c1
, c2
and c3
the vector d
the matrix T
the model matrices M_1
, ..., M_m
To build the the precision matrix for a spacetime model given the temporal and the spatial meshes.
stModel.precision(smesh, tmesh, model, theta, verbose = FALSE)
stModel.precision(smesh, tmesh, model, theta, verbose = FALSE)
smesh |
a mesh object over the spatial domain. |
tmesh |
a mesh object over the time domain. |
model |
a string identifying the model. So far we have the following models: '102', '121', '202' and '220' models. |
theta |
numeric vector of length three with
|
verbose |
logical to print intermediate objects. |
a (sparse) precision matrix, as in Lindgren et. al. (2023)
Define a graph of the union of the supplied matrices and return the row ordered diagonal plus upper triangle after padding with zeroes each one so that all the returned matrices have the same pattern.
upperPadding(M, relative = FALSE)
upperPadding(M, relative = FALSE)
M |
a matrix or a list of matrices |
relative |
logical. If a list of matrices is supplied, it indicates if it is to be returned a relative index and the value for each matrix. See details. |
If relative=FALSE, each columns of 'xx' is the elements of the corresponding matrix after being padded to fill the pattern of the union graph. If relative=TRUE, each element of 'xx' would be a list with a relative index, 'r', for each non-zero elements of each matrix is returned relative to the union graph, the non-lower elements, 'x', of the corresponding matrix, and a vector, 'o', with the number of non-zero elements for each line of each resulting matrix.
If a unique matrix is given, return the upper triangle considering the 'T' representation in the Matrix package. If a list of matrices is given, return a list of two elements: 'graph' and 'xx'. The 'graph' is the union of the graph from each matrix. If relative=FALSE, 'xx' is a matrix with number of column equals the the number of matrices imputed. If relative=TRUE, it is a list of length equal the number of matrices imputed. See details.
A <- sparseMatrix( i = c(1, 1, 2, 3, 3, 5), j = c(2, 5, 3, 4, 5, 5), x = -(0:5), symmetric = TRUE ) A upperPadding(A) B <- Diagonal(nrow(A), -colSums(A)) list(a = A, a = B) upperPadding(list(a = A, b = B)) upperPadding(list(a = A, b = B), relative = TRUE)
A <- sparseMatrix( i = c(1, 1, 2, 3, 3, 5), j = c(2, 5, 3, 4, 5, 5), x = -(0:5), symmetric = TRUE ) A upperPadding(A) B <- Diagonal(nrow(A), -colSums(A)) list(a = A, a = B) upperPadding(list(a = A, b = B)) upperPadding(list(a = A, b = B), relative = TRUE)
Define a regular grid in 'Mollweide' projection, with units in kilometers.
world_grid(size = 50, domain)
world_grid(size = 50, domain)
size |
the (in kilometers) of the grid cells. |
domain |
if provided it should be an |
a 'sf' points object with the centers of a grid set within Earth (and the supplied domain)
Retrieve the map of the countries
worldMap( crs = "+proj=moll +units=km", scale = "medium", returnclass = c("sf", "sv") )
worldMap( crs = "+proj=moll +units=km", scale = "medium", returnclass = c("sf", "sv") )
crs |
a string with the projection. Default is the Mollweide projection with units in kilometers. |
scale |
The scale of map to return. Please see the help of 'ne_countries' function from the 'rnaturalearth' package. |
returnclass |
A string determining the class of the spatial object to return. Please see the help of 'ne_countries' function from the 'rnaturalearth' package. |
The land and ocean maps are obtained with the 'rnaturalearth' package.