---
title: "Choosing a resolution"
description: >
  Decide how many cells the data support, and pass that count to the tessellation.
author: "Justin Chase"
output:
  rmarkdown::html_vignette:
    toc: true
    fig_width: 6
    fig_height: 4
vignette: >
  %\VignetteIndexEntry{Choosing a resolution}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
# gstat fits the variogram behind two of the criteria, and ggplot2 draws the
# profile. Both are Suggests, so the article is gated on them here and
# R CMD build succeeds on a machine that has neither.
# `eval` gates chunks only: knitr does not apply it to inline `r ...` code,
# which runs regardless. Inline code that reads something a chunk defines
# goes through iv(), which leaves its argument unevaluated when the chunks
# do not run, or the article fails to build without these packages.
has_gstat  <- requireNamespace("gstat",   quietly = TRUE)
has_ggplot <- requireNamespace("ggplot2", quietly = TRUE)
runs <- has_gstat && has_ggplot
iv   <- function(x) if (runs) x else "(not run)"

knitr::opts_chunk$set(
  echo      = TRUE,
  message   = FALSE,
  warning   = FALSE,
  fig.align = "center",
  out.width = "100%",
  eval      = runs
)
```

```{r missing-deps, echo=FALSE, eval=!runs, results='asis'}
cat("**gstat or ggplot2 is not installed, so the code below is shown but not",
    "run.** Install them with `install.packages(c(\"gstat\", \"ggplot2\"))`",
    "and rebuild.\n")
```

The number of cells sets what each cell value averages over. Too few cells
smooth away the variation you came to measure. Too many leave cells that hold
one observation each, where the standard error is undefined and the cell mean
is a single observation. The count also sets the variance of the cell means,
the design effect, and the size of the blocks a spatial cross-validation
holds out when it uses the cells as blocks. `resolution_profile()` scores
candidate counts and `select_resolution()` reads one off the profile. Given
the response and nothing else, they choose the count for the
[pooled value](glossary.html#pooled-value) that `summarize_by_cell()` reports by default.

*This article needs two optional packages: `gstat` for the variogram and
`ggplot2` for the figure. Where one is missing, the code that needs it is
shown but not run.*

The two calls with their defaults:

```{r default-call, eval=FALSE}
prof <- resolution_profile(pts, response_var = "z")
sel  <- select_resolution(prof)
```

`determine_optimal_levels()` reads a count off the coordinates alone, and
finds one only when the points cluster.

## A field with known structure

The examples use a simulated field, so its structure is known before anything
is fitted. The 500 points are spread uniformly over a 1,000 m square. The
field has an exponential covariance with range parameter 100 m
(`exp(-d / 100)`), so its correlation falls to 5 percent at about 300 m, three
times the range parameter, and `rnorm(n, sd = 1)` adds a nugget: measurement noise that does not
depend on distance, here of variance 1.

```{r fixture}
library(sf)
library(spatialkit)

set.seed(42)
n  <- 500
xy <- data.frame(x = 5e5 + runif(n, 0, 1000), y = 5e6 + runif(n, 0, 1000))
D  <- as.matrix(dist(xy))
xy$z <- as.numeric(t(chol(exp(-D / 100) + diag(1e-8, n))) %*% rnorm(n)) +
        rnorm(n, sd = 1)
pts  <- st_as_sf(xy, coords = c("x", "y"), crs = 32632)
```

## A size for pooled values

By default `summarize_by_cell()` reports, beside each cell's mean, a pooled
value: the cell's mean pulled toward the mean of the observations in the 8
nearest cells (nearest by the centroid of their observations), by a weight
that grows with the number of observations the cell holds.
`estimate = "pooled"` selects it, and `vignette("cells")` shows how to read
one. The count for pooled values comes from a [size](glossary.html#size),
`obs_per_cell`: the
number of observations a cell is to hold on average. A size `s` on `n` observations
gives `max(2, round(n / s))` cells. The printed output counts observations as
rows.

The default profile is one size, 4.5, scored for the pooled value, and
`select_resolution()` reads the count off it by that size:

```{r size-default}
prof_default <- resolution_profile(pts, response_var = "z")
select_resolution(prof_default)
```

The cells are density-weighted k-means cells: each point is weighted by its
local density. Density weighting gives cells of about equal counts once there
are more cells than clusters of points. At the default size that holds on
any layer whose clusters hold more than a few observations, which is why the
default size uses the density-weighted partition
(`?resolution_profile`, section "The partition").
`partition = "kmeans"` gives plain k-means cells instead.

`obs_per_cell` also takes several sizes, one [level](glossary.html#level)
each, with the centres
fitted to every row. `estimate` names the cell value the profile scores, the
pooled value unless it says otherwise:

```{r size-profile}
sizes     <- c(12, 9, 6, 4.5, 3)
prof_size <- resolution_profile(pts, response_var = "z", obs_per_cell = sizes)
as.data.frame(prof_size)[, c("levels", "scored_per_cell", "loo",
                             "share_alone", "own_weight")]
