cpearson: Calculate the Pearson correlation or dissimilarity for each...

View source: R/cmahalanobis.R

cpearsonR Documentation

Calculate the Pearson correlation or dissimilarity 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 Pearson correlation or dissimilarity about the factors inside them. You can also select "index" to calculate this statistic between each row.

Usage

cpearson(
  dataset,
  formula,
  plot = FALSE,
  min_group_size = 3,
  method = "average",
  max_index_sample = NULL,
  automatic_encoding = FALSE,
  na_removal = FALSE,
  grouping_stat = TRUE,
  dissimilarity = FALSE,
  t_test = FALSE,
  wcor = NULL
)

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 Pearson correlation or dissimilarity 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 groups, 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".

max_index_sample

A number of random samples from a dataset for which you want to calculate the statistic 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

Logical, if TRUE, the Pearson correlation or dissimilarity between factors is printed.

dissimilarity

If TRUE, default, the dissimilarity Pearson's R is displayed.

t_test

If TRUE, statistical significance using the t-student distribution is performed.

wcor

vector of weights for each column; if NULL, weights will be equal.

Value

According to the option chosen in formula, with "index" the Pearson' R matrix will be printed; instead, by specifying variables, the Pearson correlations matrix or matrices (two or more) between each pair of groups and, optionally, the plot or plots (two or more) will be printed.

Note

If "index" is selected with variables, only correlations between rows are calculated. Therefore, this snippet: "cpearson(mtcars, ~am + carb + index)" will print correlations only considering "index". Optionally, rows with NA values can be omitted. If a group is shorter than another, the longer will be trimmed to reach equal length.

Examples

# Example with the CO2 dataset

table(CO2$Plant)

cpearson(CO2, ~Plant, 
   plot = FALSE, 
   grouping_stat = TRUE, 
   automatic_encoding = TRUE)

# Example with the airquality dataset

summary(airquality)

cpearson(airquality, ~index, 
   plot = FALSE, 
   na_removal = TRUE, 
   max_index_sample = 40,
   dissimilarity = TRUE,
   t_test = FALSE)
   

cmahalanobis documentation built on Dec. 4, 2026, 5:07 p.m.