library(sf)
if(require(sp, quietly = TRUE)) {
data(meuse, package = "sp")
meuse <- st_as_sf(meuse, coords = c("x", "y"), crs = 28992,
agr = "constant")
# Create stratified variable using quartile breaks
x1 <- cut(meuse$cadmium, summary(meuse$cadmium)[-4],
include.lowest=TRUE)
levels(x1) <- seq(1,nlevels(x1),1)
x2 <- cut(meuse$lead, summary(meuse$lead)[-4],
include.lowest=TRUE)
levels(x2) <- seq(1,nlevels(x2),1)
meuse$STRAT <- paste(x1, x2, sep='.')
# Counts for each full strata (note; 2 strata have only 1 observation)
tapply(meuse$STRAT, meuse$STRAT, length)
# 2 replicates, 2 samples with replacement
ssample <- stratified.random(meuse, strata='STRAT', n=2, reps=2,
replace=TRUE)
tapply(ssample$STRAT, ssample$STRAT, length)
# 2 replicates, 2 samples no replacement
ssample.nr <- stratified.random(meuse, strata='STRAT', n=2, reps=2)
tapply(ssample.nr$STRAT, ssample.nr$STRAT, length)
# n=1 and reps=10 for sequential numbering of samples
ssample.ct <- stratified.random(meuse, strata='STRAT', n=1, reps=10,
replace=TRUE)
tapply(ssample.ct$STRAT, ssample.ct$STRAT, length)
# Plot random samples colored by replacement
ssample$REP <- factor(ssample$REP)
plot(ssample['REP'], pch=20)
}
Run the code above in your browser using DataLab