```

Three of the columns say what a size leaves a cell value to stand on.

- `loo` is the leave-one-out error of the pooled value: each observation is
  predicted by the value of its cell computed without it, and the squared
  errors are averaged. It needs no variogram. `sac = "auto"`, the default,
  fits none on a ladder of sizes, so `cp` and `reliability` are `NA` in this
  profile; `sac = NULL` fits one and fills them.
- `share_alone` is the share of the observations that are the only one in
  their cell.
- `own_weight` is the median weight a pooled value gives its cell's own mean.
  At 1 the pooled values are the plain means; at 0 each is the mean of the
  cell's neighbours.

At the default size this ladder has the same `r iv(prof_default$levels)`
cells, and the same `loo`, as the default profile. Each level's starts run on
a random stream of their own, seeded from `seed` and the level, so a level
gets the same centres on any ladder that holds it and is fitted to the same
rows.

`select_resolution(criterion = "size")` returns the level of a size. It
searches for no optimum, so the result has no
[flat region](glossary.html#flat-region) and sits on no
bound. Here it returns the default call's count:

```{r size-select}
sel_size <- select_resolution(prof_size, criterion = "size",
                              obs_per_cell = 4.5)
sel_size$best
```

```{r size-text, include=FALSE}
lowest <- prof_size$levels[which.min(prof_size$loo)]
lowest_txt <- sprintf("%d cells, %s", lowest,
                      if (lowest == max(prof_size$levels)) "the finest size offered"
                      else "which is not the finest size offered")
```

`loo` shows what each size costs, but its minimum does not pick a size. For
pooled values `loo` tends to keep falling as the cells shrink, so its minimum
tends to be the finest size offered, and the last level of the ladder would
then set the count. On 200 simulated layers of 1,500 clustered observations of
a field plus noise, profiled at seven sizes from 18 to 2, the lowest `loo` of
the pooled value was at the size 2 on 176 of the 200 layers
([`dev/sim-pooled-values.R`](https://github.com/elkronos/gis_modeling_toolkit/blob/main/dev/sim-pooled-values.R)
in the package's GitHub repository; `?resolution_profile` gives the settings).
In the table above the lowest `loo` is at
`r if (runs) lowest_txt else "a level not computed here"`. The size is
therefore the caller's choice, made on other grounds: how many observations a
cell value should stand on, which `share_alone` and `own_weight` describe.

```{r size-trade, echo=FALSE, results='asis'}
at <- function(s) {
  tab <- as.data.frame(prof_size)
  tab[tab$levels == max(2, round(nrow(pts) / s)), ]
}
alone <- function(r) {
  if (r$share_alone == 0) "no observation sits alone in its cell"
  else sprintf("%s percent of the observations sit alone in their cell",
               format(round(100 * r$share_alone, 1)))
}
coarse <- at(max(sizes)); fine <- at(min(sizes))
cat(sprintf(paste("In the table, at %s observations per cell %s and the median",
                  "own weight is %.2f; at %s, %s and it is %.2f. A larger size",
                  "gives fewer cells, whose pooled values rest more on their own",
                  "observations.\n"),
            format(max(sizes)), alone(coarse), coarse$own_weight,
            format(min(sizes)), alone(fine), fine$own_weight))
```

Under a size neither the cells, which are fitted to the coordinates, nor
their number, which is a count of rows, depend on the response, so the count
is not fitted to the values it will summarise.

A size does not ask whether the response has spatial structure the cells can
use, so the selection asks it separately. `structured` is `FALSE` when one
mean for the whole layer predicts a left-out observation as well as the cells
do or better, within `tol`. It is also `FALSE` when an F test of the cell
means against one mean gives a p-value of 0.05 or more. The count stands
either way, since a size set it and no criterion, and the functions that take
the selection as a count then warn (class `spatialkit_no_structure`) that the cell values will
say little that one mean does not.

```{r size-structured}
data.frame(structured = sel_size$structured, loo = sel_size$loo,
           one_mean = sel_size$loo_one_cell)
