n_clusters | R Documentation |
Similarly to n_factors()
for factor / principal component analysis,
n_clusters()
is the main function to find out the optimal numbers of clusters
present in the data based on the maximum consensus of a large number of
methods.
Essentially, there exist many methods to determine the optimal number of
clusters, each with pros and cons, benefits and limitations. The main
n_clusters
function proposes to run all of them, and find out the number of
clusters that is suggested by the majority of methods (in case of ties, it
will select the most parsimonious solution with fewer clusters).
Note that we also implement some specific, commonly used methods, like the Elbow or the Gap method, with their own visualization functionalities. See the examples below for more details.
n_clusters(
x,
standardize = TRUE,
include_factors = FALSE,
package = c("easystats", "NbClust", "mclust"),
fast = TRUE,
nbclust_method = "kmeans",
n_max = 10,
...
)
n_clusters_elbow(
x,
standardize = TRUE,
include_factors = FALSE,
clustering_function = stats::kmeans,
n_max = 10,
...
)
n_clusters_gap(
x,
standardize = TRUE,
include_factors = FALSE,
clustering_function = stats::kmeans,
n_max = 10,
gap_method = "firstSEmax",
...
)
n_clusters_silhouette(
x,
standardize = TRUE,
include_factors = FALSE,
clustering_function = stats::kmeans,
n_max = 10,
...
)
n_clusters_dbscan(
x,
standardize = TRUE,
include_factors = FALSE,
method = c("kNN", "SS"),
min_size = 0.1,
eps_n = 50,
eps_range = c(0.1, 3),
...
)
n_clusters_hclust(
x,
standardize = TRUE,
include_factors = FALSE,
distance_method = "correlation",
hclust_method = "average",
ci = 0.95,
iterations = 100,
...
)
x |
A data frame. |
standardize |
Standardize the dataframe before clustering (default). |
include_factors |
Logical, if |
package |
Package from which methods are to be called to determine the
number of clusters. Can be |
fast |
If |
nbclust_method |
The clustering method (passed to |
n_max |
Maximal number of clusters to test. |
... |
Arguments passed to or from other methods. For instance, when
|
clustering_function , gap_method |
Other arguments passed to other
functions. |
method , min_size , eps_n , eps_range |
Arguments for DBSCAN algorithm. |
distance_method |
The distance method (passed to |
hclust_method |
The hierarchical clustering method (passed to |
ci |
Confidence Interval (CI) level. Default to |
iterations |
The number of bootstrap replicates. This only apply in the case of bootstrapped frequentist models. |
There is also a plot()
-method implemented in the see-package.
library(parameters)
# The main 'n_clusters' function ===============================
if (require("mclust", quietly = TRUE) && require("NbClust", quietly = TRUE) &&
require("cluster", quietly = TRUE) && require("see", quietly = TRUE)) {
n <- n_clusters(iris[, 1:4], package = c("NbClust", "mclust")) # package can be "all"
n
summary(n)
as.data.frame(n) # Duration is the time elapsed for each method in seconds
plot(n)
# The following runs all the method but it significantly slower
# n_clusters(iris[1:4], standardize = FALSE, package = "all", fast = FALSE)
}
x <- n_clusters_elbow(iris[1:4])
x
as.data.frame(x)
plot(x)
#
# Gap method --------------------
if (require("see", quietly = TRUE) &&
require("cluster", quietly = TRUE) &&
require("factoextra", quietly = TRUE)) {
x <- n_clusters_gap(iris[1:4])
x
as.data.frame(x)
plot(x)
}
#
# Silhouette method --------------------------
if (require("factoextra", quietly = TRUE)) {
x <- n_clusters_silhouette(iris[1:4])
x
as.data.frame(x)
plot(x)
}
#
if (require("dbscan", quietly = TRUE)) {
# DBSCAN method -------------------------
# NOTE: This actually primarily estimates the 'eps' parameter, the number of
# clusters is a side effect (it's the number of clusters corresponding to
# this 'optimal' EPS parameter).
x <- n_clusters_dbscan(iris[1:4], method = "kNN", min_size = 0.05) # 5 percent
x
head(as.data.frame(x))
plot(x)
x <- n_clusters_dbscan(iris[1:4], method = "SS", eps_n = 100, eps_range = c(0.1, 2))
x
head(as.data.frame(x))
plot(x)
}
#
# hclust method -------------------------------
if (require("pvclust", quietly = TRUE)) {
# iterations should be higher for real analyses
x <- n_clusters_hclust(iris[1:4], iterations = 50, ci = 0.90)
x
head(as.data.frame(x), n = 10) # Print 10 first rows
plot(x)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.