Skip to contents

Mirrors the C# JenksNaturalBreaks class: partitions a one-dimensional sample into n_clusters classes minimizing the within-class sum of squared deviations.

Usage

ml_jenks_breaks(x, n_clusters, is_data_sorted = FALSE)

Arguments

x

a numeric vector to classify.

n_clusters

the number of classes. Must be at least 1 and no more than length(x).

is_data_sorted

set TRUE to skip the internal sort when x is already ascending.

Value

a list with breaks (each class's maximum value), clusters (an n_clusters by 8 matrix with columns start_index, end_index, count, min, max, sum, average, variance; the two index columns are 0-based positions into the SORTED data), and gvf, the goodness-of-variance-fit measure, which approaches 1 for a better fit.

Details

Upstream fails on fully degenerate input: if every value of x is identical, the underlying algorithm computes a negative class boundary and errors. Any spread at all is fine, including heavily tied data.

Examples

x <- c(1, 1.2, 1.4, 8, 8.3, 8.6, 30, 31, 32)
ml_jenks_breaks(x, n_clusters = 3)$breaks
#> [1]  1.4  8.6 32.0