```

The selection goes to `get_voronoi_seeds()`, as "From a count to cells"
shows. It carries a record of how the count was set, `sel_size$count_rule`,
which the seeds, the cells and the assigned layer hand on to
`summarize_by_cell()`. The summary then checks that it reports the cell value
the count was chosen for, on the cells and rows that were scored.

## Leave-one-out for plain means

With plain means the leave-one-out error does choose a count. Each
observation is predicted by the mean of the other observations of its cell,
or, when it is alone in its cell, by the mean of the cell of its nearest other
observation. `estimate = "mean"` scores that error in the profile, and the
default `obs_per_cell = "auto"` then offers the seven sizes 18, 12, 9, 6,
4.5, 3 and 2. `estimate = "mean"` in `summarize_by_cell()` reports the plain
means without the pooled columns.

```{r loo-profile}
prof_loo <- resolution_profile(pts, response_var = "z", estimate = "mean")
as.data.frame(prof_loo)[, c("levels", "scored_per_cell", "loo", "loo_se",
                            "share_alone")]

sel_loo <- select_resolution(prof_loo, criterion = "loo")
sel_loo
```

```{r loo-text, echo=FALSE, results='asis'}
if (is.na(sel_loo$edge)) {
  cat(sprintf(paste("The lowest error is at %d cells, inside the sizes offered,",
                    "so the criterion chose it.\n"), sel_loo$best))
} else {
  cat(sprintf(paste("The lowest error is at %d cells, which is %s: an end of",
                    "the ladder chose the count, and the functions that take",
                    "the selection warn of it (class",
                    "`spatialkit_bound_chosen`).\n"), sel_loo$best, sel_loo$edge))
}
```

`sel_loo$flat` is every level whose error is within `tol` of the lowest, and
it is the answer to quote, as for any criterion. `loo_se` is the standard
error of one level's mean squared error. It is not the standard error of the
difference between two levels, since their errors come from the same
observations.

The count now depends on the values of the response, so "Choosing on one
half, estimating on the other" below applies, and `select_on = "split"` reads
`loo` on one half of the layer. The error describes locations among the
observations: it compares counts where data are near, and says nothing about
the error of a map away from them. It sets the count of plain means only; for
pooled values a size does. In the simulation described under "A size for
pooled values", the lowest `loo` of the plain mean was at the finest size on
none of the 200 layers. It was at 6 observations per cell on 86 of them, at
4.5 on 51 and at 9 on 49.

`criterion = "auto"`, the default of `select_resolution()`, is the rule that
goes with the profile's estimate: `"size"` for pooled values, `"loo"` for
plain means and `"cp"` for a profile without an estimate. A profile built
with `estimate` and passed straight to `get_voronoi_seeds()` as `n` is read
the same way. Mallows' Cp needs a variogram, which neither profile of these
two sections fitted.

## A ladder of counts

`n_levels` asks `resolution_profile()` for a ladder of cell counts in place of
a size. The profile fits every level and reports what each one costs, so you
can see the shape of the trade-off before picking a point on it. The levels
are spaced logarithmically, because cell diameter falls as \(L^{-1/2}\), with
\(L\) the number of cells. The cells are density-weighted, as in the default
profile, and the profile still scores the pooled value's `loo` at every
level; `estimate = NULL` leaves those columns out.

```{r profile}
prof <- resolution_profile(pts, response_var = "z", n_levels = 16)
print(as.data.frame(prof)[, c("levels", "cell_n_median", "cell_diam_median",
                              "cp", "reliability", "loo", "moran_z")],
      digits = 3)
```

One row per level. `cell_n_median` and `cell_diam_median` say how many points
a typical cell holds and how big it is in CRS units, here metres.
`cell_diam_median` is twice the median root-mean-square distance of a cell's
points from its centre, about 0.8 of the side of a square cell of the same
area. Compare it, with that factor in mind, against something you know about
your data, such as the spacing of a sampling grid or the size of a field.
`print(prof)` gives every column, with a header that states the ladder, its
bounds and the variogram.

### What the ladder can support

The ladder runs from 2 cells to a
[support ceiling](glossary.html#support-ceiling) the data impose. The profile
records that ceiling and, beside it, a [range floor](glossary.html#range-floor)
from the autocorrelation range:

```{r bounds}
str(attr(prof, "bounds")[c("floor", "ceiling", "ceiling_from", "supported",
                           "range")])
