Simulation Envelopes of Summary Function
Computes simulation envelopes of a summary function.
envelope(Y, fun, ...) ## S3 method for class 'ppp' envelope(Y, fun=Kest, nsim=99, nrank=1, ..., funargs=list(), funYargs=funargs, simulate=NULL, fix.n=FALSE, fix.marks=FALSE, verbose=TRUE, clipdata=TRUE, transform=NULL, global=FALSE, ginterval=NULL, use.theory=NULL, alternative=c("two.sided", "less", "greater"), scale=NULL, clamp=FALSE, savefuns=FALSE, savepatterns=FALSE, nsim2=nsim, VARIANCE=FALSE, nSD=2, Yname=NULL, maxnerr=nsim, rejectNA=FALSE, silent=FALSE, do.pwrong=FALSE, envir.simul=NULL) ## S3 method for class 'ppm' envelope(Y, fun=Kest, nsim=99, nrank=1, ..., funargs=list(), funYargs=funargs, simulate=NULL, fix.n=FALSE, fix.marks=FALSE, verbose=TRUE, clipdata=TRUE, start=NULL, control=update(default.rmhcontrol(Y), nrep=nrep), nrep=1e5, transform=NULL, global=FALSE, ginterval=NULL, use.theory=NULL, alternative=c("two.sided", "less", "greater"), scale=NULL, clamp=FALSE, savefuns=FALSE, savepatterns=FALSE, nsim2=nsim, VARIANCE=FALSE, nSD=2, Yname=NULL, maxnerr=nsim, rejectNA=FALSE, silent=FALSE, do.pwrong=FALSE, envir.simul=NULL) ## S3 method for class 'kppm' envelope(Y, fun=Kest, nsim=99, nrank=1, ..., funargs=list(), funYargs=funargs, simulate=NULL, verbose=TRUE, clipdata=TRUE, transform=NULL, global=FALSE, ginterval=NULL, use.theory=NULL, alternative=c("two.sided", "less", "greater"), scale=NULL, clamp=FALSE, savefuns=FALSE, savepatterns=FALSE, nsim2=nsim, VARIANCE=FALSE, nSD=2, Yname=NULL, maxnerr=nsim, rejectNA=FALSE, silent=FALSE, do.pwrong=FALSE, envir.simul=NULL)
Y |
Object containing point pattern data.
A point pattern (object of class
|
fun |
Function that computes the desired summary statistic for a point pattern. |
nsim |
Number of simulated point patterns to be generated when computing the envelopes. |
nrank |
Integer. Rank of the envelope value amongst the |
... |
Extra arguments passed to |
funargs |
A list, containing extra arguments to be passed to |
funYargs |
Optional. A list, containing extra arguments to be passed to
|
simulate |
Optional. Specifies how to generate the simulated point patterns.
If |
fix.n |
Logical. If |
fix.marks |
Logical. If |
verbose |
Logical flag indicating whether to print progress reports during the simulations. |
clipdata |
Logical flag indicating whether the data point pattern should be
clipped to the same window as the simulated patterns,
before the summary function for the data is computed.
This should usually be |
start,control |
Optional. These specify the arguments |
nrep |
Number of iterations in the Metropolis-Hastings simulation
algorithm. Applicable only when |
transform |
Optional. A transformation to be applied to the function values, before the envelopes are computed. An expression object (see Details). |
global |
Logical flag indicating whether envelopes should be pointwise
( |
ginterval |
Optional.
A vector of length 2 specifying
the interval of r values for the simultaneous critical
envelopes. Only relevant if |
use.theory |
Logical value indicating whether to use the theoretical value,
computed by |
alternative |
Character string determining whether the envelope corresponds
to a two-sided test ( |
scale |
Optional. Scaling function for global envelopes.
A function in the R language which determines the
relative scale of deviations, as a function of
distance r, when computing the global envelopes.
Applicable only when |
clamp |
Logical value indicating how to compute envelopes when
|
savefuns |
Logical flag indicating whether to save all the simulated function values. |
savepatterns |
Logical flag indicating whether to save all the simulated point patterns. |
nsim2 |
Number of extra simulated point patterns to be generated
if it is necessary to use simulation to estimate the theoretical
mean of the summary function. Only relevant when |
VARIANCE |
Logical. If |
nSD |
Number of estimated standard deviations used to determine
the critical envelopes, if |
Yname |
Character string that should be used as the name of the
data point pattern |
maxnerr |
Maximum number of rejected patterns.
If |
rejectNA |
Logical value specifying whether to reject a simulated pattern
if the resulting values of |
silent |
Logical value specifying whether to print a report each time a simulated pattern is rejected. |
do.pwrong |
Logical. If |
envir.simul |
Environment in which to evaluate the expression |
The envelope
command performs simulations and
computes envelopes of a summary statistic based on the simulations.
The result is an object that can be plotted to display the envelopes.
The envelopes can be used to assess the goodness-of-fit of
a point process model to point pattern data.
For the most basic use, if you have a point pattern X
and
you want to test Complete Spatial Randomness (CSR), type
plot(envelope(X, Kest,nsim=39))
to see the K function
for X
plotted together with the envelopes of the
K function for 39 simulations of CSR.
The envelope
function is generic, with methods for
the classes "ppp"
, "ppm"
and "kppm"
described here. There are also methods for the classes "pp3"
,
"lpp"
and "lppm"
which are described separately
under envelope.pp3
and envelope.lpp
.
Envelopes can also be computed from other envelopes, using
envelope.envelope
.
To create simulation envelopes, the command envelope(Y, ...)
first generates nsim
random point patterns
in one of the following ways.
If Y
is a point pattern (an object of class "ppp"
)
and simulate=NULL
,
then we generate nsim
simulations of
Complete Spatial Randomness (i.e. nsim
simulated point patterns
each being a realisation of the uniform Poisson point process)
with the same intensity as the pattern Y
.
(If Y
is a multitype point pattern, then the simulated patterns
are also given independent random marks; the probability
distribution of the random marks is determined by the
relative frequencies of marks in Y
.)
If Y
is a fitted point process model (an object of class
"ppm"
or "kppm"
) and simulate=NULL
,
then this routine generates nsim
simulated
realisations of that model.
If simulate
is supplied, then it determines how the
simulated point patterns are generated. It may be either
an expression in the R language, typically containing a call
to a random generator. This expression will be evaluated
nsim
times to yield nsim
point patterns. For example
if simulate=expression(runifpoint(100))
then each simulated
pattern consists of exactly 100 independent uniform random points.
a function in the R language, typically containing a call to a
random generator. This function will be applied repeatedly
to the original data pattern Y
to yield nsim
point
patterns. For example if simulate=rlabel
then each
simulated pattern was generated by evaluating rlabel(Y)
and consists of a randomly-relabelled version of Y
.
a list of point patterns. The entries in this list will be taken as the simulated patterns.
an object of class "envelope"
. This should have been
produced by calling envelope
with the
argument savepatterns=TRUE
.
The simulated point patterns that were saved in this object
will be extracted and used as the simulated patterns for the
new envelope computation. This makes it possible to plot envelopes
for two different summary functions based on exactly the same set of
simulated point patterns.
The summary statistic fun
is applied to each of these simulated
patterns. Typically fun
is one of the functions
Kest
, Gest
, Fest
, Jest
, pcf
,
Kcross
, Kdot
, Gcross
, Gdot
,
Jcross
, Jdot
, Kmulti
, Gmulti
,
Jmulti
or Kinhom
. It may also be a character string
containing the name of one of these functions.
The statistic fun
can also be a user-supplied function;
if so, then it must have arguments X
and r
like those in the functions listed above, and it must return an object
of class "fv"
.
Upper and lower critical envelopes are computed in one of the following ways:
by default, envelopes are calculated pointwise
(i.e. for each value of the distance argument r), by sorting the
nsim
simulated values, and taking the m
-th lowest
and m
-th highest values, where m = nrank
.
For example if nrank=1
, the upper and lower envelopes
are the pointwise maximum and minimum of the simulated values.
The pointwise envelopes are not “confidence bands”
for the true value of the function! Rather,
they specify the critical points for a Monte Carlo test
(Ripley, 1981). The test is constructed by choosing a
fixed value of r, and rejecting the null hypothesis if the
observed function value
lies outside the envelope at this value of r.
This test has exact significance level
alpha = 2 * nrank/(1 + nsim)
.
if global=TRUE
, then the envelopes are
determined as follows. First we calculate the theoretical mean value of
the summary statistic (if we are testing CSR, the theoretical
value is supplied by fun
; otherwise we perform a separate
set of nsim2
simulations, compute the
average of all these simulated values, and take this average
as an estimate of the theoretical mean value). Then, for each simulation,
we compare the simulated curve to the theoretical curve, and compute the
maximum absolute difference between them (over the interval
of r values specified by ginterval
). This gives a
deviation value d[i] for each of the nsim
simulations. Finally we take the m
-th largest of the
deviation values, where m=nrank
, and call this
dcrit
. Then the simultaneous envelopes are of the form
lo = expected - dcrit
and hi = expected + dcrit
where
expected
is either the theoretical mean value theo
(if we are testing CSR) or the estimated theoretical value
mmean
(if we are testing another model). The simultaneous critical
envelopes have constant width 2 * dcrit
.
The simultaneous critical envelopes allow us to perform a different
Monte Carlo test (Ripley, 1981). The test rejects the null
hypothesis if the graph of the observed function
lies outside the envelope at any value of r.
This test has exact significance level
alpha = nrank/(1 + nsim)
.
This test can also be performed using mad.test
.
if VARIANCE=TRUE
,
the algorithm calculates the
(pointwise) sample mean and sample variance of
the simulated functions. Then the envelopes are computed
as mean plus or minus nSD
standard deviations.
These envelopes do not have an exact significance interpretation.
They are a naive approximation to
the critical points of the Neyman-Pearson test
assuming the summary statistic is approximately Normally
distributed.
The return value is an object of class "fv"
containing
the summary function for the data point pattern,
the upper and lower simulation envelopes, and
the theoretical expected value (exact or estimated) of the summary function
for the model being tested. It can be plotted
using plot.envelope
.
If VARIANCE=TRUE
then the return value also includes the
sample mean, sample variance and other quantities.
Arguments can be passed to the function fun
through
...
. This means that you simply specify these arguments in the call to
envelope
, and they will be passed to fun
.
In particular, the argument correction
determines the edge correction to be used to calculate the summary
statistic. See the section on Edge Corrections, and the Examples.
Arguments can also be passed to the function fun
through the list funargs
. This mechanism is typically used if
an argument of fun
has the same name as an argument of
envelope
. The list funargs
should contain
entries of the form name=value
, where each name
is the name
of an argument of fun
.
There is also an option, rarely used, in which different function
arguments are used when computing the summary function
for the data Y
and for the simulated patterns.
If funYargs
is given, it will be used
when the summary function for the data Y
is computed,
while funargs
will be used when computing the summary function
for the simulated patterns.
This option is only needed in rare cases: usually the basic principle
requires that the data and simulated patterns must be treated
equally, so that funargs
and funYargs
should be identical.
If Y
is a fitted cluster point process model (object of
class "kppm"
), and simulate=NULL
,
then the model is simulated directly
using simulate.kppm
.
For simultaneous critical envelopes (global=TRUE
)
the following options are also useful:
ginterval
determines the interval of r values
over which the deviation between curves is calculated.
It should be a numeric vector of length 2.
There is a sensible default (namely, the recommended plotting
interval for fun(X)
, or the range of r
values if
r
is explicitly specified).
transform
specifies a transformation of the
summary function fun
that will be carried out before the
deviations are computed.
Such transforms are useful if global=TRUE
or
VARIANCE=TRUE
.
The transform
must be an expression object
using the symbol .
to represent the function value
(and possibly other symbols recognised by with.fv
).
For example,
the conventional way to normalise the K function
(Ripley, 1981) is to transform it to the L function
L(r) = sqrt(K(r)/π)
and this is implemented by setting
transform=expression(sqrt(./pi))
.
It is also possible to extract the summary functions for each of the
individual simulated point patterns, by setting savefuns=TRUE
.
Then the return value also
has an attribute "simfuns"
containing all the
summary functions for the individual simulated patterns.
It is an "fv"
object containing
functions named sim1, sim2, ...
representing the nsim
summary functions.
It is also possible to save the simulated point patterns themselves,
by setting savepatterns=TRUE
. Then the return value also has
an attribute "simpatterns"
which is a list of length
nsim
containing all the simulated point patterns.
See plot.envelope
and plot.fv
for information about how to plot the envelopes.
Different envelopes can be recomputed from the same data
using envelope.envelope
.
Envelopes can be combined using pool.envelope
.
An object of class "envelope"
and "fv"
, see fv.object
,
which can be printed and plotted directly.
Essentially a data frame containing columns
r |
the vector of values of the argument r
at which the summary function |
obs |
values of the summary function for the data point pattern |
lo |
lower envelope of simulations |
hi |
upper envelope of simulations |
and either
theo |
theoretical value of the summary function under CSR (Complete Spatial Randomness, a uniform Poisson point process) if the simulations were generated according to CSR |
mmean |
estimated theoretical value of the summary function, computed by averaging simulated values, if the simulations were not generated according to CSR. |
Additionally, if savepatterns=TRUE
, the return value has an attribute
"simpatterns"
which is a list containing the nsim
simulated patterns. If savefuns=TRUE
, the return value
has an attribute "simfuns"
which is an object of class
"fv"
containing the summary functions
computed for each of the nsim
simulated patterns.
An error may be generated if one of the simulations produces a
point pattern that is empty, or is otherwise unacceptable to the
function fun
.
The upper envelope may be NA
(plotted as plus or minus
infinity) if some of the function values
computed for the simulated point patterns are NA
.
Whether this occurs will depend on the function fun
,
but it usually happens when the simulated point pattern does not contain
enough points to compute a meaningful value.
It is common to apply a correction for edge effects when
calculating a summary function such as the K function.
Typically the user has a choice between several possible edge
corrections.
In a call to envelope
, the user can specify the edge correction
to be applied in fun
, using the argument correction
.
See the Examples below.
Summary functions that are available in spatstat, such as
Kest
, Gest
and pcf
,
have a standard argument called correction
which specifies
the name of one or more edge corrections.
The list of available edge
corrections is different for each summary function,
and may also depend on the kind of window in which the point pattern is
recorded.
In the
case of Kest
(the default and most frequently used value of
fun
) the best edge correction is Ripley's isotropic
correction if the window is rectangular or polygonal,
and the translation correction if the window is a binary mask.
See the help files for the individual
functions for more information.
All the summary functions in spatstat
recognise the option correction="best"
which gives the “best” (most accurate) available edge correction
for that function.
In a call to envelope
, if fun
is one of the
summary functions provided in spatstat, then the default
is correction="best"
. This means that
by default, the envelope will be computed
using the “best” available edge correction.
The user can override this default by specifying the argument
correction
. For example the computation can be accelerated
by choosing another edge correction which is less accurate
than the “best” one, but faster to compute.
If fun
is a function written by the user,
then envelope
has to guess what to do.
If fun
has an argument
called correction
, or has ...
arguments,
then envelope
assumes that the function
can handle a correction argument. To compute the envelope,
fun
will be called with a correction
argument.
The default is correction="best"
, unless
overridden in the call to envelope
.
Otherwise, if fun
does not have an argument
called correction
and does not have ...
arguments,
then envelope
assumes that the function
cannot handle a correction argument. To compute the
envelope, fun
is called without a correction argument.
Adrian Baddeley Adrian.Baddeley@curtin.edu.au, Rolf Turner r.turner@auckland.ac.nz and Ege Rubak rubak@math.aau.dk.
Baddeley, A., Diggle, P.J., Hardegen, A., Lawrence, T., Milne, R.K. and Nair, G. (2014) On tests of spatial pattern based on simulation envelopes. Ecological Monographs 84 (3) 477–489.
Cressie, N.A.C. Statistics for spatial data. John Wiley and Sons, 1991.
Diggle, P.J. Statistical analysis of spatial point patterns. Arnold, 2003.
Ripley, B.D. (1981) Spatial statistics. John Wiley and Sons.
Ripley, B.D. Statistical inference for spatial processes. Cambridge University Press, 1988.
Stoyan, D. and Stoyan, H. (1994) Fractals, random shapes and point fields: methods of geometrical statistics. John Wiley and Sons.
fv.object
,
plot.envelope
,
plot.fv
,
envelope.envelope
,
pool.envelope
for handling envelopes.
There are also methods for print
and summary
.
X <- simdat online <- interactive() Nsim <- if(online) 19 else 3 # Envelope of K function under CSR plot(envelope(X, nsim=Nsim)) # Translation edge correction (this is also FASTER): if(online) { plot(envelope(X, correction="translate")) } else { E <- envelope(X, nsim=Nsim, correction="translate") } # Global envelopes if(online) { plot(envelope(X, Lest, global=TRUE)) plot(envelope(X, Kest, global=TRUE, scale=function(r) { r })) } else { E <- envelope(X, Lest, nsim=Nsim, global=TRUE) E <- envelope(X, Kest, nsim=Nsim, global=TRUE, scale=function(r) { r }) E summary(E) } # Envelope of K function for simulations from Gibbs model if(online) { fit <- ppm(cells ~1, Strauss(0.05)) plot(envelope(fit)) plot(envelope(fit, global=TRUE)) } else { fit <- ppm(cells ~1, Strauss(0.05), nd=20) E <- envelope(fit, nsim=Nsim, correction="border", nrep=100) E <- envelope(fit, nsim=Nsim, correction="border", global=TRUE, nrep=100) } # Envelope of K function for simulations from cluster model fit <- kppm(redwood ~1, "Thomas") if(online) { plot(envelope(fit, Gest)) plot(envelope(fit, Gest, global=TRUE)) } else { E <- envelope(fit, Gest, correction="rs", nsim=Nsim, global=TRUE, nrep=100) } # Envelope of G function under CSR if(online) { plot(envelope(X, Gest)) } else { E <- envelope(X, Gest, correction="rs", nsim=Nsim) } # Envelope of L function under CSR # L(r) = sqrt(K(r)/pi) if(online) { E <- envelope(X, Kest) } else { E <- envelope(X, Kest, correction="border", nsim=Nsim) } plot(E, sqrt(./pi) ~ r) # Simultaneous critical envelope for L function # (alternatively, use Lest) if(online) { plot(envelope(X, Kest, transform=expression(sqrt(./pi)), global=TRUE)) } else { E <- envelope(X, Kest, nsim=Nsim, correction="border", transform=expression(sqrt(./pi)), global=TRUE) } ## One-sided envelope if(online) { plot(envelope(X, Lest, alternative="less")) } else { E <- envelope(X, Lest, nsim=Nsim, alternative="less") } # How to pass arguments needed to compute the summary functions: # We want envelopes for Jcross(X, "A", "B") # where "A" and "B" are types of points in the dataset 'demopat' if(online) { plot(envelope(demopat, Jcross, i="A", j="B")) } else { plot(envelope(demopat, Jcross, correction="rs", i="A", j="B", nsim=Nsim)) } # Use of `simulate' expression if(online) { plot(envelope(cells, Gest, simulate=expression(runifpoint(42)))) plot(envelope(cells, Gest, simulate=expression(rMaternI(100,0.02)))) } else { plot(envelope(cells, Gest, correction="rs", simulate=expression(runifpoint(42)), nsim=Nsim)) plot(envelope(cells, Gest, correction="rs", simulate=expression(rMaternI(100, 0.02)), nsim=Nsim, global=TRUE)) } # Use of `simulate' function if(online) { plot(envelope(amacrine, Kcross, simulate=rlabel)) } else { plot(envelope(amacrine, Kcross, simulate=rlabel, nsim=Nsim)) } # Envelope under random toroidal shifts if(online) { plot(envelope(amacrine, Kcross, i="on", j="off", simulate=expression(rshift(amacrine, radius=0.25)))) } # Envelope under random shifts with erosion if(online) { plot(envelope(amacrine, Kcross, i="on", j="off", simulate=expression(rshift(amacrine, radius=0.1, edge="erode")))) } # Envelope of INHOMOGENEOUS K-function with fitted trend # The following is valid. # Setting lambda=fit means that the fitted model is re-fitted to # each simulated pattern to obtain the intensity estimates for Kinhom. # (lambda=NULL would also be valid) fit <- kppm(redwood ~1, clusters="MatClust") if(online) { plot(envelope(fit, Kinhom, lambda=fit, nsim=19)) } else { envelope(fit, Kinhom, lambda=fit, nsim=Nsim) } # Note that the principle of symmetry, essential to the validity of # simulation envelopes, requires that both the observed and # simulated patterns be subjected to the same method of intensity # estimation. In the following example it would be incorrect to set the # argument 'lambda=red.dens' in the envelope command, because this # would mean that the inhomogeneous K functions of the simulated # patterns would be computed using the intensity function estimated # from the original redwood data, violating the symmetry. There is # still a concern about the fact that the simulations are generated # from a model that was fitted to the data; this is only a problem in # small datasets. if(online) { red.dens <- density(redwood, sigma=bw.diggle, positive=TRUE) plot(envelope(redwood, Kinhom, sigma=bw.diggle, simulate=expression(rpoispp(red.dens)))) } # Precomputed list of point patterns if(online) { nX <- npoints(X) PatList <- list() for(i in 1:Nsim) PatList[[i]] <- runifpoint(nX) E <- envelope(X, Kest, nsim=19, simulate=PatList) } else { PatList <- list() for(i in 1:Nsim) PatList[[i]] <- runifpoint(10) } E <- envelope(X, Kest, nsim=Nsim, simulate=PatList) # re-using the same point patterns # EK <- envelope(X, Kest, savepatterns=TRUE) # EG <- envelope(X, Gest, simulate=EK) if(online) { EK <- envelope(X, Kest, nsim=Nsim, savepatterns=TRUE) EG <- envelope(X, Gest, nsim=Nsim, simulate=EK) }
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.