agila
agila

Reputation: 3472

R - spatstat: Calculate density for a new point

Is it possible to use spatstat to estimate the intensity function for a give ppp object and calculate its value considering a new point? For example, can I evaluate D at new_point:

# packages
library(spatstat)

# define a random point within Window(swedishpines)
new_point <- ppp(x = 45, y = 45, window = Window(swedishpines))

# estimate density
(D <- density(swedishpines))
#> real-valued pixel image
#> 128 x 128 pixel array (ny, nx)
#> enclosing rectangle: [0, 96] x [0, 100] units (one unit = 0.1 metres)

Created on 2021-03-30 by the reprex package (v1.0.0)

I was thinking that maybe I can superimpose() the two ppp objects (i.e. swedishpines and new_point) and then run density setting at = "points" and weights = c(rep(1, points(swedishpines)), 0) but I'm not sure if that's the suggested approach (and I'm not sure if the appended point is ignored during the estimation process).

I know that it may sound like a trivial question, but I read some docs and didn't find an answer or a solution.

Upvotes: 0

Views: 361

Answers (1)

Adrian Baddeley
Adrian Baddeley

Reputation: 1984

There are two ways to do this.

The first is simply to take the pixel image of intensity, and extract the pixel values at the desired locations using [:

D <- density(swedishpines)
v <- D[new_points]

See the help for density.ppp and [.im.

The other way is to use densityfun:

f <- densityfun(swedishpines)
v <- f(new_points)

See the help for densityfun.ppp

The first route is more efficient and the second way is more accurate.

Technical issue: if some of the new_points could lie outside the window of swedishpines then the value at these points is (mathematically) undefined. Both of the methods described above will simply ignore such points, and the resulting vector v will be shorter than the number of new points. If you need to handle this continengcy, the easiest way is to use D[new_points, drop=FALSE] which returns NA values for such locations.

Upvotes: 2

Related Questions