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.264264 7.962314 7.959271 8.149316 8.072197 8.126862
#> 2 clump2a 7.962314 7.149953 8.088588 7.979760 8.071090 8.204987
#> 3 clump2b 7.959271 8.088588 7.144336 7.744441 8.073214 8.001472
#> 4  clump4 8.149316 7.979760 7.744441 7.180934 7.809204 8.014533
#> 5    even 8.072197 8.071090 8.073214 7.809204 7.295921 8.005740
#> 6  random 8.126862 8.204987 8.001472 8.014533 8.005740 7.013638
#> 
#> $null_sd
#>      name   clump1  clump2a  clump2b   clump4     even   random
#> 1  clump1 0.298313 0.240891 0.239985 0.222852 0.207033 0.240334
#> 2 clump2a 0.240891 0.332545 0.251983 0.245531 0.220665 0.266549
#> 3 clump2b 0.239985 0.251983 0.330639 0.236368 0.210280 0.259388
#> 4  clump4 0.222852 0.245531 0.236368 0.297288 0.214396 0.239459
#> 5    even 0.207033 0.220665 0.210280 0.214396 0.284895 0.216704
#> 6  random 0.240334 0.266549 0.259388 0.239459 0.216704 0.358257
#> 
#> $NRI_or_NTI
#>      name    clump1   clump2a   clump2b    clump4      even    random
#> 1  clump1 10.104381  5.032619 -0.516964 -2.508474  0.046838  0.332815
#> 2 clump2a  5.032608  6.632210 -2.514581 -1.779411  0.038905  1.433395
#> 3 clump2b -0.516983 -2.514577  3.965073  1.621765  0.050926  0.206474
#> 4  clump4 -2.508492 -1.779415  1.621763  0.795502 -0.306912 -0.939824
#> 5    even  0.046838  0.038927  0.050967 -0.306869 -1.593848  0.026488
#> 6  random  0.332815  1.433393  0.206461 -0.939840  0.026488 -0.267229
#> 
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.718218 2.669371 2.666267 3.562863 3.356231 3.539708
#> 2 clump2a 2.669371 4.707704 3.012346 2.617701 3.368268 3.700558
#> 3 clump2b 2.666267 3.012346 4.664667 2.229478 3.354755 3.361006
#> 4  clump4 3.562863 2.617701 2.229478 4.729232 2.239139 3.337122
#> 5    even 3.356231 3.368268 3.354755 2.239139 4.725726 3.162998
#> 6  random 3.539708 3.700558 3.361006 3.337122 3.162998 4.679680
#> 
#> $null_sd
#>      name   clump1  clump2a  clump2b   clump4     even   random
#> 1  clump1 0.680703 0.487090 0.470868 0.613910 0.533589 0.641232
#> 2 clump2a 0.487090 0.708354 0.519399 0.433929 0.544185 0.634078
#> 3 clump2b 0.470868 0.519399 0.679073 0.384527 0.539051 0.597440
#> 4  clump4 0.613910 0.433929 0.384527 0.689897 0.390442 0.569150
#> 5    even 0.533589 0.544185 0.539051 0.390442 0.652648 0.534272
#> 6  random 0.641232 0.634078 0.597440 0.569150 0.534272 0.734771
#> 
#> $NRI_or_NTI
#>      name    clump1   clump2a   clump2b    clump4      even    random
#> 1  clump1  3.993253 -3.073960 -4.602278 -3.969862 -2.612065 -2.082386
#> 2 clump2a -3.073960  3.822527 -5.752139 -3.953720 -2.079683 -1.457932
#> 3 clump2b -4.602277 -5.752137  3.923975 -2.003818 -1.197001 -0.964941
#> 4  clump4 -3.969862 -3.953721 -2.003817  3.955997  0.612482 -1.347703
#> 5    even -2.612065 -2.079684 -1.197001  0.612482 -1.952467  1.006974
#> 6  random -2.082386 -1.457932 -0.964941 -1.347703  1.006974 -0.265824
#> 