```

The ceiling is the smaller of `floor(n / min_cell_n)`, with `n` the rows that
carry a response, and the most centres k-means can place: the number of
distinct locations, or one fewer than the points when no location repeats. On
a layer larger than `sample_n` the fits run on a subsample and the ceiling is
at most half of it. `ceiling_from` names the bound that applied. Past the
ceiling the average cell holds too few points to estimate anything from, or
k-means has more centres to place than distinct points.

The floor is `ceiling(area / range^2)`, from the fitted autocorrelation range.
Cells wider than the range average over more than one patch of the field,
mixing values the field itself keeps apart. The range is an estimate, so by
default the floor is reported and not applied: it is a level of the ladder,
`select_resolution(criterion = "reliability")` reads from it up, and
`range_floor = TRUE` starts the ladder there.

When the floor exceeds the ceiling, the data cannot support a tessellation
that respects their own correlation structure. `supported` is `FALSE`, a
warning says so, and the ladder still runs from 2 to the ceiling so the cost
of each level stays visible. The usual causes are too few points for the
extent, or an autocorrelation range shorter than the spacing between
observations.

When the layer supports the floor but the ceiling that `sample_n` sets lies
below it, the fault is the subsample, not the data. `supported` stays
`TRUE`, and a warning of class `spatialkit_floor_above_ceiling` asks for a
larger `sample_n`. In either case the bounds carry `floor_applied = FALSE`.
On a ladder the profile built itself,
`select_resolution(criterion = "reliability")` then says in its `note` that
the floor was not held.

### Four criteria and a diagnostic

```{r criteria-table, echo=FALSE, results='asis'}
cat(
"| criterion | measures | needs | direction |\n",
"|---|---|---|---|\n",
"| `elbow` | how far the log of the within-cluster sum of squares (WSS) of the coordinates sags below a power law (the straight log-log line from one cell to the last level); `NA` at every level when the points have no cluster structure | coordinates only | larger |\n",
"| `cp` | Mallows' \\(C_p\\) of the piecewise-constant approximation of the response by cell means: the fit of the cell means, penalised for the number of cells | a response, and a variogram for the nugget | smaller |\n",
"| `moran_z` | Moran's z of the cell means (of their regression residuals when predictors are given): whether neighbouring cells resemble each other | a response | none: a diagnostic, not a count rule |\n",
"| `reliability` | the share of the spread in the cell means that is signal rather than sampling noise | a variogram | larger |\n",
"| `loo` | the leave-one-out error of the cell value the profile scores, the pooled value by default | a response | smaller |\n",
sep = "")
```

`elbow` sees the coordinates and knows nothing about what you measured. `cp`
and `reliability` split the practical question in half: how well the cells
represent the field, and whether the cell values can be told apart from
noise. What `cp` estimates is the error of re-measuring the sampled sites,
each predicted by the mean of its cell, with the variogram's nugget as the
noise of one measurement. For a new location the penalty would also need the
spread of the field inside a cell, which the nugget leaves out. `moran_z`
grows as the cells get finer than the range, because neighbouring cells then
resemble each other, and at the coarsest counts where it is computed it is
near zero whatever the data. It does not say how well the cells represent the
response, so `select_resolution()` refuses it unless asked with
`diagnostic = TRUE`, and `summary()` on the profile
(`?summary.resolution_profile`) leaves it out unless it is named.

`plot(prof)` draws the criteria against the number of cells;
`?plot.resolution_profile` describes the panels.

```{r plot-alt, echo=FALSE}
oxford <- function(x) if (length(x) < 2L) as.character(x) else
  paste(paste(x[-length(x)], collapse = ", "), "and", x[length(x)])
picks <- function(cr) select_resolution(prof, criterion = cr)$best
shown <- c(cp = "Mallows' Cp", reliability = "reliability",
           loo = "the leave-one-out error")
shown <- shown[vapply(names(shown), function(cr) any(is.finite(prof[[cr]])),
                      logical(1))]
