cmahalanobis: Calculate the Mahalanobis distances for each pair of factors...

View source: R/cmahalanobis.R

cmahalanobisR Documentation

Calculate the Mahalanobis distances for each pair of factors or for the index.

Description

This function takes a dataframe and a variable or variables (two or more) in input, and returns a matrix or matrices (two or more) with the Mahalanobis distances about each pair of factors inside them. You can also select "index" to calculate the Mahalanobis distances between each row.

Usage

cmahalanobis(
  dataset,
  formula,
  plot = TRUE,
  min_group_size = 3,
  method = "average",
  pvalues_chisq = TRUE,
  max_index_sample = NULL,
  automatic_encoding = FALSE,
  na_removal = FALSE,
  grouping_stat = "mean"
)

Arguments

dataset

A dataframe.

formula

The index of the dataframe, otherwise a variable or variables (two or more) with factors which you want to calculate the Mahalanobis distances matrix or matrices (two or more).

plot

Logical, if TRUE, dendrograms with various agglomeration metrics for factors (two or more) are displayed. With "index" in formula, a dendrogram considering the observation is displayed.

min_group_size

Minimum group size to maintain. The default value is 3, therefore factors, inside variables, with less than 3 observations will be discarded. For "index", this value is always 1.

method

The agglomeration method for calculating dissimilarities between observations. Available methods are "ward.D", "ward.D2", "single", "complete", "average", "mcquitty", "median" or "centroid".

pvalues_chisq

If TRUE, print the result of the chi-squared test on squared distances. The distances with "pvalues_chisq = FALSE" are not squared; instead, with "pvalues_chisq = TRUE", the squared Mahalanobis distances with corresponding p_values will be printed. Default is FALSE.

max_index_sample

A number of random samples from a dataset for which you want to calculate the dissimilarity for index mode, useful with very large dataset.

automatic_encoding

Logical, if TRUE, names inside factor variables will be transformed in numbers with ordinal order (1,2,....).

na_removal

Logical, if TRUE, missing value removal on rows is performed.

grouping_stat

When a factor variable is specified, calculate the specified grouping statistic for each factor. Available methods are: mean (arithmetic mean), median and SDS (standard deviations).

Value

According to the option chosen in formula and in pvalues_chisq, with "index" and "pvalues_chisq = TRUE" the squared Mahalanobis distance matrix will be printed with corresponding pvalues; instead, with "index" and "pvalues_chisq = FALSE", only the Mahalanobis distances (not squared) will be printed. By specifying variables, the Mahalanobis distances matrix or matrices (two or more) between each pair of factors and, optionally, the plot or plots (two or more) will be printed.

Note

If "index" is selected with variables, only distances between rows are calculated. Therefore, this snippet: "cmahalanobis(mtcars, ~am + carb + index)" will print distances and plot only considering "index". Optionally, rows with NA values are omitted.

Examples


# Example with the CO2 dataset

table(CO2$Plant)

cmahalanobis(CO2, ~Plant, pvalues_chisq = TRUE, 
              plot = TRUE, 
              grouping_stat = 'mean', 
              na_removal = TRUE, 
              automatic_encoding = TRUE)

# Example with the airquality dataset

summary(airquality)

cmahalanobis(airquality, ~index, 
   plot = TRUE, 
   na_removal = TRUE, 
   max_index_sample = 40)
   

cmahalanobis documentation built on Aug. 31, 2026, 5:07 p.m.