Skip to contents

Outputs the phylogenetic distance between samples, based on phylogenetic distances of taxa in one sample to the taxa in the other

Usage

ph_comdist(
  sample,
  phylo,
  rand_test = FALSE,
  null_model = 0,
  randomizations = 999,
  abundance = TRUE
)

ph_comdistnt(
  sample,
  phylo,
  rand_test = FALSE,
  null_model = 0,
  randomizations = 999,
  abundance = TRUE
)

Arguments

sample

(data.frame/character) sample data.frame or path to a sample file

phylo

(character/phylo) One of: phylogeny as a newick string (will be written to a temp file) - OR path to file with a newick string - OR a an ape phylo object. required.

rand_test

(logical) do you want to use null models? Default: FALSE

null_model

(integer) which null model to use. See Details.

randomizations

(numeric) number of randomizations. Default: 999

abundance

(logical) If TRUE (default) computed accounting for abundance. Otherwise, uses presence-absence.

Value

data.frame or a list of data.frame's if use null models

Details

See phylocomr-inputs for expected input formats

Null models

  • 0 - Phylogeny shuffle: This null model shuffles species labels across the entire phylogeny. This randomizes phylogenetic relationships among species.

  • 1 - Species in each sample become random draws from sample pool: This null model maintains the species richness of each sample, but the identities of the species occurring in each sample are randomized. For each sample, species are drawn without replacement from the list of all species actually occurring in at least one sample. Thus, species in the phylogeny that are not actually observed to occur in a sample will not be included in the null communities

  • 2 - Species in each sample become random draws from phylogeny pool: This null model maintains the species richness of each sample, but the identities of the species occurring in each sample are randomized. For each sample, species are drawn without replacement from the list of all species in the phylogeny pool. All species in the phylogeny will have equal probability of being included in the null communities. By changing the phylogeny, different species pools can be simulated. For example, the phylogeny could include the species present in some larger region.

  • 3 - Independent swap: The independent swap algorithm (Gotelli and Entsminger, 2003); also known as ‘SIM9’ (Gotelli, 2000) creates swapped versions of the sample/species matrix.

Taxon name case

In the sample table, if you're passing in a file, the names in the third column must be all lowercase; if not, we'll lowercase them for you. If you pass in a data.frame, we'll lowercase them for your. All phylo tip/node labels are also lowercased to avoid any casing problems

Examples

sfile <- system.file("examples/sample_comstruct", package = "phylocomr")
pfile <- system.file("examples/phylo_comstruct", package = "phylocomr")

# from data.frame
sampledf <- read.table(sfile, header = FALSE,
  stringsAsFactors = FALSE)
