# Shannon Index Varies with Phyloseq

**URL:** <https://forum.qiime2.org/t/shannon-index-varies-with-phyloseq/5721>\
**Category:** User Support\
**Created:** [August 23, 2018, 2:23am UTC](https://forum.qiime2.org/t/shannon-index-varies-with-phyloseq/5721 "2018-08-23T02:23:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muskox](https://forum.qiime2.org/user_avatar/forum.qiime2.org/muskox/32/2144_2.png) [@Muskox](https://forum.qiime2.org/u/Muskox)\
**Post date:** [August 23, 2018, 2:23am UTC](https://forum.qiime2.org/t/shannon-index-varies-with-phyloseq/5721/1 "2018-08-23T02:23:20Z")

</div>

\*resending topic to moderators as I accidentally sent first topic w/o finishing the post (I'm so sorry!!)  
RESEND  
Please delete if not allowed, as I reference the statistical procedures of two programs

Hi everyone,

I am working with a 16S dataset of ~80 individuals. I have calculated alpha diversity in QIIME2 using the function

`qiime diversity alpha \ --i-table filtered-table-merged.qza \ --p-metric shannon \ --o-alpha-diversity shannon_vector.qza`

and in the phyloseq package in R using the function

`plot_richness(df)`

The phyloseq bow-and-whisker plot that is produced by this function estimates Shannon index values between 4 and 6, while QIIME2 calculates the Shannon index per individual between 6 and 8. I am concerned with the dissimilarity between the two programs. Both programs are using the same dataset as input. I am attaching the dataset I used with QIIME2 and with the singletons removed (because I thought this might have resulted in the lower Shannon value calculated by phyloseq) [SUBSET\_otu\_table\_w\_tax\_qiime\_merged\_nosingletons.txt](https://cdck-file-uploads-global.s3.dualstack.us-west-2.amazonaws.com/flex002/uploads/qiime21/original/2X/3/3da153ead352af32d0bfe3ed26d59691c0b9e743.txt) (3.9 KB)

From what I understand, both of the programs use the natural logarithm and should calculate the same value for the same metrics. I have tried comparing rarefied vs nonrarefied datasets, as well as unfiltered datasets vs. datasets filtered to remove singletons but the results are inconsistent with QIIME2 (range from 6-8). I was hoping somebody would be able to shed some light on this matter!

---

<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:** [August 23, 2018, 5:44am UTC](https://forum.qiime2.org/t/shannon-index-varies-with-phyloseq/5721/2 "2018-08-23T05:44:06Z")

</div>

> [@Muskox](#):
>
> I am concerned with the dissimilarity between the two programs.

Wow! Me too! 😱

Let's see what the qiime devs say. The Phyloseq [code](https://github.com/joey711/phyloseq/blob/master/R/extend_vegan.R#L241) just calls `vegan::diversity()`, and vegan should be rock solid, but phyloseq does some preprocessing stuff like dropping OTUs that are not shared by both the OTU table and the tree, so upstream stuff could change the results even if the Shannon calculation is the same.

Let's see what the Qiime devs say...

Colin

---

<div class="post-metadata">

**Author:** ![Nicholas\_Bokulich](https://forum.qiime2.org/user_avatar/forum.qiime2.org/nicholas_bokulich/32/19937_2.png) [@Nicholas\_Bokulich](https://forum.qiime2.org/u/Nicholas_Bokulich)\
**Post date:** [August 23, 2018, 4:34pm UTC](https://forum.qiime2.org/t/shannon-index-varies-with-phyloseq/5721/3 "2018-08-23T16:34:00Z")

</div>

Hi @Muskox,  
I recently ran into the exact same issue with Shannon diversity estimates with 2 different programs (except it was with another program, not phyloseq)

> [@Muskox](#):
>
> From what I understand, both of the programs use the natural logarithm and should calculate the same value for the same metrics

:qiime2: uses scikit-bio for calculating these diversity metrics — and [it looks like scikit-bio Shannon uses log base 2](https://github.com/biocore/scikit-bio/blob/master/skbio/diversity/alpha/_base.py#L859-861) not log base e.

Other than that, @colinbrislawn's suggestions re: how phyloseq processes feature tables upstream could add to the dissimilarity in results.

I hope that helps!

---

<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:** [August 23, 2018, 6:32pm UTC](https://forum.qiime2.org/t/shannon-index-varies-with-phyloseq/5721/4 "2018-08-23T18:32:33Z")

</div>

Interesting!

So now I'm wondering, what _is_ the canonical way to calculate Shannon's Diversity Index? Are we supposed to use `log2()` or `ln()`?

According to [the original paper written by Shannon (long PDF)](http://math.harvard.edu/~ctm/home/text/others/shannon/entropy/entropy.pdf), you can choose whatever base makes sense for your data:

> The choice of a logarithmic base corresponds to the choice of a unit for measuring information. If the base 2 is used the resulting units may be called binary digits, or more briefly bits...  
> If the base 10 is used the units may be called decimal digits.

Why is scikit-bio measuring bits? 🤔 Does anyone want to start a heated argument about logarithmic bases with me?

Colin

---

<div class="post-metadata">

**Author:** ![Nicholas\_Bokulich](https://forum.qiime2.org/user_avatar/forum.qiime2.org/nicholas_bokulich/32/19937_2.png) [@Nicholas\_Bokulich](https://forum.qiime2.org/u/Nicholas_Bokulich)\
**Post date:** [August 23, 2018, 7:05pm UTC](https://forum.qiime2.org/t/shannon-index-varies-with-phyloseq/5721/5 "2018-08-23T19:05:10Z")

</div>

> [@colinbrislawn](#):
>
> So now I’m wondering, what _is_ the canonical way to calculate Shannon’s Diversity Index? Are we supposed to use `log2()` or `ln()` ?

I think Shannon has answered that for us — there is no canonical way, and the [vegan R docs](http://cc.oulu.fi/~jarioksa/softhelp/vegan/html/diversity.html) seem to say as much.

> [@colinbrislawn](#):
>
> Why is scikit-bio measuring bits?

Probably because that's the most common way to measure [entropy](https://en.wikipedia.org/wiki/Entropy_(information_theory)) (a.k.a. Shannon's H) in information theory. So this is probably rooted in the different traditions of computer science vs. ecology.

But that's just a bit of speculation 😉

---

<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:** [August 23, 2018, 7:51pm UTC](https://forum.qiime2.org/t/shannon-index-varies-with-phyloseq/5721/6 "2018-08-23T19:51:33Z")

</div>

Good find in the Vegan docs! Looks like we have reached a consensus: there is no consensus about what base to use then calculating Shannon's Diversity Index.

I can see why the defaults would be different between computer science vs. ecology. 💻 🌍

And hopefully, this also answers @Muskox's question!

Colin

---

<div class="post-metadata">

**Author:** ![Muskox](https://forum.qiime2.org/user_avatar/forum.qiime2.org/muskox/32/2144_2.png) [@Muskox](https://forum.qiime2.org/u/Muskox)\
**Post date:** [August 29, 2018, 3:44am UTC](https://forum.qiime2.org/t/shannon-index-varies-with-phyloseq/5721/7 "2018-08-29T03:44:02Z")

</div>

Wow @colinbrislawn and @Nicholas_Bokulich!!!! Thank you for helping me sort this out. It does seem like we agree to disagree on just one method. Interesting link to the phyloseq code - I thought you had to script any preprocessing steps and wasn't aware the program did some of that itself. Very useful insight from each of you, much appreciated!

---

<div class="post-metadata">

**Author:** ![system](https://forum-qiime2-org.s3.dualstack.us-west-2.amazonaws.com/original/3X/2/1/21af5fe23cb6f4579467c66a9ed94e55274ca7bd.svg) [@system](https://forum.qiime2.org/u/system)\
**Post date:** [September 29, 2018, 9:44am UTC](https://forum.qiime2.org/t/shannon-index-varies-with-phyloseq/5721/8 "2018-09-29T09:44:05Z")

</div>

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.
