View source: R/selection_stratified.R
| compute_selection_by_strata | R Documentation |
Select planning units to meet area-based targets for each strata (e.g. country)
at each H3 resolution. Selection is based on a single score column (typically
ensemble_score) and a cross-scale index produced by
build_crossscale_index.
Resolutions are processed from finest to coarsest. At each strata and resolution, the function accounts for area already covered by selected planning units at other resolutions to avoid double-counting. Within each strata, it prefers native-resolution units that do not have finer descendants in the input dataset, using coarser units with descendants only when needed to reach the target.
The result is a single 0/1 selection that meets per-strata area targets while minimizing cross-scale overlap.
compute_selection_by_strata(
s,
cs_idx,
strata_masks,
target = 0.3,
admin_strata = "admin",
score_col = "ensemble_score",
area_col = "area_km2",
res_col = "res",
out_col = "selected_by_admin",
blocked_col = NULL,
target_by_strata = NULL
)
s |
An |
cs_idx |
A cross-scale index list as returned by
|
strata_masks |
Optional named list of logical vectors (length
|
target |
Default area-based target proportion (e.g. |
admin_strata |
Name of the column in |
score_col |
Name of the column in |
area_col |
Name of the column in |
res_col |
Name of the column in |
out_col |
Name of the output column that will be created in |
blocked_col |
Optional name of a column to store the global
"has finer descendants" flag as 0/1. If |
target_by_strata |
Optional named numeric vector of per-strata targets,
e.g. |
The input s with an additional column out_col (0/1), and
optionally blocked_col if requested.
# Tiny multiscale example with two strata (A and B)
parent1 <- "872a1072bffffff"
kids1 <- c("882a1072b1fffff", "882a1072b3fffff")
parent2 <- "872a10729ffffff"
parent3 <- "872a10774ffffff"
h3_vec <- c(parent1, parent2, parent3, kids1)
res_vec <- c(7L, 7L, 7L, 8L, 8L)
s <- data.frame(
h3_address = h3_vec,
res = res_vec,
admin = c("A", "B", "B", "A", "A"),
area_km2 = c(14, 14, 14, 2, 2),
ensemble_score = c(0.2, 0.9, 0.2, 0.8, 0.1)
)
maps <- build_h3_maps(s, res_levels = c(7L, 8L))
cs_idx <- build_crossscale_index(maps)
strata_masks <- list(
A = (s$admin == "A"),
B = (s$admin == "B")
)
out <- compute_selection_by_strata(
s, cs_idx,
strata_masks = strata_masks,
target = 0.3,
admin_strata = "admin",
score_col = "ensemble_score",
area_col = "area_km2",
res_col = "res",
out_col = "selected_by_admin",
target_by_strata = c(A = 0.12, B = 0.5)
)
out[, c("admin", "res", "area_km2", "ensemble_score", "selected_by_admin")]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.