Giter VIP home page Giter VIP logo

hillr's Introduction

DOI Build Status Coverage status CRAN status CRAN RStudio mirror downloads CRAN RStudio mirror downloads

hillR

This package contains R functions to calculate taxonomic, functional, and phylogenetic diversity and site similarity through Hill Numbers. The underlying methods are based on Chao, Chiu and Jost 2014 and Chiu & Chao 2014.

Installation

To install this package, run the following code:

install.packages("hillR")
# or install from Github
install.packages("hillR", repos = c('https://daijiang.r-universe.dev', 'https://cloud.r-project.org'))

Examples

dummy = FD::dummy
comm = dummy$abun
traits = dummy$trait
set.seed(123)
tree = ape::rtree(n = ncol(comm), tip.label = paste0("sp", 1:ncol(comm)))
library(hillR)

Calculate taxonomic, functional, and phylogenetic diversity of each site

hill_taxa(comm, q = 0) # taxonomic alpha diversity
##  com1  com2  com3  com4  com5  com6  com7  com8  com9 com10 
##     4     3     3     2     3     5     3     4     5     4

hill_func(comm, traits, q = 0) # functional alpha diversity
##           com1      com2      com3      com4      com5       com6      com7
## Q    0.4214846 0.2848834 0.3254045 0.2620026 0.3984715  0.4427648 0.3228745
## FDis 0.3481687 0.1670560 0.2375808 0.1146261 0.3211366  0.3302330 0.2532751
## D_q  4.0000000 3.0000000 3.0000000 2.0000000 3.0000000  5.0000000 3.0000000
## MD_q 1.6859383 0.8546501 0.9762135 0.5240052 1.1954145  2.2138241 0.9686236
## FD_q 6.7437533 2.5639502 2.9286406 1.0480104 3.5862436 11.0691207 2.9058708
##           com8       com9     com10
## Q    0.4045563  0.4515567 0.4177331
## FDis 0.2877931  0.3421687 0.3503927
## D_q  4.0000000  5.0000000 4.0000000
## MD_q 1.6182251  2.2577835 1.6709326
## FD_q 6.4729004 11.2889174 6.6837303

hill_phylo(comm, tree, q = 0) # phylogenetic alpha diversity
##     com1     com2     com3     com4     com5     com6     com7     com8 
## 5.430079 4.684280 4.461773 2.551395 5.830078 6.088533 4.763594 6.046474 
##     com9    com10 
## 6.262164 5.080340

Calculate taxonomic, functional, and phylogenetic diversity across multiple sites

hill_taxa_parti(comm, q = 0) # taxonomic diversity across all sites
##   q TD_gamma TD_alpha  TD_beta M_homog local_similarity region_similarity
## 1 0        8      3.6 2.222222    0.45        0.8641975         0.3888889

hill_func_parti(comm, traits, q = 0) # functional diversity across all sites
##   q raoQ_gamma FD_gamma FD_alpha FD_beta local_similarity region_similarity
## 1 0  0.4529152 29.66099 14.15941 2.09479        0.9889415         0.4720957

hill_phylo_parti(comm, tree, q = 0) # phylogenetic diversity across all sites
##   q PD_gamma PD_alpha  PD_beta local_similarity region_similarity
## 1 0 8.292885 5.119871 1.619745        0.9311395          0.574868

Calculate pairwise taxonomic, functional, and phylogenetic diversity

