View source: R/generate-clusters.R
genclust | R Documentation |
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
.
genclust(x, nclust = 10, weights = NULL)
x |
An |
nclust |
Integer, specifying the initial number of clusters. |
weights |
Optional |
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
.
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)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.