plot_alt <- sprintf(paste(
  "Stacked panels against the number of cells, from %d to %d: %s of the",
  "pooled value, and the absolute Moran's z of the cell means. A dot and a",
  "dotted line mark the level selected for %s, with a shaded band over each",
  "flat region; the Moran panel has no selected level."),
  min(prof$levels), max(prof$levels), oxford(shown),
  oxford(sprintf("%s at %d cells", shown,
                 vapply(names(shown), picks, numeric(1)))))
```

```{r plot-profile, fig.height=8, fig.alt=plot_alt}
plot(prof)
```

```{r cp-context, echo=FALSE}
vg  <- attr(prof, "variogram")
nug <- round(vg$nugget, 2); psl <- round(vg$psill, 2)
cp_sel <- select_resolution(prof, criterion = "cp")
```

`cp` can stop inside the ladder only when there is a nugget. On a smooth
field the piecewise-constant approximation keeps improving as cells shrink,
the penalty is too small to stop it, and the minimum lands wherever
`min_cell_n` stops the ladder. The support ceiling then does the choosing,
and `select_resolution()` flags it.

```{r cp-text, echo=FALSE, results='asis'}
cat(sprintf("This fixture has a fitted nugget of %s against a partial sill of %s,", nug, psl),
    if (isTRUE(cp_sel$at_ceiling))
      "and `cp` still falls to the last level of the ladder: its minimum is the support ceiling, as `summary()` says below.\n"
    else
      "and `cp` turns around inside the ladder.\n")
```

`reliability` is highest at a coarse count, because bigger cells hold more
points and average away more noise. Here its maximum is at
`r if (runs) prof$levels[which.max(prof$reliability)] else NA` cells, below the
range floor of `r if (runs) attr(prof, "bounds")$floor else NA`, where cells
are wider than the range. `select_resolution()` therefore
reads it from the floor up, and then returns the floor, a bound on the
analysis that the range sets.

### Choosing, and the flat region

`select_resolution()` picks the level a criterion prefers and reports the
flat region around it. On a ladder of counts, name the criterion. With none,
`select_resolution()` applies the profile's default rule, 4.5 observations
per cell for pooled values:

```{r auto-on-ladder, warning=TRUE}
auto <- select_resolution(prof)
auto$best
```

```{r auto-text, echo=FALSE, results='asis'}
asked <- max(2, round(nrow(pts) / 4.5))
if (asked %in% prof$levels) {
  cat(sprintf("That asks for %d cells, a level of this ladder, so the call returns it.\n",
              asked))
} else {
  cat(sprintf(paste("That asks for %d cells, which this ladder does not have, so",
                    "the call warns (class `spatialkit_size_not_reached`) and",
                    "returns the nearest level, %d.\n"), asked, auto$best))
}
```

```{r select}
sel <- select_resolution(prof, criterion = "reliability")
sel
```

`sel$best` is the optimum and `sel$flat` is every level within `tol` of it.
Quote the flat region when you write the analysis up. `tol` (default 0.02)
is relative to the optimum's value for `cp`, `reliability` and `loo`, and
relative to the criterion's range for `elbow` and `moran_z`, whose optima can
be zero.

Two flags say when a bound chose instead of the criterion:

```{r flags}
c(at_floor = sel$at_floor, at_ceiling = sel$at_ceiling)
```

```{r flag-text, echo=FALSE, results='asis'}
# Read in "From a count to cells", where the selection becomes seeds.
edge     <- sel$edge
on_bound <- if (is.null(edge) || is.na(edge)) " (this one does not)" else
  sprintf(", as this one does at %s", sub(" \\(.*\\)$", "", edge))
if (isTRUE(sel$at_floor)) {
  cat("`at_floor` is `TRUE` here. The reliability curve keeps rising toward",
      "coarser counts, and the floor from the autocorrelation range stops it",
      "there, so", sel$best, "is the coarsest count the range allows, set by",
      "the floor and not by the criterion; quote it as a bound, not as the",
      "criterion's choice. Changing `tol` will not move it.\n")
} else if (isTRUE(sel$at_ceiling)) {
  cat("`at_ceiling` is `TRUE` here: the support ceiling stopped the criterion,",
      "so take the number as a bound on the analysis.\n")
} else {
  cat("Both are `FALSE` here, so the optimum sits inside the ladder and the",
      "criterion chose it.\n")
}
```

Criteria can prefer different levels and still share some. `summary()` reads
the count criteria side by side and closes with the levels that every flat
region contains:

```{r compare-criteria}
summary(prof)
```

```{r flat-text, echo=FALSE, results='asis'}
# A flat region is a set: the curves are not monotone, so a flat region can
# skip levels. The sentences below read the rendered summary rather than
# quoting numbers that a different platform's k-means or variogram fit could
# move.
s      <- summary(prof)
lad    <- attr(s, "ladder")
flats  <- attr(s, "bands")[s$criterion]
common <- attr(s, "common")
pos    <- lapply(flats, function(b) sort(match(b, lad)))
holed  <- names(pos)[vapply(pos, function(p) any(diff(p) > 1L), logical(1))]
solid  <- names(pos)[vapply(pos, function(p) length(p) > 1L && all(diff(p) == 1L),
                            logical(1))]