# pairwise taxonomic diversity
hill_taxa_parti_pairwise(comm, q = 0, show_warning = FALSE, .progress = FALSE) 
## # A tibble: 45 × 8
##        q site1 site2 TD_gamma TD_alpha TD_beta local_similarity
##    <dbl> <chr> <chr>    <dbl>    <dbl>   <dbl>            <dbl>
##  1     0 com1  com2         6      3.5    1.71            0.286
##  2     0 com1  com3         5      3.5    1.43            0.571
##  3     0 com2  com3         5      3      1.67            0.333
##  4     0 com1  com4         5      3      1.67            0.333
##  5     0 com2  com4         5      2.5    2               0    
##  6     0 com3  com4         4      2.5    1.6             0.4  
##  7     0 com1  com5         6      3.5    1.71            0.286
##  8     0 com2  com5         4      3      1.33            0.667
##  9     0 com3  com5         6      3      2               0    
## 10     0 com4  com5         4      2.5    1.6             0.4  
## # ℹ 35 more rows
## # ℹ 1 more variable: region_similarity <dbl>
# pairwise functional diversity
hill_func_parti_pairwise(comm, traits, q = 0, show_warning = FALSE, .progress = FALSE) 
## # A tibble: 45 × 8
##        q site1 site2 FD_gamma FD_alpha FD_beta local_similarity
##    <dbl> <chr> <chr>    <dbl>    <dbl>   <dbl>            <dbl>
##  1     0 com1  com2     15.9     10.3     1.54            0.821
##  2     0 com1  com3     10.7      7.96    1.35            0.883
##  3     0 com2  com3     11.1      7.03    1.58            0.807
##  4     0 com1  com4     11.6      7.90    1.47            0.843
##  5     0 com2  com4     11.7      6.85    1.70            0.765
##  6     0 com3  com4      6.60     4.45    1.48            0.839
##  7     0 com1  com5     17.3     11.3     1.54            0.821
##  8     0 com2  com5      7.86     5.92    1.33            0.891
##  9     0 com3  com5     16.2      9.72    1.66            0.780
## 10     0 com4  com5      8.00     5.32    1.50            0.832
## # ℹ 35 more rows
## # ℹ 1 more variable: region_similarity <dbl>
# pairwise phylogenetic diversity
hill_phylo_parti_pairwise(comm, tree, q = 0, show_warning = FALSE, .progress = FALSE) 
## # A tibble: 45 × 8
##        q site1 site2 PD_gamma PD_alpha PD_beta local_similarity
##    <dbl> <chr> <chr>    <dbl>    <dbl>   <dbl>            <dbl>
##  1     0 com1  com2      6.79     5.06    1.34           0.657 
##  2     0 com1  com3      6.14     4.95    1.24           0.759 
##  3     0 com2  com3      6.75     4.57    1.48           0.523 
##  4     0 com1  com4      6.38     3.99    1.60           0.400 
##  5     0 com2  com4      7.13     3.62    1.97           0.0284
##  6     0 com3  com4      5.42     3.51    1.54           0.455 
##  7     0 com1  com5      7.04     5.63    1.25           0.750 
##  8     0 com2  com5      6.44     5.15    1.25           0.751 
##  9     0 com3  com5      7.71     5.15    1.50           0.502 
## 10     0 com4  com5      6.42     4.19    1.53           0.467 
## # ℹ 35 more rows
## # ℹ 1 more variable: region_similarity <dbl>

Licenses

Licensed under the MIT license. (More information here.)

Citation

Please cite this package if you use it. The citation information can be obtained by running citation('hillR') in R.

Li, (2018). hillR: taxonomic, functional, and phylogenetic diversity and similarity through Hill Numbers. Journal of Open Source Software, 3(31), 1041. https://doi.org/10.21105/joss.01041

