Title: | Implementation of the Direct Sampling Spatial Prior |
---|---|
Description: | Draw samples from the direct sampling spatial prior model as described in G. White, D. Sun, P. Speckman (2019) <arXiv:1906.05575>. The basic model assumes a Gaussian likelihood and derives a spatial prior based on thin-plate splines. |
Authors: | Gentry White [aut, cre] , Rex Parsons [aut] |
Maintainer: | Gentry White <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.1.1.9000 |
Built: | 2024-11-07 05:14:04 UTC |
Source: | https://github.com/gentrywhite/dssp |
Draw samples from the direct sampling spatial prior model as described in G. White, D. Sun, P. Speckman (2019) <arXiv:1906.05575>. The basic model assumes a Gaussian likelihood and derives a spatial prior based on thin-plate splines.
Index of help topics:
DSSP DSSP DSSP-package Implementation of the Direct Sampling Spatial Prior make.M Precision Matrix Function plot.dsspMod Diagnostic, Density and Contour Plots predict.dsspMod Predictions from a model with new data. residuals.dsspMod Get residuals from 'dsspMod' model sample.delta Function to sample from the posterior of the variance parameter sample.eta Function to sample from the posterior of the smoothing parameter eta conditioned on the data y. sample.nu Function to sample from the posterior of the spatial effects summary.dsspMod Summarise a 'dsspMod' model tps.rbf TPS radial basis function
Gentry White <[email protected]>
Gentry White [aut, cre] (<https://orcid.org/0000-0002-1170-9299>), Rex Parsons [aut] (<https://orcid.org/0000-0002-6053-8174>)
This function samples from the log-posterior of all parameters in the model and returns a list object containing the samples. It performs a few compatibility checks on the inputs, then calls the sample.eta(), sample.delta(), and sample.nu().
DSSP(formula, data, N, pars, log_prior = function(x) -x, coords = NULL)
DSSP(formula, data, N, pars, log_prior = function(x) -x, coords = NULL)
formula |
a two sided linear formula with the response on left and the covariates on the right. |
data |
a |
N |
is the number of random samples to be drawn from the joint posterior for eta, delta, and nu. |
pars |
a vector of the prior shape and rate parameters for the inverse-gamma prior distribution of delta, the variance parameter for the Gaussian likelihood. |
log_prior |
a function evaluating the log of the prior density of eta. Default to be |
coords |
spatial coordinates passed as the |
The direct sampling spatial prior model assumes that the spatial model can be written as the likelihood parameterised with mean vector nu and variance delta
where I is the identity matrix. The prior for the vector of spatial effects nu is improper but is proportional to
the prior for delta is assumed to be a inverse-gamma distribution
and the prior for eta can be specified for the user as any valid density function for eta > 0.
A list containing N samples of nu, eta, delta, and the original data X and Y.
## Use the Meuse River dataset from the package 'gstat' library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y f <- function(x) -x ## log-prior for exponential distribution for the smoothing parameter ## Draw 100 samples from the posterior of eta given the data y. OUTPUT <- DSSP( formula = log(zinc) ~ 1, data = meuse.all, N = 100, pars = c(0.001, 0.001), log_prior = f )
## Use the Meuse River dataset from the package 'gstat' library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y f <- function(x) -x ## log-prior for exponential distribution for the smoothing parameter ## Draw 100 samples from the posterior of eta given the data y. OUTPUT <- DSSP( formula = log(zinc) ~ 1, data = meuse.all, N = 100, pars = c(0.001, 0.001), log_prior = f )
This function creates the precision matrix for the spatial prior based on thin-plate splines and returns the matrix M, and its eigenvalues and eigenvectors
make.M(X, covariates)
make.M(X, covariates)
X |
a matrix of spatial coordinates. It is recommended that the coordinates be scaled and centred. |
covariates |
the observed values for the covariates (including intercept). |
The M matrix is the precision matrix for the spatial effects from the direct sampling spatial prior (DSSP) model. M is based on thin plate splines basis functions, see White et. al. 2019 for more details on how the matrix M is constructed.
A list containing the precision matrix M and the object M.eigen containing eigenvalues and eigenvectors for the matrix M.
## Use the Meuse River dataset from the package 'gstat' library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y X <- scale(coordinates(meuse.all)) make.M(X)
## Use the Meuse River dataset from the package 'gstat' library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y X <- scale(coordinates(meuse.all)) make.M(X)
Diagnostic, Density and Contour Plots
## S3 method for class 'dsspMod' plot( x, robust_residuals = TRUE, contour_plots = TRUE, nx = 100, ny = 100, nlevels = 5, ... )
## S3 method for class 'dsspMod' plot( x, robust_residuals = TRUE, contour_plots = TRUE, nx = 100, ny = 100, nlevels = 5, ... )
x |
an object of class |
robust_residuals |
whether to use robust residuals (median of predicted).
Default to be |
contour_plots |
whether or not to return a second panel with contour plots.
Defaults to |
nx |
dimension of output grid in x direction.
Used for interpolation ( |
ny |
dimension of output grid in y direction.
Used for interpolation ( |
nlevels |
number of levels used in contour plot. |
... |
additional arguments that are passed to |
a list containing the plots printed (individually and together in grid)
library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y f <- function(x) -x ## log-prior for exponential distribution for the smoothing parameter ## Draw 100 samples from the posterior of eta given the data y. OUTPUT <- DSSP( formula = log(zinc) ~ 1, data = meuse.all, N = 100, pars = c(0.001, 0.001), log_prior = f ) plot(OUTPUT, contour_plots = FALSE)
library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y f <- function(x) -x ## log-prior for exponential distribution for the smoothing parameter ## Draw 100 samples from the posterior of eta given the data y. OUTPUT <- DSSP( formula = log(zinc) ~ 1, data = meuse.all, N = 100, pars = c(0.001, 0.001), log_prior = f ) plot(OUTPUT, contour_plots = FALSE)
Predictions from a model with new data.
## S3 method for class 'dsspMod' predict(object, newdata, ...)
## S3 method for class 'dsspMod' predict(object, newdata, ...)
object |
a fitted dsspMod object. |
newdata |
a data frame for which to evaluate predictions. |
... |
optional and ignored arguments. |
returns matrix with posterior densities for each row in the input data.
data("meuse.all", package = "gstat") sp::coordinates(meuse.all) <- ~ x + y meuse.fit <- DSSP( formula = log(zinc) ~ 1, data = meuse.all[1:155, ], N = 100, function(x) -2 * log(1 + x), pars = c(0.001, 0.001) ) preds <- predict(meuse.fit, meuse.all[156:164, ])
data("meuse.all", package = "gstat") sp::coordinates(meuse.all) <- ~ x + y meuse.fit <- DSSP( formula = log(zinc) ~ 1, data = meuse.all[1:155, ], N = 100, function(x) -2 * log(1 + x), pars = c(0.001, 0.001) ) preds <- predict(meuse.fit, meuse.all[156:164, ])
dsspMod
modelGet residuals from dsspMod
model
## S3 method for class 'dsspMod' residuals(object, newdata, robust = TRUE, ...)
## S3 method for class 'dsspMod' residuals(object, newdata, robust = TRUE, ...)
object |
an object of class |
newdata |
a data frame for which to estimate residuals. |
robust |
whether or not to use median (rather than mean) of posterior density to as estimate calculate residuals. |
... |
additional arguments which are ignored. |
vector containing residuals with same length as rows in data used.
library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y f <- function(x) -x ## log-prior for exponential distribution for the smoothing parameter ## Draw 100 samples from the posterior of eta given the data y. OUTPUT <- DSSP( formula = log(zinc) ~ 1, data = meuse.all, N = 100, pars = c(0.001, 0.001), log_prior = f ) residuals(OUTPUT)
library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y f <- function(x) -x ## log-prior for exponential distribution for the smoothing parameter ## Draw 100 samples from the posterior of eta given the data y. OUTPUT <- DSSP( formula = log(zinc) ~ 1, data = meuse.all, N = 100, pars = c(0.001, 0.001), log_prior = f ) residuals(OUTPUT)
This function samples from the log-posterior density of the variance parameter from the likelihood
sample.delta(eta, ND, EV, Q, pars)
sample.delta(eta, ND, EV, Q, pars)
eta |
samples of the smoothing parameter from the sample.eta function. |
ND |
the rank of the precision matrix, the default value is n-3 for spatial data. |
EV |
eigenvalues of the precision matrix spatial prior from the function make.M(). |
Q |
the data vector from the cross-product of observed data, Y, and eigenvalues from the M matrix, V. |
pars |
a vector of the prior shape and rate parameters for the inverse-gamma prior distribution of delta. |
N samples drawn from the posterior of .
## Use the Meuse River dataset from the package 'gstat' library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y X <- scale(coordinates(meuse.all)) tmp <- make.M(X) M <- tmp$M Y <- scale(log(meuse.all$zinc)) ND <- nrow(X) - 3 M.list <- make.M(X) ## Only Needs to return the eigenvalues and vectors M <- M.list$M EV <- M.list$M.eigen$values V <- M.list$M.eigen$vectors Q <- crossprod(Y, V) f <- function(x) -x ## log-prior for exponential distribution for the smoothing parameter ## Draw 100 samples from the posterior of eta given the data y. ETA <- sample.eta(100, ND, EV, Q, f, UL = 1000) DELTA <- sample.delta(ETA, ND, EV, Q, pars = c(0.001, 0.001)) ## Old Slow Version of sample.nu() ## sample.delta<-function(eta,nd,ev,Q,pars) ## { ## N<-length(eta) ## f.beta<-function(x) ## { ## lambda<-1/(1+x*ev) ## b<-tcrossprod(Q,diag(1-lambda)) ## beta<-0.5*tcrossprod(Q,b)+pars[2] ## return(beta) ## } ## alpha<-pars[1]+nd*0.5 ## beta<-sapply(eta,f.beta) ## delta<-1/rgamma(N,shape=alpha,rate=beta) ## return(delta) ## }
## Use the Meuse River dataset from the package 'gstat' library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y X <- scale(coordinates(meuse.all)) tmp <- make.M(X) M <- tmp$M Y <- scale(log(meuse.all$zinc)) ND <- nrow(X) - 3 M.list <- make.M(X) ## Only Needs to return the eigenvalues and vectors M <- M.list$M EV <- M.list$M.eigen$values V <- M.list$M.eigen$vectors Q <- crossprod(Y, V) f <- function(x) -x ## log-prior for exponential distribution for the smoothing parameter ## Draw 100 samples from the posterior of eta given the data y. ETA <- sample.eta(100, ND, EV, Q, f, UL = 1000) DELTA <- sample.delta(ETA, ND, EV, Q, pars = c(0.001, 0.001)) ## Old Slow Version of sample.nu() ## sample.delta<-function(eta,nd,ev,Q,pars) ## { ## N<-length(eta) ## f.beta<-function(x) ## { ## lambda<-1/(1+x*ev) ## b<-tcrossprod(Q,diag(1-lambda)) ## beta<-0.5*tcrossprod(Q,b)+pars[2] ## return(beta) ## } ## alpha<-pars[1]+nd*0.5 ## beta<-sapply(eta,f.beta) ## delta<-1/rgamma(N,shape=alpha,rate=beta) ## return(delta) ## }
This function samples from the log-posterior density of the smoothing parameter from the thin-plate splines based spatial prior using a ratio-of-uniform sampler.
sample.eta(N, ND, EV, Q, UL = 1000, log_prior)
sample.eta(N, ND, EV, Q, UL = 1000, log_prior)
N |
the number of samples desired. |
ND |
the rank of the precision matrix, the default value is n-3 for spatial data. |
EV |
eigenvalues of the precision matrix spatial prior from the function make.M(). |
Q |
the data vector from the cross-product of observed data, Y, and eigenvalues from the M matrix, V. |
UL |
the upper limit for the smoothing parameter value; used for the ratio-of-uniform sampler, default is 1000. |
log_prior |
a function of x evaluating the log of the prior density for eta |
N samples drawn from the posterior of eta given the data y .
## Use the Meuse River dataset from the package 'gstat' library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y X <- scale(coordinates(meuse.all)) tmp <- make.M(X) EV <- tmp$M.eigen$values V <- tmp$M.eigen$vectors M <- tmp$M Y <- scale(log(meuse.all$zinc)) Q <- crossprod(Y, V) ND <- nrow(X) - 3 f <- function(x) -x ## log-prior for exponential distribution for the smoothing parameter ## Draw 100 samples from the posterior of eta given the data y. sample.eta(100, ND, EV, Q, UL = 1000, f)
## Use the Meuse River dataset from the package 'gstat' library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y X <- scale(coordinates(meuse.all)) tmp <- make.M(X) EV <- tmp$M.eigen$values V <- tmp$M.eigen$vectors M <- tmp$M Y <- scale(log(meuse.all$zinc)) Q <- crossprod(Y, V) ND <- nrow(X) - 3 f <- function(x) -x ## log-prior for exponential distribution for the smoothing parameter ## Draw 100 samples from the posterior of eta given the data y. sample.eta(100, ND, EV, Q, UL = 1000, f)
This function samples from the posterior density of the spatial effects from the direct sampling spatial prior (DSSP) model.
sample.nu(Y, eta, delta, EV, V)
sample.nu(Y, eta, delta, EV, V)
Y |
vector of observed data. |
eta |
samples of the smoothing parameter from the |
delta |
samples of the variance parameter from the |
EV |
eigenvalues of the precision matrix spatial prior from the function |
V |
eigenvectors of the precision matrix spatial prior from the function |
A matrix of samples with each column a random draw from the posterior
of the spatial effects from the DSSP model .
## Use the Meuse River dataset from the package 'gstat' library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y X <- scale(coordinates(meuse.all)) tmp <- make.M(X) EV <- tmp$M.eigen$values V <- tmp$M.eigen$vectors Y <- scale(log(meuse.all$zinc)) Q <- crossprod(Y, V) ND <- nrow(X) - 3 f <- function(x) -x ## log-prior for exponential distribution for the smoothing parameter ## Draw 100 samples from the posterior of eta given the data y. ETA <- sample.eta(100, ND, EV, Q, f, UL = 1000) DELTA <- sample.delta(ETA, ND, EV, Q, pars = c(0.001, 0.001)) NU <- sample.nu(Y, ETA, DELTA, EV, V)
## Use the Meuse River dataset from the package 'gstat' library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y X <- scale(coordinates(meuse.all)) tmp <- make.M(X) EV <- tmp$M.eigen$values V <- tmp$M.eigen$vectors Y <- scale(log(meuse.all$zinc)) Q <- crossprod(Y, V) ND <- nrow(X) - 3 f <- function(x) -x ## log-prior for exponential distribution for the smoothing parameter ## Draw 100 samples from the posterior of eta given the data y. ETA <- sample.eta(100, ND, EV, Q, f, UL = 1000) DELTA <- sample.delta(ETA, ND, EV, Q, pars = c(0.001, 0.001)) NU <- sample.nu(Y, ETA, DELTA, EV, V)
dsspMod
modelSummarise a dsspMod
model
## S3 method for class 'dsspMod' summary(object, prob = 0.95, robust = FALSE, mc_se = FALSE, ...)
## S3 method for class 'dsspMod' summary(object, prob = 0.95, robust = FALSE, mc_se = FALSE, ...)
object |
an object of class |
prob |
the desired probability to be covered by the credible intervals. The default is 0.95. |
robust |
whether or not to use the median (rather than the mean) to
calculate the estimates that summarise the posterior.
Default to |
mc_se |
whether or not to include the uncertainty in |
... |
additional arguments which are ignored. |
An object of class "dsspModsummary". Provides a summary of the the Direct Sampling Spatial Prior (DSSP) model. Includes details of the formula used to fit the model, and a summary of the model () and the covariates.
library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y f <- function(x) -x ## log-prior for exponential distribution for the smoothing parameter ## Draw 100 samples from the posterior of eta given the data y. OUTPUT <- DSSP( formula = log(zinc) ~ 1, data = meuse.all, N = 100, pars = c(0.001, 0.001), log_prior = f ) summary(OUTPUT)
library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y f <- function(x) -x ## log-prior for exponential distribution for the smoothing parameter ## Draw 100 samples from the posterior of eta given the data y. OUTPUT <- DSSP( formula = log(zinc) ~ 1, data = meuse.all, N = 100, pars = c(0.001, 0.001), log_prior = f ) summary(OUTPUT)
Function to compute the thin-plate splines radial basis function for internal use by the function make.M().
tps.rbf(x, is.even)
tps.rbf(x, is.even)
x |
is a Euclidean distance between two points. |
is.even |
is a logical argument indicating TRUE if the dimension of the space where the thin-plate spline smoother is being fitted is even. |
This function computes the thin-plate spline radial basis function depending on the if d is odd or even.
The resulting value of the thin-plate spline radial basis function.
## Use the Meuse River dataset from the package 'gstat' library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y X <- scale(coordinates(meuse.all)) D <- as.matrix(dist(X)) K <- tps.rbf(D, TRUE)
## Use the Meuse River dataset from the package 'gstat' library(sp) library(gstat) data(meuse.all) coordinates(meuse.all) <- ~ x + y X <- scale(coordinates(meuse.all)) D <- as.matrix(dist(X)) K <- tps.rbf(D, TRUE)