# Main function to compute diversity measures

### Description

**Main** function of the package. The diversity function computes diversity measures for a dataset with entities, categories and values.

### Usage

1 2 |

### Arguments

`data` |
A numeric matrix with entities |

`type` |
A string or a vector of strings of nemonic strings referencing to the available diversity measures. The available measures are: "variety", (Shannon) "entropy", "blau","gini-simpson", "simpson", "hill-numbers", "herfindahl-hirschman", "berger-parker", "renyi", (Pielou) "evenness", "rao", "rao-stirling". A list of short mnemonics for each measure: "v", "e", "gs", "s", "td", "hh", "bp", "re", "ev", "r", and "rs". The default for type is "all" which computes all available formulas. |

`category_row` |
A flag to indicate that categories are in the rows. The analysis assumes that the categories are in the columns of the matrix. If the categories are in the rows and the entities in the columns, then the argument "category_row" has to be set to TRUE. The default value is FALSE. |

`dis` |
Optional square matrix of distances or dissimilarities between categories. It allows the user to provide her own matrix of dissimilarities between categories. The category names have to be both in the rows and in the columns, and these must be the exact same names used by the categories in the argument "data". Only the upper triangle will be used. If the argument "dis" is not defined, and the user requires a measure that uses disparities (e.g. Rao), then a matrix of disparities is computed internally using the method defined by the argument 'method'. The default value is NULL. |

`method` |
The "rao-stirling" and "rao"-diversity indices use a disparity function to measure the distance between objects. If the user does not provide a matrix with disparities by using the argument 'dis', then a matrix of disparities is computed using the method specified in this argument (method). Possible values for this argument are distance or dissimilarity methods available in "proxy" package as for example "Euclidean", "Kullback" or "Canberra". This argument also accepts a similarity method available in the "proxy" package, as for example: "cosine", "correlation" or "Jaccard" among others. In the latter case, a correspondent transformation to a dissimilarity measure will be retrieved. A list of available methods can be queried by using the function |

`q` |
The parameter used for the hill numbers. This argument is also used for the Renyi entropy and HCDT entropy. The default value is 0. |

`alpha` |
Parameter for Rao-Stirling diversity. The default value is 1. |

`beta` |
Parameter for Rao-Stirling diversity. The default value is 1. |

`base` |
Base of the logarithm. Used in Entropy calculations. The default value is exp(1). |

### Details

Notation used in the following formulas: *N*, category count; *p_i*, proportion of entity comprises category *i*; *d_{ij}*, disparity between *i* and *j*; *q*,*α* and *β*, arguments.

The available diversity measures included in the package are listed above. The titles of the formulas are the possible mnemonic values that the argument "type" might take to compute that formula (i.e. diversity(data, type='variety') or diversity(data, type='v'):

**variety, v:**
Category counts per entity [MacArthur 1965]

*∑_i(p_i^0)*

.

**entropy, e:**
Shannon entropy per entity [Shannon 1948]

*- ∑_i(p_i \log p_i)*

**Herfindahl-Hirschman, hh, hhi:** The Herfindahl-Hirschman Index used in economy to measure the concentration of markets.

*∑_i(p_i^2)*

**gini-simpson, gs:**
Gini-Simpson index per object [Gini 1912]. This measure is also known as the Gibbs-Martin index or the Blau index in sociology, psychology and management studies.

*1 - ∑_i(p_i^2)*

**simpson, s:**
Simpson index per entity [Simpson 1949].

* D = ∑_i n_i(n_i-1) / N(N-1)*

When this measure is required, then also associated variations Simpson's Index of Diversity *1-D* and the Reciprocal Simpson *1/D* will be computed.

**hill-numbers, td,hn:**
Hill Numbers [Hill 1973]. This measure is *q* parameterized. When *q=1*, it results in the exponential of Shannon Entropy. Default for *q* is 0, this is the variety or richness.

*(∑_ip_{i}^q)^{1/(1-q)}*

**berger-parker, bp:**
Berger-Parker index is equals to the maximum *p_i* value in the entity, i.e. the proportional abundance of the most abundant type. When this measure is required, the reciprocal measure is also computed.

**renyi, re:**
Renyi entropy per object. This measure is a generalization of the Shannon entropy parameterized by *q*. It corresponds to the logarithm of the hill numbers. The default value for *q* is 0.

*(1-q)^{-1} \log(∑_i p_i^q)*

**evenness, ev:**
Pielou evenness per object across categories [Pielou, 1969]. It is based in Shannon Entropy

*-∑_i(p_i \log p_i)/\log{v} *

**rao:**
Rao diversity.

*∑_{ij}d_{ij} p_i p_j *

**rao-stirling, rs:**
Rao-Stirling diversity per object across categories [Stirling, 2007]. Default values are *α=1* and *β=1*.
For the pairwise disparities the measure allows to consider the Jaccard Index, Euclidean distances, Cosine Similarity among others.

*∑_{ij}{d_{ij}}^α {(p_i p_j )}^β*

### Value

A data frame with diversity measures as columns for each entity.

### References

Gini, C. (1912). "Italian: Variabilita e mutabilita" 'Variability and Mutability', Memorie di metodologica statistica.

Hill, M. (1973). "Diversity and evenness: a unifying notation and its consequences". Ecology 54: 427-432.

MacArthur, R. (1965). "Patterns of Species Diversity". Biology Reviews 40: 510-533.

Pielou, E. (1969). "An Introduction to Mathematical Ecology". Wiley.

Shannon, C. (1948). "A Mathematical Theory of Communication". Bell entity Technical Journal 27 (3): 379-423.

Simpson, A. (1949). "Measurement of Diversity". Nature 163: 41-48.

Stirling, A. (2007). "A General Framework for Analysing Diversity in Science, Technology and Society". Journal of the Royal Society Interface 4: 707-719.

Rafols, I., & Meyer, M. (2009). Diversity and network coherence as indicators of interdisciplinarity: case studies in bionanoscience. Scientometrics, 82(2), 263-287.

Rafols, I. (2014). Knowledge Integration and Diffusion: Measures and Mapping of Diversity and Coherence. In Y. Ding, R. Rousseau, & D. Wolfram (Eds.), Measuring Scholarly Impact (pp. 169-190). Springer International Publishing.

Chavarro, D., Tang, P., & Rafols, I. (2014). Interdisciplinarity and research on local issues: evidence from a developing country. Research Evaluation, 23(3), 195-209.

### Examples

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | ```
data(pantheon)
diversity(pantheon)
diversity(pantheon, type='variety')
diversity(geese, type='berger-parker', category_row=TRUE)
#reading csv data matrix
path_to_file <- system.file("extdata", "PantheonMatrix.csv", package = "diverse")
X <- read_data(path = path_to_file)
diversity(data=X, type="gini")
diversity(data=X, type="rao-stirling", method="cosine")
diversity(data=X, type="all", method="jaccard")
#reading csv dataframe
path_to_file <- system.file("extdata", "PantheonEdges.csv", package = "diverse")
X <- read_data(path = path_to_file)
#hill numbers
diversity(data=X, type="td", q=1)
#rao stirling with differente arguments
diversity(data=X, type="rao-stirling", method="euclidean", alpha=0, beta=1)
#more than one diversity measure
diversity(data=X, type=c('e','ev','bp','s'))
``` |