@Article{,
    title = {hillR: taxonomic, functional, and phylogenetic diversity and similarity through Hill Numbers},
    author = {Daijiang Li},
    journal = {Journal of Open Source Software},
    year = {2018},
    volume = {3},
    number = {31},
    pages = {1041},
    url = {https://doi.org/10.21105/joss.01041},
 }

Reference

Contributing

Contributions are welcome. You can provide comments and feedback or ask questions by filing an issue on Github here or making pull requests.

hillr's People

Contributors

ajrominger avatar akpaulson avatar bisaloo avatar daijiang avatar katrinleinweber avatar p-neves avatar rekyt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

hillr's Issues

hill_func() is returning D_q higher than species richness

I noticed that hill_func() is returning values of D_q (functional hill number, the effective number of equally abundant and functionally equally distinct species) higher than the values of taxonomic species richness, which should not be possible (it is not possible to have more equally abundant and functionally equally distinct species than the number of species).
I was afraid it was something wrong with my data, but I checked the example codes with "dummy" dataset from FD package and it happens too.

library(FD)
library(hillR)

dummy = FD::dummy

hill_func(comm = dummy$abun, traits = dummy$trait, q = 0)
hill_taxa(comm = dummy$abun, q = 0)

And the outputs are:

          com1      com2      com3      com4      com5      com6      com7      com8       com9
Q    0.4016663 0.1922618 0.2780442 0.1146261 0.3816159  0.404177 0.2934143 0.3343662  0.4156546
FDis 0.3481687 0.1670560 0.2375808 0.1146261 0.3211366  0.330233 0.2532751 0.2877931  0.3421687
D_q  4.0974923 3.6518111 3.2454591 3.0237158 3.0655375  5.233241 3.1470056 4.3998540  5.2114653
MD_q 1.6458245 0.7021037 0.9023810 0.3465969 1.1698580  2.115156 0.9233765 1.4711625  2.1661695
FD_q 6.7437533 2.5639502 2.9286406 1.0480104 3.5862436 11.069121 2.9058708 6.4729004 11.2889174
         com10
Q    0.3844765
FDis 0.3503927
D_q  4.1694097
MD_q 1.6030400
FD_q 6.6837303

 com1  com2  com3  com4  com5  com6  com7  com8  com9 com10 
    4     3     3     2     3     5     3     4     5     4 

Every community here has D_q higher than species richness for q = 0. Something seems wrong here, but I could not find the problem in the codes. Can you help with this?

div_by_sp

In the man files for hill_func.Rd, there is an argument {div_by_sp}{as FD calculated in this way will be highly correlated with taxonomic diversity, one simple way to correct this is to divide the results by the number of species. Default is FALSE.}
I'm not sure it it is really ok to simply correct by dividing the results by the number of species? Perhaps this issues requires a bit more context for the user. A common approach to dealing with this issue is to use null models to calculate standardized effect sizes (Swenson 2014). How would the specification of this argument in hillR intersect with this approach (or not)?

@sydnerecord I think we should move the review here.

Problems installing

Hi, I just tried to install the package and got the error "Installation failed: Not found (404)". I'd be grateful for any suggestions as I'd really like to run the package. Thank you!

`hill_func` throws error when only one trait is measured

Hi Daijiang! Just wanted to let you know that hill_func throws an error when you give it data on only one trait (i.e. when the traits argument is a data.frame with one column). This applies to hill_func_parti as well. I created a pull request that fixes this issue: #16

object 'comm' not found

I'm trying to run the hillR code using the comm data within the package. I have installed and loaded the package but I get the notification that object "comm" is not found when I try to run the code.

This is what I'm seeing:

library(hillR)
hill_taxa(comm, q = 0, MARGIN = 1, base = exp(1))
Error in as.matrix(comm) : object 'comm' not found

`hill_func` fails for all numeric traits

take the following reproducible example:

library(hillR)
library(vegan)

BCI <- data(BCI, package = "vegan") 

ncol(BCI)

BCI_trait <- matrix(rnorm(ncol(BCI)*10, 5, 1), ncol = 10)
rownames(BCI_trait) <- colnames(BCI)

# fails
hillR::hill_func(BCI, BCI_trait, q = 1, fdis = FALSE)

# add random character trait
BCI_trait <- cbind(BCI_trait, sample(LETTERS, ncol(BCI), replace = T))

#works
hillR::hill_func(BCI, BCI_trait, q = 1, fdis = FALSE)

There are 2 additional errors that you might want to have a look at:

  1. I have to set fdis = FALSE as otherwise the function fails
  2. If I provide a distance matrix I have to set traits_as_is = T and checkdata = F otherwise the checks throw an error..

Calculated Rao Q in hillR and FD packages are different

Hi,
I test your code and the trial data "dummy", comparing the results of FD package and hillR, The reported Rao Q are different, because of dependencies of hill functional diversity numbers on RaoQ, I think the calculated Functional hill numbers in your code are not correct.
Regards,
Bagher

error in `hill_taxa_parti` if sites have the same and only one species

M is a matrix with two sites that both have the same species and only one species in common.

M <- matrix(c(0,1,0,0,1,0), byrow = T, ncol = 3)
hill_taxa_parti(M)

if we try to calculate beta diversity for M we get the following error:

Error in !all.equal(sum(comm_gamma), 1) : invalid argument type

I suspect this occurs because Some species in comm data were not observed in any site, delete them... which likely reduces the matrix to a vector which might cause an error upstream which causes sum(comm_gamma) to give NA or NULL. This is, however, speculation as I haven't found which function calls this code (haven't dug deep though yet)

