| Title: | Evaluation of Clustering Algorithms |
|---|---|
| Description: | An R package that provides a suite of tools to evaluate clustering algorithms, clusterings, and individual clusters. Compatibility updates by Renova. |
| Authors: | John A. Ramey [aut, cre] |
| Maintainer: | John A. Ramey <[email protected]> |
| License: | MIT |
| Version: | 0.1.9001 |
| Built: | 2026-11-19 06:38:32 UTC |
| Source: | https://github.com/renova-r/clusteval |
This function creates a list of indices for a stratified
nonparametric bootstrap. Corresponding to our Cluster
Omission Stability statistic implemented in
clustomit, we omit each group in turn and
perform a stratified bootstrap without the group. We
denote the number of groups as num_clusters, which
is equal to nlevels(factor(y)). Specifically,
suppose that we omit the th group. That is, we
ignore all of the observations corresponding to group
. Then, we sample with replacement from each of
the remaining groups (i.e., every group except for group
), yielding a set of bootstrap indices.
boot_stratified_omit(y, num_reps = 50)boot_stratified_omit(y, num_reps = 50)
y |
a vector that denotes the grouping of each
observation. It must be coercible with |
num_reps |
the number of bootstrap replications to use for each group |
The returned list contains
elements.
named list containing indices for each bootstrap replication
set.seed(42) # We use 4 clusters, each with up to 10 observations. The sample sizes are # randomly chosen. num_clusters <- 4 sample_sizes <- sample(10, num_clusters, replace = TRUE) # Create the cluster labels, y. y <- unlist(sapply(seq_len(num_clusters), function(k) { rep(k, sample_sizes[k]) })) # Use 20 reps per group. boot_stratified_omit(y, num_reps = 20) # Use the default number of reps per group. boot_stratified_omit(y)set.seed(42) # We use 4 clusters, each with up to 10 observations. The sample sizes are # randomly chosen. num_clusters <- 4 sample_sizes <- sample(10, num_clusters, replace = TRUE) # Create the cluster labels, y. y <- unlist(sapply(seq_len(num_clusters), function(k) { rep(k, sample_sizes[k]) })) # Use 20 reps per group. boot_stratified_omit(y, num_reps = 20) # Use the default number of reps per group. boot_stratified_omit(y)
For two clusterings of the same data set, this function calculates the similarity statistic specified of the clusterings from the comemberships of the observations. Basically, the comembership is defined as the pairs of observations that are clustered together.
cluster_similarity(labels1, labels2, similarity = c("jaccard", "rand"), method = "independence")cluster_similarity(labels1, labels2, similarity = c("jaccard", "rand"), method = "independence")
labels1 |
a vector of |
labels2 |
a vector of |
similarity |
the similarity statistic to calculate |
method |
the model under which the statistic was derived |
To calculate the similarity, we compute the 2x2 contingency table, consisting of the following four cells:
the number of observation pairs where both observations are comembers in both clusterings
the number of observation pairs where the observations are comembers in the first clustering but not the second
the number of observation pairs where the observations are comembers in the second clustering but not the first
the number of observation pairs where neither pair are comembers in either clustering
Currently, we have implemented the following similarity statistics:
Rand index
Jaccard coefficient
To compute the contingency table, we use the
comembership_table function.
the similarity between the two clusterings
# Notice that the number of comemberships is 'n choose 2'. iris_kmeans <- kmeans(iris[, -5], centers = 3)$cluster iris_hclust <- cutree(hclust(dist(iris[, -5])), k = 3) cluster_similarity(iris_kmeans, iris_hclust)# Notice that the number of comemberships is 'n choose 2'. iris_kmeans <- kmeans(iris[, -5], centers = 3)$cluster iris_hclust <- cutree(hclust(dist(iris[, -5])), k = 3) cluster_similarity(iris_kmeans, iris_hclust)
An R package that provides a suite of tools to evaluate clustering algorithms, clusterings, and individual clusters.
We provide an implementation of the ClustOmit statistic, which is an approach to evaluating the stability of a clustering determined by a clustering algorithm. As discussed by Hennig (2007), arguably a stable clustering is one in which a perturbation of the original data should yield a similar clustering. However, if a perturbation of the data yields a large change in the clustering, the original clustering is considered unstable. The ClustOmit statistic provides an approach to detecting instability via a stratified, nonparametric resampling scheme. We determine the stability of the clustering via the similarity statistic specified (by default, the Jaccard coefficient).
clustomit(x, num_clusters, cluster_method, similarity = c("jaccard", "rand"), weighted_mean = TRUE, num_reps = 50, num_cores = getOption("mc.cores", 2), ...)clustomit(x, num_clusters, cluster_method, similarity = c("jaccard", "rand"), weighted_mean = TRUE, num_reps = 50, num_cores = getOption("mc.cores", 2), ...)
x |
data matrix with |
num_clusters |
the number of clusters to find with
the clustering algorithm specified in
|
cluster_method |
a character string or a function
specifying the clustering algorithm that will be used.
The method specified is matched with the
|
similarity |
the similarity statistic that is used to compare the original clustering (after a single cluster and its observations have been omitted) to its resampled counterpart. Currently, we have implemented the Jaccard and Rand similarity statistics and use the Jaccard statistic by default. |
weighted_mean |
logical value. Should the aggregate
similarity score for each bootstrap replication be
weighted by the number of observations in each of the
observed clusters? By default, yes (i.e., |
num_reps |
the number of bootstrap replicates to draw for each omitted cluster |
num_cores |
the number of coures to use. If 1 core
is specified, then |
... |
additional arguments passed to the function
specified in |
To compute the ClustOmit statistic, we first cluster the
data given in x into num_clusters clusters
with the clustering algorithm specified in
cluster_method. We then omit each cluster in turn
and all of the observations in that cluster. For the
omitted cluster, we resample from the remaining
observations and cluster the resampled observations into
num_clusters - 1 clusters again using the
clustering algorithm specified in cluster_method.
Next, we compute the similarity between the cluster
labels of the original data set and the cluster labels of
the bootstrapped sample. We approximate the sampling
distribution of the ClustOmit statistic using a
stratified, nonparametric bootstrapping scheme and use
the apparent variability in the approximated sampling
distribution as a diagnostic tool for further evaluation
of the proposed clusters. By default, we utilize the
Jaccard similarity coefficient in the calculation of the
ClustOmit statistic to provide a clear interpretation of
cluster assessment. The technical details of the
ClustOmit statistic can be found in our forthcoming
publication entitled "Cluster Stability Evaluation of
Gene Expression Data."
The ClustOmit cluster stability statistic is based on the cluster omission admissibility condition from Fisher and Van Ness (1971), who provide decision-theoretic admissibility conditions that a reasonable clustering algorithm should satisfy. The guidelines from Fisher and Van Ness (1971) establish a systematic foundation that is often lacking in the evaluation of clustering algorithms. The ClustOmit statistic is our proposed methodology to evaluate the cluster omission admissibility condition from Fisher and Van Ness (1971).
We require a clustering algorithm function to be
specified in the argument cluster_method. The
function given should accept at least two arguments:
matrix of observations to cluster
the number of clusters to find
additional arguments that can be passed on
Also, the function given should return only clustering
labels for each observation in the matrix x. The
additional arguments specified in ... are useful
if a wrapper function is used: see the example below for
an illustration.
object of class clustomit, which contains a named
list with elements
vector of the aggregated similarity statistics for each bootstrap replicate
list containing the bootstrapped similarity scores for each cluster omitted
the clustering labels determined for
the observations in x
the number of clusters found
the similarity statistic used for comparison between the original clustering and the resampled clusterings
Fisher, L. and Van Ness, J. (1971), Admissible Clustering Procedures, _Biometrika_, 58, 1, 91-104.
Hennic, C. (2007), Cluster-wise assessment of cluster stability, _Computational Statistics and Data Analysis_, 52, 258-271. http://www.jstor.org/stable/2334320
# First, we create a wrapper function for the K-means clustering algorithm # that returns only the clustering labels for each observation (row) in # \code{x}. kmeans_wrapper <- function(x, num_clusters, num_starts = 10, ...) { kmeans(x = x, centers = num_clusters, nstart = num_starts, ...)$cluster } # For this example, we generate five multivariate normal populations with the # \code{sim_data} function. x <- sim_data("normal", delta = 1.5, seed = 42)$x clustomit_out <- clustomit(x = x, num_clusters = 4, cluster_method = "kmeans_wrapper", num_cores = 1) clustomit_out2 <- clustomit(x = x, num_clusters = 5, cluster_method = kmeans_wrapper, num_cores = 1)# First, we create a wrapper function for the K-means clustering algorithm # that returns only the clustering labels for each observation (row) in # \code{x}. kmeans_wrapper <- function(x, num_clusters, num_starts = 10, ...) { kmeans(x = x, centers = num_clusters, nstart = num_starts, ...)$cluster } # For this example, we generate five multivariate normal populations with the # \code{sim_data} function. x <- sim_data("normal", delta = 1.5, seed = 42)$x clustomit_out <- clustomit(x = x, num_clusters = 4, cluster_method = "kmeans_wrapper", num_cores = 1) clustomit_out2 <- clustomit(x = x, num_clusters = 5, cluster_method = kmeans_wrapper, num_cores = 1)
For a set of clustering labels, this function computes the comembership of all pairs of observations. Basically, two observations are said to be comembers if they are clustered together.
comembership(labels)comembership(labels)
labels |
a vector of |
Tibshirani and Walther (2005) use the term 'co-membership', which we shorten to 'comembership'. Some authors instead use the terms 'connectivity' or 'co-occurrence'.
We use the Rcpp package to improve the runtime
speed of this function.
a vector of choose(n, 2) comembership bits
Tibshirani, R. and Walther, G. (2005), Cluster Validation by Prediction Strength, _Journal of Computational and Graphical Statistics_, 14, 3, 511-528. http://amstat.tandfonline.com/doi/abs/10.1198/106186005X59243.
# We generate K = 3 labels for each of n = 10 observations and compute the # comembership for all 'n choose 2' pairs. set.seed(42) K <- 3 n <- 10 labels <- sample.int(K, n, replace = TRUE) comembership_out <- comembership(labels) comembership_out # Notice that the number of comemberships is 'n choose 2'. length(comembership_out) == choose(n, 2)# We generate K = 3 labels for each of n = 10 observations and compute the # comembership for all 'n choose 2' pairs. set.seed(42) K <- 3 n <- 10 labels <- sample.int(K, n, replace = TRUE) comembership_out <- comembership(labels) comembership_out # Notice that the number of comemberships is 'n choose 2'. length(comembership_out) == choose(n, 2)
For two clusterings of the same data set, this function calculates the 2x2 contingency table of agreements and disagreements of the corresponding two vectors of comemberships. Basically, the comembership is defined as the pairs of observations that are clustered together.
comembership_table(labels1, labels2)comembership_table(labels1, labels2)
labels1 |
a vector of |
labels2 |
a vector of |
The contingency table calculated is typically utilized in the calculation of a similarity statistic (e.g., Rand index, Jaccard index) between the two clusterings. The 2x2 contingency table consists of the following four cells:
the number of observation pairs where both observations are comembers in both clusterings
the number of observation pairs where the observations are comembers in the first clustering but not the second
the number of observation pairs where the observations are comembers in the second clustering but not the first
the number of observation pairs where neither pair are comembers in either clustering
Tibshirani and Walther (2005) use the term 'co-membership', which we shorten to 'comembership'. Some authors instead use the terms 'connectivity' or 'co-occurrence'.
We use the Rcpp package to improve the runtime
speed of this function.
named list containing the calculated contingency table:
n_11
n_10
n_01
n_00
Tibshirani, R. and Walther, G. (2005). Cluster Validation by Prediction Strength. Journal of Computational and Graphical Statistics, 14, 3, 511-528. http://amstat.tandfonline.com/doi/abs/10.1198/106186005X59243.
# We generate K = 3 labels for each of n = 10 observations and compute the # comembership for all 'n choose 2' pairs. set.seed(42) K <- 3 n <- 10 labels1 <- sample.int(K, n, replace = TRUE) labels2 <- sample.int(K, n, replace = TRUE) comembership_table(labels1, labels2) # Here, we cluster the \code{\link{iris}} data set with the K-means and # hierarchical algorithms using the true number of clusters, K = 3. # Then, we compute the 2x2 contingency table agreements and disagreements of #' the comemberships. iris_kmeans <- kmeans(iris[, -5], centers = 3)$cluster iris_hclust <- cutree(hclust(dist(iris[, -5])), k = 3) comembership_table(iris_kmeans, iris_hclust)# We generate K = 3 labels for each of n = 10 observations and compute the # comembership for all 'n choose 2' pairs. set.seed(42) K <- 3 n <- 10 labels1 <- sample.int(K, n, replace = TRUE) labels2 <- sample.int(K, n, replace = TRUE) comembership_table(labels1, labels2) # Here, we cluster the \code{\link{iris}} data set with the K-means and # hierarchical algorithms using the true number of clusters, K = 3. # Then, we compute the 2x2 contingency table agreements and disagreements of #' the comemberships. iris_kmeans <- kmeans(iris[, -5], centers = 3)$cluster iris_hclust <- cutree(hclust(dist(iris[, -5])), k = 3) comembership_table(iris_kmeans, iris_hclust)
We define a intraclass covariance
(correlation) matrix to be , where , is the identity matrix,
and denotes the matrix of
ones.
intraclass_cov(p, rho, sigma2 = 1)intraclass_cov(p, rho, sigma2 = 1)
p |
the dimension of the matrix |
rho |
the intraclass covariance (correlation) constant |
sigma2 |
the coefficient of the intraclass covariance matrix |
an intraclass covariance matrix matrix of size p
For two clusterings of the same data set, this function calculates the Jaccard similarity coefficient of the clusterings from the comemberships of the observations. Basically, the comembership is defined as the pairs of observations that are clustered together.
jaccard_indep(labels1, labels2)jaccard_indep(labels1, labels2)
labels1 |
a vector of |
labels2 |
a vector of |
To calculate the Rand index, we compute the 2x2 contingency table, consisting of the following four cells:
the number of observation pairs where both observations are comembers in both clusterings
the number of observation pairs where the observations are comembers in the first clustering but not the second
the number of observation pairs where the observations are comembers in the second clustering but not the first
the number of observation pairs where neither pair are comembers in either clustering
The Jaccard similarity coefficient is defined as:
.
In the special case that the Jaccard coefficient results
in , we define . For instance, this
case can occur when both clusterings consist of all
singleton clusters.
To compute the contingency table, we use the
comembership_table function.
the Jaccard coefficient for the two sets of cluster labels (See Details.)
## Not run: # We generate K = 3 labels for each of n = 10 observations and compute the # Jaccard similarity coefficient between the two clusterings. set.seed(42) K <- 3 n <- 10 labels1 <- sample.int(K, n, replace = TRUE) labels2 <- sample.int(K, n, replace = TRUE) jaccard_indep(labels1, labels2) # Here, we cluster the \code{\link{iris}} data set with the K-means and # hierarchical algorithms using the true number of clusters, K = 3. # Then, we compute the Jaccard similarity coefficient between the two # clusterings. iris_kmeans <- kmeans(iris[, -5], centers = 3)$cluster iris_hclust <- cutree(hclust(dist(iris[, -5])), k = 3) jaccard_indep(iris_kmeans, iris_hclust) ## End(Not run)## Not run: # We generate K = 3 labels for each of n = 10 observations and compute the # Jaccard similarity coefficient between the two clusterings. set.seed(42) K <- 3 n <- 10 labels1 <- sample.int(K, n, replace = TRUE) labels2 <- sample.int(K, n, replace = TRUE) jaccard_indep(labels1, labels2) # Here, we cluster the \code{\link{iris}} data set with the K-means and # hierarchical algorithms using the true number of clusters, K = 3. # Then, we compute the Jaccard similarity coefficient between the two # clusterings. iris_kmeans <- kmeans(iris[, -5], centers = 3)$cluster iris_hclust <- cutree(hclust(dist(iris[, -5])), k = 3) jaccard_indep(iris_kmeans, iris_hclust) ## End(Not run)
For two clusterings of the same data set, this function calculates the Rand similarity coefficient of the clusterings from the comemberships of the observations. Basically, the comembership is defined as the pairs of observations that are clustered together.
rand_indep(labels1, labels2)rand_indep(labels1, labels2)
labels1 |
a vector of |
labels2 |
a vector of |
To calculate the Rand index, we compute the 2x2 contingency table, consisting of the following four cells:
the number of observation pairs where both observations are comembers in both clusterings
the number of observation pairs where the observations are comembers in the first clustering but not the second
the number of observation pairs where the observations are comembers in the second clustering but not the first
the number of observation pairs where neither pair are comembers in either clustering
The Rand similarity index is defined as:
.
To compute the contingency table, we use the
comembership_table function.
the Rand index for the two sets of cluster labels
## Not run: # We generate K = 3 labels for each of n = 10 observations and compute the # Rand similarity index between the two clusterings. set.seed(42) K <- 3 n <- 10 labels1 <- sample.int(K, n, replace = TRUE) labels2 <- sample.int(K, n, replace = TRUE) rand_indep(labels1, labels2) # Here, we cluster the \code{\link{iris}} data set with the K-means and # hierarchical algorithms using the true number of clusters, K = 3. # Then, we compute the Rand similarity index between the two clusterings. iris_kmeans <- kmeans(iris[, -5], centers = 3)$cluster iris_hclust <- cutree(hclust(dist(iris[, -5])), k = 3) rand_indep(iris_kmeans, iris_hclust) ## End(Not run)## Not run: # We generate K = 3 labels for each of n = 10 observations and compute the # Rand similarity index between the two clusterings. set.seed(42) K <- 3 n <- 10 labels1 <- sample.int(K, n, replace = TRUE) labels2 <- sample.int(K, n, replace = TRUE) rand_indep(labels1, labels2) # Here, we cluster the \code{\link{iris}} data set with the K-means and # hierarchical algorithms using the true number of clusters, K = 3. # Then, we compute the Rand similarity index between the two clusterings. iris_kmeans <- kmeans(iris[, -5], centers = 3)$cluster iris_hclust <- cutree(hclust(dist(iris[, -5])), k = 3) rand_indep(iris_kmeans, iris_hclust) ## End(Not run)
For each observation (row) in 'x', one of K labels is randomly generated. By default, the probabilities of selecting each clustering label are equal, but this can be altered by specifying 'prob', a vector of probabilities for each cluster.
random_clustering(x, K, prob = NULL)random_clustering(x, K, prob = NULL)
x |
a matrix containing the data to cluster. The rows are the sample observations, and the columns are the features. |
K |
the number of clusters |
prob |
a vector of probabilities to generate each cluster label. If NULL, each cluster label has an equal chance of being selected. |
Random clustering is often utilized as a baseline comparison clustering against which other clustering algorithms are employed to identify structure within the data. Typically, comparisons are made in terms of proposed clustering assessment and evaluation methods as well as clustering similarity measures. For the former, a specified clustering evaluation method is computed for the considered clustering algorithms as well as random clustering. If the clusters determined by a considered clustering algorithm do not differ significantly from the random clustering, we might conclude that the found clusters are no better than naively choosing clustering labels for each observation at random. Likewise, a similarity measure can be computed to compare the clusterings from each of a considered clustering algorithm and a random clustering: if the clusterings are significantly similar, once again, we might conclude the clusters found via the considered clustering algorithm do not differ significantly from those found at random. In either case, the clusters are unlikely to provide meaningful results on which the user can better understand the inherent structure within the data.
a vector of clustering labels for each observation in 'x'.
We provide a wrapper function to generate from three data-generating models:
sim_unifFive multivariate uniform distributions
sim_normalMultivariate normal distributions with intraclass covariance matrices
sim_studentMultivariate Student's t distributions each with a common covariance matrix
sim_data(family = c("uniform", "normal", "student"), ...)sim_data(family = c("uniform", "normal", "student"), ...)
family |
the family of distributions from which to generate data |
... |
optional arguments that are passed to the data-generating function |
For each data-generating model, we generate
observations from each of
multivariate distributions so that the Euclidean
distance between each of the population centroids and the
origin is equal and scaled by . For
each model, the argument delta controls this
separation.
This wrapper function is useful for simulation studies, where the efficacy of supervised and unsupervised learning methods and algorithms are evaluated as a the population separation is increased.
named list containing:
A matrix
whose rows are the observations generated and whose
columns are the p features (variables)
A vector denoting the population from which the observation in each row was generated.
set.seed(42) uniform_data <- sim_data(family = "uniform") normal_data <- sim_data(family = "normal", delta = 2) student_data <- sim_data(family = "student", delta = 1, df = 1:5)set.seed(42) uniform_data <- sim_data(family = "uniform") normal_data <- sim_data(family = "normal", delta = 2) student_data <- sim_data(family = "student", delta = 1, df = 1:5)
We generate observations from each of multivariate normal
distributions such that the Euclidean distance between
each of the means and the origin is equal and scaled by
.
sim_normal(n = rep(25, 5), p = 50, rho = rep(0.9, 5), delta = 0, sigma2 = 1, seed = NULL)sim_normal(n = rep(25, 5), p = 50, rho = rep(0.9, 5), delta = 0, sigma2 = 1, seed = NULL)
n |
a vector (of length M) of the sample sizes for each population |
p |
the dimension of the multivariate normal populations |
rho |
a vector (of length M) of the intraclass constants for each population |
delta |
the fixed distance between each population and the origin |
sigma2 |
the coefficient of each intraclass covariance matrix |
seed |
seed for random number generation (If NULL, does not set seed) |
Let denote the th population with a
-dimensional multivariate normal distribution,
with mean vector
and covariance matrix . Also, let
be the th standard basis vector (i.e., the
th element is 1 and the remaining values are 0).
Then, we define
Note that p must be divisible
by M. By default, the first 10 dimensions of
are set to delta with all remaining
dimensions set to 0, the second 10 dimensions of
are set to delta with all remaining
dimensions set to 0, and so on.
Also, we consider intraclass covariance (correlation)
matrices such that , where ,
is the identity matrix, and
denotes the matrix of ones.
By default, we let , , and
. Furthermore, we generate 25
observations from each population by default.
For and , , the populations are equal.
named list containing:
A matrix
whose rows are the observations generated and whose
columns are the p features (variables)
A vector denoting the population from which the observation in each row was generated.
data_generated <- sim_normal(n = 10 * seq_len(5), seed = 42) dim(data_generated$x) table(data_generated$y) data_generated2 <- sim_normal(p = 10, delta = 2, rho = rep(0.1, 5)) table(data_generated2$y) sample_means <- with(data_generated2, tapply(seq_along(y), y, function(i) { colMeans(x[i,]) })) (sample_means <- do.call(rbind, sample_means))data_generated <- sim_normal(n = 10 * seq_len(5), seed = 42) dim(data_generated$x) table(data_generated$y) data_generated2 <- sim_normal(p = 10, delta = 2, rho = rep(0.1, 5)) table(data_generated2$y) sample_means <- with(data_generated2, tapply(seq_along(y), y, function(i) { colMeans(x[i,]) })) (sample_means <- do.call(rbind, sample_means))
We generate observations from each of multivariate Student's t
distributions such that the Euclidean distance between
each of the means and the origin is equal and scaled by
.
sim_student(n = rep(25, 5), p = 50, df = rep(6, 5), delta = 0, Sigma = diag(p), seed = NULL)sim_student(n = rep(25, 5), p = 50, df = rep(6, 5), delta = 0, Sigma = diag(p), seed = NULL)
n |
a vector (of length M) of the sample sizes for each population |
p |
the dimension of the multivariate Student's t distributions |
df |
a vector (of length M) of the degrees of freedom for each population |
delta |
the fixed distance between each population and the origin |
Sigma |
the common covariance matrix |
seed |
seed for random number generation (If NULL, does not set seed) |
Let denote the th population with a
-dimensional multivariate Student's t
distribution, , where
is the population location vector,
is the positive-definite covariance
matrix, and is the degrees of freedom.
Let be the th standard basis vector
(i.e., the th element is 1 and the remaining
values are 0). Then, we define
Note that p
must be divisible by M. By default, the first 10
dimensions of are set to delta with
all remaining dimensions set to 0, the second 10
dimensions of are set to delta with
all remaining dimensions set to 0, and so on.
We use a common covariance matrix
for all populations.
For small values of , the tails are heavier,
and, therefore, the average number of outlying
observations is increased.
By default, we let , ,
, and , , where denotes the
identity matrix. Furthermore, we generate 25 observations
from each population by default.
For and , , the populations are equal.
named list containing:
A matrix
whose rows are the observations generated and whose
columns are the p features (variables)
A vector denoting the population from which the observation in each row was generated.
data_generated <- sim_student(n = 10 * seq_len(5), seed = 42) dim(data_generated$x) table(data_generated$y) data_generated2 <- sim_student(p = 10, delta = 2, df = rep(2, 5)) table(data_generated2$y) sample_means <- with(data_generated2, tapply(seq_along(y), y, function(i) { colMeans(x[i,]) })) (sample_means <- do.call(rbind, sample_means))data_generated <- sim_student(n = 10 * seq_len(5), seed = 42) dim(data_generated$x) table(data_generated$y) data_generated2 <- sim_student(p = 10, delta = 2, df = rep(2, 5)) table(data_generated2$y) sample_means <- with(data_generated2, tapply(seq_along(y), y, function(i) { colMeans(x[i,]) })) (sample_means <- do.call(rbind, sample_means))
We generate n observations from each of four
trivariate distributions such that the Euclidean distance
between each of the populations is a fixed constant,
delta > 0.
sim_unif(n = rep(25, 5), delta = 0, seed = NULL)sim_unif(n = rep(25, 5), delta = 0, seed = NULL)
n |
a vector (of length M = 5) of the sample sizes for each population |
delta |
the fixed distance between each population and the origin |
seed |
Seed for random number generation. (If NULL, does not set seed) |
To define the populations, let be a multivariate uniformly distributed random
vector such that is an
independently distributed uniform random variable with
for . Let
denote the th population . Then, we have the five populations:
We generate observations from population
.
For and , , the populations are equal.
Notice that the support of each population is a unit
hypercube with 4 features. Moreover, for , the populations are mutually exclusive and entirely
separated.
named list containing:
A matrix
whose rows are the observations generated and whose
columns are the p features (variables)
A vector denoting the population from which the observation in each row was generated.
data_generated <- sim_unif(seed = 42) dim(data_generated$x) table(data_generated$y) data_generated2 <- sim_unif(n = 10 * seq_len(5), delta = 1.5) table(data_generated2$y) sample_means <- with(data_generated2, tapply(seq_along(y), y, function(i) { colMeans(x[i,]) })) (sample_means <- do.call(rbind, sample_means))data_generated <- sim_unif(seed = 42) dim(data_generated$x) table(data_generated$y) data_generated2 <- sim_unif(n = 10 * seq_len(5), delta = 1.5) table(data_generated2$y) sample_means <- with(data_generated2, tapply(seq_along(y), y, function(i) { colMeans(x[i,]) })) (sample_means <- do.call(rbind, sample_means))