# 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.264264 7.962314 7.959271 8.149316 8.072197 8.126862
#> 2 clump2a 7.962314 7.149953 8.088588 7.979760 8.071090 8.204987
#> 3 clump2b 7.959271 8.088588 7.144336 7.744441 8.073214 8.001472
#> 4  clump4 8.149316 7.979760 7.744441 7.180934 7.809204 8.014533
#> 5    even 8.072197 8.071090 8.073214 7.809204 7.295921 8.005740
#> 6  random 8.126862 8.204987 8.001472 8.014533 8.005740 7.013638
#> 
#> $null_sd
#>      name   clump1  clump2a  clump2b   clump4     even   random
#> 1  clump1 0.298313 0.240891 0.239985 0.222852 0.207033 0.240334
#> 2 clump2a 0.240891 0.332545 0.251983 0.245531 0.220665 0.266549
#> 3 clump2b 0.239985 0.251983 0.330639 0.236368 0.210280 0.259388
#> 4  clump4 0.222852 0.245531 0.236368 0.297288 0.214396 0.239459
#> 5    even 0.207033 0.220665 0.210280 0.214396 0.284895 0.216704
#> 6  random 0.240334 0.266549 0.259388 0.239459 0.216704 0.358257
#> 
#> $NRI_or_NTI
#>      name    clump1   clump2a   clump2b    clump4      even    random
#> 1  clump1 10.104381  5.032619 -0.516964 -2.508474  0.046838  0.332815
#> 2 clump2a  5.032608  6.632210 -2.514581 -1.779411  0.038905  1.433395
#> 3 clump2b -0.516983 -2.514577  3.965073  1.621765  0.050926  0.206474
#> 4  clump4 -2.508492 -1.779415  1.621763  0.795502 -0.306912 -0.939824
#> 5    even  0.046838  0.038927  0.050967 -0.306869 -1.593848  0.026488
#> 6  random  0.332815  1.433393  0.206461 -0.939840  0.026488 -0.267229
#> 
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.718218 2.669371 2.666267 3.562863 3.356231 3.539708
#> 2 clump2a 2.669371 4.707704 3.012346 2.617701 3.368268 3.700558
#> 3 clump2b 2.666267 3.012346 4.664667 2.229478 3.354755 3.361006
#> 4  clump4 3.562863 2.617701 2.229478 4.729232 2.239139 3.337122
#> 5    even 3.356231 3.368268 3.354755 2.239139 4.725726 3.162998
#> 6  random 3.539708 3.700558 3.361006 3.337122 3.162998 4.679680
#> 
#> $null_sd
#>      name   clump1  clump2a  clump2b   clump4     even   random
#> 1  clump1 0.680703 0.487090 0.470868 0.613910 0.533589 0.641232
#> 2 clump2a 0.487090 0.708354 0.519399 0.433929 0.544185 0.634078
#> 3 clump2b 0.470868 0.519399 0.679073 0.384527 0.539051 0.597440
#> 4  clump4 0.613910 0.433929 0.384527 0.689897 0.390442 0.569150
#> 5    even 0.533589 0.544185 0.539051 0.390442 0.652648 0.534272
#> 6  random 0.641232 0.634078 0.597440 0.569150 0.534272 0.734771
#> 
#> $NRI_or_NTI
#>      name    clump1   clump2a   clump2b    clump4      even    random
#> 1  clump1  3.993253 -3.073960 -4.602278 -3.969862 -2.612065 -2.082386
#> 2 clump2a -3.073960  3.822527 -5.752139 -3.953720 -2.079683 -1.457932
#> 3 clump2b -4.602277 -5.752137  3.923975 -2.003818 -1.197001 -0.964941
#> 4  clump4 -3.969862 -3.953721 -2.003817  3.955997  0.612482 -1.347703
#> 5    even -2.612065 -2.079684 -1.197001  0.612482 -1.952467  1.006974
#> 6  random -2.082386 -1.457932 -0.964941 -1.347703  1.006974 -0.265824
#>