[R] Speeding up simulation of mean nearest neighbor distances

Dale Steele dale.w.steele at gmail.com
Wed Oct 3 18:45:19 CEST 2007


I've written the function below to simulate the mean 1st through nth
nearest neighbor distances for a random spatial pattern using the
functions nndist() and runifpoint() from spatsat.  It works, but runs
relatively slowly - would appreciate suggestions on how to speed up
this function.  Thanks.  --Dale

library(spatstat)

sim.nth.mdist <- function(nth,nsim) {
D <- matrix(ncol=nth, nrow=nsim)
for (i in 1:nsim) {
rpp <- runifpoint(22, win=owin(c(0,1),c(0,1)), giveup=1000)
for (k in 1:nth) D[i,k] <- mean(nndist(rpp ,k=k))
}
D
}

sim.nth.mdist(5,100)



More information about the R-help mailing list