Skip to contents

Bray-Curtis beta diversity metric. Sorenson is an alias for unweighted Bray-Curtis.

Usage

bray_curtis(counts, weighted = TRUE, pairs = NULL, cpus = n_cpus())

sorenson(counts, pairs = NULL, cpus = n_cpus())

Arguments

counts

An OTU abundance matrix where each column is a sample, and each row is an OTU. Any object coercible with as.matrix() can be given here, as well as phyloseq, rbiom, SummarizedExperiment, and TreeSummarizedExperiment objects.

weighted

If TRUE, the algorithm takes relative abundances into account. If FALSE, only presence/absence is considered.

pairs

Which combinations of samples should distances be calculated for? The default value (NULL) calculates all-vs-all. Provide a numeric or logical vector specifying positions in the distance matrix to calculate. See examples.

cpus

How many parallel processing threads should be used. The default, n_cpus(), will use all logical CPU cores.

Value

A dist object.

Calculation

In the formulas below, x and y are two columns (samples) from counts. n is the number of rows (OTUs) in counts.

$$D = \displaystyle \frac{\sum_{i = 1}^{n} |x_i - y_i|}{\sum_{i = 1}^{n} (x_i + y_i)}$$

  x <- c(4, 0, 3, 2, 6)
  y <- c(0, 8, 0, 0, 5)
  sum(abs(x-y)) / sum(x+y)
  #>  0.6428571

References

Sorenson T 1948. A method of establishing groups of equal amplitude in plant sociology based on similarity of species content. Kongelige Danske Videnskabernes Selskab, 5.

Bray JR and Curtis JT 1957. An ordination of the upland forest communities of southern Wisconsin. Ecological Monographs, 27(4). doi:10.2307/1942268

Examples

    # Example counts matrix
    ex_counts
#>                   Saliva Gums Nose Stool
#> Streptococcus        162  793   22     1
#> Bacteroides            2    4    2   611
#> Corynebacterium        0    0  498     1
#> Haemophilus          180   87    2     1
#> Propionibacterium      1    1  251     0
#> Staphylococcus         0    1  236     1
    
    # Bray-Curtis weighted distance matrix
    bray_curtis(ex_counts)
#>          Saliva      Gums      Nose
#> Gums  0.5905768                    
#> Nose  0.9601770 0.9704797          
#> Stool 0.9916667 0.9906729 0.9926199
    
    # Bray-Curtis unweighted distance matrix
    bray_curtis(ex_counts, weighted = FALSE)
#>           Saliva       Gums       Nose
#> Gums  0.11111111                      
#> Nose  0.20000000 0.09090909           
#> Stool 0.33333333 0.20000000 0.09090909
    
    # Sorenson is the same as unweighted Bray-Curtis
    sorenson(ex_counts)
#>           Saliva       Gums       Nose
#> Gums  0.11111111                      
#> Nose  0.20000000 0.09090909           
#> Stool 0.33333333 0.20000000 0.09090909
    
    # Only calculate distances for A vs all.
    bray_curtis(ex_counts, pairs = 1:3)
#>          Saliva      Gums      Nose
#> Gums  0.5905768                    
#> Nose  0.9601770        NA          
#> Stool 0.9916667        NA        NA