rmpoint {spatstat.random} | R Documentation |
Generate N Random Multitype Points
Description
Generate a random multitype point pattern with a fixed number of points, or a fixed number of points of each type.
Usage
rmpoint(n, f=1, fmax=NULL, win=unit.square(),
types, ptypes,
..., giveup=1000, verbose=FALSE,
nsim=1, drop=TRUE)
Arguments
n |
Number of marked points to generate. Either a single number specifying the total number of points, or a vector specifying the number of points of each type. |
f |
The probability density of the multitype points,
usually un-normalised.
Either a constant, a vector,
a function |
fmax |
An upper bound on the values of |
win |
Window in which to simulate the pattern.
Ignored if |
types |
All the possible types for the multitype pattern. |
ptypes |
Optional vector of probabilities for each type. |
... |
Arguments passed to |
giveup |
Number of attempts in the rejection method after which the algorithm should stop trying to generate new points. |
verbose |
Flag indicating whether to report details of performance of the simulation algorithm. |
nsim |
Number of simulated realisations to be generated. |
drop |
Logical. If |
Details
This function generates random multitype point patterns consisting of a fixed number of points.
Three different models are available:
- I. Random location and type:
-
If
n
is a single number and the argumentptypes
is missing, thenn
independent, identically distributed random multitype points are generated. Their locations(x[i],y[i])
and typesm[i]
have joint probability density proportional tof(x,y,m)
. - II. Random type, and random location given type:
-
If
n
is a single number andptypes
is given, thenn
independent, identically distributed random multitype points are generated. Their typesm[i]
have probability distributionptypes
. Given the types, the locations(x[i],y[i])
have conditional probability density proportional tof(x,y,m)
. - III. Fixed types, and random location given type:
-
If
n
is a vector, then we generaten[i]
independent, identically distributed random points of typetypes[i]
. For points of typem
the conditional probability density of location(x,y)
is proportional tof(x,y,m)
.
Note that the density f
is normalised in different ways
in Model I and Models II and III. In Model I the normalised
joint density is g(x,y,m)=f(x,y,m)/Z
where
Z = \sum_m \int\int \lambda(x,y,m) {\rm d}x \, {\rm d}y
while in Models II and III the normalised conditional density
is g(x,y\mid m) = f(x,y,m)/Z_m
where
Z_m = \int\int \lambda(x,y,m) {\rm d}x \, {\rm d}y.
In Model I, the marginal distribution of types
is p_m = Z_m/Z
.
The unnormalised density f
may be specified
in any of the following ways.
- single number:
-
If
f
is a single number, the conditional density of location given type is uniform. That is, the points of each type are uniformly distributed. In Model I, the marginal distribution of types is also uniform (all possible types have equal probability). - vector:
-
If
f
is a numeric vector, the conditional density of location given type is uniform. That is, the points of each type are uniformly distributed. In Model I, the marginal distribution of types is proportional to the vectorf
. In Model II, the marginal distribution of types isptypes
, that is, the values inf
are ignored. The argumenttypes
defaults tonames(f)
, or if that is null,1:length(f)
. - function:
-
If
f
is a function, it will be called in the formf(x,y,m,...)
at spatial location(x,y)
for points of typem
. In Model I, the joint probability density of location and type is proportional tof(x,y,m,...)
. In Models II and III, the conditional probability density of location(x,y)
given typem
is proportional tof(x,y,m,...)
. The functionf
must work correctly with vectorsx
,y
andm
, returning a vector of function values. (Note thatm
will be a factor with levelstypes
.) The valuefmax
must be given and must be an upper bound on the values off(x,y,m,...)
for all locations(x, y)
inside the windowwin
and all typesm
. The argumenttypes
must be given. - list of functions:
-
If
f
is a list of functions, then the functions will be called in the formf[[i]](x,y,...)
at spatial location(x,y)
for points of typetypes[i]
. In Model I, the joint probability density of location and type is proportional tof[[m]](x,y,...)
. In Models II and III, the conditional probability density of location(x,y)
given typem
is proportional tof[[m]](x,y,...)
. The functionf[[i]]
must work correctly with vectorsx
andy
, returning a vector of function values. The valuefmax
must be given and must be an upper bound on the values off[[i]](x,y,...)
for all locations(x, y)
inside the windowwin
. The argumenttypes
defaults tonames(f)
, or if that is null,1:length(f)
. - pixel image:
-
If
f
is a pixel image object of class"im"
(seeim.object
), the unnormalised density at a location(x,y)
for points of any type is equal to the pixel value off
for the pixel nearest to(x,y)
. In Model I, the marginal distribution of types is uniform. The argumentwin
is ignored; the window of the pixel image is used instead. The argumenttypes
must be given. - list of pixel images:
-
If
f
is a list of pixel images, then the imagef[[i]]
determines the density values of points of typetypes[i]
. The argumentwin
is ignored; the window of the pixel image is used instead. The argumenttypes
defaults tonames(f)
, or if that is null,1:length(f)
.
The implementation uses the rejection method.
For Model I, rmpoispp
is called repeatedly
until n
points have been generated.
It gives up after giveup
calls
if there are still fewer than n
points.
For Model II, the types are first generated according to
ptypes
, then
the locations of the points of each type
are generated using rpoint
.
For Model III, the locations of the points of each type
are generated using rpoint
.
Value
A point pattern (an object of class "ppp"
) if nsim=1
,
or a list of point patterns if nsim > 1
.
Author(s)
Adrian Baddeley Adrian.Baddeley@curtin.edu.au
and Rolf Turner rolfturner@posteo.net
See Also
im.object
,
owin.object
,
ppp.object
.
Examples
abc <- c("a","b","c")
##### Model I
rmpoint(25, types=abc)
rmpoint(25, 1, types=abc)
# 25 points, equal probability for each type, uniformly distributed locations
rmpoint(25, function(x,y,m) {rep(1, length(x))}, types=abc)
# same as above
rmpoint(25, list(function(x,y){rep(1, length(x))},
function(x,y){rep(1, length(x))},
function(x,y){rep(1, length(x))}),
types=abc)
# same as above
rmpoint(25, function(x,y,m) { x }, types=abc)
# 25 points, equal probability for each type,
# locations nonuniform with density proportional to x
rmpoint(25, function(x,y,m) { ifelse(m == "a", 1, x) }, types=abc)
rmpoint(25, list(function(x,y) { rep(1, length(x)) },
function(x,y) { x },
function(x,y) { x }),
types=abc)
# 25 points, UNEQUAL probabilities for each type,
# type "a" points uniformly distributed,
# type "b" and "c" points nonuniformly distributed.
##### Model II
rmpoint(25, 1, types=abc, ptypes=rep(1,3)/3)
rmpoint(25, 1, types=abc, ptypes=rep(1,3))
# 25 points, equal probability for each type,
# uniformly distributed locations
rmpoint(25, function(x,y,m) {rep(1, length(x))}, types=abc, ptypes=rep(1,3))
# same as above
rmpoint(25, list(function(x,y){rep(1, length(x))},
function(x,y){rep(1, length(x))},
function(x,y){rep(1, length(x))}),
types=abc, ptypes=rep(1,3))
# same as above
rmpoint(25, function(x,y,m) { x }, types=abc, ptypes=rep(1,3))
# 25 points, equal probability for each type,
# locations nonuniform with density proportional to x
rmpoint(25, function(x,y,m) { ifelse(m == "a", 1, x) }, types=abc, ptypes=rep(1,3))
# 25 points, EQUAL probabilities for each type,
# type "a" points uniformly distributed,
# type "b" and "c" points nonuniformly distributed.
###### Model III
rmpoint(c(12, 8, 4), 1, types=abc)
# 12 points of type "a",
# 8 points of type "b",
# 4 points of type "c",
# each uniformly distributed
rmpoint(c(12, 8, 4), function(x,y,m) { ifelse(m=="a", 1, x)}, types=abc)
rmpoint(c(12, 8, 4), list(function(x,y) { rep(1, length(x)) },
function(x,y) { x },
function(x,y) { x }),
types=abc)
# 12 points of type "a", uniformly distributed
# 8 points of type "b", nonuniform
# 4 points of type "c", nonuniform
#########
## Randomising an existing point pattern:
# same numbers of points of each type, uniform random locations (Model III)
rmpoint(table(marks(demopat)), 1, win=Window(demopat))
# same total number of points, distribution of types estimated from X,
# uniform random locations (Model II)
rmpoint(npoints(demopat), 1, types=levels(marks(demopat)), win=Window(demopat),
ptypes=table(marks(demopat)))