Title: | Rational Approximations of Fractional Stochastic Partial Differential Equations |
---|---|
Description: | Functions that compute rational approximations of fractional elliptic stochastic partial differential equations. The package also contains functions for common statistical usage of these approximations. The main references for rSPDE are Bolin, Simas and Xiong (2023) <doi:10.1080/10618600.2023.2231051> for the covariance-based method and Bolin and Kirchner (2020) <doi:10.1080/10618600.2019.1665537> for the operator-based rational approximation. These can be generated by the citation function in R. |
Authors: | David Bolin [cre, aut], Alexandre Simas [aut], Finn Lindgren [ctb] |
Maintainer: | David Bolin <[email protected]> |
License: | GPL (>=3) | file LICENSE |
Version: | 2.3.3.9000 |
Built: | 2024-11-21 19:17:30 UTC |
Source: | https://github.com/davidbolin/rspde |
rSPDE
is used for approximating fractional elliptic SPDEs
where is a differential operator and
is a general fractional power.
The approximation is based on a rational approximation of the fractional operator, and allows for computationally efficient inference and simulation.
The main functions for computing rational approximation objects are:
fractional.operators()
works for general rational operators
matern.operators()
works for random fields with stationary Matern covariance functions
spde.matern.operators()
works for random fields with defined as solutions to a possibly non-stationary Matern-type SPDE model.
rspde.matern()
R-INLA implementation of the covariance-based rational approximation for random fields with stationary Matern covariance functions
Basic statistical operations such as likelihood evaluations (see
[rSPDE.loglike], [rSPDE.matern.loglike]
) and kriging
predictions (see [predict.rSPDEobj], [predict.CBrSPDEobj]
)
using the rational approximations are also implemented.
For illustration purposes, the package contains a simple FEM implementation
for models on R. For spatial models,
the FEM implementation in the R-INLA
package is recommended.
For a more detailed introduction to the package, see the rSPDE Vignettes.
Maintainer: David Bolin [email protected]
Authors:
Alexandre Simas [email protected]
Other contributors:
Finn Lindgren [email protected] [contributor]
Useful links:
rspde_lme
objectAugment accepts a model object and a dataset and adds information about each observation in the dataset. It includes
predicted values in the .fitted
column, residuals in the .resid
column, and standard errors for the fitted values in a .se.fit
column.
It also contains the New columns always begin with a . prefix to avoid overwriting columns in the original dataset.
## S3 method for class 'rspde_lme' augment( x, newdata = NULL, loc = NULL, mesh = FALSE, which_repl = NULL, se_fit = FALSE, conf_int = FALSE, pred_int = FALSE, level = 0.95, n_samples = 100, ... )
## S3 method for class 'rspde_lme' augment( x, newdata = NULL, loc = NULL, mesh = FALSE, which_repl = NULL, se_fit = FALSE, conf_int = FALSE, pred_int = FALSE, level = 0.95, n_samples = 100, ... )
x |
A |
newdata |
A |
loc |
Prediction locations. Can either be a |
mesh |
Obtain predictions for mesh nodes? The graph must have a mesh, and either |
which_repl |
Which replicates to obtain the prediction. If |
se_fit |
Logical indicating whether or not a .se.fit column should be added to the augmented output. If TRUE, it only returns a non-NA value if type of prediction is 'link'. |
conf_int |
Logical indicating whether or not confidence intervals for the fitted variable should be built. |
pred_int |
Logical indicating whether or not prediction intervals for future observations should be built. |
level |
Level of confidence and prediction intervals if they are constructed. |
n_samples |
Number of samples when computing prediction intervals. |
... |
Additional arguments. Expert use only. |
A tidyr::tibble()
with columns:
.fitted
Fitted or predicted value.
.fittedlwrconf
Lower bound of the confidence interval, if conf_int = TRUE
.fitteduprconf
Upper bound of the confidence interval, if conf_int = TRUE
.fittedlwrpred
Lower bound of the prediction interval, if pred_int = TRUE
.fitteduprpred
Upper bound of the prediction interval, if pred_int = TRUE
.fixed
Prediction of the fixed effects.
.random
Prediction of the random effects.
.resid
The ordinary residuals, that is, the difference between observed and fitted values.
.se_fit
Standard errors of fitted values, if se_fit = TRUE.
rSPDE inlabru mapper
## S3 method for class 'inla_rspde' bru_get_mapper(model, ...) ## S3 method for class 'bru_mapper_inla_rspde' ibm_n(mapper, ...) ## S3 method for class 'bru_mapper_inla_rspde' ibm_values(mapper, ...) ## S3 method for class 'bru_mapper_inla_rspde' ibm_jacobian(mapper, input, ...)
## S3 method for class 'inla_rspde' bru_get_mapper(model, ...) ## S3 method for class 'bru_mapper_inla_rspde' ibm_n(mapper, ...) ## S3 method for class 'bru_mapper_inla_rspde' ibm_values(mapper, ...) ## S3 method for class 'bru_mapper_inla_rspde' ibm_jacobian(mapper, input, ...)
model |
An |
... |
Arguments passed on to other methods |
mapper |
A |
input |
The values for which to produce a mapping matrix |
# devel version if (requireNamespace("INLA", quietly = TRUE) && requireNamespace("inlabru", quietly = TRUE)) { library(INLA) library(inlabru) set.seed(123) m <- 100 loc_2d_mesh <- matrix(runif(m * 2), m, 2) mesh_2d <- inla.mesh.2d( loc = loc_2d_mesh, cutoff = 0.05, max.edge = c(0.1, 0.5) ) sigma <- 1 range <- 0.2 nu <- 0.8 kappa <- sqrt(8 * nu) / range op <- matern.operators( mesh = mesh_2d, nu = nu, range = range, sigma = sigma, m = 2, parameterization = "matern" ) u <- simulate(op) A <- inla.spde.make.A( mesh = mesh_2d, loc = loc_2d_mesh ) sigma.e <- 0.1 y <- A %*% u + rnorm(m) * sigma.e y <- as.vector(y) data_df <- data.frame( y = y, x1 = loc_2d_mesh[, 1], x2 = loc_2d_mesh[, 2] ) rspde_model <- rspde.matern( mesh = mesh_2d, nu_upper_bound = 2 ) cmp <- y ~ Intercept(1) + field(cbind(x1,x2), model = rspde_model) rspde_fit <- bru(cmp, data = data_df) summary(rspde_fit) } # devel.tag
# devel version if (requireNamespace("INLA", quietly = TRUE) && requireNamespace("inlabru", quietly = TRUE)) { library(INLA) library(inlabru) set.seed(123) m <- 100 loc_2d_mesh <- matrix(runif(m * 2), m, 2) mesh_2d <- inla.mesh.2d( loc = loc_2d_mesh, cutoff = 0.05, max.edge = c(0.1, 0.5) ) sigma <- 1 range <- 0.2 nu <- 0.8 kappa <- sqrt(8 * nu) / range op <- matern.operators( mesh = mesh_2d, nu = nu, range = range, sigma = sigma, m = 2, parameterization = "matern" ) u <- simulate(op) A <- inla.spde.make.A( mesh = mesh_2d, loc = loc_2d_mesh ) sigma.e <- 0.1 y <- A %*% u + rnorm(m) * sigma.e y <- as.vector(y) data_df <- data.frame( y = y, x1 = loc_2d_mesh[, 1], x2 = loc_2d_mesh[, 2] ) rspde_model <- rspde.matern( mesh = mesh_2d, nu_upper_bound = 2 ) cmp <- y ~ Intercept(1) + field(cbind(x1,x2), model = rspde_model) rspde_fit <- bru(cmp, data = data_df) summary(rspde_fit) } # devel.tag
rSPDE anisotropic inlabru mapper
## S3 method for class 'inla_rspde_anisotropic2d' bru_get_mapper(model, ...)
## S3 method for class 'inla_rspde_anisotropic2d' bru_get_mapper(model, ...)
model |
An |
... |
Arguments passed on to other methods |
rSPDE space time inlabru mapper
## S3 method for class 'inla_rspde_spacetime' bru_get_mapper(model, ...)
## S3 method for class 'inla_rspde_spacetime' bru_get_mapper(model, ...)
model |
An |
... |
Arguments passed on to other methods |
This function evaluates the log-likelihood function for observations of a non-stationary Gaussian process defined as the solution to the SPDE
The observations are assumed to be generated as
, where
are
iid mean-zero Gaussian variables. The latent model is approximated using a
rational approximation of the fractional SPDE model.
construct.spde.matern.loglike( object, Y, A, sigma.e = NULL, mu = 0, user_nu = NULL, user_m = NULL, log_scale = TRUE, return_negative_likelihood = TRUE )
construct.spde.matern.loglike( object, Y, A, sigma.e = NULL, mu = 0, user_nu = NULL, user_m = NULL, log_scale = TRUE, return_negative_likelihood = TRUE )
object |
The rational SPDE approximation,
computed using |
Y |
The observations, either a vector or a matrix where the columns correspond to independent replicates of observations. |
A |
An observation matrix that links the measurement location to the finite element basis. |
sigma.e |
IF non-null, the standard deviation of the measurement noise will be kept fixed in the returned likelihood. |
mu |
Expectation vector of the latent field (default = 0). |
user_nu |
If non-null, the shape parameter will be kept fixed in the returned likelihood. |
user_m |
If non-null, update the order of the rational approximation, which needs to be a positive integer. |
log_scale |
Should the parameters be evaluated in log-scale? |
return_negative_likelihood |
Return minus the likelihood to turn the maximization into a minimization? |
The log-likelihood function. The parameters of the returned function are given in the order theta, nu, sigma.e, whenever they are available.
matern.operators()
, predict.CBrSPDEobj()
# this example illustrates how the function can be used for maximum # likelihood estimation # Sample a Gaussian Matern process on R using a rational approximation set.seed(123) sigma.e <- 0.1 n.rep <- 10 n.obs <- 100 n.x <- 51 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = n.x) fem <- rSPDE.fem1d(x) tau <- rep(0.5, n.x) nu <- 0.8 alpha <- nu + 0.5 kappa <- rep(1, n.x) # Matern parameterization # compute rational approximation op <- spde.matern.operators( loc_mesh = x, kappa = kappa, tau = tau, alpha = alpha, parameterization = "spde", d = 1 ) # Sample the model u <- simulate(op, n.rep) # Create some data obs.loc <- runif(n = n.obs, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc) noise <- rnorm(n.obs * n.rep) dim(noise) <- c(n.obs, n.rep) Y <- as.matrix(A %*% u + sigma.e * noise) # define negative likelihood function for optimization using matern.loglike mlik <- construct.spde.matern.loglike(op, Y, A) #' #The parameters can now be estimated by minimizing mlik with optim # Choose some reasonable starting values depending on the size of the domain theta0 <- log(c(1 / sqrt(var(c(Y))), sqrt(8), 0.9, 0.01)) # run estimation and display the results theta <- optim(theta0, mlik) print(data.frame( tau = c(tau[1], exp(theta$par[1])), kappa = c(kappa[1], exp(theta$par[2])), nu = c(nu, exp(theta$par[3])), sigma.e = c(sigma.e, exp(theta$par[4])), row.names = c("Truth", "Estimates") )) # SPDE parameterization # compute rational approximation op <- spde.matern.operators( kappa = kappa, tau = tau, alpha = alpha, loc_mesh = x, d = 1, parameterization = "spde" ) # Sample the model u <- simulate(op, n.rep) # Create some data obs.loc <- runif(n = n.obs, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc) noise <- rnorm(n.obs * n.rep) dim(noise) <- c(n.obs, n.rep) Y <- as.matrix(A %*% u + sigma.e * noise) # define negative likelihood function for optimization using matern.loglike mlik <- construct.spde.matern.loglike(op, Y, A) #' #The parameters can now be estimated by minimizing mlik with optim # Choose some reasonable starting values depending on the size of the domain theta0 <- log(c(1 / sqrt(var(c(Y))), sqrt(8), 0.9, 0.01)) # run estimation and display the results theta <- optim(theta0, mlik) print(data.frame( tau = c(tau[1], exp(theta$par[1])), kappa = c(kappa[1], exp(theta$par[2])), nu = c(nu, exp(theta$par[3])), sigma.e = c(sigma.e, exp(theta$par[4])), row.names = c("Truth", "Estimates") ))
# this example illustrates how the function can be used for maximum # likelihood estimation # Sample a Gaussian Matern process on R using a rational approximation set.seed(123) sigma.e <- 0.1 n.rep <- 10 n.obs <- 100 n.x <- 51 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = n.x) fem <- rSPDE.fem1d(x) tau <- rep(0.5, n.x) nu <- 0.8 alpha <- nu + 0.5 kappa <- rep(1, n.x) # Matern parameterization # compute rational approximation op <- spde.matern.operators( loc_mesh = x, kappa = kappa, tau = tau, alpha = alpha, parameterization = "spde", d = 1 ) # Sample the model u <- simulate(op, n.rep) # Create some data obs.loc <- runif(n = n.obs, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc) noise <- rnorm(n.obs * n.rep) dim(noise) <- c(n.obs, n.rep) Y <- as.matrix(A %*% u + sigma.e * noise) # define negative likelihood function for optimization using matern.loglike mlik <- construct.spde.matern.loglike(op, Y, A) #' #The parameters can now be estimated by minimizing mlik with optim # Choose some reasonable starting values depending on the size of the domain theta0 <- log(c(1 / sqrt(var(c(Y))), sqrt(8), 0.9, 0.01)) # run estimation and display the results theta <- optim(theta0, mlik) print(data.frame( tau = c(tau[1], exp(theta$par[1])), kappa = c(kappa[1], exp(theta$par[2])), nu = c(nu, exp(theta$par[3])), sigma.e = c(sigma.e, exp(theta$par[4])), row.names = c("Truth", "Estimates") )) # SPDE parameterization # compute rational approximation op <- spde.matern.operators( kappa = kappa, tau = tau, alpha = alpha, loc_mesh = x, d = 1, parameterization = "spde" ) # Sample the model u <- simulate(op, n.rep) # Create some data obs.loc <- runif(n = n.obs, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc) noise <- rnorm(n.obs * n.rep) dim(noise) <- c(n.obs, n.rep) Y <- as.matrix(A %*% u + sigma.e * noise) # define negative likelihood function for optimization using matern.loglike mlik <- construct.spde.matern.loglike(op, Y, A) #' #The parameters can now be estimated by minimizing mlik with optim # Choose some reasonable starting values depending on the size of the domain theta0 <- log(c(1 / sqrt(var(c(Y))), sqrt(8), 0.9, 0.01)) # run estimation and display the results theta <- optim(theta0, mlik) print(data.frame( tau = c(tau[1], exp(theta$par[1])), kappa = c(kappa[1], exp(theta$par[2])), nu = c(nu, exp(theta$par[3])), sigma.e = c(sigma.e, exp(theta$par[4])), row.names = c("Truth", "Estimates") ))
cross_validation
functionTrain and test splits
create_train_test_indices( data, cv_type = c("k-fold", "loo", "lpo"), k = 5, percentage = 20, number_folds = 10 )
create_train_test_indices( data, cv_type = c("k-fold", "loo", "lpo"), k = 5, percentage = 20, number_folds = 10 )
data |
A |
cv_type |
The type of the folding to be carried out. The options are |
k |
The number of folds to be used in |
percentage |
The percentage (from 1 to 99) of the data to be used to train the model. Will only be used if |
number_folds |
Number of folds to be done if |
A list with two elements, train
containing the training indices and test
containing indices.
Obtain several scores for a list of fitted models according to a folding scheme.
cross_validation( models, model_names = NULL, scores = c("mse", "crps", "scrps", "dss"), cv_type = c("k-fold", "loo", "lpo"), k = 5, percentage = 20, number_folds = 10, n_samples = 1000, return_scores_folds = FALSE, orientation_results = c("negative", "positive"), include_best = TRUE, train_test_indexes = NULL, return_train_test = FALSE, return_post_samples = FALSE, return_true_test_values = FALSE, parallelize_RP = FALSE, n_cores_RP = parallel::detectCores() - 1, true_CV = TRUE, save_settings = FALSE, print = TRUE, fit_verbose = FALSE )
cross_validation( models, model_names = NULL, scores = c("mse", "crps", "scrps", "dss"), cv_type = c("k-fold", "loo", "lpo"), k = 5, percentage = 20, number_folds = 10, n_samples = 1000, return_scores_folds = FALSE, orientation_results = c("negative", "positive"), include_best = TRUE, train_test_indexes = NULL, return_train_test = FALSE, return_post_samples = FALSE, return_true_test_values = FALSE, parallelize_RP = FALSE, n_cores_RP = parallel::detectCores() - 1, true_CV = TRUE, save_settings = FALSE, print = TRUE, fit_verbose = FALSE )
models |
A fitted model obtained from calling the |
model_names |
A vector containing the names of the models to appear in the returned |
scores |
A vector containing the scores to be computed. The options are "mse", "crps", "scrps" and "dss". By default, all scores are computed. |
cv_type |
The type of the folding to be carried out. The options are |
k |
The number of folds to be used in |
percentage |
The percentage (from 1 to 99) of the data to be used to train the model. Will only be used if |
number_folds |
Number of folds to be done if |
n_samples |
Number of samples to compute the posterior statistics to be used to compute the scores. |
return_scores_folds |
If |
orientation_results |
character vector. The options are "negative" and "positive". If "negative", the smaller the scores the better. If "positive", the larger the scores the better. |
include_best |
Should a row indicating which model was the best for each score be included? |
train_test_indexes |
A list containing two entries |
return_train_test |
Logical. Should the training and test indexes be returned? If 'TRUE' the train and test indexes will the 'train_test' element of the returned list. |
return_post_samples |
If |
return_true_test_values |
If |
parallelize_RP |
Logical. Should the computation of CRPS and SCRPS (and for some cases, DSS) be parallelized? |
n_cores_RP |
Number of cores to be used if |
true_CV |
Should a |
save_settings |
Logical. If |
print |
Should partial results be printed throughout the computation? |
fit_verbose |
Should INLA's run during cross-validation be verbose? |
A data.frame with the fitted models and the corresponding scores.
folded.matern.covariance.1d
evaluates the 1d
folded Matern covariance function over an interval .
folded.matern.covariance.1d( h, m, kappa, nu, sigma, L = 1, N = 10, boundary = c("neumann", "dirichlet", "periodic") )
folded.matern.covariance.1d( h, m, kappa, nu, sigma, L = 1, N = 10, boundary = c("neumann", "dirichlet", "periodic") )
h , m
|
Vectors of arguments of the covariance function. |
kappa |
Range parameter. |
nu |
Shape parameter. |
sigma |
Standard deviation. |
L |
The upper bound of the interval |
N |
The truncation parameter. |
boundary |
The boundary condition. The possible conditions
are |
folded.matern.covariance.1d
evaluates the 1d folded Matern
covariance function over an interval under different
boundary conditions. For periodic boundary conditions
for Neumann boundary conditions
and for Dirichlet boundary conditions:
where is the Matern covariance function:
We consider the truncation:
and
A matrix with the corresponding covariance values.
x <- seq(from = 0, to = 1, length.out = 101) plot(x, folded.matern.covariance.1d(rep(0.5, length(x)), x, kappa = 10, nu = 1 / 5, sigma = 1 ), type = "l", ylab = "C(h)", xlab = "h" )
x <- seq(from = 0, to = 1, length.out = 101) plot(x, folded.matern.covariance.1d(rep(0.5, length(x)), x, kappa = 10, nu = 1 / 5, sigma = 1 ), type = "l", ylab = "C(h)", xlab = "h" )
folded.matern.covariance.2d
evaluates the 2d
folded Matern covariance function over an interval
.
folded.matern.covariance.2d( h, m, kappa, nu, sigma, L = 1, N = 10, boundary = c("neumann", "dirichlet", "periodic", "R2") )
folded.matern.covariance.2d( h, m, kappa, nu, sigma, L = 1, N = 10, boundary = c("neumann", "dirichlet", "periodic", "R2") )
h , m
|
Vectors with two coordinates. |
kappa |
Range parameter. |
nu |
Shape parameter. |
sigma |
Standard deviation. |
L |
The upper bound of the square |
N |
The truncation parameter. |
boundary |
The boundary condition. The possible conditions
are |
folded.matern.covariance.2d
evaluates the 1d folded
Matern covariance function over an interval
under different boundary conditions.
For periodic boundary conditions
for Neumann boundary conditions
and for Dirichlet boundary conditions:
where is the Matern covariance function:
We consider the truncation for from
to
.
The correspoding covariance.
h <- c(0.5, 0.5) m <- c(0.5, 0.5) folded.matern.covariance.2d(h, m, kappa = 10, nu = 1 / 5, sigma = 1)
h <- c(0.5, 0.5) m <- c(0.5, 0.5) folded.matern.covariance.2d(h, m, kappa = 10, nu = 1 / 5, sigma = 1)
fractional.operators
is used for computing an approximation,
which can be used for inference and simulation, of the fractional SPDE
Here is a differential operator,
is
the fractional power,
is a positive scalar or vector that
scales the variance of the solution
, and
is white noise.
fractional.operators(L, beta, C, scale.factor, m = 1, tau = 1)
fractional.operators(L, beta, C, scale.factor, m = 1, tau = 1)
L |
A finite element discretization of the operator |
beta |
The positive fractional power. |
C |
The mass matrix of the finite element discretization. |
scale.factor |
A constant |
m |
The order of the rational approximation, which needs to be a positive integer. The default value is 1. Higer values gives a more accurate approximation, which are more computationally expensive to use for inference. Currently, the largest value of m that is implemented is 4. |
tau |
The constant or vector that scales the variance of the solution. The default value is 1. |
The approximation is based on a rational approximation of the fractional operator, resulting in an approximate model on the form
where are non-fractional operators defined in terms of
polynomials
for
. The order of
is given by
m
and the order of is
where
is the integer part of
if
and
otherwise.
The discrete approximation can be written as where
and
.
Note that the matrices
and
may be be ill-conditioned
for
. In this case, the methods in
operator.operations()
should be used for operations involving the matrices, since these methods
are more numerically stable.
fractional.operators
returns an object of class "rSPDEobj".
This object contains the following quantities:
Pl |
The operator |
Pr |
The operator |
C |
The mass lumped mass matrix. |
Ci |
The inverse of |
m |
The order of the rational approximation. |
beta |
The fractional power. |
type |
String indicating the type of approximation. |
Q |
The matrix |
type |
String indicating the type of approximation. |
Pl.factors |
List with elements that can be used to assemble |
Pr.factors |
List with elements that can be used to assemble |
matern.operators()
, spde.matern.operators()
,
matern.operators()
# Compute rational approximation of a Gaussian process with a # Matern covariance function on R kappa <- 10 sigma <- 1 nu <- 0.8 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation of covariance function at 0.5 tau <- sqrt(gamma(nu) / (sigma^2 * kappa^(2 * nu) * (4 * pi)^(1 / 2) * gamma(nu + 1 / 2))) op <- fractional.operators( L = fem$G + kappa^2 * fem$C, beta = (nu + 1 / 2) / 2, C = fem$C, scale.factor = kappa^2, tau = tau ) v <- t(rSPDE.A1d(x, 0.5)) c.approx <- Sigma.mult(op, v) # plot the result and compare with the true Matern covariance plot(x, matern.covariance(abs(x - 0.5), kappa, nu, sigma), type = "l", ylab = "C(h)", xlab = "h", main = "Matern covariance and rational approximations" ) lines(x, c.approx, col = 2)
# Compute rational approximation of a Gaussian process with a # Matern covariance function on R kappa <- 10 sigma <- 1 nu <- 0.8 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation of covariance function at 0.5 tau <- sqrt(gamma(nu) / (sigma^2 * kappa^(2 * nu) * (4 * pi)^(1 / 2) * gamma(nu + 1 / 2))) op <- fractional.operators( L = fem$G + kappa^2 * fem$C, beta = (nu + 1 / 2) / 2, C = fem$C, scale.factor = kappa^2, tau = tau ) v <- t(rSPDE.A1d(x, 0.5)) c.approx <- Sigma.mult(op, v) # plot the result and compare with the true Matern covariance plot(x, matern.covariance(abs(x - 0.5), kappa, nu, sigma), type = "l", ylab = "C(h)", xlab = "h", main = "Matern covariance and rational approximations" ) lines(x, c.approx, col = 2)
Auxiliar function to obtain domain-based initial values for log-likelihood optimization in rSPDE models with a latent stationary Gaussian Matern model
get.initial.values.rSPDE( mesh = NULL, mesh.range = NULL, graph.obj = NULL, n.spde = 1, dim = NULL, B.tau = NULL, B.kappa = NULL, B.sigma = NULL, B.range = NULL, nu = NULL, parameterization = c("matern", "spde"), include.nu = TRUE, log.scale = TRUE, nu.upper.bound = NULL )
get.initial.values.rSPDE( mesh = NULL, mesh.range = NULL, graph.obj = NULL, n.spde = 1, dim = NULL, B.tau = NULL, B.kappa = NULL, B.sigma = NULL, B.range = NULL, nu = NULL, parameterization = c("matern", "spde"), include.nu = TRUE, log.scale = TRUE, nu.upper.bound = NULL )
mesh |
An in INLA mesh |
mesh.range |
The range of the mesh. |
graph.obj |
A |
n.spde |
The number of basis functions in the mesh model. |
dim |
The dimension of the domain. |
B.tau |
Matrix with specification of log-linear model for |
B.kappa |
Matrix with specification of log-linear model for |
B.sigma |
Matrix with specification of log-linear model for |
B.range |
Matrix with specification of log-linear model for |
nu |
The smoothness parameter. |
parameterization |
Which parameterization to use? |
include.nu |
Should we also provide an initial guess for nu? |
log.scale |
Should the results be provided in log scale? |
nu.upper.bound |
Should an upper bound for nu be considered? |
A vector of the form (theta_1,theta_2,theta_3) or where theta_1 is the initial guess for tau, theta_2 is the initial guess for kappa and theta_3 is the initial guess for nu.
Data frame for result objects from R-INLA fitted models to be used in ggplot2
gg_df(result, ...)
gg_df(result, ...)
result |
a result object for which the data frame is desired |
... |
further arguments passed to or from other methods. |
A data frame containing the posterior densities.
Returns a ggplot-friendly data-frame with the marginal posterior densities.
## S3 method for class 'rspde_result' gg_df( result, parameter = result$params, transform = TRUE, restrict_x_axis = NULL, restrict_quantiles = NULL, ... )
## S3 method for class 'rspde_result' gg_df( result, parameter = result$params, transform = TRUE, restrict_x_axis = NULL, restrict_quantiles = NULL, ... )
result |
An rspde_result object. |
parameter |
Vector. Which parameters to get the posterior density in the data.frame? The options are |
transform |
Should the posterior density be given in the original scale? |
restrict_x_axis |
Variables to restrict the range of x axis based on quantiles. |
restrict_quantiles |
Named list of quantiles to restrict x axis. It should contain the name of the parameter
along with a vector with two elements specifying the lower and upper quantiles. The names should be
match the ones in result$params. For example, if we want to restrict nu to the 0.05 and 0.95 quantiles
we do |
... |
currently not used. |
A data frame containing the posterior densities.
rspde_lme
objectGlance accepts a rspde_lme
object and returns a
tidyr::tibble()
with exactly one row of model summaries.
The summaries are the square root of the estimated variance of the measurement error, residual
degrees of freedom, AIC, BIC, log-likelihood,
the type of latent model used in the fit and the total number of observations.
## S3 method for class 'rspde_lme' glance(x, ...)
## S3 method for class 'rspde_lme' glance(x, ...)
x |
An |
... |
Currently not used. |
A tidyr::tibble()
with exactly one row and columns:
nobs
Number of observations used.
sigma
the square root of the estimated residual variance
logLik
The log-likelihood of the model.
AIC
Akaike's Information Criterion for the model.
BIC
Bayesian Information Criterion for the model.
deviance
Deviance of the model.
df.residual
Residual degrees of freedom.
model.type
Type of latent model fitted.
Extracts data from metric graphs to be used by 'INLA' and 'inlabru'.
graph_data_rspde( graph_rspde, name = "field", repl = NULL, repl_col = NULL, group = NULL, group_col = NULL, only_pred = FALSE, loc = NULL, bru = FALSE, tibble = FALSE, drop_na = FALSE, drop_all_na = TRUE )
graph_data_rspde( graph_rspde, name = "field", repl = NULL, repl_col = NULL, group = NULL, group_col = NULL, only_pred = FALSE, loc = NULL, bru = FALSE, tibble = FALSE, drop_na = FALSE, drop_all_na = TRUE )
graph_rspde |
An |
name |
A character string with the base name of the effect. |
repl |
Which replicates? If there is no replicates, one
can set |
repl_col |
Which "column" of the data contains the replicate variable? |
group |
Which groups? If there is no groups, one
can set |
group_col |
Which "column" of the data contains the group variable? |
only_pred |
Should only return the |
loc |
Locations. If not given, they will be chosen as the available locations on the metric graph internal dataset. |
bru |
Should the data be processed for |
tibble |
Should the data be returned as a |
drop_na |
Should the rows with at least one NA for one of the columns be removed? DEFAULT is |
drop_all_na |
Should the rows with all variables being NA be removed? DEFAULT is |
An 'INLA' and 'inlabru' friendly list with the data.
Compute prediction of a formula-based expression on a testing set based on a training set.
group_predict( models, model_names = NULL, formula = NULL, train_indices, test_indices, n_samples = 1000, pseudo_predict = TRUE, return_samples = FALSE, return_hyper_samples = FALSE, n_hyper_samples = 1, compute_posterior_means = TRUE, print = TRUE, fit_verbose = FALSE )
group_predict( models, model_names = NULL, formula = NULL, train_indices, test_indices, n_samples = 1000, pseudo_predict = TRUE, return_samples = FALSE, return_hyper_samples = FALSE, n_hyper_samples = 1, compute_posterior_means = TRUE, print = TRUE, fit_verbose = FALSE )
models |
A fitted model obtained from calling the |
model_names |
A vector containing the names of the models to appear in the returned |
formula |
A formula where the right hand side defines an R expression to evaluate for each generated sample. If |
train_indices |
A list containing the indices of the observations for the model to be trained, or a numerical vector containing the indices. |
test_indices |
A list containing the indices of the test data, where the prediction will be done, or a numerical vector containing the indices. |
n_samples |
Number of samples to compute the posterior statistics to be used to compute the scores. |
pseudo_predict |
If |
return_samples |
Should the posterior samples be returned? |
return_hyper_samples |
Should samples for the hyperparameters be obtained? |
n_hyper_samples |
Number of independent samples of the hyper parameters of size |
compute_posterior_means |
Should the posterior means be computed from the posterior samples? |
print |
Should partial results be printed throughout the computation? |
fit_verbose |
Should INLA's run during the prediction be verbose? |
A data.frame with the fitted models and the corresponding scores.
intrinsic.matern.operators
is used for computing a
covariance-based rational SPDE approximation of intrinsic
fields on defined through the SPDE
intrinsic.matern.operators( kappa, tau, alpha, beta = 1, G = NULL, C = NULL, d = NULL, mesh = NULL, graph = NULL, loc_mesh = NULL, m_alpha = 2, m_beta = 2, compute_higher_order = FALSE, return_block_list = FALSE, type_rational_approximation = c("chebfun", "brasil", "chebfunLB"), fem_mesh_matrices = NULL, scaling = NULL )
intrinsic.matern.operators( kappa, tau, alpha, beta = 1, G = NULL, C = NULL, d = NULL, mesh = NULL, graph = NULL, loc_mesh = NULL, m_alpha = 2, m_beta = 2, compute_higher_order = FALSE, return_block_list = FALSE, type_rational_approximation = c("chebfun", "brasil", "chebfunLB"), fem_mesh_matrices = NULL, scaling = NULL )
kappa |
range parameter |
tau |
precision parameter |
alpha |
Smoothness parameter |
beta |
Smoothness parameter |
G |
The stiffness matrix of a finite element discretization of the domain of interest. |
C |
The mass matrix of a finite element discretization of the domain of interest. |
d |
The dimension of the domain. |
mesh |
An inla mesh. |
graph |
An optional |
loc_mesh |
locations for the mesh for |
m_alpha |
The order of the rational approximation for the Matérn part, which needs to be a positive integer. The default value is 2. |
m_beta |
The order of the rational approximation for the intrinsic part, which needs to be a positive integer. The default value is 2. |
compute_higher_order |
Logical. Should the higher order finite element matrices be computed? |
return_block_list |
Logical. For |
type_rational_approximation |
Which type of rational approximation should be used? The current types are "chebfun", "brasil" or "chebfunLB". |
fem_mesh_matrices |
A list containing FEM-related matrices. The list should contain elements c0, g1, g2, g3, etc. |
scaling |
second lowest eigenvalue of g1 |
The covariance operator
is approximated based on rational approximations of the two fractional components. The Laplacians are equipped with homogeneous Neumann boundary conditions and a zero-mean constraint is additionally imposed to obtained a non-intrinsic model.
intrinsic.matern.operators
returns an object of
class "intrinsicCBrSPDEobj". This object is a list containing the
following quantities:
C |
The mass lumped mass matrix. |
Ci |
The inverse of |
GCi |
The stiffness matrix G times |
Gk |
The stiffness matrix G along with the higher-order FEM-related matrices G2, G3, etc. |
fem_mesh_matrices |
A list containing the mass lumped mass matrix, the stiffness matrix and the higher-order FEM-related matrices. |
m_alpha |
The order of the rational approximation for the Matérn part. |
m_beta |
The order of the rational approximation for the intrinsic part. |
alpha |
The fractional power of the Matérn part of the operator. |
beta |
The fractional power of the intrinsic part of the operator. |
type |
String indicating the type of approximation. |
d |
The dimension of the domain. |
A |
Matrix that sums the components in the approximation to the mesh nodes. |
kappa |
Range parameter of the covariance function |
tau |
Scale parameter of the covariance function. |
type |
String indicating the type of approximation. |
if (requireNamespace("RSpectra", quietly = TRUE)) { x <- seq(from = 0, to = 10, length.out = 201) beta <- 1 alpha <- 1 kappa <- 1 op <- intrinsic.matern.operators( kappa = kappa, tau = 1, alpha = alpha, beta = beta, loc_mesh = x, d = 1 ) # Compute and plot the variogram of the model Sigma <- op$A[,-1] %*% solve(op$Q[-1,-1], t(op$A[,-1])) One <- rep(1, times = ncol(Sigma)) D <- diag(Sigma) Gamma <- 0.5 * (One %*% t(D) + D %*% t(One) - 2 * Sigma) k <- 100 plot(x, Gamma[k, ], type = "l") lines(x, variogram.intrinsic.spde(x[k], x, kappa, alpha, beta, L = 10, d = 1), col = 2, lty = 2 ) }
if (requireNamespace("RSpectra", quietly = TRUE)) { x <- seq(from = 0, to = 10, length.out = 201) beta <- 1 alpha <- 1 kappa <- 1 op <- intrinsic.matern.operators( kappa = kappa, tau = 1, alpha = alpha, beta = beta, loc_mesh = x, d = 1 ) # Compute and plot the variogram of the model Sigma <- op$A[,-1] %*% solve(op$Q[-1,-1], t(op$A[,-1])) One <- rep(1, times = ncol(Sigma)) D <- diag(Sigma) Gamma <- 0.5 * (One %*% t(D) + D %*% t(One) - 2 * Sigma) k <- 100 plot(x, Gamma[k, ], type = "l") lines(x, variogram.intrinsic.spde(x[k], x, kappa, alpha, beta, L = 10, d = 1), col = 2, lty = 2 ) }
matern.covariance
evaluates the Matern covariance function
matern.covariance(h, kappa, nu, sigma)
matern.covariance(h, kappa, nu, sigma)
h |
Distances to evaluate the covariance function at. |
kappa |
Range parameter. |
nu |
Shape parameter. |
sigma |
Standard deviation. |
A vector with the values C(h).
x <- seq(from = 0, to = 1, length.out = 101) plot(x, matern.covariance(abs(x - 0.5), kappa = 10, nu = 1 / 5, sigma = 1), type = "l", ylab = "C(h)", xlab = "h" )
x <- seq(from = 0, to = 1, length.out = 101) plot(x, matern.covariance(abs(x - 0.5), kappa = 10, nu = 1 / 5, sigma = 1), type = "l", ylab = "C(h)", xlab = "h" )
matern.operators
is used for computing a rational SPDE approximation
of a stationary Gaussian random fields on with a Matern covariance
function
matern.operators( kappa = NULL, tau = NULL, alpha = NULL, sigma = NULL, range = NULL, nu = NULL, G = NULL, C = NULL, d = NULL, mesh = NULL, graph = NULL, range_mesh = NULL, loc_mesh = NULL, m = 1, type = c("covariance", "operator"), parameterization = c("spde", "matern"), compute_higher_order = FALSE, return_block_list = FALSE, type_rational_approximation = c("chebfun", "brasil", "chebfunLB"), compute_logdet = FALSE )
matern.operators( kappa = NULL, tau = NULL, alpha = NULL, sigma = NULL, range = NULL, nu = NULL, G = NULL, C = NULL, d = NULL, mesh = NULL, graph = NULL, range_mesh = NULL, loc_mesh = NULL, m = 1, type = c("covariance", "operator"), parameterization = c("spde", "matern"), compute_higher_order = FALSE, return_block_list = FALSE, type_rational_approximation = c("chebfun", "brasil", "chebfunLB"), compute_logdet = FALSE )
kappa |
Parameter kappa of the SPDE representation. If |
tau |
Parameter tau of the SPDE representation. If both sigma and tau are |
alpha |
Parameter alpha of the SPDE representation. If |
sigma |
Standard deviation of the covariance function. Used if |
range |
Range parameter of the covariance function. Used if |
nu |
Shape parameter of the covariance function. Used if |
G |
The stiffness matrix of a finite element discretization of the
domain of interest. Does not need to be given if either |
C |
The mass matrix of a finite element discretization of the domain
of interest. Does not need to be given if either |
d |
The dimension of the domain. Does not need to be given if either
|
mesh |
An optional fmesher mesh. Replaces |
graph |
An optional |
range_mesh |
The range of the mesh. Will be used to provide starting values for the parameters. Will be used if |
loc_mesh |
The mesh locations used to construct the matrices C and G. This option should be provided if one wants to use the |
m |
The order of the rational approximation, which needs to be a positive integer. The default value is 1. |
type |
The type of the rational approximation. The options are "covariance" and "operator". The default is "covariance". |
parameterization |
Which parameterization to use? |
compute_higher_order |
Logical. Should the higher order finite element matrices be computed? |
return_block_list |
Logical. For |
type_rational_approximation |
Which type of rational approximation should be used? The current types are "chebfun", "brasil" or "chebfunLB". |
compute_logdet |
Should log determinants be computed while building the model? (For covariance-based models) |
If type
is "covariance", we use the
covariance-based rational approximation of the fractional operator.
In the SPDE approach, we model as the solution of the following SPDE:
where
and
is the standard
Gaussian white noise. The covariance operator of
is given
by
. Now, let
be a finite-element
approximation of
. We can use
a rational approximation of order
on
to
obtain the following approximation:
where ,
and
are
polynomials arising from such rational approximation.
From this approximation we construct an approximate precision
matrix for
.
If type
is "operator", the approximation is based on a
rational approximation of the fractional operator
, where
.
This results in an approximate model of the form
where are non-fractional operators defined in terms
of polynomials
for
. The order of
is given
by
m
and the order of is
where
is the integer part of
if
and
otherwise.
The discrete approximation can be written as where
and
.
Note that the matrices
and
may be be
ill-conditioned for
. In this case, the methods in
operator.operations()
should be used for operations involving
the matrices, since these methods are more numerically stable.
If type
is "covariance", then matern.operators
returns an object of class "CBrSPDEobj".
This object is a list containing the
following quantities:
C |
The mass lumped mass matrix. |
Ci |
The inverse of |
GCi |
The stiffness matrix G times |
Gk |
The stiffness matrix G along with the higher-order FEM-related matrices G2, G3, etc. |
fem_mesh_matrices |
A list containing the mass lumped mass matrix, the stiffness matrix and the higher-order FEM-related matrices. |
m |
The order of the rational approximation. |
alpha |
The fractional power of the precision operator. |
type |
String indicating the type of approximation. |
d |
The dimension of the domain. |
nu |
Shape parameter of the covariance function. |
kappa |
Range parameter of the covariance function |
tau |
Scale parameter of the covariance function. |
sigma |
Standard deviation of the covariance function. |
type |
String indicating the type of approximation. |
If type
is "operator", then matern.operators
returns an object of class "rSPDEobj". This object contains the
quantities listed in the output of fractional.operators()
,
the G
matrix, the dimension of the domain, as well as the
parameters of the covariance function.
fractional.operators()
,
spde.matern.operators()
,
matern.operators()
# Compute the covariance-based rational approximation of a # Gaussian process with a Matern covariance function on R kappa <- 10 sigma <- 1 nu <- 0.8 range <- sqrt(8 * nu) / kappa # create mass and stiffness matrices for a FEM discretization nobs <- 101 x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation of covariance function at 0.5 op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) v <- t(rSPDE.A1d(x, 0.5)) # Compute the precision matrix Q <- op_cov$Q # A matrix here is the identity matrix A <- Diagonal(nobs) # We need to concatenate 3 A's since we are doing a covariance-based rational # approximation of order 2 Abar <- cbind(A, A, A) w <- rbind(v, v, v) # The approximate covariance function: c_cov.approx <- (Abar) %*% solve(Q, w) c.true <- folded.matern.covariance.1d( rep(0.5, length(x)), abs(x), kappa, nu, sigma ) # plot the result and compare with the true Matern covariance plot(x, c.true, type = "l", ylab = "C(h)", xlab = "h", main = "Matern covariance and rational approximations" ) lines(x, c_cov.approx, col = 2) # Compute the operator-based rational approximation of a Gaussian # process with a Matern covariance function on R kappa <- 10 sigma <- 1 nu <- 0.8 range <- sqrt(8 * nu) / kappa # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation of covariance function at 0.5 op <- matern.operators( range = range, sigma = sigma, nu = nu, loc_mesh = x, d = 1, type = "operator", parameterization = "matern" ) v <- t(rSPDE.A1d(x, 0.5)) c.approx <- Sigma.mult(op, v) c.true <- folded.matern.covariance.1d( rep(0.5, length(x)), abs(x), kappa, nu, sigma ) # plot the result and compare with the true Matern covariance plot(x, c.true, type = "l", ylab = "C(h)", xlab = "h", main = "Matern covariance and rational approximation" ) lines(x, c.approx, col = 2)
# Compute the covariance-based rational approximation of a # Gaussian process with a Matern covariance function on R kappa <- 10 sigma <- 1 nu <- 0.8 range <- sqrt(8 * nu) / kappa # create mass and stiffness matrices for a FEM discretization nobs <- 101 x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation of covariance function at 0.5 op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) v <- t(rSPDE.A1d(x, 0.5)) # Compute the precision matrix Q <- op_cov$Q # A matrix here is the identity matrix A <- Diagonal(nobs) # We need to concatenate 3 A's since we are doing a covariance-based rational # approximation of order 2 Abar <- cbind(A, A, A) w <- rbind(v, v, v) # The approximate covariance function: c_cov.approx <- (Abar) %*% solve(Q, w) c.true <- folded.matern.covariance.1d( rep(0.5, length(x)), abs(x), kappa, nu, sigma ) # plot the result and compare with the true Matern covariance plot(x, c.true, type = "l", ylab = "C(h)", xlab = "h", main = "Matern covariance and rational approximations" ) lines(x, c_cov.approx, col = 2) # Compute the operator-based rational approximation of a Gaussian # process with a Matern covariance function on R kappa <- 10 sigma <- 1 nu <- 0.8 range <- sqrt(8 * nu) / kappa # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation of covariance function at 0.5 op <- matern.operators( range = range, sigma = sigma, nu = nu, loc_mesh = x, d = 1, type = "operator", parameterization = "matern" ) v <- t(rSPDE.A1d(x, 0.5)) c.approx <- Sigma.mult(op, v) c.true <- folded.matern.covariance.1d( rep(0.5, length(x)), abs(x), kappa, nu, sigma ) # plot the result and compare with the true Matern covariance plot(x, c.true, type = "l", ylab = "C(h)", xlab = "h", main = "Matern covariance and rational approximation" ) lines(x, c.approx, col = 2)
The function is used for computing an approximation, which can be used for inference and simulation, of the fractional SPDE
on intervals or metric graphs. Here is Gaussian white noise,
controls the range,
with
controls the smoothness and
is related to the marginal variances
through
matern.rational( graph = NULL, loc = NULL, bc = c("free", "Neumann", "Dirichlet"), kappa = NULL, range = NULL, nu = NULL, sigma = NULL, tau = NULL, alpha = NULL, m = 2, parameterization = c("matern", "spde"), type_rational_approximation = "brasil", type_interp = "spline" )
matern.rational( graph = NULL, loc = NULL, bc = c("free", "Neumann", "Dirichlet"), kappa = NULL, range = NULL, nu = NULL, sigma = NULL, tau = NULL, alpha = NULL, m = 2, parameterization = c("matern", "spde"), type_rational_approximation = "brasil", type_interp = "spline" )
graph |
Metric graph object. The default is NULL, which means that a stationary Matern model on the line is created. |
loc |
Locations where to evaluate the model. |
bc |
Specifies the boundary conditions. The default is "free" which gives stationary Matern models on intervals. Other options are "Neumann" or "Dirichlet". |
kappa |
Range parameter |
range |
practical correlation range |
nu |
Smoothness parameter |
sigma |
Standard deviation |
tau |
Precision parameter |
alpha |
Smoothness parameter |
m |
The order of the approximation |
parameterization |
Which parameterization to use? |
type_rational_approximation |
Method used to compute the coefficients of the rational approximation. |
type_interp |
Interpolation method for the rational coefficients. |
A model object for the the approximation
s <- seq(from = 0, to = 1, length.out = 101) kappa <- 20 sigma <- 2 nu <- 0.8 r <- sqrt(8*nu)/kappa #range parameter op_cov <- matern.rational(loc = s, nu = nu, range = r, sigma = sigma, m = 2, parameterization = "matern") cov.true <- matern.covariance(abs(s-s[1]), kappa = kappa, sigma = sigma, nu = nu) cov.approx <- op_cov$covariance(ind = 1) plot(s, cov.true) lines(s, cov.approx, col = 2)
s <- seq(from = 0, to = 1, length.out = 101) kappa <- 20 sigma <- 2 nu <- 0.8 r <- sqrt(8*nu)/kappa #range parameter op_cov <- matern.rational(loc = s, nu = nu, range = r, sigma = sigma, m = 2, parameterization = "matern") cov.true <- matern.covariance(abs(s-s[1]), kappa = kappa, sigma = sigma, nu = nu) cov.approx <- op_cov$covariance(ind = 1) plot(s, cov.true) lines(s, cov.approx, col = 2)
Computes a rational approximation of the Matern covariance function on intervals.
matern.rational.cov( h, order, kappa, nu, sigma, type_rational = "brasil", type_interp = "linear" )
matern.rational.cov( h, order, kappa, nu, sigma, type_rational = "brasil", type_interp = "linear" )
h |
Distances to compute the covariance for |
order |
The order of the approximation |
kappa |
Range parameter |
nu |
Smoothness parameter |
sigma |
Standard deviation |
type_rational |
Method used to compute the coefficients of the rational approximation. |
type_interp |
Interpolation method for the rational coefficients. |
The covariance matrix of the approximation
h <- seq(from = 0, to = 1, length.out = 100) cov.true <- matern.covariance(h, kappa = 10, sigma = 1, nu = 0.8) cov.approx <- matern.rational.cov(h, kappa = 10, sigma = 1, nu = 0.8, order = 2) plot(h, cov.true) lines(h, cov.approx, col = 2)
h <- seq(from = 0, to = 1, length.out = 100) cov.true <- matern.covariance(h, kappa = 10, sigma = 1, nu = 0.8) cov.approx <- matern.rational.cov(h, kappa = 10, sigma = 1, nu = 0.8, order = 2) plot(h, cov.true) lines(h, cov.approx, col = 2)
matern2d.operators
is used for computing a rational SPDE approximation
of a stationary Gaussian random fields on with a Matern covariance
function
, based on a SPDE representation of the form
,
where $c>0$ is a constant. The matrix is defined as
matern2d.operators( hx = NULL, hy = NULL, hxy = NULL, nu = NULL, sigma = NULL, mesh = NULL, fem = NULL, m = 1, type_rational_approximation = c("chebfun", "brasil", "chebfunLB"), return_fem_matrices = FALSE )
matern2d.operators( hx = NULL, hy = NULL, hxy = NULL, nu = NULL, sigma = NULL, mesh = NULL, fem = NULL, m = 1, type_rational_approximation = c("chebfun", "brasil", "chebfunLB"), return_fem_matrices = FALSE )
hx |
Parameter in the H matrix. |
hy |
Parameter in the H matrix. |
hxy |
Parameter in the H matrix. |
nu |
Smoothness parameter. |
sigma |
standard deviation parameter. |
mesh |
An |
fem |
Optional precomputed FEM matrices. |
m |
The order of the rational approximation, which needs to be a positive integer. The default value is 1. |
type_rational_approximation |
Which type of rational approximation should be used? The current types are "chebfun", "brasil" or "chebfunLB". |
return_fem_matrices |
Should the FEM matrices be returned? |
An object of type CBrSPDEobj2d
fractional.operators()
,
spde.matern.operators()
,
matern.operators()
library(fmesher) n_loc <- 2000 loc_2d_mesh <- matrix(runif(n_loc * 2), n_loc, 2) mesh_2d <- fm_mesh_2d(loc = loc_2d_mesh, cutoff = 0.03, max.edge = c(0.1, 0.5)) op <- matern2d.operators(mesh = mesh_2d)
library(fmesher) n_loc <- 2000 loc_2d_mesh <- matrix(runif(n_loc * 2), n_loc, 2) mesh_2d <- fm_mesh_2d(loc = loc_2d_mesh, cutoff = 0.03, max.edge = c(0.1, 0.5)) op <- matern2d.operators(mesh = mesh_2d)
Functions for multiplying and solving with the
and
operators as well as the latent precision
matrix
and covariance matrix
.
These operations are done without first assembling
,
in order to avoid numerical problems caused by
ill-conditioned matrices.
Pr.mult(obj, v, transpose = FALSE) Pr.solve(obj, v, transpose = FALSE) Pl.mult(obj, v, transpose = FALSE) Pl.solve(obj, v, transpose = FALSE) Q.mult(obj, v) Q.solve(obj, v) Qsqrt.mult(obj, v, transpose = FALSE) Qsqrt.solve(obj, v, transpose = FALSE) Sigma.mult(obj, v) Sigma.solve(obj, v)
Pr.mult(obj, v, transpose = FALSE) Pr.solve(obj, v, transpose = FALSE) Pl.mult(obj, v, transpose = FALSE) Pl.solve(obj, v, transpose = FALSE) Q.mult(obj, v) Q.solve(obj, v) Qsqrt.mult(obj, v, transpose = FALSE) Qsqrt.solve(obj, v, transpose = FALSE) Sigma.mult(obj, v) Sigma.solve(obj, v)
obj |
rSPDE object |
v |
vector to apply the operation to |
transpose |
set to TRUE if the operation should be performed with the transposed object |
Pl.mult
, Pr.mult
, and Q.mult
multiplies the vector with the respective object.
Changing mult
to solve
in the function names
multiplies the vector with the inverse of the object.
Qsqrt.mult
and Qsqrt.solve
performs the
operations with the square-root type object
defined so that
.
A vector with the values of the operation
Function to get the precision matrix of a CBrSPDEobj object
precision(object, ...) ## S3 method for class 'CBrSPDEobj' precision( object, user_nu = NULL, user_kappa = NULL, user_sigma = NULL, user_range = NULL, user_tau = NULL, user_m = NULL, ... )
precision(object, ...) ## S3 method for class 'CBrSPDEobj' precision( object, user_nu = NULL, user_kappa = NULL, user_sigma = NULL, user_range = NULL, user_tau = NULL, user_m = NULL, ... )
object |
The covariance-based rational SPDE approximation,
computed using |
... |
Currently not used. |
user_nu |
If non-null, update the shape parameter of the covariance function. |
user_kappa |
If non-null, update the range parameter of the covariance function. |
user_sigma |
If non-null, update the standard deviation of the covariance function. |
user_range |
If non-null, update the range parameter of the covariance function. |
user_tau |
If non-null, update the parameter tau. |
user_m |
If non-null, update the order of the rational approximation, which needs to be a positive integer. |
The precision matrix.
simulate.CBrSPDEobj()
, matern.operators()
# Compute the covariance-based rational approximation of a # Gaussian process with a Matern covariance function on R kappa <- 10 sigma <- 1 nu <- 0.8 range <- 0.2 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation of covariance function at 0.5 tau <- sqrt(gamma(nu) / (sigma^2 * kappa^(2 * nu) * (4 * pi)^(1 / 2) * gamma(nu + 1 / 2))) op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) # Get the precision matrix: prec_matrix <- precision(op_cov)
# Compute the covariance-based rational approximation of a # Gaussian process with a Matern covariance function on R kappa <- 10 sigma <- 1 nu <- 0.8 range <- 0.2 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation of covariance function at 0.5 tau <- sqrt(gamma(nu) / (sigma^2 * kappa^(2 * nu) * (4 * pi)^(1 / 2) * gamma(nu + 1 / 2))) op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) # Get the precision matrix: prec_matrix <- precision(op_cov)
Function to get the precision matrix of a CBrSPDEobj2d object
## S3 method for class 'CBrSPDEobj2d' precision( object, user_nu = NULL, user_hx = NULL, user_hy = NULL, user_hxy = NULL, user_sigma = NULL, user_m = NULL, ... )
## S3 method for class 'CBrSPDEobj2d' precision( object, user_nu = NULL, user_hx = NULL, user_hy = NULL, user_hxy = NULL, user_sigma = NULL, user_m = NULL, ... )
object |
The covariance-based rational SPDE approximation,
computed using |
user_nu |
If non-null, update the shape parameter of the covariance function. |
user_hx |
If non-null, update the hx parameter. |
user_hy |
If non-null, update the hy parameter. |
user_hxy |
If non-null, update the hxy parameter. |
user_sigma |
If non-null, update the standard deviation of the covariance function. |
user_m |
If non-null, update the order of the rational approximation, which needs to be a positive integer. |
... |
Currently not used. |
The precision matrix.
simulate.CBrSPDEobj2d()
, matern2d.operators()
library(fmesher) n_loc <- 2000 loc_2d_mesh <- matrix(runif(n_loc * 2), n_loc, 2) mesh_2d <- fm_mesh_2d(loc = loc_2d_mesh, cutoff = 0.03, max.edge = c(0.1, 0.5)) op <- matern2d.operators(mesh = mesh_2d) Q <- precision(op)
library(fmesher) n_loc <- 2000 loc_2d_mesh <- matrix(runif(n_loc * 2), n_loc, 2) mesh_2d <- fm_mesh_2d(loc = loc_2d_mesh, cutoff = 0.03, max.edge = c(0.1, 0.5)) op <- matern2d.operators(mesh = mesh_2d) Q <- precision(op)
inla_rspde
objectsFunction to get the precision matrix of an inla_rspde
object created with the rspde.matern()
function.
## S3 method for class 'inla_rspde' precision(object, theta = NULL, ...)
## S3 method for class 'inla_rspde' precision(object, theta = NULL, ...)
object |
The |
theta |
If null, the starting values for theta will be used. Otherwise, it must be suplied as a vector.
For stationary models, we have |
... |
Currently not used. |
The precision matrix.
precision.CBrSPDEobj()
, matern.operators()
Function to get the precision matrix of a rSPDEobj1d object
## S3 method for class 'rSPDEobj1d' precision( object, user_loc = NULL, user_nu = NULL, user_kappa = NULL, user_sigma = NULL, user_range = NULL, user_tau = NULL, user_m = NULL, ordering = c("field", "location"), ldl = FALSE, ... )
## S3 method for class 'rSPDEobj1d' precision( object, user_loc = NULL, user_nu = NULL, user_kappa = NULL, user_sigma = NULL, user_range = NULL, user_tau = NULL, user_m = NULL, ordering = c("field", "location"), ldl = FALSE, ... )
object |
The covariance-based rational SPDE approximation,
computed using |
user_loc |
If non-null, update the locations where to evaluate the model. |
user_nu |
If non-null, update the shape parameter of the covariance function. |
user_kappa |
If non-null, update the range parameter of the covariance function. |
user_sigma |
If non-null, update the standard deviation of the covariance function. |
user_range |
If non-null, update the range parameter of the covariance function. |
user_tau |
If non-null, update the parameter tau. |
user_m |
If non-null, update the order of the rational approximation, which needs to be a positive integer. |
ordering |
Return the matrices ordered by field or by location? |
ldl |
Directly build the LDL factorization of the precision matrix? |
... |
Currently not used. |
A list containing the precision matrix Q
of the process and its derivatives if they exist, and
a matrix A
that extracts the elements corresponding to the process. If ldl=TRUE
, the LDL factorization
is returned instead of Q
. If the locations are not ordered, the precision matrix is given for the ordered locations,
but the A
matrix returns to the original order.
simulate.rSPDEobj1d()
, matern.rational()
# Compute the covariance-based rational approximation of a # Gaussian process with a Matern covariance function on R sigma <- 1 nu <- 0.8 range <- 0.2 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) op_cov <- matern.rational( loc = x, nu = nu, range = range, sigma = sigma, m = 2, parameterization = "matern" ) # Get the precision matrix: prec_matrix <- precision(op_cov)
# Compute the covariance-based rational approximation of a # Gaussian process with a Matern covariance function on R sigma <- 1 nu <- 0.8 range <- 0.2 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) op_cov <- matern.rational( loc = x, nu = nu, range = range, sigma = sigma, m = 2, parameterization = "matern" ) # Get the precision matrix: prec_matrix <- precision(op_cov)
Function to get the precision matrix of a spacetimeobj object
## S3 method for class 'spacetimeobj' precision( object, user_kappa = NULL, user_sigma = NULL, user_gamma = NULL, user_rho = NULL, ... )
## S3 method for class 'spacetimeobj' precision( object, user_kappa = NULL, user_sigma = NULL, user_gamma = NULL, user_rho = NULL, ... )
object |
The model object computed using |
user_kappa |
If non-null, update the range parameter of the covariance function. |
user_sigma |
If non-null, update the standard deviation of the covariance function. |
user_gamma |
If non-null, update the temporal range parameter of the covariance function. |
user_rho |
If non-null, update the drift parameter of the covariance function. |
... |
Currently not used. |
The precision matrix.
simulate.spacetimeobj()
, spacetime.operators()
s <- seq(from = 0, to = 20, length.out = 101) t <- seq(from = 0, to = 20, length.out = 31) op_cov <- spacetime.operators(space_loc = s, time_loc = t, kappa = 5, sigma = 10, alpha = 1, beta = 2, rho = 1, gamma = 0.05) prec_matrix <- precision(op_cov)
s <- seq(from = 0, to = 20, length.out = 101) t <- seq(from = 0, to = 20, length.out = 31) op_cov <- spacetime.operators(space_loc = s, time_loc = t, kappa = 5, sigma = 10, alpha = 1, beta = 2, rho = 1, gamma = 0.05) prec_matrix <- precision(op_cov)
The function is used for computing kriging predictions based
on data , where
is mean-zero Gaussian measurement noise and
is defined by
a fractional SPDE
,
where
is Gaussian white noise and
,
where
is the dimension of the domain.
## S3 method for class 'CBrSPDEobj' predict( object, A, Aprd, Y, sigma.e, mu = 0, compute.variances = FALSE, posterior_samples = FALSE, n_samples = 100, only_latent = FALSE, ... )
## S3 method for class 'CBrSPDEobj' predict( object, A, Aprd, Y, sigma.e, mu = 0, compute.variances = FALSE, posterior_samples = FALSE, n_samples = 100, only_latent = FALSE, ... )
object |
The covariance-based rational SPDE approximation,
computed using |
A |
A matrix linking the measurement locations to the basis of the FEM approximation of the latent model. |
Aprd |
A matrix linking the prediction locations to the basis of the FEM approximation of the latent model. |
Y |
A vector with the observed data, can also be a matrix where the
columns are observations
of independent replicates of |
sigma.e |
The standard deviation of the Gaussian measurement noise. Put to zero if the model does not have measurement noise. |
mu |
Expectation vector of the latent field (default = 0). |
compute.variances |
Set to also TRUE to compute the kriging variances. |
posterior_samples |
If |
n_samples |
Number of samples to be returned. Will only be used if |
only_latent |
Should the posterior samples be only given to the laten model? |
... |
further arguments passed to or from other methods. |
A list with elements
mean |
The kriging predictor (the posterior mean of u|Y). |
variance |
The posterior variances (if computed). |
set.seed(123) # Sample a Gaussian Matern process on R using a rational approximation kappa <- 10 sigma <- 1 nu <- 0.8 sigma.e <- 0.3 range <- 0.2 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) tau <- sqrt(gamma(nu) / (sigma^2 * kappa^(2 * nu) * (4 * pi)^(1 / 2) * gamma(nu + 1 / 2))) # Compute the covariance-based rational approximation op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) # Sample the model u <- simulate(op_cov) # Create some data obs.loc <- runif(n = 10, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc) Y <- as.vector(A %*% u + sigma.e * rnorm(10)) # compute kriging predictions at the FEM grid A.krig <- rSPDE.A1d(x, x) u.krig <- predict(op_cov, A = A, Aprd = A.krig, Y = Y, sigma.e = sigma.e, compute.variances = TRUE ) plot(obs.loc, Y, ylab = "u(x)", xlab = "x", main = "Data and prediction", ylim = c( min(u.krig$mean - 2 * sqrt(u.krig$variance)), max(u.krig$mean + 2 * sqrt(u.krig$variance)) ) ) lines(x, u.krig$mean) lines(x, u.krig$mean + 2 * sqrt(u.krig$variance), col = 2) lines(x, u.krig$mean - 2 * sqrt(u.krig$variance), col = 2)
set.seed(123) # Sample a Gaussian Matern process on R using a rational approximation kappa <- 10 sigma <- 1 nu <- 0.8 sigma.e <- 0.3 range <- 0.2 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) tau <- sqrt(gamma(nu) / (sigma^2 * kappa^(2 * nu) * (4 * pi)^(1 / 2) * gamma(nu + 1 / 2))) # Compute the covariance-based rational approximation op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) # Sample the model u <- simulate(op_cov) # Create some data obs.loc <- runif(n = 10, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc) Y <- as.vector(A %*% u + sigma.e * rnorm(10)) # compute kriging predictions at the FEM grid A.krig <- rSPDE.A1d(x, x) u.krig <- predict(op_cov, A = A, Aprd = A.krig, Y = Y, sigma.e = sigma.e, compute.variances = TRUE ) plot(obs.loc, Y, ylab = "u(x)", xlab = "x", main = "Data and prediction", ylim = c( min(u.krig$mean - 2 * sqrt(u.krig$variance)), max(u.krig$mean + 2 * sqrt(u.krig$variance)) ) ) lines(x, u.krig$mean) lines(x, u.krig$mean + 2 * sqrt(u.krig$variance), col = 2) lines(x, u.krig$mean - 2 * sqrt(u.krig$variance), col = 2)
The function is used for computing kriging predictions based
on data , where
is mean-zero Gaussian measurement noise and
is defined by
a SPDE as described in
matern2d.operators()
.
## S3 method for class 'CBrSPDEobj2d' predict( object, A, Aprd, Y, sigma.e, mu = 0, compute.variances = FALSE, posterior_samples = FALSE, n_samples = 100, only_latent = FALSE, ... )
## S3 method for class 'CBrSPDEobj2d' predict( object, A, Aprd, Y, sigma.e, mu = 0, compute.variances = FALSE, posterior_samples = FALSE, n_samples = 100, only_latent = FALSE, ... )
object |
The covariance-based rational SPDE approximation,
computed using |
A |
A matrix linking the measurement locations to the basis of the FEM approximation of the latent model. |
Aprd |
A matrix linking the prediction locations to the basis of the FEM approximation of the latent model. |
Y |
A vector with the observed data, can also be a matrix where the
columns are observations
of independent replicates of |
sigma.e |
The standard deviation of the Gaussian measurement noise. Put to zero if the model does not have measurement noise. |
mu |
Expectation vector of the latent field (default = 0). |
compute.variances |
Set to also TRUE to compute the kriging variances. |
posterior_samples |
If |
n_samples |
Number of samples to be returned. Will only be used if |
only_latent |
Should the posterior samples be only given to the laten model? |
... |
further arguments passed to or from other methods. |
A list with elements
mean |
The kriging predictor (the posterior mean of u|Y). |
variance |
The posterior variances (if computed). |
library(fmesher) n_loc <- 2000 loc_2d_mesh <- matrix(runif(n_loc * 2), n_loc, 2) mesh_2d <- fm_mesh_2d(loc = loc_2d_mesh, cutoff = 0.01, max.edge = c(0.1, 0.5)) op <- matern2d.operators(hx = 0.08, hy = 0.08, hxy = 0.5, nu = 0.5, sigma = 1, mesh = mesh_2d) u <- simulate(op) n.obs <- 2000 obs.loc <- cbind(runif(n.obs),runif(n.obs)) A <- fm_basis(mesh_2d,obs.loc) sigma.e <- 0.1 Y <- as.vector(A%*%u + sigma.e*rnorm(n.obs)) A <- op$make_A(obs.loc) proj <- fm_evaluator(mesh_2d, dims = c(100, 100), xlim = c(0,1), ylim = c(0,1)) Aprd <- op$make_A(proj$lattice$loc) u.krig <- predict(op, A = A, Aprd = Aprd, Y = Y, sigma.e = sigma.e)
library(fmesher) n_loc <- 2000 loc_2d_mesh <- matrix(runif(n_loc * 2), n_loc, 2) mesh_2d <- fm_mesh_2d(loc = loc_2d_mesh, cutoff = 0.01, max.edge = c(0.1, 0.5)) op <- matern2d.operators(hx = 0.08, hy = 0.08, hxy = 0.5, nu = 0.5, sigma = 1, mesh = mesh_2d) u <- simulate(op) n.obs <- 2000 obs.loc <- cbind(runif(n.obs),runif(n.obs)) A <- fm_basis(mesh_2d,obs.loc) sigma.e <- 0.1 Y <- as.vector(A%*%u + sigma.e*rnorm(n.obs)) A <- op$make_A(obs.loc) proj <- fm_evaluator(mesh_2d, dims = c(100, 100), xlim = c(0,1), ylim = c(0,1)) Aprd <- op$make_A(proj$lattice$loc) u.krig <- predict(op, A = A, Aprd = Aprd, Y = Y, sigma.e = sigma.e)
Prediction of a mixed effects regression model on a metric graph.
## S3 method for class 'rspde_lme' predict( object, newdata = NULL, loc = NULL, time = NULL, mesh = FALSE, which_repl = NULL, compute_variances = FALSE, posterior_samples = FALSE, n_samples = 100, sample_latent = FALSE, return_as_list = FALSE, return_original_order = TRUE, ..., data = deprecated() )
## S3 method for class 'rspde_lme' predict( object, newdata = NULL, loc = NULL, time = NULL, mesh = FALSE, which_repl = NULL, compute_variances = FALSE, posterior_samples = FALSE, n_samples = 100, sample_latent = FALSE, return_as_list = FALSE, return_original_order = TRUE, ..., data = deprecated() )
object |
The fitted object with the |
newdata |
A |
loc |
Prediction locations. Can either be a |
time |
Prediction times for spatio-temporal models. |
mesh |
Obtain predictions for mesh nodes? The graph must have a mesh, and
either |
which_repl |
Which replicates to use? If |
compute_variances |
Set to also TRUE to compute the kriging variances. |
posterior_samples |
If |
n_samples |
Number of samples to be returned. Will only be used if
|
sample_latent |
Do posterior samples only for the random effects? |
return_as_list |
Should the means of the predictions and the posterior samples be returned as a list, with each replicate being an element? |
return_original_order |
Should the results be return in the original (input) order or in the order inside the graph? |
... |
Additional arguments. Expert use only. |
data |
A list with elements mean
, which contains the means of the
predictions, fe_mean
, which is the prediction for the fixed effects,
re_mean
, which is the prediction for the random effects, variance
(if
compute_variance
is TRUE
), which contains the variances of the predictions,
samples
(if posterior_samples
is TRUE
), which contains the posterior samples.
The function is used for computing kriging predictions based on data
,
where
is mean-zero Gaussian measurement noise
and
is defined by
a fractional SPDE
, where
is Gaussian white noise.
## S3 method for class 'rSPDEobj' predict( object, A, Aprd, Y, sigma.e, compute.variances = FALSE, posterior_samples = FALSE, n_samples = 100, only_latent = FALSE, ... )
## S3 method for class 'rSPDEobj' predict( object, A, Aprd, Y, sigma.e, compute.variances = FALSE, posterior_samples = FALSE, n_samples = 100, only_latent = FALSE, ... )
object |
The rational SPDE approximation, computed using
|
A |
A matrix linking the measurement locations to the basis of the FEM approximation of the latent model. |
Aprd |
A matrix linking the prediction locations to the basis of the FEM approximation of the latent model. |
Y |
A vector with the observed data, can also be a matrix where the
columns are observations
of independent replicates of |
sigma.e |
The standard deviation of the Gaussian measurement noise. Put to zero if the model does not have measurement noise. |
compute.variances |
Set to also TRUE to compute the kriging variances. |
posterior_samples |
If |
n_samples |
Number of samples to be returned. Will only be used if |
only_latent |
Should the posterior samples be only given to the latent model? |
... |
further arguments passed to or from other methods. |
A list with elements
mean |
The kriging predictor (the posterior mean of u|Y). |
variance |
The posterior variances (if computed). |
samples |
A matrix containing the samples if |
# Sample a Gaussian Matern process on R using a rational approximation kappa <- 10 sigma <- 1 nu <- 0.8 sigma.e <- 0.3 range <- sqrt(8 * nu) / kappa # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation op <- matern.operators( range = range, sigma = sigma, nu = nu, loc_mesh = x, d = 1, parameterization = "matern" ) # Sample the model u <- simulate(op) # Create some data obs.loc <- runif(n = 10, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc) Y <- as.vector(A %*% u + sigma.e * rnorm(10)) # compute kriging predictions at the FEM grid A.krig <- rSPDE.A1d(x, x) u.krig <- predict(op, A = A, Aprd = A.krig, Y = Y, sigma.e = sigma.e, compute.variances = TRUE ) plot(obs.loc, Y, ylab = "u(x)", xlab = "x", main = "Data and prediction", ylim = c( min(u.krig$mean - 2 * sqrt(u.krig$variance)), max(u.krig$mean + 2 * sqrt(u.krig$variance)) ) ) lines(x, u.krig$mean) lines(x, u.krig$mean + 2 * sqrt(u.krig$variance), col = 2) lines(x, u.krig$mean - 2 * sqrt(u.krig$variance), col = 2)
# Sample a Gaussian Matern process on R using a rational approximation kappa <- 10 sigma <- 1 nu <- 0.8 sigma.e <- 0.3 range <- sqrt(8 * nu) / kappa # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation op <- matern.operators( range = range, sigma = sigma, nu = nu, loc_mesh = x, d = 1, parameterization = "matern" ) # Sample the model u <- simulate(op) # Create some data obs.loc <- runif(n = 10, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc) Y <- as.vector(A %*% u + sigma.e * rnorm(10)) # compute kriging predictions at the FEM grid A.krig <- rSPDE.A1d(x, x) u.krig <- predict(op, A = A, Aprd = A.krig, Y = Y, sigma.e = sigma.e, compute.variances = TRUE ) plot(obs.loc, Y, ylab = "u(x)", xlab = "x", main = "Data and prediction", ylim = c( min(u.krig$mean - 2 * sqrt(u.krig$variance)), max(u.krig$mean + 2 * sqrt(u.krig$variance)) ) ) lines(x, u.krig$mean) lines(x, u.krig$mean + 2 * sqrt(u.krig$variance), col = 2) lines(x, u.krig$mean - 2 * sqrt(u.krig$variance), col = 2)
The function is used for computing kriging predictions based
on data , where
is mean-zero Gaussian measurement noise and
is defined by
a spatio-temporal SPDE as described in
spacetime.operators()
.
## S3 method for class 'spacetimeobj' predict( object, A, Aprd, Y, sigma.e, mu = 0, compute.variances = FALSE, posterior_samples = FALSE, n_samples = 100, only_latent = FALSE, ... )
## S3 method for class 'spacetimeobj' predict( object, A, Aprd, Y, sigma.e, mu = 0, compute.variances = FALSE, posterior_samples = FALSE, n_samples = 100, only_latent = FALSE, ... )
object |
The covariance-based rational SPDE approximation,
computed using |
A |
A matrix linking the measurement locations to the basis of the FEM approximation of the latent model. |
Aprd |
A matrix linking the prediction locations to the basis of the FEM approximation of the latent model. |
Y |
A vector with the observed data, can also be a matrix where the
columns are observations
of independent replicates of |
sigma.e |
The standard deviation of the Gaussian measurement noise. Put to zero if the model does not have measurement noise. |
mu |
Expectation vector of the latent field (default = 0). |
compute.variances |
Set to also TRUE to compute the kriging variances. |
posterior_samples |
If |
n_samples |
Number of samples to be returned. Will only be used if |
only_latent |
Should the posterior samples be only given to the laten model? |
... |
further arguments passed to or from other methods. |
A list with elements
mean |
The kriging predictor (the posterior mean of u|Y). |
variance |
The posterior variances (if computed). |
s <- seq(from = 0, to = 20, length.out = 101) t <- seq(from = 0, to = 20, length.out = 31) op_cov <- spacetime.operators(space_loc = s, time_loc = t, kappa = 5, sigma = 10, alpha = 1, beta = 2, rho = 1, gamma = 0.05) # generate data sigma.e <- 0.01 n.obs <- 500 obs.loc <- data.frame(x = max(s)*runif(n.obs), t = max(t)*runif(n.obs)) A <- rSPDE.Ast(space_loc = s, time_loc = t, obs.s = obs.loc$x, obs.t = obs.loc$t) Aprd <- Diagonal(dim(A)[2]) x <- simulate(op_cov, nsim = 1) Y <- A%*%x + sigma.e*rnorm(n.obs) u.krig <- predict(op_cov, A, Aprd, Y, sigma.e)
s <- seq(from = 0, to = 20, length.out = 101) t <- seq(from = 0, to = 20, length.out = 31) op_cov <- spacetime.operators(space_loc = s, time_loc = t, kappa = 5, sigma = 10, alpha = 1, beta = 2, rho = 1, gamma = 0.05) # generate data sigma.e <- 0.01 n.obs <- 500 obs.loc <- data.frame(x = max(s)*runif(n.obs), t = max(t)*runif(n.obs)) A <- rSPDE.Ast(space_loc = s, time_loc = t, obs.s = obs.loc$x, obs.t = obs.loc$t) Aprd <- Diagonal(dim(A)[2]) x <- simulate(op_cov, nsim = 1) Y <- A%*%x + sigma.e*rnorm(n.obs) u.krig <- predict(op_cov, A, Aprd, Y, sigma.e)
Get the order of rational approximation.
rational.order(object)
rational.order(object)
object |
A |
The order of rational approximation.
Changing the order of the rational approximation
rational.order(x) <- value
rational.order(x) <- value
x |
A |
value |
The order of rational approximation. |
An object of the same class with the new order of rational approximation.
Get type of rational approximation.
rational.type(object)
rational.type(object)
object |
A |
The type of rational approximation.
Changing the type of the rational approximation
rational.type(x) <- value
rational.type(x) <- value
x |
A |
value |
The type of rational approximation. The current options are "chebfun", "brasil" and "chebfunLB" |
An object of the same class with the new rational approximation.
Turn off all warnings for require(), to allow clean completion of examples that require unavailable Suggested packages.
require.nowarnings(package, lib.loc = NULL, character.only = FALSE)
require.nowarnings(package, lib.loc = NULL, character.only = FALSE)
package |
The name of a package, given as a character string. |
lib.loc |
a character vector describing the location of R library trees
to search through, or |
character.only |
a logical indicating whether |
require(package)
acts the same as
require(package, quietly = TRUE)
but with warnings turned off.
In particular, no warning or error is given if the package is unavailable.
Most cases should use requireNamespace(package, quietly = TRUE)
instead,
which doesn't produce warnings.
require.nowarnings
returns (invisibly)
TRUE
if it succeeds, otherwise FALSE
## This should produce no output: if (require.nowarnings(nonexistent)) { message("Package loaded successfully") }
## This should produce no output: if (require.nowarnings(nonexistent)) { message("Package loaded successfully") }
Fitting linear mixed effects model with latent Whittle-Matern models.
rspde_lme( formula, loc, loc_time = NULL, data, model = NULL, repl = NULL, which_repl = NULL, optim_method = "L-BFGS-B", use_data_from_graph = TRUE, starting_values_latent = NULL, start_sigma_e = NULL, start_alpha = NULL, alpha = NULL, start_nu = NULL, nu = NULL, nu_upper_bound = 4, rspde_order = NULL, parallel = FALSE, n_cores = parallel::detectCores() - 1, optim_controls = list(), improve_hessian = FALSE, hessian_args = list() )
rspde_lme( formula, loc, loc_time = NULL, data, model = NULL, repl = NULL, which_repl = NULL, optim_method = "L-BFGS-B", use_data_from_graph = TRUE, starting_values_latent = NULL, start_sigma_e = NULL, start_alpha = NULL, alpha = NULL, start_nu = NULL, nu = NULL, nu_upper_bound = 4, rspde_order = NULL, parallel = FALSE, n_cores = parallel::detectCores() - 1, optim_controls = list(), improve_hessian = FALSE, hessian_args = list() )
formula |
Formula object describing the relation between the response variables and the fixed effects. If the response variable is a matrix, each column of the matrix will be treated as a replicate. |
loc |
A vector with the names of the columns in |
loc_time |
For spatio-temporal models, the name of the column in |
data |
A |
model |
Object generated by |
repl |
Vector indicating the replicate of each observation.
If |
which_repl |
Which replicates to use? If |
optim_method |
The method to be used with |
use_data_from_graph |
Logical. Only for models generated from graphs from
|
starting_values_latent |
A vector containing the starting values for the
latent model. If the latent model is generated by |
start_sigma_e |
Starting value for the standard deviation of the measurement error. |
start_alpha |
Starting value for the smoothness parameter of spatial
models. Will be used if |
alpha |
If |
start_nu |
Starting value for the smoothness parameter of spatial models. Not used for spatio-temporal models. |
nu |
If |
nu_upper_bound |
A parameter that limits the maximum value that nu can assume. Not used for spatio-temporal models. |
rspde_order |
The order of the rational approximation to be used while fitting the model. If not given, the order from the model object will be used. Not used for spatio-temporal models. |
parallel |
logical. Indicating whether to use optimParallel or not. |
n_cores |
Number of cores to be used if parallel is true. |
optim_controls |
Additional controls to be passed to |
improve_hessian |
Should a more precise estimate of the hessian be obtained? Turning on might increase the overall time. |
hessian_args |
List of controls to be used if |
A list containing the fitted model.
A finite element discretization on R can be written as
where
is a piecewise linear
"hat function" centered at location
. This function computes an
matrix
that links the basis function in the expansion to specified locations
in the domain through
.
rSPDE.A1d(x, loc)
rSPDE.A1d(x, loc)
x |
The locations of the nodes in the FEM discretization. |
loc |
The locations |
The sparse matrix A
.
David Bolin [email protected]
# create mass and stiffness matrices for a FEM discretization on [0,1] x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # create the observation matrix for some locations in the domain obs.loc <- runif(n = 10, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc)
# create mass and stiffness matrices for a FEM discretization on [0,1] x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # create the observation matrix for some locations in the domain obs.loc <- runif(n = 10, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc)
rspde.anistropic2d
computes a Finite Element Method (FEM) approximation of a
Gaussian random field defined as the solution to the stochastic partial
differential equation (SPDE):
, based on a SPDE representation of the form
,
where $c>0$ is a constant. The matrix is defined as
rspde.anistropic2d( mesh, nu = NULL, nu.upper.bound = 2, rspde.order = 1, prior.hx = NULL, prior.hy = NULL, prior.hxy = NULL, prior.sigma = NULL, prior.precision = NULL, prior.nu = NULL, prior.nu.dist = "lognormal", nu.prec.inc = 0.01, type.rational.approx = "chebfun", shared_lib = "detect", debug = FALSE, ... )
rspde.anistropic2d( mesh, nu = NULL, nu.upper.bound = 2, rspde.order = 1, prior.hx = NULL, prior.hy = NULL, prior.hxy = NULL, prior.sigma = NULL, prior.precision = NULL, prior.nu = NULL, prior.nu.dist = "lognormal", nu.prec.inc = 0.01, type.rational.approx = "chebfun", shared_lib = "detect", debug = FALSE, ... )
mesh |
Spatial mesh for the FEM approximation. |
nu |
If nu is set to a parameter, nu will be kept fixed and will not
be estimated. If nu is |
nu.upper.bound |
Upper bound for the smoothness parameter |
rspde.order |
The order of the covariance-based rational SPDE approach. The default order is 1. |
prior.hx |
A list specifying the prior for the parameter |
prior.hy |
A list specifying the prior for the parameter |
prior.hxy |
A list specifying the prior for the parameter |
prior.sigma |
A list specifying the prior for the variance parameter |
prior.precision |
A precision matrix for |
prior.nu |
a list containing the elements |
prior.nu.dist |
The distribution of the smoothness parameter. The current options are "beta" or "lognormal". The default is "lognormal". |
nu.prec.inc |
Amount to increase the precision in the beta prior distribution. Check details below. |
type.rational.approx |
Which type of rational approximation should be used? The current types are "chebfun", "brasil" or "chebfunLB". |
shared_lib |
String specifying which shared library to use for the Cgeneric implementation. Options are "detect", "INLA", or "rSPDE". You may also specify the direct path to a .so (or .dll) file. |
debug |
Logical value indicating whether to enable INLA debug mode. |
... |
Additional arguments passed internally for configuration purposes. |
An object of class inla_rspde_spacetime
representing the FEM approximation of
the space-time Gaussian random field.
library(fmesher) n_loc <- 2000 loc_2d_mesh <- matrix(runif(n_loc * 2), n_loc, 2) mesh_2d <- fm_mesh_2d(loc = loc_2d_mesh, cutoff = 0.03, max.edge = c(0.1, 0.5)) aniso_model <- rspde.anistropic2d(mesh = mesh_2d)
library(fmesher) n_loc <- 2000 loc_2d_mesh <- matrix(runif(n_loc * 2), n_loc, 2) mesh_2d <- fm_mesh_2d(loc = loc_2d_mesh, cutoff = 0.03, max.edge = c(0.1, 0.5)) aniso_model <- rspde.anistropic2d(mesh = mesh_2d)
Observation matrix for space-time models
rSPDE.Ast( mesh_space = NULL, space_loc = NULL, mesh_time = NULL, time_loc = NULL, graph = NULL, obs.s = NULL, obs.t = NULL )
rSPDE.Ast( mesh_space = NULL, space_loc = NULL, mesh_time = NULL, time_loc = NULL, graph = NULL, obs.s = NULL, obs.t = NULL )
mesh_space |
mesh object for models on 1d or 2d domains |
space_loc |
mesh locations for models on 1d domains |
mesh_time |
mesh object for time discretization |
time_loc |
mesh locations for time discretization |
graph |
MetricGraph object for models on metric graphs |
obs.s |
spatial locations of observations |
obs.t |
time points for observations |
Observation matrix linking observation locations to mesh nodes
s <- seq(from = 0, to = 20, length.out = 11) t <- seq(from = 0, to = 20, length.out = 5) n.obs <- 10 obs.loc <- data.frame(x = max(s)*runif(n.obs), t = max(t)*runif(n.obs)) A <- rSPDE.Ast(space_loc = s,time_loc = t, obs.s = obs.loc$x, obs.t = obs.loc$t)
s <- seq(from = 0, to = 20, length.out = 11) t <- seq(from = 0, to = 20, length.out = 5) n.obs <- 10 obs.loc <- data.frame(x = max(s)*runif(n.obs), t = max(t)*runif(n.obs)) A <- rSPDE.Ast(space_loc = s,time_loc = t, obs.s = obs.loc$x, obs.t = obs.loc$t)
This function returns a log-likelihood function for a
Gaussian process with a Matern covariance
function, that is observed under Gaussian measurement noise:
, where
are
iid mean-zero Gaussian variables. The latent model is approximated using
the a rational approximation
of the fractional SPDE model corresponding to the Gaussian process.
rSPDE.construct.matern.loglike( object, Y, A, sigma.e = NULL, mu = 0, user_nu = NULL, user_tau = NULL, user_kappa = NULL, user_sigma = NULL, user_range = NULL, parameterization = c("spde", "matern"), user_m = NULL, log_scale = TRUE, return_negative_likelihood = TRUE )
rSPDE.construct.matern.loglike( object, Y, A, sigma.e = NULL, mu = 0, user_nu = NULL, user_tau = NULL, user_kappa = NULL, user_sigma = NULL, user_range = NULL, parameterization = c("spde", "matern"), user_m = NULL, log_scale = TRUE, return_negative_likelihood = TRUE )
object |
The rational SPDE approximation,
computed using |
Y |
The observations, either a vector or a matrix where the columns correspond to independent replicates of observations. |
A |
An observation matrix that links the measurement location to the finite element basis. |
sigma.e |
IF non-null, the standard deviation of the measurement noise will be kept fixed in the returned likelihood. |
mu |
Expectation vector of the latent field (default = 0). |
user_nu |
If non-null, the shape parameter will be kept fixed in the returned likelihood. |
user_tau |
If non-null, the tau parameter will be kept fixed in the returned likelihood. (Replaces sigma) |
user_kappa |
If non-null, the range parameter will be kept fixed in the returned likelihood. |
user_sigma |
If non-null, the standard deviation will be kept fixed in the returned likelihood. |
user_range |
If non-null, the range parameter will be kept fixed in the returned likelihood. (Replaces kappa) |
parameterization |
If |
user_m |
If non-null, update the order of the rational approximation, which needs to be a positive integer. |
log_scale |
Should the parameters be evaluated in log-scale? |
return_negative_likelihood |
Return minus the likelihood to turn the maximization into a minimization? |
The log-likelihood function. The parameters of the returned function are given in the order sigma, kappa, nu, sigma.e, whenever they are available.
matern.operators()
, predict.CBrSPDEobj()
# this example illustrates how the function can be used for maximum # likelihood estimation set.seed(123) # Sample a Gaussian Matern process on R using a rational approximation nu <- 0.8 sigma <- 1 sigma.e <- 0.1 n.rep <- 10 n.obs <- 200 n.x <- 51 range <- 0.2 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = n.x) # Compute the covariance-based rational approximation op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) # Sample the model u <- simulate(op_cov, n.rep) # Create some data obs.loc <- runif(n = n.obs, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc) noise <- rnorm(n.obs * n.rep) dim(noise) <- c(n.obs, n.rep) Y <- as.matrix(A %*% u + sigma.e * noise) # Define the negative likelihood function for optimization # using CBrSPDE.matern.loglike # Matern parameterization loglike <- rSPDE.construct.matern.loglike(op_cov, Y, A, parameterization = "matern") # The parameters can now be estimated by minimizing mlik with optim # Choose some reasonable starting values depending on the size of the domain theta0 <- c( get.initial.values.rSPDE(mesh.range = 1, dim = 1), log(0.1 * sd(as.vector(Y))) ) # run estimation and display the results theta <- optim(theta0, loglike, method = "L-BFGS-B" ) print(data.frame( sigma = c(sigma, exp(theta$par[1])), range = c(range, exp(theta$par[2])), nu = c(nu, exp(theta$par[3])), sigma.e = c(sigma.e, exp(theta$par[4])), row.names = c("Truth", "Estimates") ))
# this example illustrates how the function can be used for maximum # likelihood estimation set.seed(123) # Sample a Gaussian Matern process on R using a rational approximation nu <- 0.8 sigma <- 1 sigma.e <- 0.1 n.rep <- 10 n.obs <- 200 n.x <- 51 range <- 0.2 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = n.x) # Compute the covariance-based rational approximation op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) # Sample the model u <- simulate(op_cov, n.rep) # Create some data obs.loc <- runif(n = n.obs, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc) noise <- rnorm(n.obs * n.rep) dim(noise) <- c(n.obs, n.rep) Y <- as.matrix(A %*% u + sigma.e * noise) # Define the negative likelihood function for optimization # using CBrSPDE.matern.loglike # Matern parameterization loglike <- rSPDE.construct.matern.loglike(op_cov, Y, A, parameterization = "matern") # The parameters can now be estimated by minimizing mlik with optim # Choose some reasonable starting values depending on the size of the domain theta0 <- c( get.initial.values.rSPDE(mesh.range = 1, dim = 1), log(0.1 * sd(as.vector(Y))) ) # run estimation and display the results theta <- optim(theta0, loglike, method = "L-BFGS-B" ) print(data.frame( sigma = c(sigma, exp(theta$par[1])), range = c(range, exp(theta$par[2])), nu = c(nu, exp(theta$par[3])), sigma.e = c(sigma.e, exp(theta$par[4])), row.names = c("Truth", "Estimates") ))
This function computes mass and stiffness matrices for a FEM approximation on R, assuming Neumann boundary conditions. These matrices are needed when discretizing the operators in rational approximations.
rSPDE.fem1d(x)
rSPDE.fem1d(x)
x |
Locations of the nodes in the FEM approximation. |
The function returns a list with the following elements
G |
The stiffness matrix with elements |
C |
The mass matrix with elements |
Cd |
Mass lumped mass matrix. |
B |
Matrix with elements |
David Bolin [email protected]
# create mass and stiffness matrices for a FEM discretization on [0,1] x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x)
# create mass and stiffness matrices for a FEM discretization on [0,1] x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x)
This function computes mass and stiffness matrices for a mesh in 2D, assuming Neumann boundary conditions.
rSPDE.fem2d(FV, P)
rSPDE.fem2d(FV, P)
FV |
Matrix where each row defines a triangle |
P |
Locations of the nodes in the mesh. |
The function returns a list with the following elements
G |
The stiffness matrix with elements |
C |
The mass matrix with elements |
Cd |
The mass lumped matrix with diagonal elements |
Hxx |
Matrix with elements |
Hyy |
Matrix with elements |
Hxy |
Matrix with elements |
Hyx |
Matrix with elements |
Bx |
Matrix with elements |
By |
Matrix with elements |
David Bolin [email protected]
P <- rbind(c(0, 0), c(1, 0), c(1, 1), c(0, 1)) FV <- rbind(c(1, 2, 3), c(2, 3, 4)) fem <- rSPDE.fem2d(FV, P)
P <- rbind(c(0, 0), c(1, 0), c(1, 1), c(0, 1)) FV <- rbind(c(1, 2, 3), c(2, 3, 4)) fem <- rSPDE.fem2d(FV, P)
This function evaluates the log-likelihood function for a
fractional SPDE model
that is observed under
Gaussian measurement noise:
,
where
are iid mean-zero Gaussian variables and
, where
is the expectation vector of the latent field.
rSPDE.loglike(obj, Y, A, sigma.e, mu = 0)
rSPDE.loglike(obj, Y, A, sigma.e, mu = 0)
obj |
The rational SPDE approximation, computed using
|
Y |
The observations, either a vector or a matrix where the columns correspond to independent replicates of observations. |
A |
An observation matrix that links the measurement location to the finite element basis. |
sigma.e |
The standard deviation of the measurement noise. |
mu |
Expectation vector of the latent field (default = 0). |
The log-likelihood value.
This example below shows how the function can be used to evaluate the likelihood of a latent Matern model.
# Sample a Gaussian Matern process on R using a rational approximation kappa <- 10 sigma <- 1 nu <- 0.8 sigma.e <- 0.3 range <- 0.2 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation op <- matern.operators( range = range, sigma = sigma, nu = nu, loc_mesh = x, d = 1, type = "operator", parameterization = "matern" ) # Sample the model u <- simulate(op) # Create some data obs.loc <- runif(n = 10, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc) Y <- as.vector(A %*% u + sigma.e * rnorm(10)) # compute log-likelihood of the data lik1 <- rSPDE.loglike(op, Y, A, sigma.e) cat(lik1)
# Sample a Gaussian Matern process on R using a rational approximation kappa <- 10 sigma <- 1 nu <- 0.8 sigma.e <- 0.3 range <- 0.2 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation op <- matern.operators( range = range, sigma = sigma, nu = nu, loc_mesh = x, d = 1, type = "operator", parameterization = "matern" ) # Sample the model u <- simulate(op) # Create some data obs.loc <- runif(n = 10, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc) Y <- as.vector(A %*% u + sigma.e * rnorm(10)) # compute log-likelihood of the data lik1 <- rSPDE.loglike(op, Y, A, sigma.e) cat(lik1)
Constructs observation/prediction weight matrices
for rSPDE models based on inla.mesh
or
inla.mesh.1d
objects.
rspde.make.A( mesh = NULL, loc = NULL, A = NULL, dim = NULL, rspde.order = 1, nu = NULL, index = NULL, group = NULL, repl = 1L, n.group = NULL, n.repl = NULL )
rspde.make.A( mesh = NULL, loc = NULL, A = NULL, dim = NULL, rspde.order = 1, nu = NULL, index = NULL, group = NULL, repl = 1L, n.group = NULL, n.repl = NULL )
mesh |
An |
loc |
Locations, needed if an INLA mesh is provided |
A |
The A matrix from the standard SPDE approach, such as the matrix
returned by |
dim |
the dimension. Should only be provided if an
|
rspde.order |
The order of the covariance-based rational SPDE approach. |
nu |
If |
index |
For each observation/prediction value, an index into loc. Default is seq_len(nrow(A.loc)). |
group |
For each observation/prediction value, an index into the group model. |
repl |
For each observation/prediction value, the replicate index. |
n.group |
The size of the group model. |
n.repl |
The total number of replicates. |
The matrix for rSPDE models.
# devel version if (requireNamespace("INLA", quietly = TRUE)) { library(INLA) set.seed(123) loc <- matrix(runif(100 * 2) * 100, 100, 2) mesh <- inla.mesh.2d( loc = loc, cutoff = 50, max.edge = c(50, 500) ) A <- rspde.make.A(mesh, loc = loc, rspde.order = 3) } # devel.tag
# devel version if (requireNamespace("INLA", quietly = TRUE)) { library(INLA) set.seed(123) loc <- matrix(runif(100 * 2) * 100, 100, 2) mesh <- inla.mesh.2d( loc = loc, cutoff = 50, max.edge = c(50, 500) ) A <- rspde.make.A(mesh, loc = loc, rspde.order = 3) } # devel.tag
Generates a list of named index vectors for an rSPDE model.
rspde.make.index( name, n.spde = NULL, n.group = 1, n.repl = 1, mesh = NULL, rspde.order = 1, nu = NULL, dim = NULL )
rspde.make.index( name, n.spde = NULL, n.group = 1, n.repl = 1, mesh = NULL, rspde.order = 1, nu = NULL, dim = NULL )
name |
A character string with the base name of the effect. |
n.spde |
The number of basis functions in the mesh model. |
n.group |
The size of the group model. |
n.repl |
The total number of replicates. |
mesh |
An |
rspde.order |
The order of the rational approximation |
nu |
If |
dim |
the dimension of the domain. Should only be provided if
|
A list of named index vectors.
name |
Indices into the vector of latent variables |
name.group |
'group' indices |
name.repl |
Indices for replicates |
# devel version if (requireNamespace("INLA", quietly = TRUE)) { library(INLA) set.seed(123) m <- 100 loc_2d_mesh <- matrix(runif(m * 2), m, 2) mesh_2d <- inla.mesh.2d( loc = loc_2d_mesh, cutoff = 0.05, max.edge = c(0.1, 0.5) ) sigma <- 1 range <- 0.2 nu <- 0.8 kappa <- sqrt(8 * nu) / range op <- matern.operators( mesh = mesh_2d, nu = nu, range = range, sigma = sigma, m = 2, parameterization = "matern" ) u <- simulate(op) A <- inla.spde.make.A( mesh = mesh_2d, loc = loc_2d_mesh ) sigma.e <- 0.1 y <- A %*% u + rnorm(m) * sigma.e Abar <- rspde.make.A(mesh = mesh_2d, loc = loc_2d_mesh) mesh.index <- rspde.make.index(name = "field", mesh = mesh_2d) st.dat <- inla.stack( data = list(y = as.vector(y)), A = Abar, effects = mesh.index ) rspde_model <- rspde.matern( mesh = mesh_2d, nu.upper.bound = 2 ) f <- y ~ -1 + f(field, model = rspde_model) rspde_fit <- inla(f, data = inla.stack.data(st.dat), family = "gaussian", control.predictor = list(A = inla.stack.A(st.dat)) ) result <- rspde.result(rspde_fit, "field", rspde_model) summary(result) } # devel.tag
# devel version if (requireNamespace("INLA", quietly = TRUE)) { library(INLA) set.seed(123) m <- 100 loc_2d_mesh <- matrix(runif(m * 2), m, 2) mesh_2d <- inla.mesh.2d( loc = loc_2d_mesh, cutoff = 0.05, max.edge = c(0.1, 0.5) ) sigma <- 1 range <- 0.2 nu <- 0.8 kappa <- sqrt(8 * nu) / range op <- matern.operators( mesh = mesh_2d, nu = nu, range = range, sigma = sigma, m = 2, parameterization = "matern" ) u <- simulate(op) A <- inla.spde.make.A( mesh = mesh_2d, loc = loc_2d_mesh ) sigma.e <- 0.1 y <- A %*% u + rnorm(m) * sigma.e Abar <- rspde.make.A(mesh = mesh_2d, loc = loc_2d_mesh) mesh.index <- rspde.make.index(name = "field", mesh = mesh_2d) st.dat <- inla.stack( data = list(y = as.vector(y)), A = Abar, effects = mesh.index ) rspde_model <- rspde.matern( mesh = mesh_2d, nu.upper.bound = 2 ) f <- y ~ -1 + f(field, model = rspde_model) rspde_fit <- inla(f, data = inla.stack.data(st.dat), family = "gaussian", control.predictor = list(A = inla.stack.A(st.dat)) ) result <- rspde.result(rspde_fit, "field", rspde_model) summary(result) } # devel.tag
Creates an INLA object for a stationary Matern model with general smoothness parameter.
rspde.matern( mesh, nu.upper.bound = NULL, rspde.order = 1, nu = NULL, B.sigma = matrix(c(0, 1, 0), 1, 3), B.range = matrix(c(0, 0, 1), 1, 3), parameterization = c("spde", "matern", "matern2"), B.tau = matrix(c(0, 1, 0), 1, 3), B.kappa = matrix(c(0, 0, 1), 1, 3), start.nu = NULL, start.theta = NULL, prior.nu = NULL, theta.prior.mean = NULL, theta.prior.prec = 0.1, prior.std.dev.nominal = 1, prior.range.nominal = NULL, prior.kappa.mean = NULL, prior.tau.mean = NULL, start.lstd.dev = NULL, start.lrange = NULL, start.ltau = NULL, start.lkappa = NULL, prior.theta.param = c("theta", "spde"), prior.nu.dist = c("beta", "lognormal"), nu.prec.inc = 1, type.rational.approx = c("chebfun", "brasil", "chebfunLB"), debug = FALSE, shared_lib = "detect", ... )
rspde.matern( mesh, nu.upper.bound = NULL, rspde.order = 1, nu = NULL, B.sigma = matrix(c(0, 1, 0), 1, 3), B.range = matrix(c(0, 0, 1), 1, 3), parameterization = c("spde", "matern", "matern2"), B.tau = matrix(c(0, 1, 0), 1, 3), B.kappa = matrix(c(0, 0, 1), 1, 3), start.nu = NULL, start.theta = NULL, prior.nu = NULL, theta.prior.mean = NULL, theta.prior.prec = 0.1, prior.std.dev.nominal = 1, prior.range.nominal = NULL, prior.kappa.mean = NULL, prior.tau.mean = NULL, start.lstd.dev = NULL, start.lrange = NULL, start.ltau = NULL, start.lkappa = NULL, prior.theta.param = c("theta", "spde"), prior.nu.dist = c("beta", "lognormal"), nu.prec.inc = 1, type.rational.approx = c("chebfun", "brasil", "chebfunLB"), debug = FALSE, shared_lib = "detect", ... )
mesh |
The mesh to build the model. It can be an |
nu.upper.bound |
Upper bound for the smoothness parameter. If |
rspde.order |
The order of the covariance-based rational SPDE approach. The default order is 1. |
nu |
If nu is set to a parameter, nu will be kept fixed and will not
be estimated. If nu is |
B.sigma |
Matrix with specification of log-linear model for |
B.range |
Matrix with specification of log-linear model for |
parameterization |
Which parameterization to use? |
B.tau |
Matrix with specification of log-linear model for |
B.kappa |
Matrix with specification of log-linear model for |
start.nu |
Starting value for nu. |
start.theta |
Starting values for the model parameters. In the stationary case, if |
prior.nu |
a list containing the elements |
theta.prior.mean |
A vector for the mean priors of |
theta.prior.prec |
A precision matrix for the prior of |
prior.std.dev.nominal |
Prior std. deviation to be used for the priors and for the starting values. |
prior.range.nominal |
Prior range to be used for the priors and for the starting values. |
prior.kappa.mean |
Prior kappa to be used for the priors and for the starting values. |
prior.tau.mean |
Prior tau to be used for the priors and for the starting values. |
start.lstd.dev |
Starting value for log of std. deviation. Will not be used if start.ltau is non-null. Will be only used in the stationary case and if |
start.lrange |
Starting value for log of range. Will not be used if start.lkappa is non-null. Will be only used in the stationary case and if |
start.ltau |
Starting value for log of tau. Will be only used in the stationary case and if |
start.lkappa |
Starting value for log of kappa. Will be only used in the stationary case and if |
prior.theta.param |
Should the lognormal prior be on |
prior.nu.dist |
The distribution of the smoothness parameter. The current options are "beta" or "lognormal". The default is "lognormal". |
nu.prec.inc |
Amount to increase the precision in the beta prior distribution. Check details below. |
type.rational.approx |
Which type of rational approximation should be used? The current types are "chebfun", "brasil" or "chebfunLB". |
debug |
INLA debug argument |
shared_lib |
Which shared lib to use for the cgeneric implementation? If "detect", it will check if the shared lib exists locally, in which case it will use it. Otherwise it will use INLA's shared library. If "INLA", it will use the shared lib from INLA's installation. If 'rSPDE', then it will use the local installation (does not work if your installation is from CRAN). Otherwise, you can directly supply the path of the .so (or .dll) file. |
... |
Only being used internally. |
prior.kappa |
a |
prior.tau |
a list containing the elements |
prior.range |
a |
prior.std.dev |
a |
An INLA model.
Creates an INLA object for a stationary intrinsic Matern model. Currently, alpha is fixed to 2 and beta is fixed to 1.
rspde.intrinsic.matern( mesh, alpha = 2, mean.correction = FALSE, prior.lkappa.mean = NULL, prior.ltau.mean = 1, prior.lkappa.prec = 0.1, prior.ltau.prec = 0.1, start.ltau = NULL, start.lkappa = NULL, true.scaling = TRUE, diagonal = 0, debug = FALSE, shared_lib = "detect", ... )
rspde.intrinsic.matern( mesh, alpha = 2, mean.correction = FALSE, prior.lkappa.mean = NULL, prior.ltau.mean = 1, prior.lkappa.prec = 0.1, prior.ltau.prec = 0.1, start.ltau = NULL, start.lkappa = NULL, true.scaling = TRUE, diagonal = 0, debug = FALSE, shared_lib = "detect", ... )
mesh |
The mesh to build the model. It can be an |
alpha |
Smoothness parameter, need to be 1 or 2. |
mean.correction |
Add mean correction for extreme value models? |
prior.lkappa.mean |
Prior on log kappa to be used for the priors and for the starting values. |
prior.ltau.mean |
Prior on log tau to be used for the priors and for the starting values. |
prior.lkappa.prec |
Precision to be used on the prior on log kappa to be used for the priors and for the starting values. |
prior.ltau.prec |
Precision to be used on the prior on log tau to be used for the priors and for the starting values. |
start.ltau |
Starting value for log of tau. |
start.lkappa |
Starting value for log of kappa. |
true.scaling |
Compute the true normalizing constant manually? Default |
diagonal |
Value of diagonal correction for INLA stability. Default 0. |
debug |
INLA debug argument |
shared_lib |
Which shared lib to use for the cgeneric implementation? If "detect", it will check if the shared lib exists locally, in which case it will use it. Otherwise it will use INLA's shared library. If "INLA", it will use the shared lib from INLA's installation. If 'rSPDE', then it will use the local installation (does not work if your installation is from CRAN). Otherwise, you can directly supply the path of the .so (or .dll) file. |
... |
Only being used internally. |
An INLA model.
This function evaluates the log-likelihood function for a
Gaussian process with a Matern covariance
function, that is observed under Gaussian measurement noise:
, where
are
iid mean-zero Gaussian variables. The latent model is approximated using
the a rational approximation
of the fractional SPDE model corresponding to the Gaussian process.
rSPDE.matern.loglike( object, Y, A, sigma.e, mu = 0, user_nu = NULL, user_kappa = NULL, user_sigma = NULL, user_range = NULL, user_tau = NULL, user_m = NULL )
rSPDE.matern.loglike( object, Y, A, sigma.e, mu = 0, user_nu = NULL, user_kappa = NULL, user_sigma = NULL, user_range = NULL, user_tau = NULL, user_m = NULL )
object |
The rational SPDE approximation,
computed using |
Y |
The observations, either a vector or a matrix where the columns correspond to independent replicates of observations. |
A |
An observation matrix that links the measurement location to the finite element basis. |
sigma.e |
The standard deviation of the measurement noise. |
mu |
Expectation vector of the latent field (default = 0). |
user_nu |
If non-null, update the shape parameter of the covariance function. |
user_kappa |
If non-null, update the range parameter of the covariance function. |
user_sigma |
If non-null, update the standard deviation of the covariance function. |
user_range |
If non-null, update the range parameter of the covariance function. |
user_tau |
If non-null, update the parameter tau. |
user_m |
If non-null, update the order of the rational approximation, which needs to be a positive integer. |
The log-likelihood value.
matern.operators()
, predict.CBrSPDEobj()
# this example illustrates how the function can be used for maximum likelihood estimation set.seed(123) # Sample a Gaussian Matern process on R using a rational approximation nu <- 0.8 kappa <- 5 sigma <- 1 sigma.e <- 0.1 n.rep <- 10 n.obs <- 100 n.x <- 51 range <- 0.2 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = n.x) fem <- rSPDE.fem1d(x) tau <- sqrt(gamma(nu) / (sigma^2 * kappa^(2 * nu) * (4 * pi)^(1 / 2) * gamma(nu + 1 / 2))) # Compute the covariance-based rational approximation op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) # Sample the model u <- simulate(op_cov, n.rep) # Create some data obs.loc <- runif(n = n.obs, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc) noise <- rnorm(n.obs * n.rep) dim(noise) <- c(n.obs, n.rep) Y <- as.matrix(A %*% u + sigma.e * noise) # Define the negative likelihood function for optimization # using CBrSPDE.matern.loglike # Notice that we are also using sigma instead of tau, so it can be compared # to matern.loglike() mlik_cov <- function(theta, Y, A, op_cov) { kappa <- exp(theta[1]) sigma <- exp(theta[2]) nu <- exp(theta[3]) return(-rSPDE.matern.loglike( object = op_cov, Y = Y, A = A, user_kappa = kappa, user_sigma = sigma, user_nu = nu, sigma.e = exp(theta[4]) )) } # The parameters can now be estimated by minimizing mlik with optim # Choose some reasonable starting values depending on the size of the domain theta0 <- log(c(sqrt(8), 1 / sqrt(var(c(Y))), 0.9, 0.01)) # run estimation and display the results theta <- optim(theta0, mlik_cov, Y = Y, A = A, op_cov = op_cov, method = "L-BFGS-B" ) print(data.frame( range = c(range, exp(theta$par[1])), sigma = c(sigma, exp(theta$par[2])), nu = c(nu, exp(theta$par[3])), sigma.e = c(sigma.e, exp(theta$par[4])), row.names = c("Truth", "Estimates") ))
# this example illustrates how the function can be used for maximum likelihood estimation set.seed(123) # Sample a Gaussian Matern process on R using a rational approximation nu <- 0.8 kappa <- 5 sigma <- 1 sigma.e <- 0.1 n.rep <- 10 n.obs <- 100 n.x <- 51 range <- 0.2 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = n.x) fem <- rSPDE.fem1d(x) tau <- sqrt(gamma(nu) / (sigma^2 * kappa^(2 * nu) * (4 * pi)^(1 / 2) * gamma(nu + 1 / 2))) # Compute the covariance-based rational approximation op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) # Sample the model u <- simulate(op_cov, n.rep) # Create some data obs.loc <- runif(n = n.obs, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc) noise <- rnorm(n.obs * n.rep) dim(noise) <- c(n.obs, n.rep) Y <- as.matrix(A %*% u + sigma.e * noise) # Define the negative likelihood function for optimization # using CBrSPDE.matern.loglike # Notice that we are also using sigma instead of tau, so it can be compared # to matern.loglike() mlik_cov <- function(theta, Y, A, op_cov) { kappa <- exp(theta[1]) sigma <- exp(theta[2]) nu <- exp(theta[3]) return(-rSPDE.matern.loglike( object = op_cov, Y = Y, A = A, user_kappa = kappa, user_sigma = sigma, user_nu = nu, sigma.e = exp(theta[4]) )) } # The parameters can now be estimated by minimizing mlik with optim # Choose some reasonable starting values depending on the size of the domain theta0 <- log(c(sqrt(8), 1 / sqrt(var(c(Y))), 0.9, 0.01)) # run estimation and display the results theta <- optim(theta0, mlik_cov, Y = Y, A = A, op_cov = op_cov, method = "L-BFGS-B" ) print(data.frame( range = c(range, exp(theta$par[1])), sigma = c(sigma, exp(theta$par[2])), nu = c(nu, exp(theta$par[3])), sigma.e = c(sigma.e, exp(theta$par[4])), row.names = c("Truth", "Estimates") ))
rspde.matern.precision
is used for computing the
precision matrix of the
covariance-based rational SPDE approximation of a stationary Gaussian random
fields on with a Matern covariance function
rspde.matern.precision( kappa, nu, tau = NULL, sigma = NULL, rspde.order, dim, fem_mesh_matrices, only_fractional = FALSE, return_block_list = FALSE, type_rational_approx = "chebfun" )
rspde.matern.precision( kappa, nu, tau = NULL, sigma = NULL, rspde.order, dim, fem_mesh_matrices, only_fractional = FALSE, return_block_list = FALSE, type_rational_approx = "chebfun" )
kappa |
Range parameter of the covariance function. |
nu |
Shape parameter of the covariance function. |
tau |
Scale parameter of the covariance function. If sigma is not provided, tau should be provided. |
sigma |
Standard deviation of the covariance function. If tau is not provided, sigma should be provided. |
rspde.order |
The order of the rational approximation |
dim |
The dimension of the domain |
fem_mesh_matrices |
A list containing the FEM-related matrices. The list should contain elements c0, g1, g2, g3, etc. |
only_fractional |
Logical. Should only the fractional-order part of the precision matrix be returned? |
return_block_list |
Logical. For |
type_rational_approx |
Which type of rational approximation should be used? The current types are "chebfun", "brasil" or "chebfunLB". |
The precision matrix
set.seed(123) nobs <- 101 x <- seq(from = 0, to = 1, length.out = nobs) fem <- rSPDE.fem1d(x) kappa <- 40 sigma <- 1 d <- 1 nu <- 2.6 tau <- sqrt(gamma(nu) / (kappa^(2 * nu) * (4 * pi)^(d / 2) * gamma(nu + d / 2))) range <- sqrt(8 * nu) / kappa op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, compute_higher_order = TRUE, parameterization = "matern" ) v <- t(rSPDE.A1d(x, 0.5)) c.true <- matern.covariance(abs(x - 0.5), kappa, nu, sigma) Q <- rspde.matern.precision( kappa = kappa, nu = nu, tau = tau, rspde.order = 2, d = 1, fem_mesh_matrices = op_cov$fem_mesh_matrices ) A <- Diagonal(nobs) Abar <- cbind(A, A, A) w <- rbind(v, v, v) c.approx_cov <- (Abar) %*% solve(Q, w) # plot the result and compare with the true Matern covariance plot(x, matern.covariance(abs(x - 0.5), kappa, nu, sigma), type = "l", ylab = "C(h)", xlab = "h", main = "Matern covariance and rational approximations" ) lines(x, c.approx_cov, col = 2)
set.seed(123) nobs <- 101 x <- seq(from = 0, to = 1, length.out = nobs) fem <- rSPDE.fem1d(x) kappa <- 40 sigma <- 1 d <- 1 nu <- 2.6 tau <- sqrt(gamma(nu) / (kappa^(2 * nu) * (4 * pi)^(d / 2) * gamma(nu + d / 2))) range <- sqrt(8 * nu) / kappa op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, compute_higher_order = TRUE, parameterization = "matern" ) v <- t(rSPDE.A1d(x, 0.5)) c.true <- matern.covariance(abs(x - 0.5), kappa, nu, sigma) Q <- rspde.matern.precision( kappa = kappa, nu = nu, tau = tau, rspde.order = 2, d = 1, fem_mesh_matrices = op_cov$fem_mesh_matrices ) A <- Diagonal(nobs) Abar <- cbind(A, A, A) w <- rbind(v, v, v) c.approx_cov <- (Abar) %*% solve(Q, w) # plot the result and compare with the true Matern covariance plot(x, matern.covariance(abs(x - 0.5), kappa, nu, sigma), type = "l", ylab = "C(h)", xlab = "h", main = "Matern covariance and rational approximations" ) lines(x, c.approx_cov, col = 2)
rspde.matern.precision.integer.opt
is
used for computing the precision matrix of stationary
Gaussian random fields on with a Matern
covariance function
,
where is a natural number.
rspde.matern.precision.integer( kappa, nu, tau = NULL, sigma = NULL, dim, fem_mesh_matrices )
rspde.matern.precision.integer( kappa, nu, tau = NULL, sigma = NULL, dim, fem_mesh_matrices )
kappa |
Range parameter of the covariance function. |
nu |
Shape parameter of the covariance function. |
tau |
Scale parameter of the covariance function. |
sigma |
Standard deviation of the covariance function. If tau is not provided, sigma should be provided. |
dim |
The dimension of the domain |
fem_mesh_matrices |
A list containing the FEM-related matrices. The list should contain elements c0, g1, g2, g3, etc. |
The precision matrix
set.seed(123) nobs <- 101 x <- seq(from = 0, to = 1, length.out = nobs) fem <- rSPDE.fem1d(x) kappa <- 40 sigma <- 1 d <- 1 nu <- 0.5 tau <- sqrt(gamma(nu) / (kappa^(2 * nu) * (4 * pi)^(d / 2) * gamma(nu + d / 2))) range <- sqrt(8 * nu) / kappa op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) v <- t(rSPDE.A1d(x, 0.5)) c.true <- matern.covariance(abs(x - 0.5), kappa, nu, sigma) Q <- rspde.matern.precision.integer( kappa = kappa, nu = nu, tau = tau, d = 1, fem_mesh_matrices = op_cov$fem_mesh_matrices ) A <- Diagonal(nobs) c.approx_cov <- A %*% solve(Q, v) # plot the result and compare with the true Matern covariance plot(x, matern.covariance(abs(x - 0.5), kappa, nu, sigma), type = "l", ylab = "C(h)", xlab = "h", main = "Matern covariance and rational approximations" ) lines(x, c.approx_cov, col = 2)
set.seed(123) nobs <- 101 x <- seq(from = 0, to = 1, length.out = nobs) fem <- rSPDE.fem1d(x) kappa <- 40 sigma <- 1 d <- 1 nu <- 0.5 tau <- sqrt(gamma(nu) / (kappa^(2 * nu) * (4 * pi)^(d / 2) * gamma(nu + d / 2))) range <- sqrt(8 * nu) / kappa op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) v <- t(rSPDE.A1d(x, 0.5)) c.true <- matern.covariance(abs(x - 0.5), kappa, nu, sigma) Q <- rspde.matern.precision.integer( kappa = kappa, nu = nu, tau = tau, d = 1, fem_mesh_matrices = op_cov$fem_mesh_matrices ) A <- Diagonal(nobs) c.approx_cov <- A %*% solve(Q, v) # plot the result and compare with the true Matern covariance plot(x, matern.covariance(abs(x - 0.5), kappa, nu, sigma), type = "l", ylab = "C(h)", xlab = "h", main = "Matern covariance and rational approximations" ) lines(x, c.approx_cov, col = 2)
rspde.matern.precision.integer.opt
is used
for computing the optimized version of the precision matrix of
stationary Gaussian random fields on with a Matern
covariance function
where is a natural number.
rspde.matern.precision.integer.opt( kappa, nu, tau, d, fem_matrices, graph = NULL )
rspde.matern.precision.integer.opt( kappa, nu, tau, d, fem_matrices, graph = NULL )
kappa |
Range parameter of the covariance function. |
nu |
Shape parameter of the covariance function. |
tau |
Scale parameter of the covariance function. |
d |
The dimension of the domain |
fem_matrices |
A list containing the FEM-related matrices. The list should contain elements C, G, G_2, G_3, etc. |
graph |
The sparsity graph of the matrices. If NULL, only a vector of the elements will be returned, if non-NULL, a sparse matrix will be returned. |
The precision matrix
rspde.matern.precision
is used for computing the
optimized version of the precision matrix of the
covariance-based rational SPDE approximation of a stationary Gaussian random
fields on with a Matern covariance function
rspde.matern.precision.opt( kappa, nu, tau, rspde.order, dim, fem_matrices, graph = NULL, sharp, type_rational_approx )
rspde.matern.precision.opt( kappa, nu, tau, rspde.order, dim, fem_matrices, graph = NULL, sharp, type_rational_approx )
kappa |
Range parameter of the covariance function. |
nu |
Shape parameter of the covariance function. |
tau |
Scale parameter of the covariance function. |
rspde.order |
The order of the rational approximation |
dim |
The dimension of the domain |
fem_matrices |
A list containing the FEM-related matrices. The list should contain elements C, G, G_2, G_3, etc. |
graph |
The sparsity graph of the matrices. If NULL, only a vector of the elements will be returned, if non-NULL, a sparse matrix will be returned. |
sharp |
The sparsity graph should have the correct sparsity (costs more to perform a sparsity analysis) or an upper bound for the sparsity? |
type_rational_approx |
Which type of rational approximation should be used? The current types are "chebfun", "brasil" or "chebfunLB". |
The precision matrix
inla.mesh
for
rSPDE objectsCalculate a lattice projection to/from an inla.mesh
for
rSPDE objects
rspde.mesh.project(...) rspde.mesh.projector( mesh, nu = NULL, rspde.order = 1, loc = NULL, lattice = NULL, xlim = NULL, ylim = NULL, dims = c(100, 100), projection = NULL, ... ) ## S3 method for class 'inla.mesh' rspde.mesh.project( mesh, loc = NULL, field = NULL, rspde.order = 1, nu = NULL, ... ) ## S3 method for class 'rspde.mesh.projector' rspde.mesh.project(projector, field, ...) ## S3 method for class 'inla.mesh.1d' rspde.mesh.project(mesh, loc, field = NULL, rspde.order = 1, nu = NULL, ...)
rspde.mesh.project(...) rspde.mesh.projector( mesh, nu = NULL, rspde.order = 1, loc = NULL, lattice = NULL, xlim = NULL, ylim = NULL, dims = c(100, 100), projection = NULL, ... ) ## S3 method for class 'inla.mesh' rspde.mesh.project( mesh, loc = NULL, field = NULL, rspde.order = 1, nu = NULL, ... ) ## S3 method for class 'rspde.mesh.projector' rspde.mesh.project(projector, field, ...) ## S3 method for class 'inla.mesh.1d' rspde.mesh.project(mesh, loc, field = NULL, rspde.order = 1, nu = NULL, ...)
... |
Additional parameters. |
mesh |
An |
nu |
The smoothness parameter. If |
rspde.order |
The order of the rational approximation. |
loc |
Projection locations. Can be a matrix or a SpatialPoints or a SpatialPointsDataFrame object. |
lattice |
An |
xlim |
X-axis limits for a lattice. For R2 meshes, defaults to covering the domain. |
ylim |
Y-axis limits for a lattice. For R2 meshes, defaults to covering the domain. |
dims |
Lattice dimensions. |
projection |
One of c("default", "longlat", "longsinlat", "mollweide"). |
field |
Basis function weights, one per mesh basis function, describing the function to be evaluated at the projection locations. |
projector |
A |
This function is built upon the inla.mesh.project and inla.mesh.projector functions from INLA.
A list with projection information for rspde.mesh.project. For rspde.mesh.projector(mesh, ...), a rspde.mesh.projector object. For rspde.mesh.project(projector, field, ...), a field projected from the mesh onto the locations given by the projector object.
Creates an INLA object for a stationary Matern model on a metric graph with general smoothness parameter.
rspde.metric_graph( graph_obj, h = NULL, nu.upper.bound = 2, rspde.order = 1, nu = NULL, debug = FALSE, B.sigma = matrix(c(0, 1, 0), 1, 3), B.range = matrix(c(0, 0, 1), 1, 3), parameterization = c("matern", "spde"), B.tau = matrix(c(0, 1, 0), 1, 3), B.kappa = matrix(c(0, 0, 1), 1, 3), start.nu = NULL, start.theta = NULL, prior.nu = NULL, theta.prior.mean = NULL, theta.prior.prec = 0.1, prior.std.dev.nominal = 1, prior.range.nominal = NULL, prior.kappa.mean = NULL, prior.tau.mean = NULL, start.lstd.dev = NULL, start.lrange = NULL, start.ltau = NULL, start.lkappa = NULL, prior.theta.param = c("theta", "spde"), prior.nu.dist = c("lognormal", "beta"), nu.prec.inc = 1, type.rational.approx = c("chebfun", "brasil", "chebfunLB"), shared_lib = "INLA" )
rspde.metric_graph( graph_obj, h = NULL, nu.upper.bound = 2, rspde.order = 1, nu = NULL, debug = FALSE, B.sigma = matrix(c(0, 1, 0), 1, 3), B.range = matrix(c(0, 0, 1), 1, 3), parameterization = c("matern", "spde"), B.tau = matrix(c(0, 1, 0), 1, 3), B.kappa = matrix(c(0, 0, 1), 1, 3), start.nu = NULL, start.theta = NULL, prior.nu = NULL, theta.prior.mean = NULL, theta.prior.prec = 0.1, prior.std.dev.nominal = 1, prior.range.nominal = NULL, prior.kappa.mean = NULL, prior.tau.mean = NULL, start.lstd.dev = NULL, start.lrange = NULL, start.ltau = NULL, start.lkappa = NULL, prior.theta.param = c("theta", "spde"), prior.nu.dist = c("lognormal", "beta"), nu.prec.inc = 1, type.rational.approx = c("chebfun", "brasil", "chebfunLB"), shared_lib = "INLA" )
graph_obj |
The graph object to build the model. Needs to be of class |
h |
The width of the mesh in case the mesh was not built. |
nu.upper.bound |
Upper bound for the smoothness parameter. |
rspde.order |
The order of the covariance-based rational SPDE approach. |
nu |
If nu is set to a parameter, nu will be kept fixed and will not
be estimated. If nu is |
debug |
INLA debug argument |
B.sigma |
Matrix with specification of log-linear model for |
B.range |
Matrix with specification of log-linear model for |
parameterization |
Which parameterization to use? |
B.tau |
Matrix with specification of log-linear model for |
B.kappa |
Matrix with specification of log-linear model for |
start.nu |
Starting value for nu. |
start.theta |
Starting values for the model parameters. In the stationary case, if |
prior.nu |
a list containing the elements |
theta.prior.mean |
A vector for the mean priors of |
theta.prior.prec |
A precision matrix for the prior of |
prior.std.dev.nominal |
Prior std. deviation to be used for the priors and for the starting values. |
prior.range.nominal |
Prior range to be used for the priors and for the starting values. |
prior.kappa.mean |
Prior kappa to be used for the priors and for the starting values. |
prior.tau.mean |
Prior tau to be used for the priors and for the starting values. |
start.lstd.dev |
Starting value for log of std. deviation. Will not be used if start.ltau is non-null. Will be only used in the stationary case and if |
start.lrange |
Starting value for log of range. Will not be used if start.lkappa is non-null. Will be only used in the stationary case and if |
start.ltau |
Starting value for log of tau. Will be only used in the stationary case and if |
start.lkappa |
Starting value for log of kappa. Will be only used in the stationary case and if |
prior.theta.param |
Should the lognormal prior be on |
prior.nu.dist |
The distribution of the smoothness parameter. The current options are "beta" or "lognormal". The default is "beta". |
nu.prec.inc |
Amount to increase the precision in the beta prior distribution. Check details below. |
type.rational.approx |
Which type of rational approximation should be used? The current types are "chebfun", "brasil" or "chebfunLB". |
shared_lib |
Which shared lib to use for the cgeneric implementation? If "INLA", it will use the shared lib from INLA's installation. If 'rSPDE', then it will use the local installation (does not work if your installation is from CRAN). Otherwise, you can directly supply the path of the .so (or .dll) file. |
prior.kappa |
a |
prior.tau |
a list containing the elements |
prior.range |
a |
prior.std.dev |
a |
An INLA model.
Extract field and parameter values and distributions for an rspde effect from an inla result object.
rspde.result( inla, name, rspde, compute.summary = TRUE, parameterization = "detect", n_samples = 5000, n_density = 1024 )
rspde.result( inla, name, rspde, compute.summary = TRUE, parameterization = "detect", n_samples = 5000, n_density = 1024 )
inla |
An |
name |
A character string with the name of the rSPDE effect in the inla formula. |
rspde |
The |
compute.summary |
Should the summary be computed? |
parameterization |
If 'detect', the parameterization from the model will be used. Otherwise, the options are 'spde', 'matern' and 'matern2'. |
n_samples |
The number of samples to be used if parameterization is different from the one used to fit the model. |
n_density |
The number of equally spaced points to estimate the density. |
If the model was fitted with matern
parameterization (the default), it returns a list containing:
marginals.range |
Marginal densities for the range parameter |
marginals.log.range |
Marginal densities for log(range) |
marginals.std.dev |
Marginal densities for std. deviation |
marginals.log.std.dev |
Marginal densities for log(std. deviation) |
marginals.values |
Marginal densities for the field values |
summary.log.range |
Summary statistics for log(range) |
summary.log.std.dev |
Summary statistics for log(std. deviation) |
summary.values |
Summary statistics for the field values |
If compute.summary
is TRUE
, then the list will also contain
summary.kappa |
Summary statistics for kappa |
summary.tau |
Summary statistics for tau |
If the model was fitted with the spde
parameterization, it returns a list containing:
marginals.kappa |
Marginal densities for kappa |
marginals.log.kappa |
Marginal densities for log(kappa) |
marginals.log.tau |
Marginal densities for log(tau) |
marginals.tau |
Marginal densities for tau |
marginals.values |
Marginal densities for the field values |
summary.log.kappa |
Summary statistics for log(kappa) |
summary.log.tau |
Summary statistics for log(tau) |
summary.values |
Summary statistics for the field values |
If compute.summary
is TRUE
, then the list will also contain
summary.kappa |
Summary statistics for kappa |
summary.tau |
Summary statistics for tau |
For both cases, if nu was estimated, then the list will also contain
marginals.nu |
Marginal densities for nu |
If nu was estimated and a beta prior was used, then the list will also contain
marginals.logit.nu |
Marginal densities for logit(nu) |
summary.logit.nu |
Marginal densities for logit(nu) |
If nu was estimated and a truncated lognormal prior was used, then the list will also contain
marginals.log.nu |
Marginal densities for log(nu) |
summary.log.nu |
Marginal densities for log(nu) |
If nu was estimated and compute.summary
is TRUE
,
then the list will also contain
summary.nu |
Summary statistics for nu |
# devel version if (requireNamespace("INLA", quietly = TRUE)) { library(INLA) set.seed(123) m <- 100 loc_2d_mesh <- matrix(runif(m * 2), m, 2) mesh_2d <- inla.mesh.2d( loc = loc_2d_mesh, cutoff = 0.05, max.edge = c(0.1, 0.5) ) sigma <- 1 range <- 0.2 nu <- 0.8 kappa <- sqrt(8 * nu) / range op <- matern.operators( mesh = mesh_2d, nu = nu, range = range, sigma = sigma, m = 2, parameterization = "matern" ) u <- simulate(op) A <- inla.spde.make.A( mesh = mesh_2d, loc = loc_2d_mesh ) sigma.e <- 0.1 y <- A %*% u + rnorm(m) * sigma.e Abar <- rspde.make.A(mesh = mesh_2d, loc = loc_2d_mesh) mesh.index <- rspde.make.index(name = "field", mesh = mesh_2d) st.dat <- inla.stack( data = list(y = as.vector(y)), A = Abar, effects = mesh.index ) rspde_model <- rspde.matern( mesh = mesh_2d, nu.upper.bound = 2 ) f <- y ~ -1 + f(field, model = rspde_model) rspde_fit <- inla(f, data = inla.stack.data(st.dat), family = "gaussian", control.predictor = list(A = inla.stack.A(st.dat)) ) result <- rspde.result(rspde_fit, "field", rspde_model) summary(result) } # devel.tag
# devel version if (requireNamespace("INLA", quietly = TRUE)) { library(INLA) set.seed(123) m <- 100 loc_2d_mesh <- matrix(runif(m * 2), m, 2) mesh_2d <- inla.mesh.2d( loc = loc_2d_mesh, cutoff = 0.05, max.edge = c(0.1, 0.5) ) sigma <- 1 range <- 0.2 nu <- 0.8 kappa <- sqrt(8 * nu) / range op <- matern.operators( mesh = mesh_2d, nu = nu, range = range, sigma = sigma, m = 2, parameterization = "matern" ) u <- simulate(op) A <- inla.spde.make.A( mesh = mesh_2d, loc = loc_2d_mesh ) sigma.e <- 0.1 y <- A %*% u + rnorm(m) * sigma.e Abar <- rspde.make.A(mesh = mesh_2d, loc = loc_2d_mesh) mesh.index <- rspde.make.index(name = "field", mesh = mesh_2d) st.dat <- inla.stack( data = list(y = as.vector(y)), A = Abar, effects = mesh.index ) rspde_model <- rspde.matern( mesh = mesh_2d, nu.upper.bound = 2 ) f <- y ~ -1 + f(field, model = rspde_model) rspde_fit <- inla(f, data = inla.stack.data(st.dat), family = "gaussian", control.predictor = list(A = inla.stack.A(st.dat)) ) result <- rspde.result(rspde_fit, "field", rspde_model) summary(result) } # devel.tag
rspde.spacetime
computes a Finite Element Method (FEM) approximation of a
Gaussian random field defined as the solution to the stochastic partial
differential equation (SPDE):
where is a Whittle-Matérn covariance operator with smoothness parameter
and range parameter
. This function is designed to handle
space-time random fields using either 1D spatial models or higher-dimensional
FEM-based approaches.
rspde.spacetime( mesh_space = NULL, mesh_time = NULL, space_loc = NULL, time_loc = NULL, drift = TRUE, alpha, beta, prior.kappa = NULL, prior.sigma = NULL, prior.rho = NULL, prior.gamma = NULL, prior.precision = NULL, shared_lib = "detect", debug = FALSE, ... )
rspde.spacetime( mesh_space = NULL, mesh_time = NULL, space_loc = NULL, time_loc = NULL, drift = TRUE, alpha, beta, prior.kappa = NULL, prior.sigma = NULL, prior.rho = NULL, prior.gamma = NULL, prior.precision = NULL, shared_lib = "detect", debug = FALSE, ... )
mesh_space |
Spatial mesh for the FEM approximation, or a |
mesh_time |
Temporal mesh for the FEM approximation. |
space_loc |
A vector of spatial locations for mesh nodes in 1D spatial models.
This should be provided when |
time_loc |
A vector of temporal locations for mesh nodes. This should be
provided when |
drift |
Logical value indicating whether the drift term should be included.
If |
alpha |
Integer smoothness parameter |
beta |
Integer smoothness parameter |
prior.kappa |
A list specifying the prior for the range parameter |
prior.sigma |
A list specifying the prior for the variance parameter |
prior.rho |
A list specifying the prior for the drift coefficient |
prior.gamma |
A list specifying the prior for the weight |
prior.precision |
A precision matrix for |
shared_lib |
String specifying which shared library to use for the Cgeneric implementation. Options are "detect", "INLA", or "rSPDE". You may also specify the direct path to a .so (or .dll) file. |
debug |
Logical value indicating whether to enable INLA debug mode. |
... |
Additional arguments passed internally for configuration purposes. |
An object of class inla_rspde_spacetime
representing the FEM approximation of
the space-time Gaussian random field.
library(INLA) library(MetricGraph) graph <- metric_graph$new() graph$build_mesh(h = 0.1) graph$compute_fem() # Define the time locations time_loc <- seq(from = 0, to = 10, length.out = 11) # Create the model model <- rspde.spacetime(mesh_space = graph, time_loc = time_loc, alpha = 2, beta = 1)
library(INLA) library(MetricGraph) graph <- metric_graph$new() graph$build_mesh(h = 0.1) graph$compute_fem() # Define the time locations time_loc <- seq(from = 0, to = 10, length.out = 11) # Create the model model <- rspde.spacetime(mesh_space = graph, time_loc = time_loc, alpha = 2, beta = 1)
The function samples a Gaussian random field based using the covariance-based rational SPDE approximation.
## S3 method for class 'CBrSPDEobj' simulate( object, nsim = 1, seed = NULL, user_nu = NULL, user_kappa = NULL, user_sigma = NULL, user_range = NULL, user_tau = NULL, user_theta = NULL, user_m = NULL, ... )
## S3 method for class 'CBrSPDEobj' simulate( object, nsim = 1, seed = NULL, user_nu = NULL, user_kappa = NULL, user_sigma = NULL, user_range = NULL, user_tau = NULL, user_theta = NULL, user_m = NULL, ... )
object |
The covariance-based rational SPDE approximation,
computed using |
nsim |
The number of simulations. |
seed |
An object specifying if and how the random number generator should be initialized (‘seeded’). |
user_nu |
If non-null, update the shape parameter of the covariance function. |
user_kappa |
If non-null, update the range parameter of the covariance function. |
user_sigma |
If non-null, update the standard deviation of the covariance function. |
user_range |
If non-null, update the range parameter of the covariance function. |
user_tau |
If non-null, update the parameter tau. |
user_theta |
For non-stationary models. If non-null, update the vector of parameters. |
user_m |
If non-null, update the order of the rational approximation, which needs to be a positive integer. |
... |
Currently not used. |
A matrix with the n
samples as columns.
# Sample a Gaussian Matern process on R using a rational approximation kappa <- 10 sigma <- 1 nu <- 0.8 range <- sqrt(8 * nu) / kappa # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation of covariance function at 0.5 tau <- sqrt(gamma(nu) / (sigma^2 * kappa^(2 * nu) * (4 * pi)^(1 / 2) * gamma(nu + 1 / 2))) op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) # Sample the model and plot the result Y <- simulate(op_cov) plot(x, Y, type = "l", ylab = "u(x)", xlab = "x")
# Sample a Gaussian Matern process on R using a rational approximation kappa <- 10 sigma <- 1 nu <- 0.8 range <- sqrt(8 * nu) / kappa # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation of covariance function at 0.5 tau <- sqrt(gamma(nu) / (sigma^2 * kappa^(2 * nu) * (4 * pi)^(1 / 2) * gamma(nu + 1 / 2))) op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) # Sample the model and plot the result Y <- simulate(op_cov) plot(x, Y, type = "l", ylab = "u(x)", xlab = "x")
The function samples a Gaussian random field based using the covariance-based rational SPDE approximation.
## S3 method for class 'CBrSPDEobj2d' simulate( object, nsim = 1, seed = NULL, user_nu = NULL, user_hx = NULL, user_hy = NULL, user_hxy = NULL, user_sigma = NULL, user_m = NULL, ... )
## S3 method for class 'CBrSPDEobj2d' simulate( object, nsim = 1, seed = NULL, user_nu = NULL, user_hx = NULL, user_hy = NULL, user_hxy = NULL, user_sigma = NULL, user_m = NULL, ... )
object |
The covariance-based rational SPDE approximation,
computed using |
nsim |
The number of simulations. |
seed |
An object specifying if and how the random number generator should be initialized (‘seeded’). |
user_nu |
If non-null, update the shape parameter of the covariance function. |
user_hx |
If non-null, update the hx parameter. |
user_hy |
If non-null, update the hy parameter. |
user_hxy |
If non-null, update the hxy parameter. |
user_sigma |
If non-null, update the standard deviation of the covariance function. |
user_m |
If non-null, update the order of the rational approximation, which needs to be a positive integer. |
... |
Currently not used. |
A matrix with the n
samples as columns.
library(fmesher) n_loc <- 2000 loc_2d_mesh <- matrix(runif(n_loc * 2), n_loc, 2) mesh_2d <- fm_mesh_2d(loc = loc_2d_mesh, cutoff = 0.03, max.edge = c(0.1, 0.5)) op <- matern2d.operators(mesh = mesh_2d, sigma = 1, nu = 1, hx = 0.1, hy = 0.1, hxy = 0) u <- simulate(op)
library(fmesher) n_loc <- 2000 loc_2d_mesh <- matrix(runif(n_loc * 2), n_loc, 2) mesh_2d <- fm_mesh_2d(loc = loc_2d_mesh, cutoff = 0.03, max.edge = c(0.1, 0.5)) op <- matern2d.operators(mesh = mesh_2d, sigma = 1, nu = 1, hx = 0.1, hy = 0.1, hxy = 0) u <- simulate(op)
The function samples a Gaussian random field based using the covariance-based rational SPDE approximation.
## S3 method for class 'intrinsicCBrSPDEobj' simulate(object, nsim = 1, seed = NULL, integral.constraint = TRUE, ...)
## S3 method for class 'intrinsicCBrSPDEobj' simulate(object, nsim = 1, seed = NULL, integral.constraint = TRUE, ...)
object |
The covariance-based rational SPDE approximation,
computed using |
nsim |
The number of simulations. |
seed |
An object specifying if and how the random number generator should be initialized (‘seeded’). |
integral.constraint |
Should the contraint on the integral be done? |
... |
Currently not used. |
A matrix with the nsim
samples as columns.
The function samples a Gaussian random field based on a pre-computed rational SPDE approximation.
## S3 method for class 'rSPDEobj' simulate(object, nsim = 1, seed = NULL, ...)
## S3 method for class 'rSPDEobj' simulate(object, nsim = 1, seed = NULL, ...)
object |
The rational SPDE approximation, computed
using |
nsim |
The number of simulations. |
seed |
an object specifying if and how the random number generator should be initialized (‘seeded’). |
... |
Currently not used. |
A matrix with the n
samples as columns.
# Sample a Gaussian Matern process on R using a rational approximation kappa <- 10 sigma <- 1 nu <- 0.8 range <- sqrt(8 * nu) / kappa # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation op <- matern.operators( range = range, sigma = sigma, nu = nu, loc_mesh = x, d = 1, parameterization = "matern" ) # Sample the model and plot the result Y <- simulate(op) plot(x, Y, type = "l", ylab = "u(x)", xlab = "x")
# Sample a Gaussian Matern process on R using a rational approximation kappa <- 10 sigma <- 1 nu <- 0.8 range <- sqrt(8 * nu) / kappa # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation op <- matern.operators( range = range, sigma = sigma, nu = nu, loc_mesh = x, d = 1, parameterization = "matern" ) # Sample the model and plot the result Y <- simulate(op) plot(x, Y, type = "l", ylab = "u(x)", xlab = "x")
The function samples a Gaussian random field based on a pre-computed rational SPDE approximation.
## S3 method for class 'rSPDEobj1d' simulate(object, nsim = 1, seed = NULL, ...)
## S3 method for class 'rSPDEobj1d' simulate(object, nsim = 1, seed = NULL, ...)
object |
The rational SPDE approximation, computed
using |
nsim |
The number of simulations. |
seed |
an object specifying if and how the random number generator should be initialized (‘seeded’). |
... |
Currently not used. |
A matrix with the n
samples as columns.
# Sample a Gaussian Matern process on R using a rational approximation range <- 0.2 sigma <- 1 nu <- 0.8 # compute rational approximation x <- seq(from = 0, to = 1, length.out = 100) op <- matern.rational( range = range, sigma = sigma, nu = nu, loc = x ) # Sample the model and plot the result Y <- simulate(op) plot(x, Y, type = "l", ylab = "u(x)", xlab = "x")
# Sample a Gaussian Matern process on R using a rational approximation range <- 0.2 sigma <- 1 nu <- 0.8 # compute rational approximation x <- seq(from = 0, to = 1, length.out = 100) op <- matern.rational( range = range, sigma = sigma, nu = nu, loc = x ) # Sample the model and plot the result Y <- simulate(op) plot(x, Y, type = "l", ylab = "u(x)", xlab = "x")
Simulation of space-time models
## S3 method for class 'spacetimeobj' simulate( object, nsim = 1, seed = NULL, user_kappa = NULL, user_sigma = NULL, user_gamma = NULL, user_rho = NULL, ... )
## S3 method for class 'spacetimeobj' simulate( object, nsim = 1, seed = NULL, user_kappa = NULL, user_sigma = NULL, user_gamma = NULL, user_rho = NULL, ... )
object |
Space-time object created by |
nsim |
The number of simulations. |
seed |
an object specifying if and how the random number generator should be initialized (‘seeded’). |
user_kappa |
kappa parameter if it should be updated |
user_sigma |
sigma parameter if it should be updated |
user_gamma |
gamma parameter if it should be updated |
user_rho |
rho parameter if it should be updated |
... |
Currently not used. |
A matrix with the simulations as columns.
s <- seq(from = 0, to = 20, length.out = 101) t <- seq(from = 0, to = 20, length.out = 31) op_cov <- spacetime.operators(space_loc = s, time_loc = t, kappa = 5, sigma = 10, alpha = 1, beta = 2, rho = 1, gamma = 0.05) x <- simulate(op_cov, nsim = 1) image(matrix(x, nrow = length(s), ncol = length(t)))
s <- seq(from = 0, to = 20, length.out = 101) t <- seq(from = 0, to = 20, length.out = 31) op_cov <- spacetime.operators(space_loc = s, time_loc = t, kappa = 5, sigma = 10, alpha = 1, beta = 2, rho = 1, gamma = 0.05) x <- simulate(op_cov, nsim = 1) image(matrix(x, nrow = length(s), ncol = length(t)))
spacetime.operators
is used for computing a FEM approximation of a Gaussian
random field defined as a solution to the SPDE
where C is a Whittle-Matern covariance operator with smoothness parameter
and range parameter
spacetime.operators( mesh_space = NULL, mesh_time = NULL, space_loc = NULL, time_loc = NULL, graph = NULL, kappa = NULL, sigma = NULL, gamma = NULL, rho = NULL, alpha = NULL, beta = NULL )
spacetime.operators( mesh_space = NULL, mesh_time = NULL, space_loc = NULL, time_loc = NULL, graph = NULL, kappa = NULL, sigma = NULL, gamma = NULL, rho = NULL, alpha = NULL, beta = NULL )
mesh_space |
Spatial mesh for FEM approximation |
mesh_time |
Temporal mesh for FEM approximation |
space_loc |
Locations of mesh nodes for spatial mesh for 1d models. |
time_loc |
Locations of temporal mesh nodes. |
graph |
An optional |
kappa |
Positive spatial range parameter |
sigma |
Positive variance parameter |
gamma |
Temporal range parameter. |
rho |
Drift parameter. Real number on metric graphs and one-dimensional spatial domains, a vector with two number on 2d domains. |
alpha |
Integer smoothness parameter alpha. |
beta |
Integer smoothness parameter beta. |
An object of type spacetimeobj.
s <- seq(from = 0, to = 20, length.out = 101) t <- seq(from = 0, to = 20, length.out = 31) op_cov <- spacetime.operators(space_loc = s, time_loc = t, kappa = 5, sigma = 10, alpha = 1, beta = 2, rho = 1, gamma = 0.05) Q <- op_cov$Q v <- rep(0,dim(Q)[1]) v[1565] <- 1 Sigma <- solve(Q,v) image(matrix(Sigma, nrow=length(s), ncol = length(t)))
s <- seq(from = 0, to = 20, length.out = 101) t <- seq(from = 0, to = 20, length.out = 31) op_cov <- spacetime.operators(space_loc = s, time_loc = t, kappa = 5, sigma = 10, alpha = 1, beta = 2, rho = 1, gamma = 0.05) Q <- op_cov$Q v <- rep(0,dim(Q)[1]) v[1565] <- 1 Sigma <- solve(Q,v) image(matrix(Sigma, nrow=length(s), ncol = length(t)))
Constructs observation/prediction weight matrices
for rSPDE models with integer smoothness based on inla.mesh
or
inla.mesh.1d
objects.
spde.make.A( mesh = NULL, loc = NULL, A = NULL, index = NULL, group = NULL, repl = 1L, n.group = NULL, n.repl = NULL )
spde.make.A( mesh = NULL, loc = NULL, A = NULL, index = NULL, group = NULL, repl = 1L, n.group = NULL, n.repl = NULL )
mesh |
An |
loc |
Locations, needed if an INLA mesh is provided |
A |
The A matrix from the standard SPDE approach, such as the matrix
returned by |
index |
For each observation/prediction value, an index into loc. Default is seq_len(nrow(A.loc)). |
group |
For each observation/prediction value, an index into the group model. |
repl |
For each observation/prediction value, the replicate index. |
n.group |
The size of the group model. |
n.repl |
The total number of replicates. |
The matrix for rSPDE models.
# devel version if (requireNamespace("fmesher", quietly = TRUE)) { library(fmesher) set.seed(123) loc <- matrix(runif(100 * 2) * 100, 100, 2) mesh <- fm_mesh_2d( loc = loc, cutoff = 50, max.edge = c(50, 500) ) A <- spde.make.A(mesh, loc = loc) } # devel.tag
# devel version if (requireNamespace("fmesher", quietly = TRUE)) { library(fmesher) set.seed(123) loc <- matrix(runif(100 * 2) * 100, 100, 2) mesh <- fm_mesh_2d( loc = loc, cutoff = 50, max.edge = c(50, 500) ) A <- spde.make.A(mesh, loc = loc) } # devel.tag
This function evaluates the log-likelihood function for observations of a Gaussian process defined as the solution to the SPDE
spde.matern.loglike( object, Y, A, sigma.e, mu = 0, user_nu = NULL, user_kappa = NULL, user_tau = NULL, user_theta = NULL, user_m = NULL )
spde.matern.loglike( object, Y, A, sigma.e, mu = 0, user_nu = NULL, user_kappa = NULL, user_tau = NULL, user_theta = NULL, user_m = NULL )
object |
The rational SPDE approximation,
computed using |
Y |
The observations, either a vector or a matrix where the columns correspond to independent replicates of observations. |
A |
An observation matrix that links the measurement location to the finite element basis. |
sigma.e |
IF non-null, the standard deviation of the measurement noise will be kept fixed in the returned likelihood. |
mu |
Expectation vector of the latent field (default = 0). |
user_nu |
If non-null, the shape parameter will be kept fixed in the returned likelihood. |
user_kappa |
If non-null, updates the range parameter. |
user_tau |
If non-null, updates the parameter tau. |
user_theta |
If non-null, updates the parameter theta (that connects tau and kappa to the model matrices in |
user_m |
If non-null, update the order of the rational approximation, which needs to be a positive integer. |
The observations are assumed to be generated as
, where
are
iid mean-zero Gaussian variables. The latent model is approximated using a
rational approximation of the fractional SPDE model.
The log-likelihood value.
# this example illustrates how the function can be used for maximum # likelihood estimation # Sample a Gaussian Matern process on R using a rational approximation sigma.e <- 0.1 n.rep <- 10 n.obs <- 100 n.x <- 51 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = n.x) fem <- rSPDE.fem1d(x) tau <- rep(0.5, n.x) nu <- 0.8 alpha <- nu + 1 / 2 kappa <- rep(1, n.x) # compute rational approximation op <- spde.matern.operators( kappa = kappa, tau = tau, alpha = alpha, parameterization = "spde", d = 1, loc_mesh = x ) # Sample the model u <- simulate(op, n.rep) # Create some data obs.loc <- runif(n = n.obs, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc) noise <- rnorm(n.obs * n.rep) dim(noise) <- c(n.obs, n.rep) Y <- as.matrix(A %*% u + sigma.e * noise) # define negative likelihood function for optimization using matern.loglike mlik <- function(theta) { return(-spde.matern.loglike(op, Y, A, sigma.e = exp(theta[4]), user_nu = exp(theta[3]), user_kappa = exp(theta[2]), user_tau = exp(theta[1]) )) } #' #The parameters can now be estimated by minimizing mlik with optim # Choose some reasonable starting values depending on the size of the domain theta0 <- log(c(1 / sqrt(var(c(Y))), sqrt(8), 0.9, 0.01)) # run estimation and display the results theta <- optim(theta0, mlik) print(data.frame( tau = c(tau[1], exp(theta$par[1])), kappa = c(kappa[1], exp(theta$par[2])), nu = c(nu, exp(theta$par[3])), sigma.e = c(sigma.e, exp(theta$par[4])), row.names = c("Truth", "Estimates") ))
# this example illustrates how the function can be used for maximum # likelihood estimation # Sample a Gaussian Matern process on R using a rational approximation sigma.e <- 0.1 n.rep <- 10 n.obs <- 100 n.x <- 51 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = n.x) fem <- rSPDE.fem1d(x) tau <- rep(0.5, n.x) nu <- 0.8 alpha <- nu + 1 / 2 kappa <- rep(1, n.x) # compute rational approximation op <- spde.matern.operators( kappa = kappa, tau = tau, alpha = alpha, parameterization = "spde", d = 1, loc_mesh = x ) # Sample the model u <- simulate(op, n.rep) # Create some data obs.loc <- runif(n = n.obs, min = 0, max = 1) A <- rSPDE.A1d(x, obs.loc) noise <- rnorm(n.obs * n.rep) dim(noise) <- c(n.obs, n.rep) Y <- as.matrix(A %*% u + sigma.e * noise) # define negative likelihood function for optimization using matern.loglike mlik <- function(theta) { return(-spde.matern.loglike(op, Y, A, sigma.e = exp(theta[4]), user_nu = exp(theta[3]), user_kappa = exp(theta[2]), user_tau = exp(theta[1]) )) } #' #The parameters can now be estimated by minimizing mlik with optim # Choose some reasonable starting values depending on the size of the domain theta0 <- log(c(1 / sqrt(var(c(Y))), sqrt(8), 0.9, 0.01)) # run estimation and display the results theta <- optim(theta0, mlik) print(data.frame( tau = c(tau[1], exp(theta$par[1])), kappa = c(kappa[1], exp(theta$par[2])), nu = c(nu, exp(theta$par[3])), sigma.e = c(sigma.e, exp(theta$par[4])), row.names = c("Truth", "Estimates") ))
spde.matern.operators
is used for computing a rational SPDE
approximation of a Gaussian random
fields on defined as a solution to the SPDE
spde.matern.operators( kappa = NULL, tau = NULL, theta = NULL, B.tau = matrix(c(0, 1, 0), 1, 3), B.kappa = matrix(c(0, 0, 1), 1, 3), B.sigma = matrix(c(0, 1, 0), 1, 3), B.range = matrix(c(0, 0, 1), 1, 3), alpha = NULL, nu = NULL, parameterization = c("spde", "matern"), G = NULL, C = NULL, d = NULL, graph = NULL, mesh = NULL, range_mesh = NULL, loc_mesh = NULL, m = 1, type = c("covariance", "operator"), type_rational_approximation = c("chebfun", "brasil", "chebfunLB") )
spde.matern.operators( kappa = NULL, tau = NULL, theta = NULL, B.tau = matrix(c(0, 1, 0), 1, 3), B.kappa = matrix(c(0, 0, 1), 1, 3), B.sigma = matrix(c(0, 1, 0), 1, 3), B.range = matrix(c(0, 0, 1), 1, 3), alpha = NULL, nu = NULL, parameterization = c("spde", "matern"), G = NULL, C = NULL, d = NULL, graph = NULL, mesh = NULL, range_mesh = NULL, loc_mesh = NULL, m = 1, type = c("covariance", "operator"), type_rational_approximation = c("chebfun", "brasil", "chebfunLB") )
kappa |
Vector with the, possibly spatially varying, range parameter evaluated at the locations of the mesh used for the finite element discretization of the SPDE. |
tau |
Vector with the, possibly spatially varying, precision parameter evaluated at the locations of the mesh used for the finite element discretization of the SPDE. |
theta |
Theta parameter that connects B.tau and B.kappa to tau and kappa through a log-linear regression, in case the parameterization is |
B.tau |
Matrix with specification of log-linear model for |
B.kappa |
Matrix with specification of log-linear model for |
B.sigma |
Matrix with specification of log-linear model for |
B.range |
Matrix with specification of log-linear model for |
alpha |
smoothness parameter. Will be used if the parameterization is 'spde'. |
nu |
Shape parameter of the covariance function. Will be used if the parameterization is 'matern'. |
parameterization |
Which parameterization to use? |
G |
The stiffness matrix of a finite element discretization of the domain of interest. |
C |
The mass matrix of a finite element discretization of the domain of interest. |
d |
The dimension of the domain. Does not need to be given if
|
graph |
An optional |
mesh |
An optional inla mesh. |
range_mesh |
The range of the mesh. Will be used to provide starting values for the parameters. Will be used if |
loc_mesh |
The mesh locations used to construct the matrices C and G. This option should be provided if one wants to use the |
m |
The order of the rational approximation, which needs to be a positive integer. The default value is 1. |
type |
The type of the rational approximation. The options are "covariance" and "operator". The default is "covariance". |
type_rational_approximation |
Which type of rational approximation should be used? The current types are "chebfun", "brasil" or "chebfunLB". |
The approximation is based on a rational approximation of the
fractional operator , where
. This results in an approximate model
on the form
where are
non-fractional operators defined in terms of polynomials
for
. The order of
is given by
m
and the order
of is
where
is the integer
part of
if
and
otherwise.
The discrete approximation can be written as where
and
. Note that the matrices
and
may be be ill-conditioned for
.
In this case, the metehods in
operator.operations()
should be used for operations involving the matrices, since
these methods are more numerically stable.
spde.matern.operators
returns an object of
class "rSPDEobj. This object contains the
quantities listed in the output of fractional.operators()
as well as the smoothness parameter .
fractional.operators()
,
spde.matern.operators()
,
matern.operators()
# Sample non-stationary Matern field on R tau <- 1 nu <- 0.8 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # define a non-stationary range parameter kappa <- seq(from = 2, to = 20, length.out = length(x)) alpha <- nu + 1 / 2 # compute rational approximation op <- spde.matern.operators( kappa = kappa, tau = tau, alpha = alpha, G = fem$G, C = fem$C, d = 1 ) # sample the field u <- simulate(op) # plot the sample plot(x, u, type = "l", ylab = "u(s)", xlab = "s")
# Sample non-stationary Matern field on R tau <- 1 nu <- 0.8 # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # define a non-stationary range parameter kappa <- seq(from = 2, to = 20, length.out = length(x)) alpha <- nu + 1 / 2 # compute rational approximation op <- spde.matern.operators( kappa = kappa, tau = tau, alpha = alpha, G = fem$G, C = fem$C, d = 1 ) # sample the field u <- simulate(op) # plot the sample plot(x, u, type = "l", ylab = "u(s)", xlab = "s")
Summary method for class "CBrSPDEobj"
## S3 method for class 'CBrSPDEobj' summary(object, ...) ## S3 method for class 'summary.CBrSPDEobj' print(x, ...) ## S3 method for class 'CBrSPDEobj' print(x, ...)
## S3 method for class 'CBrSPDEobj' summary(object, ...) ## S3 method for class 'summary.CBrSPDEobj' print(x, ...) ## S3 method for class 'CBrSPDEobj' print(x, ...)
object |
an object of class "CBrSPDEobj", usually, a result of a call
to |
... |
further arguments passed to or from other methods. |
x |
an object of class "summary.CBrSPDEobj", usually, a result of a call
to |
# Compute the covariance-based rational approximation of a # Gaussian process with a Matern covariance function on R kappa <- 10 sigma <- 1 nu <- 0.8 range <- sqrt(8 * nu) / kappa # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation of covariance function at 0.5 tau <- sqrt(gamma(nu) / (sigma^2 * kappa^(2 * nu) * (4 * pi)^(1 / 2) * gamma(nu + 1 / 2))) op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) op_cov
# Compute the covariance-based rational approximation of a # Gaussian process with a Matern covariance function on R kappa <- 10 sigma <- 1 nu <- 0.8 range <- sqrt(8 * nu) / kappa # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation of covariance function at 0.5 tau <- sqrt(gamma(nu) / (sigma^2 * kappa^(2 * nu) * (4 * pi)^(1 / 2) * gamma(nu + 1 / 2))) op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) op_cov
Summary method for class "CBrSPDEobj2d"
## S3 method for class 'CBrSPDEobj2d' summary(object, ...) ## S3 method for class 'summary.CBrSPDEobj2d' print(x, ...) ## S3 method for class 'CBrSPDEobj2d' print(x, ...)
## S3 method for class 'CBrSPDEobj2d' summary(object, ...) ## S3 method for class 'summary.CBrSPDEobj2d' print(x, ...) ## S3 method for class 'CBrSPDEobj2d' print(x, ...)
object |
an object of class "CBrSPDEobj2d", usually, a result of a call
to |
... |
further arguments passed to or from other methods. |
x |
an object of class "summary.CBrSPDEobj2d", usually, a result of a call
to |
library(fmesher) n_loc <- 2000 loc_2d_mesh <- matrix(runif(n_loc * 2), n_loc, 2) mesh_2d <- fm_mesh_2d(loc = loc_2d_mesh, cutoff = 0.03, max.edge = c(0.1, 0.5)) op <- matern2d.operators(mesh = mesh_2d) op
library(fmesher) n_loc <- 2000 loc_2d_mesh <- matrix(runif(n_loc * 2), n_loc, 2) mesh_2d <- fm_mesh_2d(loc = loc_2d_mesh, cutoff = 0.03, max.edge = c(0.1, 0.5)) op <- matern2d.operators(mesh = mesh_2d) op
rspde_lme
Objects.Function providing a summary of results related to mixed effects regression models with Whittle-Matern latent models.
## S3 method for class 'rspde_lme' summary(object, all_times = FALSE, ...)
## S3 method for class 'rspde_lme' summary(object, all_times = FALSE, ...)
object |
an object of class "rspde_lme" containing results from the fitted model. |
all_times |
Show all computed times. |
... |
not used. |
An object of class summary_rspde_lme
containing several
informations of a rspde_lme object.
inla_rspde
model from a rspde_result
objectSummary for posteriors of rSPDE field parameters in their original scales.
## S3 method for class 'rspde_result' summary(object, digits = 6, ...)
## S3 method for class 'rspde_result' summary(object, digits = 6, ...)
object |
A |
digits |
integer, used for number formatting with signif() |
... |
Currently not used. |
Returns a data.frame
containing the summary.
# devel version if (requireNamespace("INLA", quietly = TRUE)) { library(INLA) set.seed(123) m <- 100 loc_2d_mesh <- matrix(runif(m * 2), m, 2) mesh_2d <- inla.mesh.2d( loc = loc_2d_mesh, cutoff = 0.05, max.edge = c(0.1, 0.5) ) sigma <- 1 range <- 0.2 nu <- 0.8 kappa <- sqrt(8 * nu) / range op <- matern.operators( mesh = mesh_2d, nu = nu, range = range, sigma = sigma, m = 2, parameterization = "matern" ) u <- simulate(op) A <- inla.spde.make.A( mesh = mesh_2d, loc = loc_2d_mesh ) sigma.e <- 0.1 y <- A %*% u + rnorm(m) * sigma.e Abar <- rspde.make.A(mesh = mesh_2d, loc = loc_2d_mesh) mesh.index <- rspde.make.index(name = "field", mesh = mesh_2d) st.dat <- inla.stack( data = list(y = as.vector(y)), A = Abar, effects = mesh.index ) rspde_model <- rspde.matern( mesh = mesh_2d, nu.upper.bound = 2 ) f <- y ~ -1 + f(field, model = rspde_model) rspde_fit <- inla(f, data = inla.stack.data(st.dat), family = "gaussian", control.predictor = list(A = inla.stack.A(st.dat)) ) result <- rspde.result(rspde_fit, "field", rspde_model) summary(result) } # devel.tag
# devel version if (requireNamespace("INLA", quietly = TRUE)) { library(INLA) set.seed(123) m <- 100 loc_2d_mesh <- matrix(runif(m * 2), m, 2) mesh_2d <- inla.mesh.2d( loc = loc_2d_mesh, cutoff = 0.05, max.edge = c(0.1, 0.5) ) sigma <- 1 range <- 0.2 nu <- 0.8 kappa <- sqrt(8 * nu) / range op <- matern.operators( mesh = mesh_2d, nu = nu, range = range, sigma = sigma, m = 2, parameterization = "matern" ) u <- simulate(op) A <- inla.spde.make.A( mesh = mesh_2d, loc = loc_2d_mesh ) sigma.e <- 0.1 y <- A %*% u + rnorm(m) * sigma.e Abar <- rspde.make.A(mesh = mesh_2d, loc = loc_2d_mesh) mesh.index <- rspde.make.index(name = "field", mesh = mesh_2d) st.dat <- inla.stack( data = list(y = as.vector(y)), A = Abar, effects = mesh.index ) rspde_model <- rspde.matern( mesh = mesh_2d, nu.upper.bound = 2 ) f <- y ~ -1 + f(field, model = rspde_model) rspde_fit <- inla(f, data = inla.stack.data(st.dat), family = "gaussian", control.predictor = list(A = inla.stack.A(st.dat)) ) result <- rspde.result(rspde_fit, "field", rspde_model) summary(result) } # devel.tag
Summary method for class "rSPDEobj"
## S3 method for class 'rSPDEobj' summary(object, ...) ## S3 method for class 'summary.rSPDEobj' print(x, ...) ## S3 method for class 'rSPDEobj' print(x, ...)
## S3 method for class 'rSPDEobj' summary(object, ...) ## S3 method for class 'summary.rSPDEobj' print(x, ...) ## S3 method for class 'rSPDEobj' print(x, ...)
object |
an object of class "rSPDEobj", usually, a result of a call
to |
... |
further arguments passed to or from other methods. |
x |
an object of class "summary.rSPDEobj", usually, a result of a call
to |
Summary method for class "rSPDEobj1d"
## S3 method for class 'rSPDEobj1d' summary(object, ...) ## S3 method for class 'summary.rSPDEobj1d' print(x, ...) ## S3 method for class 'rSPDEobj1d' print(x, ...)
## S3 method for class 'rSPDEobj1d' summary(object, ...) ## S3 method for class 'summary.rSPDEobj1d' print(x, ...) ## S3 method for class 'rSPDEobj1d' print(x, ...)
object |
an object of class "rSPDEobj1d", usually, a result of a call
to |
... |
further arguments passed to or from other methods. |
x |
an object of class "summary.rSPDEobj1d", usually, a result of a call
to |
Summary method for class "spacetimeobj"
## S3 method for class 'spacetimeobj' summary(object, ...) ## S3 method for class 'summary.spacetimeobj' print(x, ...) ## S3 method for class 'spacetimeobj' print(x, ...)
## S3 method for class 'spacetimeobj' summary(object, ...) ## S3 method for class 'summary.spacetimeobj' print(x, ...) ## S3 method for class 'spacetimeobj' print(x, ...)
object |
an object of class "spacetimeobj", usually, a result of a call
to |
... |
further arguments passed to or from other methods. |
x |
an object of class "summary.spacetimeobj", usually, a result of a call
to |
This function takes a vector of transformed parameters and applies the appropriate transformations to return them in the original scale for use in anisotropic SPDE models.
transform_parameters_anisotropic(theta, nu_upper_bound = NULL)
transform_parameters_anisotropic(theta, nu_upper_bound = NULL)
theta |
A numeric vector of length 4 or 5, containing the transformed parameters in this order:
|
nu_upper_bound |
(optional) A numeric value representing the upper bound for the smoothness parameter nu.
This is only used, and must be provided, if |
A named list with the parameters in the original scale:
The original scale for hx (exponential of lhx).
The original scale for hy (exponential of lhy).
The original scale for hxy (inverse logit transformation of logit_hxy).
The original scale for sigma (exponential of lsigma).
The original scale for nu (using the forward_nu transformation). Only included if lnu
is provided.
# With lnu theta <- c(log(0.1), log(0.2), log((0.3 + 1) / (1 - 0.3)), log(0.5), log(1)) nu_upper_bound <- 2 transform_parameters_anisotropic(theta, nu_upper_bound) # Without lnu theta <- c(log(0.1), log(0.2), log((0.3 + 1) / (1 - 0.3)), log(0.5)) transform_parameters_anisotropic(theta)
# With lnu theta <- c(log(0.1), log(0.2), log((0.3 + 1) / (1 - 0.3)), log(0.5), log(1)) nu_upper_bound <- 2 transform_parameters_anisotropic(theta, nu_upper_bound) # Without lnu theta <- c(log(0.1), log(0.2), log((0.3 + 1) / (1 - 0.3)), log(0.5)) transform_parameters_anisotropic(theta)
Function to change the parameters of a CBrSPDEobj object
## S3 method for class 'CBrSPDEobj' update( object, user_nu = NULL, user_alpha = NULL, user_kappa = NULL, user_tau = NULL, user_sigma = NULL, user_range = NULL, user_theta = NULL, user_m = NULL, mesh = NULL, loc_mesh = NULL, graph = NULL, range_mesh = NULL, compute_higher_order = object$higher_order, parameterization = NULL, type_rational_approximation = object$type_rational_approximation, return_block_list = object$return_block_list, ... )
## S3 method for class 'CBrSPDEobj' update( object, user_nu = NULL, user_alpha = NULL, user_kappa = NULL, user_tau = NULL, user_sigma = NULL, user_range = NULL, user_theta = NULL, user_m = NULL, mesh = NULL, loc_mesh = NULL, graph = NULL, range_mesh = NULL, compute_higher_order = object$higher_order, parameterization = NULL, type_rational_approximation = object$type_rational_approximation, return_block_list = object$return_block_list, ... )
object |
The covariance-based rational SPDE approximation,
computed using |
user_nu |
If non-null, update the shape parameter of the covariance function. Will be used if parameterization is 'matern'. |
user_alpha |
If non-null, update the fractional SPDE order parameter. Will be used if parameterization is 'spde'. |
user_kappa |
If non-null, update the parameter kappa of the SPDE. Will be used if parameterization is 'spde'. |
user_tau |
If non-null, update the parameter tau of the SPDE. Will be used if parameterization is 'spde'. |
user_sigma |
If non-null, update the standard deviation of the covariance function. Will be used if parameterization is 'matern'. |
user_range |
If non-null, update the range parameter of the covariance function. Will be used if parameterization is 'matern'. |
user_theta |
For non-stationary models. If non-null, update the vector of parameters. |
user_m |
If non-null, update the order of the rational approximation, which needs to be a positive integer. |
mesh |
An optional inla mesh. Replaces |
loc_mesh |
The mesh locations used to construct the matrices C and G. This option should be provided if one wants to use the |
graph |
An optional |
range_mesh |
The range of the mesh. Will be used to provide starting values for the parameters. Will be used if |
compute_higher_order |
Logical. Should the higher order finite element matrices be computed? |
parameterization |
If non-null, update the parameterization. Only works for stationary models. |
type_rational_approximation |
Which type of rational approximation should be used? The current types are "chebfun", "brasil" or "chebfunLB". |
return_block_list |
Logical. For |
... |
Currently not used. |
It returns an object of class "CBrSPDEobj. This object contains the
same quantities listed in the output of matern.operators()
.
simulate.CBrSPDEobj()
, matern.operators()
# Compute the covariance-based rational approximation of a # Gaussian process with a Matern covariance function on R kappa <- 10 sigma <- 1 nu <- 0.8 range <- sqrt(8 * nu) / kappa # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation of covariance function at 0.5 op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) op_cov # Update the range parameter of the model: op_cov <- update(op_cov, user_kappa = 20) op_cov
# Compute the covariance-based rational approximation of a # Gaussian process with a Matern covariance function on R kappa <- 10 sigma <- 1 nu <- 0.8 range <- sqrt(8 * nu) / kappa # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation of covariance function at 0.5 op_cov <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, parameterization = "matern" ) op_cov # Update the range parameter of the model: op_cov <- update(op_cov, user_kappa = 20) op_cov
Function to change the parameters of a CBrSPDEobj object
## S3 method for class 'CBrSPDEobj2d' update( object, user_hx = NULL, user_hy = NULL, user_hxy = NULL, user_sigma = NULL, user_nu = NULL, user_m = NULL, ... )
## S3 method for class 'CBrSPDEobj2d' update( object, user_hx = NULL, user_hy = NULL, user_hxy = NULL, user_sigma = NULL, user_nu = NULL, user_m = NULL, ... )
object |
The covariance-based rational SPDE approximation,
computed using |
user_hx |
If non-null, update the hx parameter. |
user_hy |
If non-null, update the hy parameter. |
user_hxy |
If non-null, update the hxy parameter. |
user_sigma |
If non-null, update the standard deviation of the covariance function. |
user_nu |
If non-null, update the shape parameter of the covariance function. Will be used if parameterization is 'matern'. |
user_m |
If non-null, update the order of the rational approximation, which needs to be a positive integer. |
... |
Currently not used. |
It returns an object of class "CBrSPDEobj2d.
simulate.CBrSPDEobj2d()
, matern2d.operators()
library(fmesher) n_loc <- 2000 loc_2d_mesh <- matrix(runif(n_loc * 2), n_loc, 2) mesh_2d <- fm_mesh_2d(loc = loc_2d_mesh, cutoff = 0.03, max.edge = c(0.1, 0.5)) op <- matern2d.operators(mesh = mesh_2d) op <- update(op, nu = 0.5)
library(fmesher) n_loc <- 2000 loc_2d_mesh <- matrix(runif(n_loc * 2), n_loc, 2) mesh_2d <- fm_mesh_2d(loc = loc_2d_mesh, cutoff = 0.03, max.edge = c(0.1, 0.5)) op <- matern2d.operators(mesh = mesh_2d) op <- update(op, nu = 0.5)
Function to change the parameters of a rSPDEobj object
## S3 method for class 'rSPDEobj' update( object, user_nu = NULL, user_alpha = NULL, user_kappa = NULL, user_sigma = NULL, user_range = NULL, user_tau = NULL, user_theta = NULL, user_m = NULL, mesh = NULL, loc_mesh = NULL, graph = NULL, range_mesh = NULL, parameterization = NULL, ... )
## S3 method for class 'rSPDEobj' update( object, user_nu = NULL, user_alpha = NULL, user_kappa = NULL, user_sigma = NULL, user_range = NULL, user_tau = NULL, user_theta = NULL, user_m = NULL, mesh = NULL, loc_mesh = NULL, graph = NULL, range_mesh = NULL, parameterization = NULL, ... )
object |
The operator-based rational SPDE approximation,
computed using |
user_nu |
If non-null, update the shape parameter of the covariance function. |
user_alpha |
If non-null, update the fractional order. |
user_kappa |
If non-null, update the range parameter of the covariance function. |
user_sigma |
If non-null, update the standard deviation of the covariance function. |
user_range |
If non-null, update the range parameter of the covariance function. |
user_tau |
If non-null, update the parameter tau. |
user_theta |
If non-null, update the parameter theta, that connects tau and kappa to the model matrices. |
user_m |
If non-null, update the order of the rational approximation, which needs to be a positive integer. |
mesh |
An optional inla mesh. Replaces |
loc_mesh |
The mesh locations used to construct the matrices C and G. This option should be provided if one wants to use the |
graph |
An optional |
range_mesh |
The range of the mesh. Will be used to provide starting values for the parameters. Will be used if |
parameterization |
If non-null, update the parameterization. Only works for stationary models. |
... |
Currently not used. |
It returns an object of class "rSPDEobj. This object contains the
same quantities listed in the output of matern.operators()
.
simulate.rSPDEobj()
, matern.operators()
# Compute the operator-based rational approximation of a # Gaussian process with a Matern covariance function on R kappa <- 10 sigma <- 1 nu <- 0.8 range <- sqrt(8 * nu) / kappa # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation of covariance function at 0.5 op <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, type = "operator", parameterization = "matern" ) op # Update the range parameter of the model: op <- update(op, user_kappa = 20) op
# Compute the operator-based rational approximation of a # Gaussian process with a Matern covariance function on R kappa <- 10 sigma <- 1 nu <- 0.8 range <- sqrt(8 * nu) / kappa # create mass and stiffness matrices for a FEM discretization x <- seq(from = 0, to = 1, length.out = 101) fem <- rSPDE.fem1d(x) # compute rational approximation of covariance function at 0.5 op <- matern.operators( loc_mesh = x, nu = nu, range = range, sigma = sigma, d = 1, m = 2, type = "operator", parameterization = "matern" ) op # Update the range parameter of the model: op <- update(op, user_kappa = 20) op
Function to change the parameters of a rSPDEobj1d object
## S3 method for class 'rSPDEobj1d' update( object, user_nu = NULL, user_alpha = NULL, user_kappa = NULL, user_tau = NULL, user_sigma = NULL, user_range = NULL, user_theta = NULL, user_m = NULL, loc = NULL, graph = NULL, parameterization = NULL, type_rational_approximation = object$type_rational_approximation, ... )
## S3 method for class 'rSPDEobj1d' update( object, user_nu = NULL, user_alpha = NULL, user_kappa = NULL, user_tau = NULL, user_sigma = NULL, user_range = NULL, user_theta = NULL, user_m = NULL, loc = NULL, graph = NULL, parameterization = NULL, type_rational_approximation = object$type_rational_approximation, ... )
object |
The covariance-based rational SPDE approximation,
computed using |
user_nu |
If non-null, update the shape parameter of the covariance function. Will be used if parameterization is 'matern'. |
user_alpha |
If non-null, update the fractional SPDE order parameter. Will be used if parameterization is 'spde'. |
user_kappa |
If non-null, update the parameter kappa of the SPDE. Will be used if parameterization is 'spde'. |
user_tau |
If non-null, update the parameter tau of the SPDE. Will be used if parameterization is 'spde'. |
user_sigma |
If non-null, update the standard deviation of the covariance function. Will be used if parameterization is 'matern'. |
user_range |
If non-null, update the range parameter of the covariance function. Will be used if parameterization is 'matern'. |
user_theta |
For non-stationary models. If non-null, update the vector of parameters. |
user_m |
If non-null, update the order of the rational approximation, which needs to be a positive integer. |
loc |
The locations of interest for evaluating the model. |
graph |
An optional |
parameterization |
If non-null, update the parameterization. |
type_rational_approximation |
Which type of rational approximation should be used? The current types are "chebfun", "brasil" or "chebfunLB". |
... |
Currently not used. |
It returns an object of class "rSPDEobj1d". This object contains the
same quantities listed in the output of matern.rational()
.
simulate.rSPDEobj1d()
, matern.rational()
s <- seq(from = 0, to = 1, length.out = 101) kappa <- 20 sigma <- 2 nu <- 0.8 r <- sqrt(8*nu)/kappa #range parameter op_cov <- matern.rational(loc = s, nu = nu, range = r, sigma = sigma, m = 2, parameterization = "matern") cov1 <- op_cov$covariance(ind = 1) op_cov <- update(op_cov, user_range = 0.2) cov2 <- op_cov$covariance(ind = 1) plot(s, cov1, type = "l") lines(s, cov2, col = 2)
s <- seq(from = 0, to = 1, length.out = 101) kappa <- 20 sigma <- 2 nu <- 0.8 r <- sqrt(8*nu)/kappa #range parameter op_cov <- matern.rational(loc = s, nu = nu, range = r, sigma = sigma, m = 2, parameterization = "matern") cov1 <- op_cov$covariance(ind = 1) op_cov <- update(op_cov, user_range = 0.2) cov2 <- op_cov$covariance(ind = 1) plot(s, cov1, type = "l") lines(s, cov2, col = 2)
Update space-time operator object with new parameters
update.spacetimeobj( object, user_kappa = NULL, user_sigma = NULL, user_gamma = NULL, user_rho = NULL )
update.spacetimeobj( object, user_kappa = NULL, user_sigma = NULL, user_gamma = NULL, user_rho = NULL )
object |
Space-time object created by |
user_kappa |
kappa value to be updated. |
user_sigma |
sigma value to be updated. |
user_gamma |
gamma value to be updated. |
user_rho |
rho value to be updated. |
An object of type spacetimeobj with updated parameters.
s <- seq(from = 0, to = 20, length.out = 101) t <- seq(from = 0, to = 20, length.out = 31) op_cov <- spacetime.operators(space_loc = s, time_loc = t, kappa = 5, sigma = 10, alpha = 1, beta = 2, rho = 1, gamma = 0.05) op_cov <- update.spacetimeobj(op_cov, user_kappa = 4, user_sigma = 2, user_gamma = 0.1)
s <- seq(from = 0, to = 20, length.out = 101) t <- seq(from = 0, to = 20, length.out = 31) op_cov <- spacetime.operators(space_loc = s, time_loc = t, kappa = 5, sigma = 10, alpha = 1, beta = 2, rho = 1, gamma = 0.05) op_cov <- update.spacetimeobj(op_cov, user_kappa = 4, user_sigma = 2, user_gamma = 0.1)
Variogram of intrinsic SPDE
model
with Neumann boundary conditions and a mean-zero constraint on a
square for
or
.
variogram.intrinsic.spde( s0 = NULL, s = NULL, kappa = NULL, alpha = NULL, beta = NULL, tau = 1, L = NULL, N = 100, d = NULL )
variogram.intrinsic.spde( s0 = NULL, s = NULL, kappa = NULL, alpha = NULL, beta = NULL, tau = 1, L = NULL, N = 100, d = NULL )
s0 |
The location where the variogram should be evaluated, either a double for 1d or a vector for 2d |
s |
A vector (in 1d) or matrix (in 2d) with all locations where the variogram is computed |
kappa |
Range parameter. |
alpha |
Smoothness parameter. |
beta |
Smoothness parameter. |
tau |
Precision parameter. |
L |
The side length of the square domain. |
N |
The number of terms in the Karhunen-Loeve expansion. |
d |
The dimension (1 or 2). |
The variogram is computed based on a Karhunen-Loeve expansion of the covariance function.
if (requireNamespace("RSpectra", quietly = TRUE)) { x <- seq(from = 0, to = 10, length.out = 201) beta <- 1 alpha <- 1 kappa <- 1 op <- intrinsic.matern.operators( kappa = kappa, tau = 1, alpha = alpha, beta = beta, loc_mesh = x, d = 1 ) # Compute and plot the variogram of the model Sigma <- op$A[,-1] %*% solve(op$Q[-1,-1], t(op$A[,-1])) One <- rep(1, times = ncol(Sigma)) D <- diag(Sigma) Gamma <- 0.5 * (One %*% t(D) + D %*% t(One) - 2 * Sigma) k <- 100 plot(x, Gamma[k, ], type = "l") lines(x, variogram.intrinsic.spde(x[k], x, kappa, alpha, beta, L = 10, d = 1), col = 2, lty = 2 ) }
if (requireNamespace("RSpectra", quietly = TRUE)) { x <- seq(from = 0, to = 10, length.out = 201) beta <- 1 alpha <- 1 kappa <- 1 op <- intrinsic.matern.operators( kappa = kappa, tau = 1, alpha = alpha, beta = beta, loc_mesh = x, d = 1 ) # Compute and plot the variogram of the model Sigma <- op$A[,-1] %*% solve(op$Q[-1,-1], t(op$A[,-1])) One <- rep(1, times = ncol(Sigma)) D <- diag(Sigma) Gamma <- 0.5 * (One %*% t(D) + D %*% t(One) - 2 * Sigma) k <- 100 plot(x, Gamma[k, ], type = "l") lines(x, variogram.intrinsic.spde(x[k], x, kappa, alpha, beta, L = 10, d = 1), col = 2, lty = 2 ) }