Function Reference — MixedDiversityAnalysis

The MixedDiversityAnalysis package provides five main functions for analysing diversity among genotypes using quantitative, qualitative, binary, and ordinal traits. Together, these functions support mixed-trait dissimilarity calculation, hierarchical clustering, Principal Coordinates Analysis (PCoA), trait separation assessment, and visualization.

1. mixed_distance()

Purpose

The mixed_distance() function calculates pairwise dissimilarities among genotypes using a combination of quantitative, qualitative, binary, and ordinal traits.

It uses a custom mixed-trait dissimilarity method and does not implement Gower’s distance.

Syntax

mixed_distance(data, types)

Arguments

The order of types must correspond to the order of the columns in data.

Method

The dissimilarity between two genotypes is calculated as the mean of the available trait-specific dissimilarities. Missing trait comparisons are excluded from that pair’s mean. If no traits can be compared for a pair, its dissimilarity is returned as NA.

Return value

The function returns an object of class mixed_distance, containing:

Example

dist_result <- mixed_distance(
  data = my_data,
  types = trait_types
)

Interpretation

Smaller dissimilarity values indicate more similar genotypes, whereas larger values indicate greater differences in their trait profiles. Values are generally scaled between 0 and 1 under the defined trait-specific calculations.


2. mixed_cluster()

Purpose

The mixed_cluster() function performs hierarchical clustering using a dissimilarity object generated by mixed_distance(). It supports different linkage methods and can select the number of clusters using average silhouette width.

Syntax

mixed_cluster(
  distance_object,
  k = "optimal",
  method = "average",
  k_range = NULL,
  min_cluster_size = 1
)

Arguments

Method

The function performs hierarchical clustering using the selected linkage method.

When k = "optimal", candidate cluster solutions are evaluated using average silhouette width. Solutions containing clusters smaller than min_cluster_size are excluded. If multiple solutions have the same best score, the smaller number of clusters is preferred.

When a fixed integer is supplied for k, it must be between 2 and the number of observations minus 1.

Return value

The function returns an object of class mixed_cluster, containing:

Example

cluster_result <- mixed_cluster(
  distance_object = dist_result,
  k = "optimal",
  method = "average",
  k_range = 2:10,
  min_cluster_size = 5
)

Interpretation

Cluster membership groups genotypes according to their mixed-trait dissimilarities. Cluster sizes and silhouette width can help evaluate the resulting groups.

A higher average silhouette width generally indicates better separation and cohesion. However, the selected solution should also be assessed for biological relevance and whether small clusters are meaningful.


3. mixed_pcoa()

Purpose

The mixed_pcoa() function performs Principal Coordinates Analysis (PCoA) using the mixed-trait dissimilarity matrix. It provides a low-dimensional representation of relationships among genotypes.

Syntax

mixed_pcoa(distance_object, dimensions = 2)

Arguments

Method

The function uses stats::cmdscale() to perform PCoA, requesting eigenvalues and an additive correction using the Cailliez method.

This correction helps accommodate non-Euclidean dissimilarity matrices. It does not change the original dissimilarity matrix stored in the mixed_distance object.

Return value

The function returns an object of class mixed_pcoa, containing:

Example

pcoa_result <- mixed_pcoa(
  distance_object = dist_result,
  dimensions = 2
)

Interpretation

Genotypes plotted close together have more similar mixed-trait profiles in the ordination space, while those farther apart are generally more dissimilar.

The first two coordinates can be used to visualize broad diversity patterns. The reported percentages should be considered when interpreting the axes.


4. mixed_diversity()

Purpose

The mixed_diversity() function evaluates how strongly individual traits separate the clusters obtained from hierarchical clustering. It provides a descriptive ranking of traits according to their separation scores.

Syntax

mixed_diversity(data, types, clusters)

Arguments

The rows of data and the entries of clusters must correspond to the same genotypes in the same order.

Method

The function calculates trait-specific separation scores according to trait type:

The results are sorted in descending order of separation score.

Return value

The function returns a data frame with class "mixed_trait", containing:

The "mixed_trait" class is the current return class used by the package; it does not mean the function is named mixed_trait().

Example

trait_result <- mixed_diversity(
  data = my_data,
  types = trait_types,
  clusters = cluster_result$clusters
)

print(trait_result)

Interpretation

Traits with higher separation scores show stronger descriptive association with the identified cluster structure, according to the metric used for their type.

These scores do not establish causality, statistical significance, or independent predictive importance. They should be interpreted alongside biological knowledge and, where appropriate, further statistical analyses.


5. mixed_visualize()

Purpose

The mixed_visualize() function generates graphical summaries of clustering, silhouette quality, PCoA ordination, and trait separation results.

Syntax

mixed_visualize(
  object,
  type = c("dendrogram", "silhouette", "pcoa", "trait"),
  clusters = NULL,
  labels = NULL,
  main = NULL,
  ellipse = FALSE,
  ...
)

Arguments

Supported visualizations

Dendrogram

Displays the hierarchical relationships among genotypes or clusters. The required object is a mixed_cluster result.

mixed_visualize(
  cluster_result,
  type = "dendrogram"
)

Silhouette plot

Displays the silhouette widths of individual genotypes, helping assess cluster cohesion and separation. The required object is a mixed_cluster result.

mixed_visualize(
  cluster_result,
  type = "silhouette"
)

PCoA plot

Displays genotypes using their PCoA coordinates. The required object is a mixed_pcoa result. Cluster assignments can be supplied to distinguish groups.

mixed_visualize(
  pcoa_result,
  type = "pcoa",
  clusters = cluster_result$clusters,
  labels = rownames(my_data),
  ellipse = TRUE
)

Ellipses may be omitted when a cluster has too few distinct coordinates or when its coordinate covariance is degenerate. They are visual summaries and should not automatically be interpreted as formal confidence regions for cluster membership.

Trait separation plot

Displays the trait separation scores calculated by mixed_diversity(). The required object is the result returned by that function, which has class "mixed_trait".

mixed_visualize(
  trait_result,
  type = "trait"
)

Interpretation

The visualization options provide complementary views of the analysis:

Plots with many genotypes may become crowded. Shorter labels or a subset of labels can improve readability.


The five functions are designed to be used together:

# 1. Calculate mixed-trait dissimilarities
dist_result <- mixed_distance(
  data = my_data,
  types = trait_types
)

# 2. Perform hierarchical clustering
cluster_result <- mixed_cluster(
  distance_object = dist_result,
  k = "optimal",
  method = "average",
  k_range = 2:10,
  min_cluster_size = 5
)

# 3. Perform PCoA
pcoa_result <- mixed_pcoa(
  distance_object = dist_result,
  dimensions = 2
)

# 4. Assess trait separation
trait_result <- mixed_diversity(
  data = my_data,
  types = trait_types,
  clusters = cluster_result$clusters
)

# 5. Visualize the results
mixed_visualize(
  cluster_result,
  type = "dendrogram"
)

mixed_visualize(
  cluster_result,
  type = "silhouette"
)

mixed_visualize(
  pcoa_result,
  type = "pcoa",
  clusters = cluster_result$clusters
)

mixed_visualize(
  trait_result,
  type = "trait"
)

Important considerations

Before running the workflow:

Summary

MixedDiversityAnalysis integrates mixed-trait dissimilarity calculation, hierarchical clustering, PCoA, trait separation assessment, and visualization in one workflow. Together, these functions support the characterization of genetic diversity and the exploration of relationships among genotypes containing different types of traits.