phylo_str <- readLines(pfile)
ph_comdist(sample = sampledf, phylo = phylo_str)
#> # A tibble: 6 × 7
#>   name    clump1 clump2a clump2b clump4  even random
#>   <chr>    <dbl>   <dbl>   <dbl>  <dbl> <dbl>  <dbl>
#> 1 clump1    4.25    6.75    8.08   8.71  8.06   8.05
#> 2 clump2a   6.75    4.94    8.72   8.42  8.06   7.82
#> 3 clump2b   8.08    8.72    5.83   7.36  8.06   7.95
#> 4 clump4    8.71    8.42    7.36   6.94  7.88   8.24
#> 5 even      8.06    8.06    8.06   7.87  7.75   8   
#> 6 random    8.05    7.82    7.95   8.24  8      7.11
ph_comdistnt(sample = sampledf, phylo = phylo_str)
#> # A tibble: 6 × 7
#>   name    clump1 clump2a clump2b clump4  even random
#>   <chr>    <dbl>   <dbl>   <dbl>  <dbl> <dbl>  <dbl>
#> 1 clump1    2       4.17    4.83   6     4.75   4.88
#> 2 clump2a   4.17    2       6      4.33  4.5    4.62
#> 3 clump2b   4.83    6       2      3     4      3.94
#> 4 clump4    6       4.33    3      2     2      4.10
#> 5 even      4.75    4.5     4      2     6      2.62
#> 6 random    4.88    4.62    3.94   4.10  2.62   4.88
ph_comdist(sample = sampledf, phylo = phylo_str, rand_test = TRUE)
#> $obs
#>      name   clump1  clump2a  clump2b   clump4     even   random
#> 1  clump1 4.250000 6.749999 8.083335 8.708335 8.062500 8.046875
#> 2 clump2a 6.750001 4.944444 8.722219 8.416661 8.062505 7.822916
#> 3 clump2b 8.083340 8.722218 5.833330 7.361108 8.062505 7.947916
#> 4  clump4 8.708339 8.416662 7.361108 6.944441 7.875004 8.239582
#> 5    even 8.062500 8.062500 8.062496 7.874995 7.750000 8.000000
#> 6  random 8.046875 7.822917 7.947919 8.239586 8.000000 7.109375
#> 
#> $null_mean
#>      name   clump1  clump2a  clump2b   clump4     even   random
#> 1  clump1 7.267392 7.972866 7.969661 8.156934 8.057714 8.125532
#> 2 clump2a 7.972866 7.159024 8.081661 7.979206 8.066652 8.195719
#> 3 clump2b 7.969661 8.081661 7.178259 7.746609 8.064397 8.023438
#> 4  clump4 8.156934 7.979206 7.746609 7.165974 7.806639 8.018419
#> 5    even 8.057714 8.066652 8.064397 7.806639 7.281719 7.989083
#> 6  random 8.125532 8.195719 8.023438 8.018419 7.989083 7.022476
#> 
#> $null_sd
#>      name   clump1  clump2a  clump2b   clump4     even   random
#> 1  clump1 0.294767 0.225718 0.213368 0.238877 0.210817 0.236426
#> 2 clump2a 0.225718 0.315629 0.247145 0.242299 0.218824 0.263069
#> 3 clump2b 0.213368 0.247145 0.298411 0.235539 0.206874 0.246979
#> 4  clump4 0.238877 0.242299 0.235539 0.323362 0.222175 0.253752
#> 5    even 0.210817 0.218824 0.206874 0.222175 0.306682 0.236570
#> 6  random 0.236426 0.263069 0.246979 0.253752 0.236570 0.343427
#> 
#> $NRI_or_NTI
#>      name    clump1   clump2a   clump2b    clump4      even    random
#> 1  clump1 10.236538  5.417685 -0.532761 -2.308305 -0.022704  0.332693
#> 2 clump2a  5.417672  7.016409 -2.591827 -1.805436  0.018954  1.417131
#> 3 clump2b -0.532783 -2.591823  4.506969  1.636675  0.009146  0.305787
#> 4  clump4 -2.308321 -1.805440  1.636673  0.685091 -0.307710 -0.871570
#> 5    even -0.022704  0.018976  0.009188 -0.307670 -1.526924 -0.046148
#> 6  random  0.332693  1.417129  0.305774 -0.871585 -0.046148 -0.253035
#> 
ph_comdistnt(sample = sampledf, phylo = phylo_str, rand_test = TRUE)
#> $obs
#>      name   clump1  clump2a  clump2b   clump4  even   random
#> 1  clump1 2.000000 4.166667 4.833334 6.000000 4.750 4.875000
#> 2 clump2a 4.166667 2.000000 6.000000 4.333333 4.500 4.625000
#> 3 clump2b 4.833333 6.000000 2.000000 3.000000 4.000 3.937500
#> 4  clump4 6.000000 4.333334 3.000000 2.000000 2.000 4.104167
#> 5    even 4.750000 4.500000 4.000000 2.000000 6.000 2.625000
#> 6  random 4.875000 4.625000 3.937500 4.104167 2.625 4.875000
#> 
#> $null_mean
#>      name   clump1  clump2a  clump2b   clump4     even   random
#> 1  clump1 4.688689 2.695598 2.677578 3.593593 3.329830 3.528612
#> 2 clump2a 2.695598 4.703537 2.969636 2.617702 3.344143 3.657159
#> 3 clump2b 2.677578 2.969636 4.721054 2.235237 3.348850 3.333333
#> 4  clump4 3.593593 2.617702 2.235237 4.735404 2.226126 3.342699
#> 5    even 3.329830 3.344143 3.348850 2.226126 4.738989 3.141223
#> 6  random 3.528612 3.657159 3.333333 3.342699 3.141223 4.715090
#> 
#> $null_sd
#>      name   clump1  clump2a  clump2b   clump4     even   random
#> 1  clump1 0.662520 0.478496 0.469232 0.607863 0.542942 0.619508
#> 2 clump2a 0.478496 0.676985 0.502633 0.444840 0.525448 0.608356
#> 3 clump2b 0.469232 0.502633 0.689157 0.406468 0.551602 0.568216
#> 4  clump4 0.607863 0.444840 0.406468 0.667793 0.395197 0.569462
#> 5    even 0.542942 0.525448 0.551602 0.395197 0.661914 0.557359
#> 6  random 0.619508 0.608356 0.568216 0.569462 0.557359 0.719303
#> 
#> $NRI_or_NTI
#>      name    clump1   clump2a   clump2b    clump4      even    random
#> 1  clump1  4.058276 -3.074359 -4.594223 -3.958797 -2.615695 -2.173317
#> 2 clump2a -3.074359  3.993495 -6.028983 -3.856735 -2.199755 -1.590912
#> 3 clump2b -4.594223 -6.028981  3.948379 -1.881483 -1.180471 -1.063270
#> 4  clump4 -3.958797 -3.856736 -1.881482  4.096185  0.572187 -1.337171
#> 5    even -2.615695 -2.199756 -1.180471  0.572187 -1.905098  0.926195
#> 6  random -2.173317 -1.590912 -1.063270 -1.337171  0.926195 -0.222312
#> 

