# Data Mining Algorithms In R/Clustering/Dissimilarity Matrix Calculation
## Introduction

Dissimilarity may be defined as the distance between two samples under some criterion, in other words, how different these samples are. Considering the Cartesian Plane, one could say that the euclidean distance between two points is the measure of their dissimilarity. The Dissimilarity index can also be defined as the percentage of a group that would have to move to another group so the samples to achieve an even distribution.
The Dissimilarity matrix is a matrix that expresses the similarity pair to pair between two sets. It's square and symmetric. The diagonal members are defined as zero, meaning that zero is the measure of dissimilarity between an element and itself. Thus, the information the matrix holds can be seen as a triangular matrix. MARCHIORO et al. (2003) used the matrix of dissimilarity to determine the differences between oat specimens and discover good generators for the future generations.
The concept of Dissimilarity may be used in a more general way, to determine the pairwise difference between samples. As an example, this was used by da Silveira and Hanashiro (2009) to study the impact of similarity and dissimilarity between superior and subordinate in the quality of their relationship. The similarity notion is a key concept for Clustering, in the way to decide which clusters should be combined or divided when observing sets. An appropriate metric use is strategic in order to achieve the best clustering, because it directly influences the shape of clusters. The Dissimilarity Matrix (or Distance matrix) is used in many algorithms of Density-based and Hierarchical clustering, like LSDBC.
The Dissimilarity Matrix Calculation can be used, for example, to find Genetic Dissimilarity among oat genotypes. The way of arranging the sequences of protein, RNA and DNA to identify regions of similarity that may be a consequence of relationships between the sequences, in bioinformatics, is defined as sequence alignment. Sequence alignment is part of genome assembly, where sequences are aligned to find overlaps so that long sequences can be formed.

## Algorithm

The matrix may be calculated by iterating over each element and calculating its dissimilarity to every other element. Let A be a Dissimilarity Matrix of size NxN, and B a set of N elements. A$_{ij}$ is the dissimilarity between elements B$_i$ and B$_j$.
where the function Dissimilarity is defined as follows:
ApplyDissimilarityCriterion is a function that calculates the dissimilarity between two elements based in the defined criterion. Here is a list of some criteria:
* Euclidean Distance
* Squared Euclidean Distance
* Manhattan Distance
* Maximum Distance
* Mahalanobis Distance
* Cosine Similarity

## Implementation in R

Cluster Analysis, extended original from Peter Rousseeuw, Anja Struyf and Mia Hubert.
Package: cluster Version: 1.12.1 Priority: recommended Depends: R (>= 2.5.0), stats, graphics, utils Published: 2009-10-06 Author: Martin Maechler, based on S original by Peter Rousseeuw, Anja.Struyf@uia.ua.ac.be and Mia.Hubert@uia.ua.ac.be, and initial R port by Kurt.Hornik@R-project.org Maintainer: Martin Maechler <maechler at stat.math.ethz.ch> License: GPL (>= 2) Citation: cluster citation info In views: Cluster, Environmetrics, Multivariate CRAN checks: cluster results
The package can be downloaded from the CRAN website. It can be installed using the install.packages function, directly in R environment. The function daisy is used to calculate the dissimilarity matrix. It can be found in the cluster package.
The Dissimilarity Object is the representation of the Dissimilarity Matrix. The matrix is symmetric and the diagonal is not interesting, thus the lower triangle is represented by a vector to save storage space. To generate the dissimilarity matrix one must use the daisy function as follows:
Usage
daisy(x, metric = c("euclidean", "manhattan", "gower"), stand = FALSE, type = list)
Arguments
* x:numeric matrix or data frame. The dissimilarities will be computed between the rows of x.
* metric: character string specifying the metric to be used. The currently available options are "euclidian", which is the default, "manhattan" and "gower".
* stand: logical flag: If the value is true, then the measurements in x are standardized before calculating the dissimilarities.
* type: list specifying some or all of the types of the variables(columns) in x. The options are: "ordratio" (ratio scaled variables treated like ordinary variables), "logicalratio" (ratio scaled variables that must be logarithmically transformed), "asymm" (asymmetric binary variables) and "symm" (symmetric binary variables). Each entry is a vector containing the names or numbers of the corresponding columns of x.
Return
The function returns a dissimilarity object.
For further information, please refer to the daisy documentation.

