[R-sig-Geo] GRTS sampling

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Tue Feb 12 10:17:11 CET 2013


Dear Dick,

I have written my own functions for GRTS sampling. The idea is that you create a square matrix with dimensions which are a power of 2. Then it is easy to use a recursive function. The output is a matrix with the order of the cell. Sampling n elements reduces to take the first n elements.

QuadratRanking <- function(Ranking, Level = 0){
  if(ncol(Ranking) > 1){
    Sequence <- sample(0:3)
    Q1 <- QuadratRanking(Ranking[seq_len(nrow(Ranking)) <= nrow(Ranking) / 2, seq_len(ncol(Ranking)) <= ncol(Ranking) / 2, drop = FALSE] + Sequence[1] * 4 ^ Level, Level = Level + 1)
    Q2 <- QuadratRanking(Ranking[seq_len(nrow(Ranking)) > nrow(Ranking) / 2, seq_len(ncol(Ranking)) <= ncol(Ranking) / 2, drop = FALSE] + Sequence[2] * 4 ^ Level, Level = Level + 1)
    Q3 <- QuadratRanking(Ranking[seq_len(nrow(Ranking)) <= nrow(Ranking) / 2, seq_len(ncol(Ranking)) > ncol(Ranking) / 2, drop = FALSE] + Sequence[3] * 4 ^ Level, Level = Level + 1)
    Q4 <- QuadratRanking(Ranking[seq_len(nrow(Ranking)) > nrow(Ranking) / 2, seq_len(ncol(Ranking)) > ncol(Ranking) / 2, drop = FALSE] + Sequence[4] * 4 ^ Level, Level = Level + 1)
    cbind(rbind(Q1, Q2), rbind(Q3, Q4))
  } else {
    Ranking
  }
}
DimGrid <- 2 ^2
QuadratRanking(matrix(0L, ncol = DimGrid, nrow = DimGrid), Level = 0)


Here is a solution when you want to sample within a polygon

GRTS <- function(spPolygon, Precision, Subset = TRUE, RandomStart = TRUE){
  library(sp)
  QuadratRanking <- function(Ranking, Level = 0){
    if(ncol(Ranking) > 1){
      Sequence <- sample(0:3)
      Q1 <- QuadratRanking(Ranking[seq_len(nrow(Ranking)) <= nrow(Ranking) / 2, seq_len(ncol(Ranking)) <= ncol(Ranking) / 2, drop = FALSE] + Sequence[1] * 4 ^ Level, Level = Level + 1)
      Q2 <- QuadratRanking(Ranking[seq_len(nrow(Ranking)) > nrow(Ranking) / 2, seq_len(ncol(Ranking)) <= ncol(Ranking) / 2, drop = FALSE] + Sequence[2] * 4 ^ Level, Level = Level + 1)
      Q3 <- QuadratRanking(Ranking[seq_len(nrow(Ranking)) <= nrow(Ranking) / 2, seq_len(ncol(Ranking)) > ncol(Ranking) / 2, drop = FALSE] + Sequence[3] * 4 ^ Level, Level = Level + 1)
      Q4 <- QuadratRanking(Ranking[seq_len(nrow(Ranking)) > nrow(Ranking) / 2, seq_len(ncol(Ranking)) > ncol(Ranking) / 2, drop = FALSE] + Sequence[4] * 4 ^ Level, Level = Level + 1)
      cbind(rbind(Q1, Q2), rbind(Q3, Q4))
    } else {
      Ranking
    }
  }
  Xrange <- bbox(spPolygon)[1, ]
  Yrange <- bbox(spPolygon)[2, ]
  DimGrid <- ceiling(max(diff(Xrange), diff(Yrange)) / Precision)
  DimGrid <- 2 ^ min(seq_len(100)[2 ^ seq_len(100) > DimGrid])

  GRTS <- QuadratRanking(matrix(0L, ncol = DimGrid, nrow = DimGrid), Level = 0)
  GRTS <- GRTS[seq_len(ceiling(diff(Xrange)/Precision)), seq_len(ceiling(diff(Yrange)/Precision))]
  if(RandomStart){
    GRID <- GridTopology(cellcentre.offset = c(x = min(Xrange), y = min(Yrange)) + runif(2, min = 0, max = Precision), cellsize = c(Precision, Precision), cells.dim = dim(GRTS))
  } else {
    GRID <- GridTopology(cellcentre.offset = c(x = min(Xrange), y = min(Yrange)) + 0.5 * Precision, cellsize = c(Precision, Precision), cells.dim = dim(GRTS))
  }
  GRTS <- SpatialGridDataFrame(grid = GRID, data = data.frame(Ranking = as.vector(GRTS)), proj4string = proj4string(spPolygon))
  rm(GRID)
  if(Subset){
    gc()
    gridded(GRTS) <- FALSE
    GRTS <- GRTS[!is.na(over(GRTS, spPolygon)[, 1]), ]
    GRTS <- GRTS[order(GRTS$Ranking), ]
    GRTS$Ranking <- seq_along(GRTS$Ranking)
  }
  GRTS
}

If anyone would like to add it to a package, please feel free to do so. As long as it released under a GPL like license and properly credited.

Best regards,

Thierry

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry.Onkelinx at inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-project.org] Namens Dick Brus
Verzonden: maandag 11 februari 2013 15:45
Aan: r-sig-geo at r-project.org
Onderwerp: [R-sig-Geo] GRTS sampling

I try to understand the hierarchical randomization as applied in the GRTS design.

I randomized 16 points on a square grid, see R script below. The argument do.sample was set to FALSE, so that the result consists of a dataframe of all 16 points instead of a GRTS sample. When plotting the order of the points it appears that the numbers 1-4 are not always in different subsquares (of which there are four) , for instance points 1 and 3 are both in the lower-right subsquare. What am I doing wrong?

library(spsurvey)

frame<-expand.grid(x=seq(from=1,to=4,by=1),y=seq(from=1,to=4,by=1))

#population size (total number of sampling units in Kandahar)
N<-nrow(frame)

#set number of sampling units (sample size) to be selected
n<-4

design<-list(None=list(panel=c(Panel1=n), seltype="Equal", caty.n=c("Caty 1"=n), over=0)) x<-frame$x y<-frame$y index<-1:N
att<-data.frame(x=x,y=y,ids=index)

#do.sample=FALSE gives all sampling units in hierarchical? randomized order res<-grts(design, DesignID="Site", SiteBegin=1, type.frame="finite",
         src.frame="att.frame", in.shape=NULL, sp.object=NULL, att.frame=att,
         id=NULL, xcoord="x", ycoord="y", stratum=NULL, mdcaty=NULL, startlev=2,
         maxlev=11, maxtry=1000, shift.grid=FALSE, do.sample=rep(FALSE, length(design)),
         shapefile=FALSE, prjfilename=NULL, out.shape="sample")
frame.randomized<-data.frame(res$xcoord,res$ycoord)
names(frame.randomized)<-c("x","y")
frame.randomized$id<-1:N

png(file = "GRTSRandomizedFrameSquare.png", width = 480, height = 480) print(
  ggplot(data = frame.randomized) +
    geom_text(
      data = frame.randomized,
      mapping = aes(
        x = x,
        y = y,
        label = id
      ),
      size = 5
    ) +
    coord_equal()
)
dev.off()

<http://r-sig-geo.2731867.n2.nabble.com/file/n7582569/GRTSRandomizedFrameSquare.png>



--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/GRTS-sampling-tp7582569.html
Sent from the R-sig-geo mailing list archive at Nabble.com.

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.



More information about the R-sig-Geo mailing list