# from files
sample_str <- paste0(readLines(sfile), collapse = "\n")
sfile2 <- tempfile()
cat(sample_str, file = sfile2, sep = '\n')
pfile2 <- tempfile()
cat(phylo_str, file = pfile2, sep = '\n')
ph_comdist(sample = sfile2, phylo = pfile2)
#> # A tibble: 6 × 7
#>   name    clump1 clump2a clump2b clump4  even random
#>   <chr>    <dbl>   <dbl>   <dbl>  <dbl> <dbl>  <dbl>
#> 1 clump1    4.25    6.75    8.08   8.71  8.06   8.05
#> 2 clump2a   6.75    4.94    8.72   8.42  8.06   7.82
#> 3 clump2b   8.08    8.72    5.83   7.36  8.06   7.95
#> 4 clump4    8.71    8.42    7.36   6.94  7.88   8.24
#> 5 even      8.06    8.06    8.06   7.87  7.75   8   
#> 6 random    8.05    7.82    7.95   8.24  8      7.11
ph_comdistnt(sample = sfile2, phylo = pfile2)
#> # A tibble: 6 × 7
#>   name    clump1 clump2a clump2b clump4  even random
#>   <chr>    <dbl>   <dbl>   <dbl>  <dbl> <dbl>  <dbl>
#> 1 clump1    2       4.17    4.83   6     4.75   4.88
#> 2 clump2a   4.17    2       6      4.33  4.5    4.62
#> 3 clump2b   4.83    6       2      3     4      3.94
#> 4 clump4    6       4.33    3      2     2      4.10
#> 5 even      4.75    4.5     4      2     6      2.62
#> 6 random    4.88    4.62    3.94   4.10  2.62   4.88
ph_comdist(sample = sfile2, phylo = pfile2, rand_test = TRUE)
#> $obs
#>      name   clump1  clump2a  clump2b   clump4     even   random
#> 1  clump1 4.250000 6.749999 8.083335 8.708335 8.062500 8.046875
#> 2 clump2a 6.750001 4.944444 8.722219 8.416661 8.062505 7.822916
#> 3 clump2b 8.083340 8.722218 5.833330 7.361108 8.062505 7.947916
#> 4  clump4 8.708339 8.416662 7.361108 6.944441 7.875004 8.239582
#> 5    even 8.062500 8.062500 8.062496 7.874995 7.750000 8.000000
#> 6  random 8.046875 7.822917 7.947919 8.239586 8.000000 7.109375
#> 
#> $null_mean
#>      name   clump1  clump2a  clump2b   clump4     even   random
#> 1  clump1 7.267392 7.972866 7.969661 8.156934 8.057714 8.125532
#> 2 clump2a 7.972866 7.159024 8.081661 7.979206 8.066652 8.195719
#> 3 clump2b 7.969661 8.081661 7.178259 7.746609 8.064397 8.023438
#> 4  clump4 8.156934 7.979206 7.746609 7.165974 7.806639 8.018419
#> 5    even 8.057714 8.066652 8.064397 7.806639 7.281719 7.989083
#> 6  random 8.125532 8.195719 8.023438 8.018419 7.989083 7.022476
#> 
#> $null_sd
#>      name   clump1  clump2a  clump2b   clump4     even   random
#> 1  clump1 0.294767 0.225718 0.213368 0.238877 0.210817 0.236426
#> 2 clump2a 0.225718 0.315629 0.247145 0.242299 0.218824 0.263069
#> 3 clump2b 0.213368 0.247145 0.298411 0.235539 0.206874 0.246979
#> 4  clump4 0.238877 0.242299 0.235539 0.323362 0.222175 0.253752
#> 5    even 0.210817 0.218824 0.206874 0.222175 0.306682 0.236570
#> 6  random 0.236426 0.263069 0.246979 0.253752 0.236570 0.343427
#> 
#> $NRI_or_NTI
#>      name    clump1   clump2a   clump2b    clump4      even    random
#> 1  clump1 10.236538  5.417685 -0.532761 -2.308305 -0.022704  0.332693
#> 2 clump2a  5.417672  7.016409 -2.591827 -1.805436  0.018954  1.417131
#> 3 clump2b -0.532783 -2.591823  4.506969  1.636675  0.009146  0.305787
#> 4  clump4 -2.308321 -1.805440  1.636673  0.685091 -0.307710 -0.871570
#> 5    even -0.022704  0.018976  0.009188 -0.307670 -1.526924 -0.046148
#> 6  random  0.332693  1.417129  0.305774 -0.871585 -0.046148 -0.253035
#> 
ph_comdistnt(sample = sfile2, phylo = pfile2, rand_test = TRUE)
#> $obs
#>      name   clump1  clump2a  clump2b   clump4  even   random
#> 1  clump1 2.000000 4.166667 4.833334 6.000000 4.750 4.875000
#> 2 clump2a 4.166667 2.000000 6.000000 4.333333 4.500 4.625000
#> 3 clump2b 4.833333 6.000000 2.000000 3.000000 4.000 3.937500
#> 4  clump4 6.000000 4.333334 3.000000 2.000000 2.000 4.104167
#> 5    even 4.750000 4.500000 4.000000 2.000000 6.000 2.625000
#> 6  random 4.875000 4.625000 3.937500 4.104167 2.625 4.875000
#> 
#> $null_mean
#>      name   clump1  clump2a  clump2b   clump4     even   random
#> 1  clump1 4.688689 2.695598 2.677578 3.593593 3.329830 3.528612
#> 2 clump2a 2.695598 4.703537 2.969636 2.617702 3.344143 3.657159
#> 3 clump2b 2.677578 2.969636 4.721054 2.235237 3.348850 3.333333
#> 4  clump4 3.593593 2.617702 2.235237 4.735404 2.226126 3.342699
#> 5    even 3.329830 3.344143 3.348850 2.226126 4.738989 3.141223
#> 6  random 3.528612 3.657159 3.333333 3.342699 3.141223 4.715090
#> 
#> $null_sd
#>      name   clump1  clump2a  clump2b   clump4     even   random
#> 1  clump1 0.662520 0.478496 0.469232 0.607863 0.542942 0.619508
#> 2 clump2a 0.478496 0.676985 0.502633 0.444840 0.525448 0.608356
#> 3 clump2b 0.469232 0.502633 0.689157 0.406468 0.551602 0.568216
#> 4  clump4 0.607863 0.444840 0.406468 0.667793 0.395197 0.569462
#> 5    even 0.542942 0.525448 0.551602 0.395197 0.661914 0.557359
#> 6  random 0.619508 0.608356 0.568216 0.569462 0.557359 0.719303
#> 
#> $NRI_or_NTI
#>      name    clump1   clump2a   clump2b    clump4      even    random
#> 1  clump1  4.058276 -3.074359 -4.594223 -3.958797 -2.615695 -2.173317
#> 2 clump2a -3.074359  3.993495 -6.028983 -3.856735 -2.199755 -1.590912
#> 3 clump2b -4.594223 -6.028981  3.948379 -1.881483 -1.180471 -1.063270
#> 4  clump4 -3.958797 -3.856736 -1.881482  4.096185  0.572187 -1.337171
#> 5    even -2.615695 -2.199756 -1.180471  0.572187 -1.905098  0.926195
#> 6  random -2.173317 -1.590912 -1.063270 -1.337171  0.926195 -0.222312
#>