Title: | Artificial Spatial and Spatio-Temporal Densities on Bounded Windows |
---|---|
Description: | Simple utilities to design and generate density functions on bounded regions in space and space-time, and simulate independent, identically distributed data therefrom. See Davies & Lawson (2019) <doi:10.1080/00949655.2019.1575066> for example. |
Authors: | Anna K. Redmond [aut], Tilman M. Davies [aut, cre] |
Maintainer: | Tilman M. Davies <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.4-2 |
Built: | 2024-12-23 06:35:21 UTC |
Source: | CRAN |
Provides functions to design synthetic spatial and spatiotemporal densities and relative risk functions based mainly on Gaussian mixture distributions, and simulate independent and identically distributed data therefrom.
Package: | spagmix |
Version: | 0.4-2 |
Date: | 2024-06-25 |
License: | GPL (>= 2) |
Appraisal of existing, refined, and new statistical methods for the analysis of spatial and spatiotemporal point pattern data usually involves numeric experimentation. Motivated by relevant problems in nonparametric density estimation (see e.g. Wand & Jones, 1995), spagmix
(“spatial Gaussian mixtures”) provides some simple utilities for designing heterogeneous density and density-ratio or relative risk (Bithell 1990, 1991; Kelsall & Diggle, 1995) functions in space and space-time (see Fernando & Hazelton, 2014 for the latter). The package is also capable of producing realisations of (possibly inhomogeneous) spatial log-Gaussian Cox process intensities (Møller et al., 1998; see also Davies & Hazelton, 2013).
Additionally, functions for simulating datasets given these scenarios are included. For examples of how these kinds of synthetic functions have been used in simulation studies in various publications, see for example Clark & Lawson, 2004; Davies & Hazelton, 2010; Davies, 2013a,b; Davies & Hazelton, 2013; Fernando et al., 2014; Davies et al., 2016; Davies et al., 2018a; and Davies & Lawson, 2019.
We have designed the objects of spagmix
to use and be compatible with standard object classes of the spatstat
(Baddeley & Turner, 2005; Baddeley et al., 2015) and sparr
(Davies et al., 2018b) packages. The content of spagmix
can be broken up as follows:
Artificial polygonal windows
Some pre-made synthetic spatial windows; these are all single closed polygons as objects of class owin
and are lazy-loaded:bx
, heart
, shp1
, shp2
, star
, toywin
Spatial scenariossgmix
is used to create spatial (2D) Gaussian mixture distributions on a bounded subset of the plane.rgmix
also creates 2D Gaussian mixture densities, but does so by stochastic generation of the contributing bumps.rrmix
creates Gaussian mixture relative risk scenarios based on a supplied control density (see e.g. Davies & Hazelton, 2010).
lgcpmix
generates a spatial log-Gaussian Cox process intensity in space, given a deterministic intensity function and residual correlation governed by a stochastic realisation of a Gaussian field with a specified covariance structure.
Spatiotemporal scenariosstgmix
is used to create spatiotemporal (3D) Gaussian mixture densities on a bounded subset of the plane and a single closed interval in time.stkey
is used to create spatiotemporal densities by pixel-wise interpolation of multiple spatial image ‘keyframes’.
rrstmix
is a spatiotemporal version of rrmix
, used to create artificial spatiotemporal relative risk functions. Note the control density may be purely spatial, representing a distribution ‘at-risk’ points that does not change over time (Fernando & Hazelton, 2014).
Data generation
To generate purely spatial data for a single spatial density, the user is directed to rpoint
of the spatstat
package or rimpoly
of the sparr
package.rpoispoly
is a wrapper of rimpoly
, and is used to generate realisations of Poisson point processes in space, given an intensity function.
rrpoint
is a wrapper of rimpoly
, and is used to generate iid datasets based on a synthetic spatial relative risk surface object.rstpoint
is a 3D rejection algorithm for sampling iid data from a supplied spatiotemporal density.rrstpoint
is a wrapper of rstpoint
to generate iid datasets from a synthetic spatiotemporal relative risk surface object.
Miscellaneousplot.stim
is an S3
plotting method for spatiotemporal density objects.stintegral
computes the 3D integral of a spatiotemporal density object.unify.owin
is a wrapper for affine
that transforms any spatial owin
to fall inside the unit square.
Depends on spatstat
functionality (Baddeley & Turner, 2005; Baddeley et al., 2015) and imports from abind
(Plate & Heiberger, 2016), sparr
(Davies et al., 2018b), and mvtnorm (Genz et al., 2018). We also highly recommend the rgl
package (Adler et al., 2018) which can be used to create interactive plots of spatiotemporal data.
A.K. Redmond and T.M. Davies
Dept. of Mathematics & Statistics, University of Otago, Dunedin, New Zealand
Maintainer: T.M.D. [email protected]
Adler, D., Murdoch, D. and others (2018), rgl: 3D Visualization Using OpenGL, R package version 0.99.16 https://CRAN.R-project.org/package=rgl
Baddeley, A., Rubak, E. and Turner, R. (2015), Spatial Point Patterns: Methodology and Applications with R, Chapman and Hall/CRC Press, London.
Baddeley, A. and Turner, R. (2005), Spatstat: an R package for analyzing spatial point patterns, Journal of Statistical Software, 12(6), 1-42.
Bithell, J.F. (1990), An application of density estimation to geographical epidemiology, Statistics in Medicine, 9, 691-701.
Bithell, J.F. (1991), Estimation of relative risk function,. Statistics in Medicine, 10, 1745-1751.
Clark, A.B. and Lawson, A.B. (2004), An evaluation of non-parametric relative risk estimators for disease maps, Computational Statistics & Data Analysis, 47, 63-78.
Davies, T.M. (2013a), Jointly optimal bandwidth selection for the planar kernel-smoothed density-ratio, Spatial and Spatio-temporal Epidemiology, 5, 51-65.
Davies, T.M. (2013b), Scaling oversmoothing factors for kernel estimation of spatial relative risk, Epidemiological Methods, 2(1), 67-83.
Davies, T.M. and Hazelton, M.L. (2010), Adaptive kernel estimation of spatial relative risk, Statistics in Medicine, 29(23), 2423-2437.
Davies, T.M. and Hazelton, M.L. (2013), Assessing minimum contrast parameter estimation for spatial and spatiotemporal log-Gaussian Cox processes, Statistica Neerlandica, 67(4), 355-389.
Davies, T.M., Jones, K. and Hazelton, M.L. (2016), Symmetric adaptive smoothing regimens for estimation of the spatial relative risk function, Computational Statistics & Data Analysis, 101, 12-28.
Davies, T.M. and Lawson, A.B. (2019), An evaluation of likelihood-based bandwidth selectors for spatial and spatiotemporal kernel estimates, Journal of Statistical Computation and Simulation, 89 1131-1152.
Davies, T.M., Flynn, C.R. and Hazelton, M.L. (2018a), On the utility of asymptotic bandwidth selectors for spatially adaptive kernel density estimation, Statistics & Probability Letters, 138, 75-81.
Davies, T.M., Marshall, J.C. and Hazelton, M.L. (2018b), Tutorial on kernel estimation of continuous spatial and spatiotemporal relative risk, Statistics in Medicine, 37(7), 1191-1221.
Fernando, W.T.P.S., Ganesalingam, S. and Hazelton, M.L. (2014), A comparison of estimators of the geographical relative risk function, Journal of Statistical Computation and Simulation, 84(7), 1471-1485.
Fernando, W.T.P.S. and Hazelton, M.L. (2014), Generalizing the spatial relative risk function, Spatial and Spatio-temporal Epidemiology, 8, 1-10.
Genz, A., Bretz, F., Miwa, T., Mi, X., Leisch, F., Scheipl, F. and Hothorn, T. (2018), mvtnorm: Multivariate Normal and t Distributions, R package version 1.0-8. URL http://CRAN.R-project.org/package=mvtnorm
Kelsall, J.E. and Diggle, P.J. (1995), Kernel estimation of relative risk, Bernoulli, 1, 3-16.
Møller, J., Syversveen, A.R. and Waagepetersen, R.P. (1998), Log-Gaussian Cox processes, Scandinavian Journal of Statistics, 25(3) 451–482.
Plate, T. and Heiberger, R. (2016), abind: Combine Multidimensional Arrays, R package version 1.4-5. https://CRAN.R-project.org/package=abind
Generate a realisation of a (possibly inhomogeneous) log-Gaussian Cox process (LGCP) spatial intensity function with an identifiable mean structure.
lgcpmix(lambda, ...)
lgcpmix(lambda, ...)
lambda |
A pixel |
... |
Additional arguments controlling the Gaussian random field to be passed to |
This function allows the user to generate a spatial intensity function of the form
for , where
(passed to
lambda
) is the deterministic spatial intensity over the spatial domain , and
is a Gaussian random field on
. This Gaussian field, implemented through
rLGCP
, is defined with a particular spatial covariance function (specified via the model
argument given to ...
) with variance and scale parameters and
respectively, as well as any additionally required parameter values (all specified in the
param
argument, also given to ...
). For example, requesting model = "exponential"
with param = list(var=
,scale=
))
imposes an exponential covariance structure on the generated field whereby for the Euclidean distance between any two spatial locations
.
The mean parameter of the Gaussian field
is internally fixed at
; negative half the variance. This is for identifiability of the mean structure, forcing
for all
(see theoretical properties in Møller et al., 1998). In turn, this means the deterministic intensity function
is solely responsible for describing fixed heterogeneity in spatial intensity over
(as such, the pixel
im
age supplied to lambda
as must be non-negatively-valued and yield a finite integral), with the randomly generated Gaussian field left to describe residual stochastic spatial correlation. This presents a highly flexible class of model, even with stationarity and isotropy of the Gaussian field itself, and is intuitively sensible in a variety of applications. See Diggle et al. (2005) and Davies & Hazelton (2013) for example. Given this, any user-supplied value of
mu
in ...
(intended for rLGCP
) is irrelevant and will be ignored/overwritten.
To generate a subsequent dataset, use e.g. rpoispp
or rpoispoly
.
A pixel im
age giving the generated intensity function, comprised of the product of lambda
(fixed, and unchanging in repeated calls to this function) and the exponentiated Gaussian field (with expected value 1, this is stochastic and varies in repeated calls).
T.M. Davies.
Davies, T.M. and Hazelton, M.L. (2013), Assessing minimum contrast parameter estimation for spatial and spatiotemporal log-Gaussian Cox processes, Statistica Neerlandica, 67(4) 355–389.
Diggle, P.J., Rowlingson, B. and Su, T. (2005), Point process methodology for on-line spatio-temporal disease surveillance, Environmetrics, 16 423–434.
Møller, J., Syversveen, A.R. and Waagepetersen, R.P. (1998), Log-Gaussian Cox processes, Scandinavian Journal of Statistics, 25(3) 451–482.
## Homogeneous example ## # Create constant intensity image integrating to 500 homog <- as.im(as.mask(toywin)) homog <- homog/integral(homog)*500 # Corresponding LGCP realisations using exponential covariance structure oldpar <- par(mfrow=c(2,2),mar=rep(1.5,4)) for(i in 1:4){ temp <- lgcpmix(homog,model="exponential",param=list(var=1,scale=0.2)) plot(temp,main=paste("Realisation",i),log=TRUE) } par(oldpar) ## Inhomogeneous examples ## # Create deterministic trend mn <- cbind(c(0.25,0.8),c(0.31,0.82),c(0.43,0.64),c(0.63,0.62),c(0.21,0.26)) v1 <- matrix(c(0.0023,-0.0009,-0.0009,0.002),2) v2 <- matrix(c(0.0016,0.0015,0.0015,0.004),2) v3 <- matrix(c(0.0007,0.0004,0.0004,0.0011),2) v4 <- matrix(c(0.0023,-0.0041,-0.0041,0.0099),2) v5 <- matrix(c(0.0013,0.0011,0.0011,0.0014),2) vr <- array(NA,dim=c(2,2,5)) for(i in 1:5) vr[,,i] <- get(paste("v",i,sep="")) intens <- sgmix(mean=mn,vcv=vr,window=toywin,p0=0.1,int=500) # Two realisations (identical calls to function), exponential covariance structure r1exp <- lgcpmix(lambda=intens,model="exponential",param=list(var=2,scale=0.05)) r2exp <- lgcpmix(lambda=intens,model="exponential",param=list(var=2,scale=0.05)) # Two more realisations, Matern covariance with smoothness 1 r1mat <- lgcpmix(lambda=intens,model="matern",param=list(var=2,scale=0.05,nu=1)) r2mat <- lgcpmix(lambda=intens,model="matern",param=list(var=2,scale=0.05,nu=1)) # Plot everything, including 'intens' alone (no correlation) oldpar <- par(mar=rep(2,4)) layout(matrix(c(1,2,4,1,3,5),3)) plot(intens,main="intens alone",log=TRUE) plot(r1exp,main="realisation 1\nexponential covar",log=TRUE) plot(r2exp,main="realisation 2\nexponential covar",log=TRUE) plot(r1mat,main="realisation 1\nMatern covar",log=TRUE) plot(r2mat,main="realisation 2\nMatern covar",log=TRUE) par(oldpar) # Plot example datasets dint <- rpoispoly(intens,w=toywin) d1exp <- rpoispoly(r1exp,w=toywin) d2exp <- rpoispoly(r2exp,w=toywin) d1mat <- rpoispoly(r1mat,w=toywin) d2mat <- rpoispoly(r2mat,w=toywin) oldpar <- par(mar=rep(2,4)) layout(matrix(c(1,2,4,1,3,5),3)) plot(dint,main="intens alone",log=TRUE) plot(d1exp,main="realisation 1\nexponential covar",log=TRUE) plot(d2exp,main="realisation 2\nexponential covar",log=TRUE) plot(d1mat,main="realisation 1\nMatern covar",log=TRUE) plot(d2mat,main="realisation 2\nMatern covar",log=TRUE) par(oldpar)
## Homogeneous example ## # Create constant intensity image integrating to 500 homog <- as.im(as.mask(toywin)) homog <- homog/integral(homog)*500 # Corresponding LGCP realisations using exponential covariance structure oldpar <- par(mfrow=c(2,2),mar=rep(1.5,4)) for(i in 1:4){ temp <- lgcpmix(homog,model="exponential",param=list(var=1,scale=0.2)) plot(temp,main=paste("Realisation",i),log=TRUE) } par(oldpar) ## Inhomogeneous examples ## # Create deterministic trend mn <- cbind(c(0.25,0.8),c(0.31,0.82),c(0.43,0.64),c(0.63,0.62),c(0.21,0.26)) v1 <- matrix(c(0.0023,-0.0009,-0.0009,0.002),2) v2 <- matrix(c(0.0016,0.0015,0.0015,0.004),2) v3 <- matrix(c(0.0007,0.0004,0.0004,0.0011),2) v4 <- matrix(c(0.0023,-0.0041,-0.0041,0.0099),2) v5 <- matrix(c(0.0013,0.0011,0.0011,0.0014),2) vr <- array(NA,dim=c(2,2,5)) for(i in 1:5) vr[,,i] <- get(paste("v",i,sep="")) intens <- sgmix(mean=mn,vcv=vr,window=toywin,p0=0.1,int=500) # Two realisations (identical calls to function), exponential covariance structure r1exp <- lgcpmix(lambda=intens,model="exponential",param=list(var=2,scale=0.05)) r2exp <- lgcpmix(lambda=intens,model="exponential",param=list(var=2,scale=0.05)) # Two more realisations, Matern covariance with smoothness 1 r1mat <- lgcpmix(lambda=intens,model="matern",param=list(var=2,scale=0.05,nu=1)) r2mat <- lgcpmix(lambda=intens,model="matern",param=list(var=2,scale=0.05,nu=1)) # Plot everything, including 'intens' alone (no correlation) oldpar <- par(mar=rep(2,4)) layout(matrix(c(1,2,4,1,3,5),3)) plot(intens,main="intens alone",log=TRUE) plot(r1exp,main="realisation 1\nexponential covar",log=TRUE) plot(r2exp,main="realisation 2\nexponential covar",log=TRUE) plot(r1mat,main="realisation 1\nMatern covar",log=TRUE) plot(r2mat,main="realisation 2\nMatern covar",log=TRUE) par(oldpar) # Plot example datasets dint <- rpoispoly(intens,w=toywin) d1exp <- rpoispoly(r1exp,w=toywin) d2exp <- rpoispoly(r2exp,w=toywin) d1mat <- rpoispoly(r1mat,w=toywin) d2mat <- rpoispoly(r2mat,w=toywin) oldpar <- par(mar=rep(2,4)) layout(matrix(c(1,2,4,1,3,5),3)) plot(dint,main="intens alone",log=TRUE) plot(d1exp,main="realisation 1\nexponential covar",log=TRUE) plot(d2exp,main="realisation 2\nexponential covar",log=TRUE) plot(d1mat,main="realisation 1\nMatern covar",log=TRUE) plot(d2mat,main="realisation 2\nMatern covar",log=TRUE) par(oldpar)
plot
method for class stim
.
## S3 method for class 'stim' plot(x, fix.range = FALSE, sleep = 0.2, override.par = TRUE, ...)
## S3 method for class 'stim' plot(x, fix.range = FALSE, sleep = 0.2, override.par = TRUE, ...)
x |
An object of class |
fix.range |
Logical value indicating whether use the same color scale limits for each plot in the sequence. Ignored if the user supplies a pre-defined |
sleep |
Single positive numeric value giving the amount of time (in
seconds) to |
override.par |
Logical value indicating whether to override the
existing graphics device parameters prior to plotting, resetting
|
... |
Additional graphical parameters to be passed to
|
Actual visualisation is deferred to
plot.im
, for which there are a variety of
customisations available the user can access via ...
.
The stim
object is plotted as an animation, one pixel image
after another, separated by sleep
seconds. If instead you intend the
individual images to be plotted in an array of images, you should first set
up your plot device layout, and ensure override.par = FALSE
so that
the function does not reset these device parameters itself. In such an
instance, one might also want to set sleep = 0
.
Plots to the active graphics device.
T.M. Davies
# See help(stgmix) and help(stkey) for examples
# See help(stgmix) and help(stkey) for examples
Generates a pixel image of a bivariate normal mixture density observed on a bounded window using a specified number of contributing densities with randomly selected means and variance-covariance matrices.
rgmix(N, window, v = 4, S = NULL, extras = FALSE, ...)
rgmix(N, window, v = 4, S = NULL, extras = FALSE, ...)
N |
The number of Gaussian components to generate for the mixture. |
window |
An object of class |
v |
The degrees of freedom for the inverse-Wishart distribution of the variance-covariance matrices (must be at least 4). The default value of 4 ensures the generated covariance matrices are centered on |
S |
A symmetric, positive-definite |
extras |
A logical value indicating whether, in addition to returning the pixel |
... |
Additional arguments to be passed to |
This function creates and returns a bivariate Gaussian mixture density on a bounded window
based on N
randomly generated mean locations and corresponding randomly generated variance-covariance matrices. First, the N
mean locations are generated based on a uniform distribution over the spatial window
. Each location is then associated with a covariance matrix generated from an inverse-Wishart distribution with v
degrees of freedom and scale matrix S
.
Once the above steps are completed, the function calls sgmix
with the chosen mean and covariance matrices, thereby creating the Gaussian mixture. Resolution and other aspects of this call can be controlled by using ...
, passing the contents internally to sgmix
. By default, all generated Gaussian components have equal weight in contributing to the final mixture density. The user can alter this by passing p0
and p
to the ...
, though should take care that the length of p
is N
, and that p0
and p
sum to 1, as outlined in the documentation for sgmix
.
If extras = FALSE
(default), then a pixel im
age of the final mixture density. If extras = TRUE
, a list is returned with members f
(the pixel im
age of the final mixture density); mn
(a
N
matrix with each column giving the mean location of each of the N
Gaussian bumps); and vcv
(a
N
array with layers giving the covariance matrices associated with the means in the columns of mn
).
A.K. Redmond and T.M. Davies
set.seed(321) dens1 <- rgmix(7,window=toywin) plot(dens1) set.seed(456) dens2 <- rgmix(7,window=toywin) plot(dens2) # Explicitly return details of generated means and covariances set.seed(321) dens1.detailed <- rgmix(7,window=toywin,extras=TRUE) dens1.detailed$f dens1.detailed$mn dens1.detailed$vcv # Set underlying uniform proportion and compare with dens2 from above set.seed(456) dens2.wunif <- rgmix(7,window=toywin,p0=0.3) plot(rpoint(500,dens2)) plot(rpoint(500,dens2.wunif)) # Explicitly setting scale matrix for inverse-wishart generation of covariances dens3 <- rgmix(3,window=toywin,S=matrix(c(0.025,-0.004,-0.004,0.02),2)) plot(dens3)
set.seed(321) dens1 <- rgmix(7,window=toywin) plot(dens1) set.seed(456) dens2 <- rgmix(7,window=toywin) plot(dens2) # Explicitly return details of generated means and covariances set.seed(321) dens1.detailed <- rgmix(7,window=toywin,extras=TRUE) dens1.detailed$f dens1.detailed$mn dens1.detailed$vcv # Set underlying uniform proportion and compare with dens2 from above set.seed(456) dens2.wunif <- rgmix(7,window=toywin,p0=0.3) plot(rpoint(500,dens2)) plot(rpoint(500,dens2.wunif)) # Explicitly setting scale matrix for inverse-wishart generation of covariances dens3 <- rgmix(3,window=toywin,S=matrix(c(0.025,-0.004,-0.004,0.02),2)) plot(dens3)
Generates a single realisation of a spatial Poisson point process based on a pixel im
age and a polygonal owin
.
rpoispoly(z, w = NULL, correction = 1.1, maxpass = 50)
rpoispoly(z, w = NULL, correction = 1.1, maxpass = 50)
z |
A pixel image of class |
w |
A polygonal window of class |
correction |
An adjustment to the number of points generated at the initial pass of the internal loop in an effort to minimise the total number of passes required to reach |
maxpass |
The maximum number of passes allowed before the function exits. If this is reached before |
This is a wrapper function for rimpoly
that operates much like rpoispp
, but with artificial corrections at the edges of boundary pixels. This allows the user to generate a realisation of a 2D Poisson point process using a supplied pixel im
age as the spatial intensity function, but return the result with a polygonal owin
instead of a binary image mask.
Let be a randomly generated integer from a Poisson distribution with mean given by the integral of the intensity function
z
. When the user specifies their own polygonal window in w
, a while
loop is called and repeated as many
times as necessary (up to maxpass
times) to find points inside
w
(when w = NULL
, then the union of the pixels of z
is used, obtained via as.polygonal(Window(z))
). The loop is necessary because the standard behaviour of rpoispp
can (and often does)
yield points that sit in corners of pixels which lie outside a corresponding irregular polygon w
.
The correction
argument is used to determine how many points are generated initially,
which will be ceiling(correction*n)
; to minimise the number of required passes over the loop this is by default set to give a number slightly higher than the requested .
An error is thrown if Window(z)
and w
do not overlap.
An object of class ppp
containing the Poisson-generated points, defined with the polygonal owin
, w
.
Note that this is an artificial correction that forces the Poisson-generated number of points to be found inside any supplied polygon
w
(even if w
only partially covers the domain of z
). As such, this function only makes sense in terms of the theory of a Poisson point process if the polygon w
corresponds exactly to the pixellised intensity. For practical intents and purposes, it therefore must be assumed in using this function that a supplied polygon w
is/was the original basis for the discretisation into the pixel image for the purposes of producing the intensity z
, and hence that any adverse effects arising from imposing w
as the window of the final result are negligible. See ‘Examples’.
T.M. Davies
Diggle, P.J. (2014) Statistical Analysis of Spatial and Spatiotemporal Point Patterns, 3rd Ed, Chapman & Hall, Boca Raton, USA.
mn <- cbind(c(0.25,0.8),c(0.31,0.82),c(0.43,0.64),c(0.63,0.62),c(0.21,0.26)) v1 <- matrix(c(0.0023,-0.0009,-0.0009,0.002),2) v2 <- matrix(c(0.0016,0.0015,0.0015,0.004),2) v3 <- matrix(c(0.0007,0.0004,0.0004,0.0011),2) v4 <- matrix(c(0.0023,-0.0041,-0.0041,0.0099),2) v5 <- matrix(c(0.0013,0.0011,0.0011,0.0014),2) vr <- array(NA,dim=c(2,2,5)) for(i in 1:5) vr[,,i] <- get(paste("v",i,sep="")) intens <- sgmix(mean=mn,vcv=vr,window=toywin,p0=0.1,int=500) aa <- rpoispp(intens) # Default spatstat function bb <- rpoispoly(intens) # No polygon supplied; just uses pixel union cc <- rpoispoly(intens,w=toywin) # Original irregular polygon plot(intens,log=TRUE) plot(aa,main=paste("aa\nn =",npoints(aa))) plot(bb,main=paste("bb\nn =",npoints(bb))) plot(cc,main=paste("cc\nn =",npoints(cc)))
mn <- cbind(c(0.25,0.8),c(0.31,0.82),c(0.43,0.64),c(0.63,0.62),c(0.21,0.26)) v1 <- matrix(c(0.0023,-0.0009,-0.0009,0.002),2) v2 <- matrix(c(0.0016,0.0015,0.0015,0.004),2) v3 <- matrix(c(0.0007,0.0004,0.0004,0.0011),2) v4 <- matrix(c(0.0023,-0.0041,-0.0041,0.0099),2) v5 <- matrix(c(0.0013,0.0011,0.0011,0.0014),2) vr <- array(NA,dim=c(2,2,5)) for(i in 1:5) vr[,,i] <- get(paste("v",i,sep="")) intens <- sgmix(mean=mn,vcv=vr,window=toywin,p0=0.1,int=500) aa <- rpoispp(intens) # Default spatstat function bb <- rpoispoly(intens) # No polygon supplied; just uses pixel union cc <- rpoispoly(intens,w=toywin) # Original irregular polygon plot(intens,log=TRUE) plot(aa,main=paste("aa\nn =",npoints(aa))) plot(bb,main=paste("bb\nn =",npoints(bb))) plot(cc,main=paste("cc\nn =",npoints(cc)))
Generates an appropriately scaled spatial (bivariate) relative risk surface using a supplied control density and isotropic Gaussian-style hotspots.
rrmix(g, rhotspots, rsds, rweights, rbase = 1, log = TRUE)
rrmix(g, rhotspots, rsds, rweights, rbase = 1, log = TRUE)
g |
A pixel |
rhotspots |
A |
rsds |
A positive numeric vector of length |
rweights |
A vector of length |
rbase |
The base level of the relative risk surface (default is 1). The peaks and troughs will be added or subtracted from this base level prior to normalisation. |
log |
A logical value. If |
A useful tool for the comparison of two estimated density functions on the same spatial region is the relative risk function,
, (Bithell, 1990; 1991; Kelsall and Diggle, 1995), defined simply as a density-ratio:
Various methods have been developed to improve estimation of , most commonly with a motivation in geographical epidemiology, where the ‘numerator’ density
pertains to the observed disease cases and the ‘denominator’ density
reflects the distribution of the at-risk controls (Kelsall and Diggle, 1995; Hazelton and Davies, 2009; Davies and Hazelton, 2010). To test newly developed methodology, simulations based on known relative risk scenarios are usually necessary. This function allows the user to design such scenarios, as used in Hazelton and Davies (2009), Davies and Hazelton (2010), and Davies (2013) for example.
This function calculates a relative risk surface based on Gaussian-style ‘bumps’ added and subtracted from a base level of
rbase
, with the peaks and troughs centered at the coordinates given by rhotspots
with relative weights of rweights
and isotropic standard deviations of rsds
. The risk surface is computed as
rbase
rweights[
]
rsds[
]
||
rhotspots[,
]
||
where || . || denotes Euclidean norm. Because and
are both densities, the risk surface as defined above must then be rescaled with respect to the supplied control density
(argument
g
) to ensure that
This is automatically performed inside the function. The case density that gives rise to the designed is then easily recovered because
. By default, the function returns the log-relative risk surface
alongside the case and control densities.
An object of class rrim
. This is a solist
of three pixel im
ages: f
as the case density, g
the control density (a copy of the argument of the same name, integrating to 1), and r
as the (log) relative risk surface.
A.K. Redmond and T.M. Davies
Bithell, J.F. (1990), An application of density estimation to geographical epidemiology, Statistics in Medicine, 9, 691-701.
Bithell, J.F. (1991), Estimation of relative risk functions, Statistics in Medicine, 10, 1745-1751.
Davies, T.M. (2013), Jointly optimal bandwidth selection
for the planar kernel-smoothed density-ratio, Spatial and
Spatio-temporal Epidemiology, 5, 51-65.
Davies, T.M. and Hazelton, M.L. (2010), Adaptive kernel estimation of spatial relative risk, Statistics in Medicine, 29(23) 2423-2437.
Kelsall, J.E. and Diggle, P.J. (1995a), Kernel estimation of relative risk, Bernoulli, 1, 3-16.
set.seed(1) gg <- rgmix(3,window=toywin,S=matrix(c(0.08^2,0,0,0.1^2),nrow=2),p0=0.2) rho <- rrmix(g=gg, rhotspots=cbind(c(0.8,0.3),c(0.4,0.4),c(0.6,0.5),c(0.3,0.5)), rsds=c(0.005,0.025,0.01,0.025), rweights=c(3,2,10,5)*10) rho.sample <- rrpoint(c(400,800),rho,toywin) oldpar <- par(mfrow=c(2,2)) plot(rho$g,main="control density") plot(rho$f,main="case density") plot(rho$r,main="log relative risk surface") plot(rho.sample$controls,main="sample data") points(rho.sample$cases,col=2) legend("topright",col=2:1,legend=c("cases","controls"),pch=1) par(oldpar)
set.seed(1) gg <- rgmix(3,window=toywin,S=matrix(c(0.08^2,0,0,0.1^2),nrow=2),p0=0.2) rho <- rrmix(g=gg, rhotspots=cbind(c(0.8,0.3),c(0.4,0.4),c(0.6,0.5),c(0.3,0.5)), rsds=c(0.005,0.025,0.01,0.025), rweights=c(3,2,10,5)*10) rho.sample <- rrpoint(c(400,800),rho,toywin) oldpar <- par(mfrow=c(2,2)) plot(rho$g,main="control density") plot(rho$f,main="case density") plot(rho$r,main="log relative risk surface") plot(rho.sample$controls,main="sample data") points(rho.sample$cases,col=2) legend("topright",col=2:1,legend=c("cases","controls"),pch=1) par(oldpar)
Generates a pair of random, independent point patterns corresponding to a case density and a control density, for relative risk analyses.
rrpoint(n, r, W = NULL, correction = 1.1, maxpass = 50) rrstpoint(n, r, W = NULL, correction = 1.5, maxpass = 50)
rrpoint(n, r, W = NULL, correction = 1.1, maxpass = 50) rrstpoint(n, r, W = NULL, correction = 1.5, maxpass = 50)
n |
The number of points to be generated. This must be a numeric vector of length 2 giving the number of points to generate for the case and control densities respectively. Alternatively a single number can be supplied; then the same number of points is generated for both densities. |
r |
The relative risk surface object containing the definitions of the case and control probability densities: an object of class |
W |
The polygonal |
correction |
An adjustment to the number of points generated at the initial pass of the internal loop in an effort to minimise the total number of passes required to reach |
maxpass |
The maximum number of passes allowed before the function exits. If this is reached before |
These functions randomly generate a pair of independent spatial or spatiotemporal point patterns of n
points based on the case and control density functions stored in r
. At any given pass for each density, n
* correction
points are generated and rejection sampling is used to accept some of the points; this is repeated until the required number of points is found.
The argument W
is optional, but is useful when the user wants the spatial window of the resulting point pattern to be a corresponding irregular polygon, as opposed to being based on the boundary of a binary image mask (which, when the pixel im
ages in r
are converted to a polygon directly, gives jagged edges based on the union of the pixels).
A list with two components, cases
and controls
, each of which is an object of class ppp
containing the n
generated points. for spatiotemporal densities, the marks
of the object will contain the correspondingly generated observation times.
T.M. Davies
# Using 'rrim' object: set.seed(1) gg <- rgmix(3,window=toywin,S=matrix(c(0.08^2,0,0,0.1^2),nrow=2),p0=0.2) rho <- rrmix(g=gg, rhotspots=cbind(c(0.8,0.3),c(0.4,0.4),c(0.6,0.5),c(0.3,0.5)), rsds=c(0.005,0.025,0.01,0.025), rweights=c(3,2,10,5)*10) rho.sample <- rrpoint(n=c(400,800),r=rho,W=toywin) oldpar <- par(mfrow=c(2,2)) plot(rho$g,main="control density") plot(rho$f,main="case density") plot(rho$r,main="log relative risk surface") plot(rho.sample$controls,main="sample data") points(rho.sample$cases,col=2) legend("topright",col=2:1,legend=c("cases","controls"),pch=1) par(oldpar) # Using 'rrs' object: require("sparr") data(pbc) pbccas <- split(pbc)$case pbccon <- split(pbc)$control h0 <- OS(pbc,nstar="geometric") f <- bivariate.density(pbccas,h0=h0,hp=2,adapt=TRUE,pilot.density=pbccas, edge="diggle",davies.baddeley=0.05,verbose=FALSE) g <- bivariate.density(pbccon,h0=h0,hp=2,adapt=TRUE,pilot.density=pbccas, edge="diggle",davies.baddeley=0.05,verbose=FALSE) pbcrr <- risk(f,g,tolerate=TRUE,verbose=FALSE) pbcrr.pt <- rrpoint(n=1000,r=pbcrr) par(mfrow=c(1,3)) plot(pbcrr) plot(pbcrr.pt$cases) plot(pbcrr.pt$controls) # Using 'rrstim' object: set.seed(321) gg <- rgmix(7,window=shp2) rsk <- rrstmix(g=gg,rhotspots=matrix(c(-1,-1,2,2.5,0,5),nrow=3), rsds=sqrt(cbind(rep(0.75,3),c(0.05,0.01,0.5))), rweights=c(-0.4,7),tlim=c(0,6),tres=64) plot(rsk$r,fix.range=TRUE) rsk.pt <- rrstpoint(1000,r=rsk,W=shp2) par(mfrow=c(1,2)) plot(rsk.pt$cases) plot(rsk.pt$controls) # Using 'rrst' object: require("sparr") data(fmd) fmdcas <- fmd$cases fmdcon <- fmd$controls f <- spattemp.density(fmdcas,h=6,lambda=8) g <- bivariate.density(fmdcon,h0=6) rho <- spattemp.risk(f,g) rho.pt <- rrstpoint(1000,r=rho) par(mfrow=c(1,2)) plot(rho.pt$cases) plot(rho.pt$controls)
# Using 'rrim' object: set.seed(1) gg <- rgmix(3,window=toywin,S=matrix(c(0.08^2,0,0,0.1^2),nrow=2),p0=0.2) rho <- rrmix(g=gg, rhotspots=cbind(c(0.8,0.3),c(0.4,0.4),c(0.6,0.5),c(0.3,0.5)), rsds=c(0.005,0.025,0.01,0.025), rweights=c(3,2,10,5)*10) rho.sample <- rrpoint(n=c(400,800),r=rho,W=toywin) oldpar <- par(mfrow=c(2,2)) plot(rho$g,main="control density") plot(rho$f,main="case density") plot(rho$r,main="log relative risk surface") plot(rho.sample$controls,main="sample data") points(rho.sample$cases,col=2) legend("topright",col=2:1,legend=c("cases","controls"),pch=1) par(oldpar) # Using 'rrs' object: require("sparr") data(pbc) pbccas <- split(pbc)$case pbccon <- split(pbc)$control h0 <- OS(pbc,nstar="geometric") f <- bivariate.density(pbccas,h0=h0,hp=2,adapt=TRUE,pilot.density=pbccas, edge="diggle",davies.baddeley=0.05,verbose=FALSE) g <- bivariate.density(pbccon,h0=h0,hp=2,adapt=TRUE,pilot.density=pbccas, edge="diggle",davies.baddeley=0.05,verbose=FALSE) pbcrr <- risk(f,g,tolerate=TRUE,verbose=FALSE) pbcrr.pt <- rrpoint(n=1000,r=pbcrr) par(mfrow=c(1,3)) plot(pbcrr) plot(pbcrr.pt$cases) plot(pbcrr.pt$controls) # Using 'rrstim' object: set.seed(321) gg <- rgmix(7,window=shp2) rsk <- rrstmix(g=gg,rhotspots=matrix(c(-1,-1,2,2.5,0,5),nrow=3), rsds=sqrt(cbind(rep(0.75,3),c(0.05,0.01,0.5))), rweights=c(-0.4,7),tlim=c(0,6),tres=64) plot(rsk$r,fix.range=TRUE) rsk.pt <- rrstpoint(1000,r=rsk,W=shp2) par(mfrow=c(1,2)) plot(rsk.pt$cases) plot(rsk.pt$controls) # Using 'rrst' object: require("sparr") data(fmd) fmdcas <- fmd$cases fmdcon <- fmd$controls f <- spattemp.density(fmdcas,h=6,lambda=8) g <- bivariate.density(fmdcon,h0=6) rho <- spattemp.risk(f,g) rho.pt <- rrstpoint(1000,r=rho) par(mfrow=c(1,2)) plot(rho.pt$cases) plot(rho.pt$controls)
Generates an appropriately scaled spatiotemporal (trivariate) relative risk surface using a supplied control density and Gaussian-style hotspots.
rrstmix(g, rhotspots, rsds, rweights, rbase = 1, log = TRUE, tlim = NULL, tres = NULL)
rrstmix(g, rhotspots, rsds, rweights, rbase = 1, log = TRUE, tlim = NULL, tres = NULL)
g |
The control density as a |
rhotspots |
A |
rsds |
A |
rweights |
A vector of length |
rbase |
The base level of the relative risk surface (default is 1). The peaks and troughs will be added or subtracted from this base level prior to normalisation. |
log |
A logical value. If |
tlim |
Only used if |
tres |
Only used if |
This function is the spatiotemporal (trivariate) equivalent of rrmix
. See ‘Details’ in the documentation for that function for more information.
An oject of class rrstim
. This is a list with the following components:
f |
An object of class |
g |
A copy of the object passed to the argument |
r |
An object of class |
A.K. Redmond and T.M. Davies
Fernando, W.T.P.S. and Hazelton, M.L. (2014), Generalizing the spatial relative risk function, Spatial and Spatio-temporal Epidemiology, 8, 1-10.
# time-varying control density gg1 <- stgmix(mean=matrix(c(2,1,3,0,-1,5),nrow=3), vcv=array(c(1,0,0,0,1,0,0,0,1,2,0,0,0,1,0,0,0,2),dim=c(3,3,2)), window=shp2,tlim=c(0,6)) rsk1 <- rrstmix(g=gg1,rhotspots=matrix(c(-2,0,2,1,2,5.5),nrow=3), rsds=sqrt(cbind(rep(1.5,3),rep(0.25,3))),rweights=c(-0.5,5)) plot(rsk1$g,sleep=0.1,fix.range=TRUE) plot(rsk1$f,sleep=0.1,fix.range=TRUE) plot(rsk1$r,sleep=0.1,fix.range=TRUE) # time-constant control density set.seed(321) gg2 <- rgmix(7,window=shp2) rsk2 <- rrstmix(g=gg2,rhotspots=matrix(c(-1,-1,2,2.5,0,5),nrow=3), rsds=sqrt(cbind(rep(0.75,3),c(0.05,0.01,0.5))), rweights=c(-0.4,7),tlim=c(0,6),tres=64) plot(rsk2$g,sleep=0.1,fix.range=TRUE) plot(rsk2$f,sleep=0.1,fix.range=TRUE) plot(rsk2$r,sleep=0.1,fix.range=TRUE)
# time-varying control density gg1 <- stgmix(mean=matrix(c(2,1,3,0,-1,5),nrow=3), vcv=array(c(1,0,0,0,1,0,0,0,1,2,0,0,0,1,0,0,0,2),dim=c(3,3,2)), window=shp2,tlim=c(0,6)) rsk1 <- rrstmix(g=gg1,rhotspots=matrix(c(-2,0,2,1,2,5.5),nrow=3), rsds=sqrt(cbind(rep(1.5,3),rep(0.25,3))),rweights=c(-0.5,5)) plot(rsk1$g,sleep=0.1,fix.range=TRUE) plot(rsk1$f,sleep=0.1,fix.range=TRUE) plot(rsk1$r,sleep=0.1,fix.range=TRUE) # time-constant control density set.seed(321) gg2 <- rgmix(7,window=shp2) rsk2 <- rrstmix(g=gg2,rhotspots=matrix(c(-1,-1,2,2.5,0,5),nrow=3), rsds=sqrt(cbind(rep(0.75,3),c(0.05,0.01,0.5))), rweights=c(-0.4,7),tlim=c(0,6),tres=64) plot(rsk2$g,sleep=0.1,fix.range=TRUE) plot(rsk2$f,sleep=0.1,fix.range=TRUE) plot(rsk2$r,sleep=0.1,fix.range=TRUE)
Generates a random spatiotemporal point pattern containing independent, identically distributed points with a specified distribution.
rstpoint(n, f, W = NULL, correction = 1.5, maxpass = 50)
rstpoint(n, f, W = NULL, correction = 1.5, maxpass = 50)
n |
The number of points to be generated. |
f |
The probability density of the points, an object of class |
W |
The polygonal |
correction |
An adjustment to the number of points generated at the initial pass of the internal loop in an effort to minimise the total number of passes required to reach |
maxpass |
The maximum number of passes allowed before the function exits. If this is reached before |
This function randomly generates a spatiotemporal point pattern of exactly n
points based on the density function f
. At any given pass, n
* correction
points are generated and rejection sampling is used to accept some of the points; this is repeated until the required number of points is found.
The argument W
is optional, but is useful when the user wants the spatial window of the resulting point pattern to be a corresponding irregular polygon, as opposed to being based on the boundary of a binary image mask (which, when the pixel im
ages in f
are converted to a polygon directly, gives jagged edges based on the union of the pixels).
An object of class ppp
containing the n
generated points. The marks
of the object contain the correspondingly generated observation times.
A.K. Redmond and T.M. Davies
r1a <- sgmix(cbind(c(0.5,0.5)),vcv=0.01,window=toywin,p0=0.5,p=c(0.5),res=128) r1b <- sgmix(cbind(c(0.5,0.5),c(0.4,0.6)),vcv=c(0.06,0.015),window=toywin, p0=0.1,p=c(0.5,0.4),res=128) r1c <- sgmix(cbind(c(0.4,0.6)),vcv=c(0.1),window=toywin,p0=0.1,p=c(0.9),res=128) sts1 <- stkey(start=r1a, stop=r1c, tlim=c(1,10), tres=64, window=toywin, kf=solist(r1a,r1b), kftimes=c(2,6), fscale=0.1+0.9*dnorm(seq(-3,3,length=64),mean=0,sd=1)) plot(sts1,sleep=0.1) Y <- rstpoint(500,sts1,W=toywin,correction=10,maxpass=500) plot(Y) require("rgl") plot3d(Y$x,Y$y,marks(Y))
r1a <- sgmix(cbind(c(0.5,0.5)),vcv=0.01,window=toywin,p0=0.5,p=c(0.5),res=128) r1b <- sgmix(cbind(c(0.5,0.5),c(0.4,0.6)),vcv=c(0.06,0.015),window=toywin, p0=0.1,p=c(0.5,0.4),res=128) r1c <- sgmix(cbind(c(0.4,0.6)),vcv=c(0.1),window=toywin,p0=0.1,p=c(0.9),res=128) sts1 <- stkey(start=r1a, stop=r1c, tlim=c(1,10), tres=64, window=toywin, kf=solist(r1a,r1b), kftimes=c(2,6), fscale=0.1+0.9*dnorm(seq(-3,3,length=64),mean=0,sd=1)) plot(sts1,sleep=0.1) Y <- rstpoint(500,sts1,W=toywin,correction=10,maxpass=500) plot(Y) require("rgl") plot3d(Y$x,Y$y,marks(Y))
Generates a pixel image of a specified bivariate normal mixture density observed on a bounded window.
sgmix(mean, vcv, window, p0 = 0, p = NULL, resolution = 128, int = 1)
sgmix(mean, vcv, window, p0 = 0, p = NULL, resolution = 128, int = 1)
mean |
A |
vcv |
Either a |
window |
An object of class |
p0 |
The proportion of uniform density that contributes to the mixture (default is 0). |
p |
A numeric vector of the |
resolution |
The number of pixels along each side of the grid for the pixel image (default is 128). |
int |
A positive numeric value for post-hoc rescaling of the density (useful if the user wishes to return an intensity function). Defaults to 1 for no change in scaling. |
This function generates a pixel im
age of a 2D density function made of a mixture of bivariate normals; each component is restricted to conserve probability mass over a bounded subset of the plane. A warning will appear if less than 1% of the integral of each Gaussian bump is inside the observational window.
An object of class im
giving the mixture density.
A.K. Redmond
# Example using isotropic standard deviations m1 <- c(0.4,0.5) m2 <- c(0.2,0.7) s1 <- 0.1 s2 <- 0.025 dens1 <- sgmix(mean=cbind(m1,m2),vcv=c(s1,s2),window=toywin,p0=0.3,p=c(0.5,0.2)) plot(dens1,log=TRUE) pts1 <- rpoint(200,dens1) # generate random points via spatstat.core::rpoint points(pts1) # Example using full covariance matrices mn <- cbind(c(0.25,0.8),c(0.31,0.82),c(0.43,0.64),c(0.63,0.62),c(0.21,0.26)) v1 <- matrix(c(0.0023,-0.0009,-0.0009,0.002),2) v2 <- matrix(c(0.0016,0.0015,0.0015,0.004),2) v3 <- matrix(c(0.0007,0.0004,0.0004,0.0011),2) v4 <- matrix(c(0.0023,-0.0041,-0.0041,0.0099),2) v5 <- matrix(c(0.0013,0.0011,0.0011,0.0014),2) vr <- array(NA,dim=c(2,2,5)) for(i in 1:5) vr[,,i] <- get(paste("v",i,sep="")) dens2 <- sgmix(mean=mn,vcv=vr,window=toywin,p0=0.1) plot(dens2,log=TRUE) pts2 <- rpoint(200,dens2) points(pts2)
# Example using isotropic standard deviations m1 <- c(0.4,0.5) m2 <- c(0.2,0.7) s1 <- 0.1 s2 <- 0.025 dens1 <- sgmix(mean=cbind(m1,m2),vcv=c(s1,s2),window=toywin,p0=0.3,p=c(0.5,0.2)) plot(dens1,log=TRUE) pts1 <- rpoint(200,dens1) # generate random points via spatstat.core::rpoint points(pts1) # Example using full covariance matrices mn <- cbind(c(0.25,0.8),c(0.31,0.82),c(0.43,0.64),c(0.63,0.62),c(0.21,0.26)) v1 <- matrix(c(0.0023,-0.0009,-0.0009,0.002),2) v2 <- matrix(c(0.0016,0.0015,0.0015,0.004),2) v3 <- matrix(c(0.0007,0.0004,0.0004,0.0011),2) v4 <- matrix(c(0.0023,-0.0041,-0.0041,0.0099),2) v5 <- matrix(c(0.0013,0.0011,0.0011,0.0014),2) vr <- array(NA,dim=c(2,2,5)) for(i in 1:5) vr[,,i] <- get(paste("v",i,sep="")) dens2 <- sgmix(mean=mn,vcv=vr,window=toywin,p0=0.1) plot(dens2,log=TRUE) pts2 <- rpoint(200,dens2) points(pts2)
Generates a pixel image array of a specified trivariate normal mixture density observed on a bounded window in space and time.
stgmix(mean, vcv, window, tlim, p0 = 0, p = NULL, sres = 128, tres = sres, int = 1)
stgmix(mean, vcv, window, tlim, p0 = 0, p = NULL, sres = 128, tres = sres, int = 1)
mean |
A |
vcv |
A |
window |
An object of class |
tlim |
A vector of length 2 giving the boundaries of the time interval on which the mixture density is defined. |
p0 |
The proportion of uniform density that contributes to the final mixture (default is 0). |
p |
A numeric vector of the |
sres |
The spatial resolution (number of pixels) along each side of the spatial grid (default is 128). |
tres |
The temporal resolution (default is to equate with |
int |
A positive numeric value for post-hoc rescaling of the density (useful if the user wishes to return a spatiotemporal intensity function). Defaults to 1 for no change in scaling. |
This function creates a 3D array of a density function made up of a mixture of trivariate normals with the interpretation of a continuous probability density function in space-time. As such, each component is restricted to conserve mass over a 3D region specified by a fixed polygonal
window
in space, stretched over defined temporal limits (tlim
). A warning will appear if less than 1% of the integral of each Gaussian bump is inside this observational spatiotemporal polyhedron.
An object of class stim
giving the trivariate density. This is a list with six components:
a |
The |
v |
A pixel |
xcol |
Grid coordinates in the spatial x-axis (corresponds to each spatial |
yrow |
Grid coordinates in the spatial y-axis (corresponds to each spatial |
tlay |
Grid coordinates in the temporal axis (corresponds to the order of the spatial |
W |
A copy of |
A.K. Redmond and T.M. Davies
require("abind") m1 <- c(0.3,0.3,2) m2 <- c(0.5,0.8,8) m3 <- c(0.7,0.6,7) v1 <- diag(c(0.01^2,0.01^2,1)) v2 <- diag(c(0.005,0.005,0.5)) v3 <- diag(c(0.005,0.005,0.5)) stg1 <- stgmix(mean=cbind(m1,m2,m3), vcv=abind(v1,v2,v3,along=3), window=toywin,tlim=c(1,10), p0=0.1,tres=64) plot(stg1,log=TRUE) mn <- matrix(c(0,0,0,-2,1,4,1,-2,8),nrow=3) vr <- array(c(1,0,0,0,1,0,0,0,1,1,0,0.5,0,1,0,0.5,0,3,1,0,0,0,2,0,0,0,1), dim=c(3,3,3)) stg2 <- stgmix(mean=mn,vcv=vr,window=shp1, tlim=c(0,10),tres=50) plot(stg2,fix.range=TRUE,sleep=0.1)
require("abind") m1 <- c(0.3,0.3,2) m2 <- c(0.5,0.8,8) m3 <- c(0.7,0.6,7) v1 <- diag(c(0.01^2,0.01^2,1)) v2 <- diag(c(0.005,0.005,0.5)) v3 <- diag(c(0.005,0.005,0.5)) stg1 <- stgmix(mean=cbind(m1,m2,m3), vcv=abind(v1,v2,v3,along=3), window=toywin,tlim=c(1,10), p0=0.1,tres=64) plot(stg1,log=TRUE) mn <- matrix(c(0,0,0,-2,1,4,1,-2,8),nrow=3) vr <- array(c(1,0,0,0,1,0,0,0,1,1,0,0.5,0,1,0,0.5,0,3,1,0,0,0,2,0,0,0,1), dim=c(3,3,3)) stg2 <- stgmix(mean=mn,vcv=vr,window=shp1, tlim=c(0,10),tres=50) plot(stg2,fix.range=TRUE,sleep=0.1)
Integrates an object of class stim
or stden
.
stintegral(x)
stintegral(x)
x |
The integral is evaluated arithmetically as the sum of the product of the value of each voxel and the voxel area, for those voxels inside the relevant space-time window (i.e. ignoring NA
s).
A single numeric value giving the integral sought.
T.M. Davies
# 'stim' objects require("abind") m1 <- c(0.3,0.3,2) m2 <- c(0.5,0.8,8) m3 <- c(0.7,0.6,7) v1 <- diag(c(0.01^2,0.01^2,1)) v2 <- diag(c(0.005,0.005,0.5)) v3 <- diag(c(0.005,0.005,0.5)) stg1 <- stgmix(mean=cbind(m1,m2,m3),vcv=abind(v1,v2,v3,along=3), window=toywin,tlim=c(0,10),p0=0.1,tres=64) stg2 <- stgmix(mean=cbind(m1,m2,m3),vcv=abind(v1,v2,v3,along=3), window=toywin,tlim=c(0,10),p0=0.1,tres=64,int=200) stintegral(stg1) stintegral(stg2) # 'sten' objects require("sparr") data(burk) hlam <- OS.spattemp(burk$cases) bden <- spattemp.density(burk$cases,h=hlam[1],lambda=hlam[2],sres=64,verbose=FALSE) stintegral(bden)
# 'stim' objects require("abind") m1 <- c(0.3,0.3,2) m2 <- c(0.5,0.8,8) m3 <- c(0.7,0.6,7) v1 <- diag(c(0.01^2,0.01^2,1)) v2 <- diag(c(0.005,0.005,0.5)) v3 <- diag(c(0.005,0.005,0.5)) stg1 <- stgmix(mean=cbind(m1,m2,m3),vcv=abind(v1,v2,v3,along=3), window=toywin,tlim=c(0,10),p0=0.1,tres=64) stg2 <- stgmix(mean=cbind(m1,m2,m3),vcv=abind(v1,v2,v3,along=3), window=toywin,tlim=c(0,10),p0=0.1,tres=64,int=200) stintegral(stg1) stintegral(stg2) # 'sten' objects require("sparr") data(burk) hlam <- OS.spattemp(burk$cases) bden <- spattemp.density(burk$cases,h=hlam[1],lambda=hlam[2],sres=64,verbose=FALSE) stintegral(bden)
Uses the supplied spatial pixel images and scalings to linearly interpolate the behaviour of the function over time, creating a trivariate density function in space-time.
stkey(start, stop, tlim, kf = NULL, tres = 64, kftimes = NULL, fscales = NULL, window = NULL)
stkey(start, stop, tlim, kf = NULL, tres = 64, kftimes = NULL, fscales = NULL, window = NULL)
start |
The spatial pixel |
stop |
The pixel |
tlim |
A numeric vector of length 2 representing the temporal window i.e. the time interval over which the interpolation takes place. |
kf |
A |
tres |
The resolution of the resulting array in the time dimension (default is 64). |
kftimes |
A vector of times that position the interim keyframes in |
fscales |
A numeric vector of unnormalised, relative point-intensity scales. This may be provided either as of |
window |
An object of class |
This function interpolates in a pixel-wise fashion between the im
objects supplied as start
and stop
(and kf
if supplied), placing them as keyframes at the times in tlim
(for start
and stop
) and kftimes
(for the members of kf
). The final result is rescaled such that its total integrated volume over the defined spatiotemporal domain is 1, yeilding a trivariate density function.
If fscale
is a vector of length tres
, each element will correspond to the relative overall scaling of one of the resulting interpolated pixel images. If it is of length length(kf) + 2
, the scales will correspond to start
, each keyframe in kf
and stop
in that order. The values in this argument are only interpretable in a relative sense: for example, with a single keyframe suppled to kf
(in addition to the required start
and stop
), then fscales = c(0.5,1,0.5)
has exactly the same effect on the final result as fscales = c(1,2,1)
, and is interpreted as yielding a point density that reaches twice the concentration at the time of the supplied keyframe relative to the start
and stop
margins. Supplying fscale
as a vector of length tres
thus allows finer control over the relative point density over time, such as for the incorporation of harmonic seasonal variation.
Like stgmix
, an object of class stim
giving the trivariate density. This is a list with six components:
a |
The |
v |
A pixel |
xcol |
Grid coordinates in the spatial x-axis (corresponds to each spatial |
yrow |
Grid coordinates in the spatial y-axis (corresponds to each spatial |
tlay |
Grid coordinates in the temporal axis (corresponds to the order of the spatial |
W |
A copy of |
A.K. Redmond and T.M. Davies
mn <- matrix(c(0,0,1,2,0.5,-1),nrow=2) vr <- array(c(0.2,0,0,2,1,0,0,1,1,0.3,0.3,0.5),dim=c(2,2,3)) im1 <- sgmix(mn,c(1,2,1),shp1,p=c(0.4,0.3,0.3)) im2 <- sgmix(matrix(c(-3,0,0,-2,-1,2),nrow=2),c(3,1,1),shp1,p=c(0.4,0.3,0.3)) im3 <- sgmix(mn,vr,shp1,p0=0.1) kf1 <- stkey(start=im1,stop=im2,tlim=c(5,20),window=shp1) plot(kf1) kf2 <- stkey(start=im1,stop=im1,tlim=c(0,15),kf=solist(im1,im1),kftimes=c(2,8), fscale=c(1,2,1.5,1),window=shp1) plot(kf2,fix.range=TRUE) kf3 <- stkey(start=im1,stop=im2,tlim=c(0,20),kf=solist(im1,im2),kftimes=c(8,12), fscale=c(1,2,2,1),window=shp1) plot(kf3,fix.range=TRUE) ff <- c(sin((1:64)/3)+1.5) plot(ff,type="l") kf4 <- stkey(start=im1,stop=im2,kf=solist(im3),kftimes=25,tlim=c(0,50),fscale=ff,window=shp1) plot(kf4,fix.range=TRUE)
mn <- matrix(c(0,0,1,2,0.5,-1),nrow=2) vr <- array(c(0.2,0,0,2,1,0,0,1,1,0.3,0.3,0.5),dim=c(2,2,3)) im1 <- sgmix(mn,c(1,2,1),shp1,p=c(0.4,0.3,0.3)) im2 <- sgmix(matrix(c(-3,0,0,-2,-1,2),nrow=2),c(3,1,1),shp1,p=c(0.4,0.3,0.3)) im3 <- sgmix(mn,vr,shp1,p0=0.1) kf1 <- stkey(start=im1,stop=im2,tlim=c(5,20),window=shp1) plot(kf1) kf2 <- stkey(start=im1,stop=im1,tlim=c(0,15),kf=solist(im1,im1),kftimes=c(2,8), fscale=c(1,2,1.5,1),window=shp1) plot(kf2,fix.range=TRUE) kf3 <- stkey(start=im1,stop=im2,tlim=c(0,20),kf=solist(im1,im2),kftimes=c(8,12), fscale=c(1,2,2,1),window=shp1) plot(kf3,fix.range=TRUE) ff <- c(sin((1:64)/3)+1.5) plot(ff,type="l") kf4 <- stkey(start=im1,stop=im2,kf=solist(im3),kftimes=25,tlim=c(0,50),fscale=ff,window=shp1) plot(kf4,fix.range=TRUE)
Synthetic spatial windows for use in testing, simulations and demonstrations.
data(bx) data(heart) data(shp1) data(shp2) data(star) data(toywin)
data(bx) data(heart) data(shp1) data(shp2) data(star) data(toywin)
Each of these is a single closed polygon of class owin
.
bx
is a box on [-5,5]^2.
heart
is a heart, professing love for all things spatstat
.
shp1
is shape of mystery.
shp2
is a slightly more symmetric shape of mystery.
star
is a star that shines brightly in even non-spatial contexts.
toywin
is the eponymous toy window used in publications e.g. Davies & Lawson (2019).
These are lazy-loaded so may be called directly by name upon loading of spagmix
.
A.K. Redmond and T.M. Davies
Davies, T.M. and Lawson, A.B. (2019), An evaluation of likelihood-based bandwidth selectors for spatial and spatiotemporal kernel estimates, Journal of Statistical Computation and Simulation, 89 1131-1152.
oldpar <- par(mfrow=c(2,3)) plot(bx);axis(1);axis(2) plot(heart);axis(1);axis(2) plot(shp1);axis(1);axis(2) plot(shp2);axis(1);axis(2) plot(star);axis(1);axis(2) plot(toywin);axis(1);axis(2) par(oldpar)
oldpar <- par(mfrow=c(2,3)) plot(bx);axis(1);axis(2) plot(heart);axis(1);axis(2) plot(shp1);axis(1);axis(2) plot(shp2);axis(1);axis(2) plot(star);axis(1);axis(2) plot(toywin);axis(1);axis(2) par(oldpar)
Rescales any owin
to fall inside the unit square.
unify.owin(W)
unify.owin(W)
W |
An object of class |
This function is a simple wrapper for affine
deployed to rescale a supplied owin
to fall inside the unit square.
The rescaled owin
.
W <- Window(chorley) U <- unify.owin(W) oldpar <- par(mfrow=c(1,2)) plot(W,axes=TRUE) plot(U,axes=TRUE) par(oldpar)
W <- Window(chorley) U <- unify.owin(W) oldpar <- par(mfrow=c(1,2)) plot(W,axes=TRUE) plot(U,axes=TRUE) par(oldpar)