require(spatstat)
data(redwood)
# count the number of points within radius 0.2 of each point of X
nneighbours <- applynbd(redwood, R=0.2, function(Y, ...){Y$n-1})
# equivalent to:
nneighbours <- applynbd(redwood, R=0.2, function(Y, ...){Y$n}, exclude=TRUE)
# compute the distance to the second nearest neighbour of each point
secondnndist <- applynbd(redwood, N = 2, function(Y, cur, d, r){max(d)}, exclude=TRUE)
# marked point pattern
data(longleaf)
# compute the median of the marks of all neighbours of a point
dbh.med <- applynbd(longleaf, R=40, exclude=TRUE,
function(Y,...) { median(Y$marks)})
# ANIMATION explaining the definition of the K function
# (arguments `fullpicture' and 'rad' are passed to FUN)
showoffK <- function(Y, u, d, r, fullpicture,rad) {
plot(fullpicture, main="")
points(Y[r>0], cex=2)
points(u[1],u[2],pch="+",cex=3)
theta <- seq(0,2*pi,length=100)
polygon(u[1]+ rad * cos(theta),u[2]+rad*sin(theta))
text(u[1]+rad/3,u[2]+rad/2,Y$n-1,cex=3)
if(runif(1) < 0.1)
system("sleep 1")
return(Y$n - 1)
}
applynbd(redwood, R=0.2, showoffK, fullpicture=redwood, rad=0.2)
# animation explaining the definition of the G function
showoffG <- function(Y, u, d, r, fullpicture) {
plot(fullpicture, main="")
points(Y, cex=2)
points(u[1],u[2],pch="+",cex=3)
v <- c(Y$x[1],Y$y[1])
segments(u[1],u[2],v[1],v[2],lwd=2)
w <- (u + v)/2
nnd <- sqrt(sum((u-v)^2))
text(w[1],w[2],round(nnd,3),cex=2)
if(runif(1) < 0.1)
system("sleep 1")
return(nnd)
}
data(cells)
applynbd(cells, N=1, showoffG, exclude=TRUE, fullpicture=cells)
Run the code above in your browser using DataLab