example <- if (length(holed)) {
  p <- pos[[holed[1L]]]
  skipped <- lad[setdiff(seq(min(p), max(p)), p)]
  sprintf("the flat region of `%s` above holds %s and leaves out the %s between them.",
          holed[1L], oxford(lad[p]), oxford(skipped))
} else {
  "a flat region that skips a level prints as a comma-separated list."
}
run <- if (length(solid)) {
  p <- pos[[solid[1L]]]
  sprintf("A run of %d adjacent levels prints as a range, as `%s` does with `%d to %d`.",
          length(p), solid[1L], lad[min(p)], lad[max(p)])
} else "A run of adjacent levels prints as a range."
cat("A flat region is a set of levels and can skip some, because the criterion",
    "curves are not monotone:", example, run,
    "`attr(summary(prof), \"common\")` returns the levels that every flat region",
    "contains, for use in code, and `attr(summary(prof), \"bands\")` each",
    "criterion's flat region in full.",
    if (length(common))
      "Those common levels are counts that every criterion accepts.\n"
    else
      paste("Here no level is in every flat region, so no single count satisfies",
            "every criterion, and you have to say which question your analysis",
            "asks.\n"))
```

`summary()` reports each criterion's best level and flat region side by side
and does not choose among them.

## An elbow from the coordinates alone

`determine_optimal_levels()` reads the coordinates and no response. It
returns a few candidate cell counts, best first, when the WSS curve of a
k-means clustering of the points has an elbow, and `NA` when it has none. The
curve runs from 1 cell to `max_levels`. On points drawn around six centres it
finds the elbow:

```{r elbow-clustered}
set.seed(1)
centre <- data.frame(x = runif(6, 100, 900), y = runif(6, 100, 900))
k      <- sample(6, 300, replace = TRUE)
clus   <- st_as_sf(data.frame(x = 5e5 + centre$x[k] + rnorm(300, sd = 25),
                              y = 5e6 + centre$y[k] + rnorm(300, sd = 25)),
                   coords = c("x", "y"), crs = 32632)
determine_optimal_levels(clus, max_levels = 40)
```

The fixture's points are uniform, so their WSS falls like `c / k` all the way
down and bends nowhere:

```{r doi, warning=TRUE}
lv <- determine_optimal_levels(pts, max_levels = 40)
lv
```

The call returns `NA`, with `structured = FALSE` and the largest sag of the
curve as attributes, and the warning of class `spatialkit_no_elbow` says why.
`on_no_elbow = "warn"` returns the count the rule lands on instead, with the
same warning, which says that `max_levels` chose it: a larger `max_levels`
would give a larger answer. On points like these, use the profile.

When there is an elbow, `lv` is what `get_voronoi_seeds()` takes as `n`, and
"From a count to cells" below names the other arguments that take it. Given
an `NA` result itself, `get_voronoi_seeds()` and `build_tessellation()` stop
and say where it came from. `lv[1]` drops the attributes that tell them, and
they then say only that `NA` is not a number of cells.

`criterion = "morans_i"` ranks the counts near the elbow by Moran's z of the
cell means, a diagnostic. `criterion = "combined"` keeps the elbow's ranking
of those counts and reports Moran's I beside it. Both need a `response_var`,
and `?determine_optimal_levels` covers them. The ladder of
`determine_optimal_levels()` has no floor from the autocorrelation range, so
it can prefer cells wider than the range. That is one reason it can disagree
with `select_resolution(criterion = "reliability")`, which reads from the
floor up.

## Choosing on one half, estimating on the other

Picking the level from the same response you then aggregate is a form of
double-dipping: the cell count was chosen to suit one realisation of the
field. `select_on = "split"` profiles part of the layer and hands back the
row positions of both parts.

```{r split}
prof_split <- resolution_profile(pts, response_var = "z", n_levels = 16,
                                 select_on = "split")
