https://github.com/cran/spatstat
Revision e575bb3736f6d70d2bd2b23ea2e4474cdbae00be authored by Adrian Baddeley on 11 November 2010, 13:09:43 UTC, committed by cran-robot on 11 November 2010, 13:09:43 UTC
1 parent cafe44b
Raw File
Tip revision: e575bb3736f6d70d2bd2b23ea2e4474cdbae00be authored by Adrian Baddeley on 11 November 2010, 13:09:43 UTC
version 1.21-1
Tip revision: e575bb3
quadratresample.R
#
# quadratresample.R
#
# resample a point pattern by resampling quadrats
#
# $Revision: 1.3 $  $Date: 2008/09/26 19:51:53 $
#

quadratresample <- function(X, nx, ny=nx, ..., replace=FALSE, nsamples=1) {
  stopifnot(is.ppp(X))
  if(X$window$type != "rectangle")
    stop("Resampling is only implemented for rectangular windows")
  # create tessellation
  A <- quadrats(X, nx=nx, ny=ny)
  # split data over tessellation
  B <- split(X, A)
  nq <- length(B)
  # determine bottom left corner of each tile
  V <- lapply(B, function(z) { w <- z$window;
                               c(w$xrange[1], w$yrange[1]) })
  out <- list()
  for(i in 1:nsamples) {
    # resample tiles
    ind <- sample(1:nq, nq, replace=replace)
    Xresampled <- X
    Bresampled <- B
    for(j in 1:nq) {
      k <- ind[j]
      Bresampled[[j]] <- shift(B[[k]], unlist(V[[j]]) - unlist(V[[k]]))
    }
    split(Xresampled, A) <- Bresampled
    out[[i]] <- Xresampled
  }
  if(nsamples == 1)
    return(out[[1]])
  return(as.listof(out))
}

back to top