edit: forgot to say - thank you so much for this package it was thoroughly missing!

hill_func with categorical traits

HI!
Thanks for implementing this package.
I have one question, is it possible to run the hill_func function using categorical traits?

Thanks a lot!

Installation instructions - dependencies

Currently the devtool option automates the dependencies, but for posterity it may be helpful to also list the dependencies and package version numbers in case there are ever issues with devtools.

hill_func() returns same-ish values regardles of similarity

So - I was trying to futz with this package to understand how it works, and find that D_Q returns about the same number regardless of the trait values. Below is a reprex:

library(hillR)
library(mvtnorm)

#make a bunch of species where I can determine how
#correlated their abundances are
cmat <-  matrix(rep(0.9,25), ncol = 5)
diag(cmat) <- 1

#the data
z <- mvtnorm::rmvnorm(10, rep(10, 5),
                      cmat)

#check how correlated they are
dist(t(z))

#turn it into a data frame
z <- as.data.frame(z)

#traits
tdat <- data.frame(
  t1 = c(rep(1, 4), 1.1),
  t2 = c(rep(1, 4), 1.1),
  t3 = c(rep(1, 4), 1.1)
)

rownames(tdat) <- names(z)

hill_func(z,
          tdat,
          q = 2,
          fdis = FALSE)

This returns D_q values of ~ 5 for all plots.

          [,1]      [,2]      [,3]      [,4]      [,5]
Q     1.246470  1.247999  1.260358  1.184194  1.254891
D_q   4.984464  4.982051  4.955814  5.108023  4.965449
MD_q  6.212983  6.217595  6.246097  6.048892  6.231096
FD_q 30.968389 30.976372 30.954496 30.897877 30.940189
          [,6]      [,7]      [,8]      [,9]     [,10]
Q     1.230411  1.234581  1.246552  1.226033  1.255536
D_q   5.017417  5.009502  4.984898  5.025869  4.967230
MD_q  6.173487  6.184635  6.213932  6.161883  6.236534
FD_q 30.974956 30.981939 30.975818 30.968813 30.978294

This seems odd. Note how I put together the traits. Now, if I crank up that 1.1 to 100 to make things very different, I still get D_Q of around 5. If I change it to vectors of 5 uniform numbers, D_q is still ~ 5. If I change the correlation matrix of species to be very uncorrelated (0.001), I still get a D_q of around 5 in each plot. I would expect somewhere along the line, I should get D_q values in the 1-2 range.

What is wrong here? Or am I fundamentally missing something (always possible!)

Note, I even tried zeroing out some species

z[sample(1:10, 10, replace = TRUE), sample(1:5, 10, replace = TRUE)] <- 0

And still got D_q values of ~ 5 for all plots.

Something..... seems wrong here.

R session abored

When the R code run the hill_phylo(comm, tree, q = 0) in version 0.5.1, the R studio is always in session aborted. although, I had tried from the R4.1.3 to R4.2.1, even re-install the R and the RStudio. I am looking forward to getting your help to solve this problem, thank you!!!
61bd77ae291a154837da86d8655296a

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.