Estimates the inhomogeneous \(K\) function of a non-stationary point pattern.

```
Kinhom(X, lambda=NULL, …, r = NULL, breaks = NULL,
correction=c("border", "bord.modif", "isotropic", "translate"),
renormalise=TRUE,
normpower=1,
update=TRUE,
leaveoneout=TRUE,
nlarge = 1000,
lambda2=NULL, reciplambda=NULL, reciplambda2=NULL,
diagonal=TRUE,
sigma=NULL, varcov=NULL,
ratio=FALSE)
```

X

The observed data point pattern,
from which an estimate of the inhomogeneous \(K\) function
will be computed.
An object of class `"ppp"`

or in a format recognised by `as.ppp()`

lambda

Optional.
Values of the estimated intensity function.
Either a vector giving the intensity values
at the points of the pattern `X`

,
a pixel image (object of class `"im"`

) giving the
intensity values at all locations, a fitted point process model
(object of class `"ppm"`

or `"kppm"`

)
or a `function(x,y)`

which
can be evaluated to give the intensity value at any location.

…

Extra arguments. Ignored if `lambda`

is present.
Passed to `density.ppp`

if `lambda`

is omitted.

r

vector of values for the argument \(r\) at which the inhomogeneous \(K\) function should be evaluated. Not normally given by the user; there is a sensible default.

breaks

This argument is for internal use only.

correction

A character vector containing any selection of the
options `"border"`

, `"bord.modif"`

,
`"isotropic"`

, `"Ripley"`

, `"translate"`

,
`"translation"`

,
`"none"`

or `"best"`

.
It specifies the edge correction(s) to be applied.
Alternatively `correction="all"`

selects all options.

renormalise

Logical. Whether to renormalise the estimate. See Details.

normpower

Integer (usually either 1 or 2). Normalisation power. See Details.

update

Logical value indicating what to do when `lambda`

is a fitted model
(class `"ppm"`

, `"kppm"`

or `"dppm"`

).
If `update=TRUE`

(the default),
the model will first be refitted to the data `X`

(using `update.ppm`

or `update.kppm`

)
before the fitted intensity is computed.
If `update=FALSE`

, the fitted intensity of the
model will be computed without re-fitting it to `X`

.

leaveoneout

Logical value (passed to `density.ppp`

or
`fitted.ppm`

) specifying whether to use a
leave-one-out rule when calculating the intensity.

nlarge

Optional. Efficiency threshold.
If the number of points exceeds `nlarge`

, then only the
border correction will be computed, using a fast algorithm.

lambda2

Advanced use only. Matrix containing estimates of the products \(\lambda(x_i)\lambda(x_j)\) of the intensities at each pair of data points \(x_i\) and \(x_j\).

reciplambda

Alternative to `lambda`

.
Values of the estimated *reciprocal* \(1/\lambda\)
of the intensity function.
Either a vector giving the reciprocal intensity values
at the points of the pattern `X`

,
a pixel image (object of class `"im"`

) giving the
reciprocal intensity values at all locations,
or a `function(x,y)`

which can be evaluated to give the
reciprocal intensity value at any location.

reciplambda2

Advanced use only. Alternative to `lambda2`

.
A matrix giving values of the estimated *reciprocal products*
\(1/\lambda(x_i)\lambda(x_j)\)
of the intensities at each pair of data points
\(x_i\) and \(x_j\).

diagonal

Do not use this argument.

sigma,varcov

Optional arguments passed to `density.ppp`

to control the smoothing bandwidth, when `lambda`

is
estimated by kernel smoothing.

ratio

Logical.
If `TRUE`

, the numerator and denominator of
each edge-corrected estimate will also be saved,
for use in analysing replicated point patterns.

An object of class `"fv"`

(see `fv.object`

).

Essentially a data frame containing at least the following columns,

the vector of values of the argument \(r\) at which \(K_{\mbox{\scriptsize\rm inhom}}(r)\) has been estimated

vector of values of \(\pi r^2\), the theoretical value of \(K_{\mbox{\scriptsize\rm inhom}}(r)\) for an inhomogeneous Poisson process

