https://github.com/cran/spatstat
Revision 915786b60d089debd144a92c457952ebe8503995 authored by Adrian Baddeley on 14 May 2010, 00:00:00 UTC, committed by Gabor Csardi on 14 May 2010, 00:00:00 UTC
1 parent e1a5e08
Raw File
Tip revision: 915786b60d089debd144a92c457952ebe8503995 authored by Adrian Baddeley on 14 May 2010, 00:00:00 UTC
version 1.19-0
Tip revision: 915786b
adaptive.density.R
#
#  adaptive.density.R
#
#  $Revision: 1.2 $   $Date: 2008/10/01 00:50:28 $
#
#

adaptive.density <- function(X, f=0.1, ..., nrep=1) {
  stopifnot(is.ppp(X))
  npoints <- X$n
  stopifnot(is.numeric(f) && length(f) == 1 && f > 0 & f < 1)
  ntess <- floor(f * npoints)
  if(ntess == 0) {
    # naive estimate of intensity
    W <- X$window
    lam <- npoints/area.owin(W)
    return(as.im(lam, W, ...))
  }
  if(nrep > 1) {
    # estimate is the average of nrep randomised estimates
    total <- 0
    for(i in seq(nrep)) {
      estimate <- adaptive.density(X, f, ..., nrep=1)
      total <- eval.im(total + estimate)
    }
    average <- eval.im(total/nrep)
    return(average)
  }
  ncount <- npoints - ntess
  fcount <- ncount/npoints
  itess <- sample(seq(npoints), ntess, replace=FALSE)
  Xtess <- X[itess]
  Xcount <- X[-itess]
  tes <- dirichlet(Xtess)
  meanintensity <- function(x) { x$n/area.owin(x$window) }
  lam <- unlist(lapply(split(Xcount, tes), meanintensity))
  tesim <- as.im(tes, ...)
  out <- eval.im(lam[as.integer(tesim)]/fcount)
  return(out)
}
back to top