This function has to be used only in conjunction with "optimizeStrataSpatial", i.e. in the case of optimizing with only continuous stratification variables and with a component of spatial autocorrelation. The function "KmeansSolutionSpatial" has a twofold objective: - to give indications about a possible best number of final strata (by fixing a convenient value for "maxclusters", and leaving NA to "nstrata"; - to give an initial solution fo the optimization step. If the parameter "nstrata" is not indicated, the optimal number of clusters is determined inside each domain, and the overall solution is obtained by concatenating optimal clusters obtained in domains. The result is a dataframe with two columns: the first indicates the clusters, the second the domains.

KmeansSolutionSpatial(
  frame,
  fitting = 1,
  range = c(0),
  kappa = 3,
  errors,
  nstrata = NA,
  minnumstrat = 2,
  maxclusters = NA,
  showPlot = TRUE,
  maxsamp = 5000,
  nugget = 0,
  vgm_model = "exponential"
)

Arguments

frame

The (mandatory) dataframe containing the information related to each unit in the sampling frame.

fitting

Default is 1, to be used when the frame contains the prediction-error variances (var columns). A value < 1 (R2 of the prediction model) is meant only for the rescaling correction, with all variances = 0: with positive variances the function stops (the error variance would be counted twice). If only R2 is known, use fitting = 1 with a constant variance var(predictions) * (1 - R2) / R2. See buildStrataDFSpatial().

range

Range of the correlation function of the prediction errors (in the units of the coordinates), as defined by the variogram model given in vgm_model. 0 means no spatial correlation.

kappa

Factor used in evaluating spatial autocorrelation with the exponential and gaussian models: 1 if range is the range parameter of the variogram (as returned by gstat), 3 if it is the practical range. Not used by the spherical model. Any finite value > 0 is accepted (a value other than 1 and 3 gives a warning); 0 or negative values give an error. Default is 3.

errors

The (mandatory) dataframe containing the precision constraints on target variables.

nstrata

Number of aggregate strata (if NULL, it is optimized by varying the number of cluster from 2 to half number of atomic strata). Default is NA.

minnumstrat

Minimum number of units to be selected in each stratum. Default is 2.

maxclusters

Maximum number of clusters to be considered in the execution of kmeans algorithm. If not indicated it will be set equal to the number of atomic strata divided by 2.

showPlot

Allows to visualise on a plot the different sample sizes for each number of aggregate strata. Default is TRUE.

maxsamp

This is the maximum number of frame units per domain accepted by the function. If the number of units exceeds this maximum, a subset (size equal to maxsamp) is sampled from the frame.

nugget

Relative nugget c0/(c0+c1) of the variogram of the prediction errors, in [0, 1]: the share of the error variance that is not spatially correlated. Default is 0.

vgm_model

Variogram model fitted on the prediction errors: "exponential" (default), "spherical" or "gaussian" (or the gstat abbreviations "Exp", "Sph", "Gau"). A single value, or one value for each target variable Y. See buildStrataDFSpatial() for the correlation functions.

Value

A dataframe containing the solution

Author

Giulio Barcaroli

Examples

if (FALSE) { # \dontrun{
library(sp)
library(gstat)
library(automap)
library(SamplingStrata)
#################
# data
#################
# locations (155 observed points)
data("meuse")
# grid of points (3103)
data("meuse.grid")
meuse.grid$id <- c(1:nrow(meuse.grid))
coordinates(meuse)<-c("x","y")
coordinates(meuse.grid)<-c("x","y")
#################
# kriging
#################
v <- variogram(lead ~ dist + soil, data=meuse)
fit.vgm <- autofitVariogram(lead ~ elev + soil, meuse, model = "Exp")
plot(v, fit.vgm$var_model)
fit.vgm$var_model
# model    psill    range
# 1   Nug 1524.895   0.0000
# 2   Exp 8275.431 458.3303
# relative nugget c0/(c0+c1)
nug <- fit.vgm$var_model$psill[1] / sum(fit.vgm$var_model$psill)
g <- NULL
g <- gstat(g, "Pb", lead ~ dist + soil, meuse)
g
vm <- variogram(g)
vm.fit <- fit.lmc(vm, g, vgm(psill=fit.vgm$var_model$psill[2],
                  model="Exp", range=fit.vgm$var_model$range[2],
                  nugget=fit.vgm$var_model$psill[1]))
# Prediction on the whole grid
preds <- predict(vm.fit, meuse.grid)
names(preds)
# [1] "Pb.pred" "Pb.var"
preds$Pb.pred <- ifelse(preds$Pb.pred < 0,0,preds$Pb.pred)
df <- NULL
df$Pb.pred <- preds@data$Pb.pred
df$Pb.var <- preds@data$Pb.var
df$dom1 <- 1
df <- as.data.frame(df)
df$id <- meuse.grid$id
#####################################
# Optimization with kmeans clustering
#####################################
frame <- buildFrameDF(df=df,
                      id="id",
                      X=c("Pb.pred"),
                      Y=c("Pb.pred"),
                      domainvalue = "dom1")
frame$var1 <- df$Pb.var
frame$lon <- meuse.grid$x
frame$lat <- meuse.grid$y
cv <- as.data.frame(list(DOM=rep("DOM1",1),
                         CV1=rep(0.05,1),
                         domainvalue=c(1:1) ))
km <- KmeansSolutionSpatial(frame,
                            errors = cv,
                            fitting = 1,
                            range = fit.vgm$var_model$range[2],
                            kappa=1,
                            nugget = nug,
                            nstrata=NA,
                            maxclusters = 5)
############################
# Analysis and visualization
############################
strataKm <- aggrStrataSpatial(dataset=frame,
                        fitting = 1,
                        range = fit.vgm$var_model$range[2],
                        kappa=1,
                        nugget = nug,
                        vett=km$suggestions,
                        dominio=1)
strataKm$SOLUZ <- bethel(strataKm,cv)
sum(strataKm$SOLUZ)
framenew <- frame
framenew$LABEL <- km$suggestions
strataKm$STRATO <- strataKm$stratum
ssKm <- summaryStrata(framenew,strataKm)
ssKm
frameres <- SpatialPointsDataFrame(data=framenew,
                                   coords=cbind(framenew$lon,framenew$lat) )
frameres2 <- SpatialPixelsDataFrame(points=frameres[c("lon","lat")],
                                    data=framenew)
frameres2$LABEL <- as.factor(frameres2$LABEL)
spplot(frameres2,c("LABEL"), col.regions=bpy.colors(5))
} # }