| Title: | G-means Clustering |
|---|---|
| Description: | Gaussian-means (G-means) clustering is a clustering algorithm that extends the k-means algorithm by automatically determining the number of clusters. |
| Authors: | Maximilian Mücke [aut, cre] (ORCID: <https://orcid.org/0009-0000-9432-9795>) |
| Maintainer: | Maximilian Mücke <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.0.9000 |
| Built: | 2026-10-09 21:10:00 UTC |
| Source: | https://github.com/m-muecke/gmeans |
Perform the Anderson-Darling normality test.
ad.test(x)ad.test(x)
x |
( |
The Anderson-Darling test is an EDF omnibus test for the composite hypothesis of normality. The test statistic is
where . Here, is the cumulative
distribution function of the standard normal distribution, and and are mean
and standard deviation of the data values. The p-value is computed from the modified statistic
according to Table 4.9 in Stephens (1986).
A list inheriting from classes "htest" containing the following components:
statistic: the value of the statistic.
p.value: the p-value of the test.
method: the character string "Anderson-Darling normality test".
data.name: a character string giving the name(s) of the data.
Adapted from nortest::ad.test()
Stephens, A. M (1986). “Goodness-of-Fit-Techniques.” In D'Agostino, B. R (eds.), chapter Tests based on EDF statistics. CRC Press.
Thode, C. H (2002). Testing for normality, 1 edition. CRC Press. doi:10.1201/9780203910894.
stats::shapiro.test() for performing the Shapiro-Wilk test for normality.
nortest::cvm.test(), nortest::lillie.test(), nortest::pearson.test(),
nortest::sf.test() for performing further tests for normality. stats::qqnorm() for
producing a normal quantile-quantile plot.
set.seed(123) ad.test(rnorm(100, mean = 5, sd = 3)) ad.test(runif(100, min = 2, max = 4))set.seed(123) ad.test(rnorm(100, mean = 5, sd = 3)) ad.test(runif(100, min = 2, max = 4))
Compute Within-Cluster Sum of Squares
compute_wss(object, newdata = NULL)compute_wss(object, newdata = NULL)
object |
( |
newdata |
( |
WSS is defined as
where is a data point and is the centroid of the cluster to which
is assigned. When new data is provided, the function predicts the nearest cluster for
each new observation and computes the WSS for these points based on their predicted clusters.
A numeric() vector with one within-cluster sum of squares per cluster, in the order of
the rows of object$centers. Clusters with no assigned points contribute 0.
km <- kmeans(mtcars, 5) compute_wss(km) # or with new data compute_wss(km, mtcars)km <- kmeans(mtcars, 5) compute_wss(km) # or with new data compute_wss(km, mtcars)
Perform G-means clustering on a data matrix.
gmeans(x, k_init = 1L, k_max = Inf, level = 1e-04, iter.max = 100L, ...)gmeans(x, k_init = 1L, k_max = Inf, level = 1e-04, iter.max = 100L, ...)
x |
( |
k_init |
( |
k_max |
( |
level |
( |
iter.max |
( |
... |
( |
The G-means clustering algorithm is an extension of the traditional k-means algorithm that
automatically determines the number of clusters by iteratively testing the Gaussianity of data
within clusters. The process begins with a specified initial number of clusters (k_init) and
iteratively increases the number of clusters until it reaches the specified maximum (k_max) or
the data within clusters is determined to be Gaussian at the specified significance level
(level).
The algorithm is outlined as follows:
Let be the initial set of centers (usually ).
Perform k-means clustering on the dataset using the current set of centers ,
i.e., .
For each center , identify the set of data points
that are assigned to .
Use the Anderson-Darling test to check if the set of data points
follows a Gaussian distribution at the confidence
level .
If the data points appear Gaussian, keep . Otherwise, replace with two new
centers, found by k-means on the cluster started from ,
where is the main principal component of the cluster and its eigenvalue.
Repeat from step 2 until no more centers are added.
An object of class c("gmeans", "kmeans") with the components of a stats::kmeans()
object plus k_init, k_max, and level, the settings used to fit the model. See
gmeans_tidiers for summarizing the result as data frames.
Hamerly, Greg, Elkan, Charles (2003). “Learning the k in k-means.” In Thrun S, Saul L, Schölkopf B (eds.), Advances in Neural Information Processing Systems, volume 16. https://proceedings.neurips.cc/paper_files/paper/2003/file/234833147b97bb6aed53a8f4f1c7a7d8-Paper.pdf.
set.seed(123) x <- rbind( matrix(rnorm(100, sd = 0.3), ncol = 2), matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2) ) colnames(x) <- c("x", "y") cl <- gmeans(x)set.seed(123) x <- rbind( matrix(rnorm(100, sd = 0.3), ncol = 2), matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2) ) colnames(x) <- c("x", "y") cl <- gmeans(x)
Summarize a G-means clustering model as data frames, following the conventions of the broom package. The generics come from the generics package, so load it or broom to call the methods.
## S3 method for class 'gmeans' tidy(x, col.names = colnames(x$centers), ...) ## S3 method for class 'gmeans' augment(x, data, ...) ## S3 method for class 'gmeans' glance(x, ...)## S3 method for class 'gmeans' tidy(x, col.names = colnames(x$centers), ...) ## S3 method for class 'gmeans' augment(x, data, ...) ## S3 method for class 'gmeans' glance(x, ...)
x |
( |
col.names |
( |
... |
( |
data |
( |
A data.frame():
tidy(): one row per cluster with the centers, size, withinss, and cluster.
augment(): data with a .cluster factor giving the cluster assignments.
glance(): one row with k, k_init, k_max, level, totss, tot.withinss, betweenss,
and iter.
library(generics) x <- as.matrix(iris[, -5]) cl <- gmeans(x) tidy(cl) glance(cl) head(augment(cl, x))library(generics) x <- as.matrix(iris[, -5]) cl <- gmeans(x) tidy(cl) glance(cl) head(augment(cl, x))
Predicted values based on the G-means clustering model.
## S3 method for class 'gmeans' predict( object, newdata, method = c("euclidean", "manhattan", "minkowski"), p = 2, ... )## S3 method for class 'gmeans' predict( object, newdata, method = c("euclidean", "manhattan", "minkowski"), p = 2, ... )
object |
( |
newdata |
( |
method |
( |
p |
( |
... |
( |
The predict method for G-means clustering assigns new data points to the nearest cluster center
identified by the G-means algorithm. The method uses the specified distance metric to calculate
the distance between each new data point and all cluster centers, and then assigns each point to
the cluster with the closest center.
The method argument specifies the distance metric to use. The following options:
"euclidean": The Euclidean distance is the default metric used in the k-means
and is defined as
"manhattan": The Manhattan distance is defined as
"minkowski": The Minkowski distance is defined as
where is a parameter that defines the distance type (e.g., for Euclidean,
for Manhattan).
An integer() vector with one cluster index per row of newdata.
Adapted from clue
clue::cl_predict() to predict on a plain stats::kmeans() object.
x <- as.matrix(iris[, -5]) cl <- gmeans(x) newdata <- x[1:10, ] predict(cl, newdata)x <- as.matrix(iris[, -5]) cl <- gmeans(x) newdata <- x[1:10, ] predict(cl, newdata)
Summarize the result of a G-means clustering: the number of clusters found, the settings used to fit the model, a per-cluster table of sizes, within-cluster sums of squares and centers, and the overall sums of squares.
## S3 method for class 'gmeans' summary(object, ...) ## S3 method for class 'summary.gmeans' print(x, digits = max(3L, getOption("digits") - 3L), ...)## S3 method for class 'gmeans' summary(object, ...) ## S3 method for class 'summary.gmeans' print(x, digits = max(3L, getOption("digits") - 3L), ...)
object |
( |
... |
( |
x |
( |
digits |
( |
summary() returns an object of class "summary.gmeans", a list with components:
k, k_init, k_max, level: the number of clusters found and the settings used to fit the
model.
clusters: a data.frame() with one row per cluster and the columns cluster, size,
withinss, followed by the cluster centers.
totss, tot.withinss, betweenss, iter: as in stats::kmeans().
print() returns x invisibly.
x <- as.matrix(iris[, -5]) cl <- gmeans(x) summary(cl)x <- as.matrix(iris[, -5]) cl <- gmeans(x) summary(cl)