if (FALSE) {
library(SamplingStrata)
data("swissmunicipalities")
swissmunicipalities$id <- c(1:nrow(swissmunicipalities))
swissmunicipalities$dom <- 1
frame <- buildFrameDF(swissmunicipalities,
id = "id",
domainvalue = "REG",
X = c("POPTOT", "HApoly"),
Y = c("Surfacesbois", "Airind")
)
ndom <- length(unique(frame$domainvalue))
cv <- as.data.frame(list(
DOM = rep("DOM1",ndom),
CV1 = rep(0.1,ndom),
CV2 = rep(0.1,ndom),
domainvalue = c(1:ndom)))
# Solution with kmean clustering
kmean <- KmeansSolution2(frame,model=NULL,errors=cv,nstrata=NA,maxclusters=4)
# Number of strata for each domain
nstrat <- tapply(kmean$suggestions,
kmean$domainvalue,
FUN=function(x) length(unique(x)))
# Prepare suggestion for optimization step
sugg <- prepareSuggestion(kmean = kmean,
frame = frame,
nstrat = nstrat)
# Optimization
solution <- optimStrata (
method="continuous",
errors=cv,
framesamp=frame,
iter = 50,
pops = 10,
nStrata = nstrat,
suggestions = sugg)
}
Run the code above in your browser using DataLab