Make Euclidean distance matrices for use in CReSS and SALSA model frameworks
Source:R/functions.R
makeDists.Rd
This function makes two Euclidean distance matrices. One for the distances between all spatial observations and all spatial knot locations. The other, if specified, is the distances between knot locations. It is experimental owing to the addition of the creation of infinity-block distance matrices. The original functionality is unchanged.
Usage
makeDists(
datacoords,
knotcoords,
knotmat = TRUE,
polys = NULL,
type = "A",
plot.transition = FALSE,
grid.dim = c(100, 100)
)
Arguments
- datacoords
Coordinates of the data locations. If a design matrix interaction is required, add a third column containing the factor level.
- knotcoords
Coordinates of the legal knot locations. If a design matrix interaction is required, add a third column containing the factor level for each knot location.
- knotmat
(
default=TRUE
). Should a matrix of knot-knot distances be created- polys
(
default=NULL
). If geodesic distances are to be calculated, provide a list of polygons defining exclusion areas.- type
(
default='A'
). One of 'A' or 'B'. 'A' is used when theknotcoords
are a subset ofdatacoords
AND the attributes ofknotcoords
give the index of points fromdatacoords
(as happens ifgetKnotgrid()
is used). 'B' is used for prediction (whendatacoords
is a prediction grid and soknotcoords
is not a subset) or when the knotgrid was not generated usinggetKnotgrid()
.- plot.transition
(
default=TRUE
). Logical stating whether to plot the transition matrix. Useful to see if the boundaries are being obeyed.- grid.dim
This is a vector of length two which specifies the dimesions of the grid used to create the transition matrix (default
c(100, 100)
. If the transition matrix shows that boundaries are being ignored, the grid dimensions will need to increase. However, increasing the grid, whilst improving accuracy, also increases computational time.
Details
The data-knot matrix is used in the CReSS basis and the knot-knot matrix is used in SALSA to determine where a nearest knot to `move' should be.
If three columns are provided for datacoords
and knotcoords
the matrix returned has infinity for distances between knots and data associated with differing factor levels.
Examples
# load data
data(ns.data.re)
# load knot grid data
data(knotgrid.ns)
# make distance matrices for datatoknots and knottoknots
distMats<-makeDists(cbind(ns.data.re$x.pos, ns.data.re$y.pos), na.omit(knotgrid.ns))
# ~~~~~~~~~~~~~~~~~~
# Example with block-infinity distance matrix
data(nysted.analysisdata)
myknots <- selectFctrKnots(nysted.analysisdata[,c('x.pos', 'y.pos', 'impact')], nk=150)
#> Loading required package: fields
#> Loading required package: spam
#> Spam version 2.9-1 (2022-08-07) is loaded.
#> Type 'help( Spam)' or 'demo( spam)' for a short introduction
#> and overview of this package.
#> Help for individual functions is also obtained by adding the
#> suffix '.spam' to the function name, e.g. 'help( chol.spam)'.
#>
#> Attaching package: 'spam'
#> The following objects are masked from 'package:mvtnorm':
#>
#> rmvnorm, rmvt
#> The following object is masked from 'package:Matrix':
#>
#> det
#> The following objects are masked from 'package:base':
#>
#> backsolve, forwardsolve
#> Loading required package: viridis
#> Loading required package: viridisLite
#>
#> Try help(fields) to get started.
dists <- makeDists(datacoords = nysted.analysisdata[,c('x.pos', 'y.pos', 'impact')],
knotcoords = myknots,
knotmat = TRUE)