If ratio=TRUE then the return value also has two attributes called "numerator" and "denominator" which are "fv" objects containing the numerators and denominators of each estimate of K_{\mbox{\scriptsize\rm inhom}}(r)Kinhom(r).

This computes a generalisation of the \(K\) function for inhomogeneous point patterns, proposed by Baddeley, Moller and Waagepetersen (2000).

The ``ordinary'' \(K\) function
(variously known as the reduced second order moment function
and Ripley's \(K\) function), is
described under `Kest`

. It is defined only
for stationary point processes.

The inhomogeneous \(K\) function \(K_{\mbox{\scriptsize\rm inhom}}(r)\) is a direct generalisation to nonstationary point processes. Suppose \(x\) is a point process with non-constant intensity \(\lambda(u)\) at each location \(u\). Define \(K_{\mbox{\scriptsize\rm inhom}}(r)\) to be the expected value, given that \(u\) is a point of \(x\), of the sum of all terms \(1/\lambda(x_j)\) over all points \(x_j\) in the process separated from \(u\) by a distance less than \(r\). This reduces to the ordinary \(K\) function if \(\lambda()\) is constant. If \(x\) is an inhomogeneous Poisson process with intensity function \(\lambda(u)\), then \(K_{\mbox{\scriptsize\rm inhom}}(r) = \pi r^2\).

Given a point pattern dataset, the inhomogeneous \(K\) function can be estimated essentially by summing the values \(1/(\lambda(x_i)\lambda(x_j))\) for all pairs of points \(x_i, x_j\) separated by a distance less than \(r\).

This allows us to inspect a point pattern for evidence of interpoint interactions after allowing for spatial inhomogeneity of the pattern. Values \(K_{\mbox{\scriptsize\rm inhom}}(r) > \pi r^2\) are suggestive of clustering.

The argument `lambda`

should supply the
(estimated) values of the intensity function \(\lambda\).
It may be either

- a numeric vector
containing the values of the intensity function at the points of the pattern

`X`

.- a pixel image
(object of class

`"im"`

) assumed to contain the values of the intensity function at all locations in the window.- a fitted point process model
(object of class

`"ppm"`

,`"kppm"`

or`"dppm"`

) whose fitted*trend*can be used as the fitted intensity. (If`update=TRUE`

the model will first be refitted to the data`X`

before the trend is computed.)- a function
which can be evaluated to give values of the intensity at any locations.

- omitted:
if

`lambda`

is omitted, then it will be estimated using a `leave-one-out' kernel smoother.

If `lambda`

is a numeric vector, then its length should
be equal to the number of points in the pattern `X`

.
The value `lambda[i]`

is assumed to be the
the (estimated) value of the intensity
\(\lambda(x_i)\) for
the point \(x_i\) of the pattern \(X\).
Each value must be a positive number; `NA`

's are not allowed.

If `lambda`

is a pixel image, the domain of the image should
cover the entire window of the point pattern. If it does not (which
may occur near the boundary because of discretisation error),
then the missing pixel values
will be obtained by applying a Gaussian blur to `lambda`

using
`blur`

, then looking up the values of this blurred image
for the missing locations.
(A warning will be issued in this case.)

If `lambda`

is a function, then it will be evaluated in the
form `lambda(x,y)`

where `x`

and `y`

are vectors
of coordinates of the points of `X`

. It should return a numeric
vector with length equal to the number of points in `X`

.

If `lambda`

is omitted, then it will be estimated using
a `leave-one-out' kernel smoother,
as described in Baddeley, Moller
and Waagepetersen (2000). The estimate `lambda[i]`

for the
point `X[i]`

is computed by removing `X[i]`

from the
point pattern, applying kernel smoothing to the remaining points using
`density.ppp`

, and evaluating the smoothed intensity
at the point `X[i]`

. The smoothing kernel bandwidth is controlled
by the arguments `sigma`

and `varcov`

, which are passed to
`density.ppp`

along with any extra arguments.

Edge corrections are used to correct bias in the estimation
of \(K_{\mbox{\scriptsize\rm inhom}}\).
Each edge-corrected estimate of
\(K_{\mbox{\scriptsize\rm inhom}}(r)\) is
of the form
$$
\widehat K_{\mbox{\scriptsize\rm inhom}}(r) = (1/A)
\sum_i \sum_j \frac{1\{d_{ij} \le r\}
e(x_i,x_j,r)}{\lambda(x_i)\lambda(x_j)}
$$
where `A`

is a constant denominator,
\(d_{ij}\) is the distance between points
\(x_i\) and \(x_j\), and
\(e(x_i,x_j,r)\) is
an edge correction factor. For the `border' correction,
$$
e(x_i,x_j,r) =
\frac{1(b_i > r)}{\sum_j 1(b_j > r)/\lambda(x_j)}
$$
where \(b_i\) is the distance from \(x_i\)
to the boundary of the window. For the `modified border'
correction,
$$
e(x_i,x_j,r) =
\frac{1(b_i > r)}{\mbox{area}(W \ominus r)}
$$
where \(W \ominus r\) is the eroded window obtained
by trimming a margin of width \(r\) from the border of the original
window.
For the `translation' correction,
$$
e(x_i,x_j,r) =
\frac 1 {\mbox{area}(W \cap (W + (x_j - x_i)))}
$$
and for the `isotropic' correction,
$$
e(x_i,x_j,r) =
\frac 1 {\mbox{area}(W) g(x_i,x_j)}
$$
where \(g(x_i,x_j)\) is the fraction of the
circumference of the circle with centre \(x_i\) and radius
\(||x_i - x_j||\) which lies inside the window.

If `renormalise=TRUE`

(the default), then the estimates
described above
are multiplied by \(c^{\mbox{normpower}}\) where
\(
c = \mbox{area}(W)/\sum (1/\lambda(x_i)).
\)
This rescaling reduces the variability and bias of the estimate
in small samples and in cases of very strong inhomogeneity.
The default value of `normpower`

is 1 (for consistency with
previous versions of spatstat)
but the most sensible value is 2, which would correspond to rescaling
the `lambda`

values so that
\(
\sum (1/\lambda(x_i)) = \mbox{area}(W).
\)

If the point pattern `X`

contains more than about 1000 points,
the isotropic and translation edge corrections can be computationally
prohibitive. The computations for the border method are much faster,
and are statistically efficient when there are large numbers of
points. Accordingly, if the number of points in `X`

exceeds
the threshold `nlarge`

, then only the border correction will be
computed. Setting `nlarge=Inf`

or `correction="best"`

will prevent this from happening.
Setting `nlarge=0`

is equivalent to selecting only the border
correction with `correction="border"`

.

The pair correlation function can also be applied to the
result of `Kinhom`

; see `pcf`

.

Baddeley, A.,
Moller, J. and Waagepetersen, R. (2000)
Non- and semiparametric estimation of interaction in
inhomogeneous point patterns.
*Statistica Neerlandica* **54**, 329--350.

```
# NOT RUN {
# inhomogeneous pattern of maples
X <- unmark(split(lansing)$maple)
# }
# NOT RUN {
# (1) intensity function estimated by model-fitting
# Fit spatial trend: polynomial in x and y coordinates
fit <- ppm(X, ~ polynom(x,y,2), Poisson())
# (a) predict intensity values at points themselves,
# obtaining a vector of lambda values
lambda <- predict(fit, locations=X, type="trend")
# inhomogeneous K function
Ki <- Kinhom(X, lambda)
plot(Ki)
# (b) predict intensity at all locations,
# obtaining a pixel image
lambda <- predict(fit, type="trend")
Ki <- Kinhom(X, lambda)
plot(Ki)
# (2) intensity function estimated by heavy smoothing
Ki <- Kinhom(X, sigma=0.1)
plot(Ki)
# (3) simulated data: known intensity function
lamfun <- function(x,y) { 50 + 100 * x }
# inhomogeneous Poisson process
Y <- rpoispp(lamfun, 150, owin())
# inhomogeneous K function
Ki <- Kinhom(Y, lamfun)
plot(Ki)
# How to make simulation envelopes:
# Example shows method (2)
if(interactive()) {
smo <- density.ppp(X, sigma=0.1)
Ken <- envelope(X, Kinhom, nsim=99,
simulate=expression(rpoispp(smo)),
sigma=0.1, correction="trans")
plot(Ken)
}
# }
```

Run the code above in your browser using DataLab