# How to simplify the formula gini index in QIIME2? For example write in a R script.

**URL:** <https://forum.qiime2.org/t/how-to-simplify-the-formula-gini-index-in-qiime2-for-example-write-in-a-r-script/31784>\
**Category:** General Discussion\
**Tags:** diversity, alpha\
**Created:** [October 31, 2024, 10:19am UTC](https://forum.qiime2.org/t/how-to-simplify-the-formula-gini-index-in-qiime2-for-example-write-in-a-r-script/31784 "2024-10-31T10:19:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yangyue](https://forum.qiime2.org/user_avatar/forum.qiime2.org/yangyue/32/19097_2.png) [@yangyue](https://forum.qiime2.org/u/yangyue)\
**Post date:** [October 31, 2024, 10:19am UTC](https://forum.qiime2.org/t/how-to-simplify-the-formula-gini-index-in-qiime2-for-example-write-in-a-r-script/31784/1 "2024-10-31T10:19:25Z")

</div>

The result of Gini index on QIIME2 is puzzling！  
I've used the following to calculate Gini index on QIIME2, for example:

```auto
qiime diversity alpha \
--i-table table1_1m2-featuretable.qza \
--p-metric gini_index \
--o-alpha-diversity table1_1m2-featuretable-gini_index.qza
qiime metadata tabulate \
  --m-input-file table1_1m2-featuretable-gini_index.qza \
qiime tools view table1_1m2-featuretable-all.qzv

```

I've checked [skbio.diversity.alpha.gini\_index — scikit-bio 0.6.2 documentation](https://scikit.bio/docs/latest/generated/skbio.diversity.alpha.gini_index.html#skbio.diversity.alpha.gini_index) these days, and I have visted its related code website [scikit-bio/skbio/diversity/alpha/\_gini.py at 0.6.2 · scikit-bio/scikit-bio · GitHub](https://github.com/scikit-bio/scikit-bio/blob/0.6.2/skbio/diversity/alpha/_gini.py), it seems simple, but I can't understand, could it be written in R script format?  
I've tried to calculate it (Gini index) in R, and the following are four methods I've run in R, however their results are in variant, and none of them can match the result calculated by QIIME2 (name of the data is called D1):

```auto
# Using two packages
library("ineq");
library("DescTools")

```

```auto
# Method 1:
Gini_D <- matrix(nrow = 1, ncol = nrow(D1))
for (i in c(1:nrow(D1))) {
  Gini_D[i] <- ineq::Gini(factor(as.numeric(D1[i,])))
  rownames(Gini_D)<-"Gini_D"
}
Gini_D

```

```auto
# Method 2:
Gini_D <- matrix(nrow = 1, ncol = nrow(D1))
for (i in c(1:nrow(D1))) {
  Gini_D[i] <- DescTools::Gini(as.numeric(D1[i,]))
  rownames(Gini_D)<-"Gini_D"
}
Gini_D

```

```auto
# Method 3:
Gini_D <- matrix(nrow = 1, ncol = nrow(D1))
for (i in c(1:nrow(D1))) {
  Gini_D[i] <- DescTools::GiniSimpson(factor(as.numeric(D1[i,])))
  rownames(Gini_D)<-"Gini_D"
}
Gini_D

```

```auto
# Method 4:
Gini_D <- matrix(nrow = 1, ncol = nrow(D1))
for (i in c(1:nrow(D1))) {
  Gini_D[i] <- DescTools::GiniDeltas(factor(as.numeric(D1[i,])))
  rownames(Gini_D)<-"Gini_D"
}
Gini_D

```

---

<div class="post-metadata">

**Author:** ![colinbrislawn](https://forum.qiime2.org/user_avatar/forum.qiime2.org/colinbrislawn/32/6221_2.png) [@colinbrislawn](https://forum.qiime2.org/u/colinbrislawn)\
**Post date:** [November 1, 2024, 3:28am UTC](https://forum.qiime2.org/t/how-to-simplify-the-formula-gini-index-in-qiime2-for-example-write-in-a-r-script/31784/3 "2024-11-01T03:28:34Z")

</div>

In case you have not found this, here's the docs for DescTools:

> **[Gini-Simpson Coefficient, Gini-Deltas coefficient and Hunter-Gaston Index —...](https://andrisignorell.github.io/DescTools/reference/GiniSimpson.html)**
>
> Calculate the Gini-Simpson coefficient, the Gini variant proposed by Deltas and the Hunter-Gaston Index.

> Calculate the Gini-Simpson coefficient, the Gini variant proposed by Deltas and the Hunter-Gaston Index.

These variants are _supposed_ to be different.

I love a benchmark, but don't forget the lit review!

Check the docs for `ineq::Gini()` and `DescTools::Gini` and report back!

---

<div class="post-metadata">

**Author:** ![yangyue](https://forum.qiime2.org/user_avatar/forum.qiime2.org/yangyue/32/19097_2.png) [@yangyue](https://forum.qiime2.org/u/yangyue)\
**Post date:** [November 1, 2024, 8:36am UTC](https://forum.qiime2.org/t/how-to-simplify-the-formula-gini-index-in-qiime2-for-example-write-in-a-r-script/31784/4 "2024-11-01T08:36:37Z")

</div>

Thank you! It has been resolved.  
The resolve code as below, I've set an origial point (0,0) with each calculation, and set areaB as a trapezoids, D1 is the datasheet.

```auto
Gini_D <- matrix(nrow = 1, ncol = nrow(D1))
under_areaB_array <- matrix(nrow = 1, ncol = nrow(D1))
upper_areaA_array <- matrix(nrow = 1, ncol = nrow(D1))
for (i in c(1:nrow(D1))) {
  interval <- 1/ncol(D1)
  area_total <- 1*1/2
  orderedD1 <- D1[i,][order(as.numeric(D1[i,]))]
  orderedD1_accum <- cumsum(as.numeric(orderedD1))
  orderedD1_accum_rel <- c(0,as.numeric(orderedD1_accum/sum(orderedD1)))
  under_areaB_array[i] <- 0.5*interval*(0+sum(orderedD1_accum_rel[c(2,3,4,5)]*2)+orderedD1_accum_rel[6]) ##Please change this line with the actural value of ncol(D1)
  upper_areaA_array[i] <- area_total-as.numeric(under_areaB_array[i])
  Gini_D[i] <- upper_areaA_array[i]/area_total-interval
  rownames(Gini_D)<-"Gini_D"
}
Gini_D

```

Now, the result matches with the QIIME2 output. Congratulations! 🤝  
Please arise your suggestions. Thank you!
