Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. library(raster)
  2. set.seed(17) # Seed a reproducible random sequence
  3. nr <- 30 # Number of rows
  4. nc <- 50 # Number of columns
  5. #
  6. # Create a zone raster for normalizing the probabilities.
  7. #
  8. zone <- raster(ncol=nc, nrow=nr)
  9. zone[] <- 0
  10. #
  11. # Create a probability raster (for illustrating the algorithm later).
  12. #
  13. p <- raster(ncol=nc, nrow=nr)
  14. p[] <- (1:(nc*nr) - 1/2) / (nc*nr) + rnorm(nc*nr, sd=0.5)
  15. p <- abs(focal(p, ngb=5, run=mean))
  16. z <- zonal(p, zone, stat='sum')
  17. p <- p / z[[2]] # This normalizes p to sum to unity as required
  18. #------------------------------------------------------------------------------#
  19. #
  20. # The algorithm begins here.
  21. #
  22. pvec <- sort(getValues(p), decreasing=TRUE) # The probabilities, sorted
  23. d <- cumsum(pvec) # Cumulative probabilities
  24. dpos <- d[d <= 0.95] # Position to stop
  25. region <- p # Initialize the output
  26. region[p < pvec[length(dpos)]] <- NA # Exclude the last 5% of the probability
  27. plot(region) # Display the result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement