genclust: Generate clusters for spatial clustering

View source: R/generate-clusters.R

genclustR Documentation

Generate clusters for spatial clustering

Description

Creates an undirected graph from spatial polygonal data, computes its minimum spanning tree (MST), and generates nclust clusters. This function is used to initialize cluster membership in a clustering algorithm, such as sfclust.

Usage

genclust(x, nclust = 10, weights = NULL)

Arguments

x

An sf or sfc object representing spatial polygonal data. It can also be a matrix or Matrix object with non-zero values representing weighted connectivity between units.

nclust

Integer, specifying the initial number of clusters.

weights

Optional numeric vector or matrix of weights between units in x. It should have dimensions n^2, where n is the number of units in x. If NULL, random weights are assigned.

Value

A list with three elements:

  • graph: The undirected graph object representing spatial contiguity.

  • mst: The minimum spanning tree.

  • membership: The cluster membership for elements in x.

Examples


library(sfclust)
library(sf)

x <- st_make_grid(cellsize = c(1, 1), offset = c(0, 0), n = c(3, 2))

# using distance between geometries
clust <- genclust(x, nclust = 3, weights = st_distance(st_centroid(x)))
print(clust)
plot(st_sf(x, cluster = factor(clust$membership)))

# using increasing weights
cluster_ini <- genclust(x, nclust = 3, weights = 1:36)
print(cluster_ini)
plot(st_sf(x, cluster = factor(cluster_ini$membership)))

# using on random weights
cluster_ini <- genclust(x, nclust = 3, weights = runif(36))
print(cluster_ini)
plot(st_sf(x, cluster = factor(cluster_ini$membership)))


sfclust documentation built on June 8, 2025, 10:11 a.m.