halves <- attr(prof_split, "split")
halves
```

The split is spatially blocked, and records the method and seed that produced
it. A random half would put neighbours of every selection point in the
estimation set, and on an autocorrelated field that leaks the structure you
are choosing against. Blocking reduces that leak without removing it: the two
halves share a border, and points on either side of it within the
autocorrelation range are still correlated.

Choose from `prof_split`, then aggregate the rows in `halves$estimation`. The
cells on the ladder are still drawn on every point, so the count it picks is
a count for the whole layer, and the ladder is as long as the full profile's.
Only the steps that read the response (the variogram, `cp`, `moran_z`, `loo`)
use the selection half. Those steps see half the data, so the profile is
noisier and its flat regions can be wider. The estimation
rows also fill only their own half of the layer. A cell inside the selection
half gets none of them, and a cell across the border between the halves is
estimated from the part of it on the estimation side. Read standard errors
only off cells whose points are all estimation rows.

```{r split-ladder}
str(attr(prof_split, "bounds")[c("floor", "ceiling", "n", "supported")])
```

The floor can differ from the full profile's, because it comes from the range
estimated on the selection half.

## From a count to cells

`n` in `get_voronoi_seeds()`, `approx_n_cells` in `build_tessellation()` and
`target_cells` in `create_grid_polygons()` each take a plain number or what
`determine_optimal_levels()`, `resolution_profile()` or `select_resolution()`
returns. Of the candidates `determine_optimal_levels()` ranks, they use the
first. `build_tessellation()` reads `approx_n_cells` for its `"hex"` and
`"square"` methods.

For Voronoi cells, hand the `select_resolution()` result to
`get_voronoi_seeds()` as `n`. It returns the centres of the partition the
profile scored, and a k-means partition, weighted or not, is the Voronoi
partition of its centres, so the Voronoi cells of those seeds are the cells
the criteria judged. The bare count `sel$best` would run a new k-means and
give other cells. When the selection sits on a
bound`r if (runs) on_bound else ""`, the call warns that a bound chose the
count (class `spatialkit_bound_chosen`). `build_tessellation()` then builds
the cells on the seeds:

```{r next-step, warning=TRUE}
bnd   <- clip_target_for(pts, expand = 0.02, quiet = TRUE)
seeds <- get_voronoi_seeds(boundary = bnd, method = "kmeans", n = sel,
                           sample_points = pts)
cells <- build_tessellation(seeds, boundary = bnd, method = "voronoi",
                            quiet = TRUE)
nrow(cells$cells)
```

A Voronoi tessellation grows one cell per seed, so its count is set when you
place the seeds. Passing `approx_n_cells` with `method = "voronoi"` warns and
is ignored, and without the seeding step you get one cell per observation, a
nearest-neighbour interpolation of the points.

For a hex or square grid the selection goes to `build_tessellation()` itself,
which gives the same warning on a bound, and the points go in directly:

```{r next-step-grid, warning=TRUE}
hex <- build_tessellation(pts, boundary = bnd, method = "hex",
                          approx_n_cells = sel, quiet = TRUE)
nrow(hex$cells)
```

```{r grid-text, echo=FALSE, results='asis'}
cat(sprintf(paste("The hex grid has %d cells for a request of %d. The target",
                  "is adjusted for hexagonal packing, and the hexagons the",
                  "boundary cuts are kept as cells, so `approx_n_cells` is",
                  "approximate. The seeded Voronoi tessellation has %s, because",
                  "the seeds are the cells.\n"),
            nrow(hex$cells), sel$best,
            if (nrow(cells$cells) == sel$best) "exactly the count asked for"
            else sprintf("%d", nrow(cells$cells))))
```

## Next

The next article in reading order is `vignette("cells")`, which draws the
cells, puts each observation in one and reports each cell's value with the
right standard error. This article relied on `?resolution_profile`,
`?select_resolution`, `?summary.resolution_profile`,
`?plot.resolution_profile`, `?determine_optimal_levels`,
`?get_voronoi_seeds` and `?build_tessellation`. `?resolution_profile` gives
each criterion's definition and its references, and
`?determine_optimal_levels` explains why its Moran's I criteria need more
than nine cells. `vignette("glossary")` defines the terms this article uses.