## Visualization

For the example, we will use the agriculture dataset available in R.
The dissimilarity matrix, using the euclidean metric, can be calculated with the command: daisy(agriculture, metric = "euclidean").
The result the of calculation will be displayed directly in the screen, and if you wanna reuse it you can simply assign it to an object: x <- daisy(agriculture, metric = "euclidean").
The object returned by the daisy function is a dissimilarity object, defined earlier in this text.
To visualize the matrix use the next command: as.matrix(x)
To obtain a summary of the data stored in the matrix, you can use: summary(x)
You can also use the dissimilarity matrix in the print method to obtain the lower triangle (the one that matters and is stored) of the matrix:

## Case Study

To illustrate the Dissimilarity Matrix technique, a simple case study will be shown.

### Scenario

In the context of species enhancement programs, it is desirable to have very heterogeneous generations, so it is possible to induce the desired characteristics. When trying to induce a giving set of characteristics, it is necessary to choose a parent specimen that will result in the next generations.
Genetic dissimilarity measures have become the interest of many authors (Santos et al., 1997; Gaur et al., 1978; Casler,1995) in characterizing and identifying genetic contributions of different species. As dissimilarity measures to show the genetic variability intensity, the Euclidian distance and the Mahalanobis distance are the most used in plant genetic enhancement programs. The objective is to choose genetic constitutions that may result in superior combinations through their progeny.

### Input Data

The data used here was extracted from MARCHIORO et al. (2003). 18 oat genotypes were measured in many aspects to be later compared. Here we use the measure of days until flowering(DUF) for genotypes subject to fungicides.
| Genotype | DUF (days) |
| UPF 7. | 104 |
| UPF 15. | 100 |
| UPF 16. | 97 |
| UPF 17. | 96 |
| UPF 18. | 102 |
| UPF 19. | 98 |
| UFRGS 7. | 95 |
| UFRGS 14. | 99 |
| UFRGS 15. | 101 |
| UFRGS 16. | 101 |
| UFRGS 17. | 100 |
| UFRGS 18. | 105 |
| UFRGS 19. | 92 |
| URS 20. | 97 |
| URS 21. | 93 |
| IAC 7. | 91 |
| OR 2. | 97 |
| OR 3. | 98 |
This data is the input for the daisy function to calculate the dissimilarity matrix. From the results it is possible to define the best parent to achieve very heterogeneous future generations.
Data construction in R:

### Execution

To determine the dissimilarity matrix of the data selected in this case study, use the command below:

### Output

The dissimilarity matrix stored in dis_mat can be visualized as showed below:

### Analysis

The output shows that there is a high dissimilarity between the genotypes. This is in agreement with the results in MARCHIORO et al. (2003). Cross breeding these genotypes, it is possible to achieve high dissimilarity in the next generations, which is very good, since the genetic enhancement program can have more genetic combinations to explore.

## References

* 1 2 3 MARCHIORO, Volmir Sergio, DE CARVALHO, Fernando Irajá Félix, DE OLIVEIRA, Antônio Costa CRUZ, Pedro Jacinto, LORENCETTI, Claudir, BENIN, Giovani, DA SILVA, José Antônio Gonzales, SCHMIDT, Douglas A. M., GENETIC DISSIMILARITY AMONG OAT GENOTYPES, Ciênc. agrotec., Lavras. V.27, n.2, p.285-294, mar./abr., 2003
* ↑ DA SILVEIRA, Nereida Salette Paulo, HANASHIRO, Darcy Mitiko Mori, Similarity and Dissimilarity between Superiors and Subordinates and Their Implications for Dyadic Relationship Qualit, RAC, Curitiba, v. 13, n. 1, art. 7, p. 117-135, Jan./Mar. 2009