Skip to contents

Compute gap statistic for determining optimal number of clusters

Usage

tidy_gap_stat(data, FUN_cluster = NULL, max_k = 10, B = 50, nstart = 25)

Arguments

data

A data frame or tibble

FUN_cluster

Clustering function (default: uses kmeans internally)

max_k

Maximum number of clusters (default: 10)

B

Number of bootstrap samples (default: 50)

nstart

If using kmeans, number of random starts (default: 25)

Value

A list of class "tidy_gap" containing:

  • gap_data: tibble with gap statistics for each k

  • k_firstSEmax: optimal k via maxSE's firstSEmax method, the smallest k within one standard error of the first local maximum (most conservative)

  • k_globalmax: optimal k via the globalmax method, the k with the largest gap (most liberal)

  • k_firstmax: optimal k via the firstmax method, the first local maximum of the gap

  • recommended_k: recommended k (uses firstSEmax)

  • model: the clusGap result

Examples

# \donttest{
gap <- tidy_gap_stat(iris[, 1:4], max_k = 6, B = 10)
gap$recommended_k
#> [1] 6
# }