Package: phyr 1.1.0

Daijiang Li

phyr: Model Based Phylogenetic Analysis

A collection of functions to do model-based phylogenetic analysis. It includes functions to calculate community phylogenetic diversity, to estimate correlations among functional traits while accounting for phylogenetic relationships, and to fit phylogenetic generalized linear mixed models. The Bayesian phylogenetic generalized linear mixed models are fitted with the 'INLA' package (<https://www.r-inla.org>).

Authors:Anthony Ives [aut], Russell Dinnage [aut], Lucas A. Nell [aut], Matthew Helmus [aut], Daijiang Li [aut, cre]

phyr_1.1.0.tar.gz
phyr_1.1.0.tar.gz(r-4.5-noble)phyr_1.1.0.tar.gz(r-4.4-noble)
phyr_1.1.0.tgz(r-4.4-emscripten)phyr_1.1.0.tgz(r-4.3-emscripten)
phyr.pdf |phyr.html
phyr/json (API)
NEWS

# Install 'phyr' in R:
install.packages('phyr', repos = 'https://cloud.r-project.org')

Bug tracker:https://github.com/daijiang/phyr/issues34 issues

Pkgdown site:https://daijiang.github.io

Uses libs:
  • openblas– Optimized BLAS
  • c++– GNU Standard C++ Library v3
Datasets:
  • comm_a - Example community data
  • comm_b - Example community data
  • envi - Example environmental data
  • oldfield - Phylogeny and community data from an Oldfield ecosystem in Southern Ontario, Canada
  • phylotree - Example phylogeny
  • traits - Example species traits data

On CRAN:

Conda:

openblascpp

4.61 score 2 packages 4.6k downloads 36 exports 46 dependencies

Last updated 4 years agofrom:a4cc0a4ca9. Checks:3 OK. Indexed: no.

TargetResultLatest binary
Doc / VignettesOKMar 22 2025
R-4.5-linux-x86_64OKMar 22 2025
R-4.4-linux-x86_64OKMar 22 2025

Exports:%nin%align_comm_Vboot_cicommunityPGLMMcommunityPGLMM.matrix.structurecommunityPGLMM.plot.recommunityPGLMM.predicted.valuescommunityPGLMM.profile.LRTcommunityPGLMM.show.recor_phylofixefget_design_matrixmatch_comm_treepcdpcd_predpglmmpglmm_comparepglmm_matrix_structurepglmm_plot_ranefpglmm_plot_repglmm_predicted_valuespglmm_profile_LRTplot_bayesplot_dataprep_dat_pglmmpscpsdpsepsrpsvpsv.sppranefrefit_bootsrm_site_noobsrm_sp_noobsvcv2

Dependencies:apebootclicpp11deldirdigestdplyrfansigenericsgluegridExtragtableinterpjpeglatticelatticeExtralifecyclelme4magrittrMASSMatrixminqamvtnormnlmenloptrpillarpkgconfigpngpurrrR6rbibutilsRColorBrewerRcppRcppArmadilloRcppEigenRdpackreformulasrlangstringistringrtibbletidyrtidyselectutf8vctrswithr

Performance benchmark

Rendered frombenchmarks.Rmdusingknitr::rmarkdownon Mar 22 2025.

Last update: 2019-10-24
Started: 2019-10-24

Plot random terms of communityPGLMM

Rendered fromplot-re.Rmdusingknitr::rmarkdownon Mar 22 2025.

Last update: 2020-12-18
Started: 2019-10-24

Usage of pglmm()

Rendered frompglmm.Rmdusingknitr::rmarkdownon Mar 22 2025.

Last update: 2020-12-18
Started: 2019-10-24

Citation

To cite package ‘phyr’ in publications use:

Ives A, Dinnage R, Nell LA, Helmus M, Li D (2020). phyr: Model Based Phylogenetic Analysis. R package version 1.1.0, https://CRAN.R-project.org/package=phyr.

Corresponding BibTeX entry:

  @Manual{,
    title = {phyr: Model Based Phylogenetic Analysis},
    author = {Anthony Ives and Russell Dinnage and Lucas A. Nell and
      Matthew Helmus and Daijiang Li},
    year = {2020},
    note = {R package version 1.1.0},
    url = {https://CRAN.R-project.org/package=phyr},
  }

Readme and manuals

Installation

To install this package:

devtools::install_github("daijiang/phyr")
# or install from binary file (may not be the latest version)
# macOS
install.packages("https://raw.githubusercontent.com/daijiang/phyr/master/phyr_1.0.3.tgz", repos = NULL)
# Windows
install.packages("https://raw.githubusercontent.com/daijiang/phyr/master/phyr_0.1.6.zip", repos = NULL)

Main functions

The phyr package has three groups of functions:

  1. community phylogenetic diversity metrics (alpha: psv, psr, pse, etc. and beta: pcd), which were included in the picante package originally. They were updated with c++ to improve speed.
  2. models to estimate correlation between functional traits while accounting for phylogenetic relationships (cor_phylo), which was included in the ape package originally. It has new syntax, much improved performance (c++), and bootstrapping option.
  3. phylogenetic generalized linear mixed models (pglmm), which was originally included in the pez package. It has new model formula syntax that allows straightforward model set up, a faster version of maximum likelihood implementation via c++, and a Bayesian model fitting framework based on INLA.
    • We hope the model formula proposed here can be used to standardize PGLMMs set up across different tools (e.g. brms for Stan).
    • PGLMM for comparative data (pglmm.compare), which was originally from ape::binaryPGLMM() but has more features.

Usage examples of pglmm() pglmm()

pglmm use similar syntax as lme4::lmer to specify random terms: add __ (two underscores) at the end of grouping variable (e.g. sp) to specify both phylogenetic and non-phylogenetic random terms; use (1|sp__@site) to specify nested term (i.e. species phylogenetic matrix V_sp nested within the diagonal of site matrix I_site) to test phylogenetic overdispersion or underdispersion. This should be the most commonly used one and is equal to kronecker(I_site, V_sp).

We can also use a second phylogeny for bipartite questions. For example, (1|parasite@host__) will be converted to kronecker(V_host, I_parasite); (1|parasite__@host__) will be converted to kronecker(V_host, V_parasite).

For details about model formula, see documentation ?phyr::pglmm. More application examples can be found in Ives 2018 Chapter 4.

library(phyr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
comm = comm_a
comm$site = row.names(comm)
dat = tidyr::gather(comm, key = "sp", value = "freq", -site) %>% 
  left_join(envi, by = "site") %>% 
  left_join(traits, by = "sp")
dat$pa = as.numeric(dat$freq > 0)
head(dat)
##    site          sp freq  sand shade   precip       tmin sla veg.height
## 1 s3293 Acer_rubrum    0 80.75  20.9 1.902397  0.1288019 294      170.5
## 2 s3294 Acer_rubrum    3 83.36  45.1 1.902397  0.1288019 294      170.5
## 3 s3295 Acer_rubrum    8 88.83  58.9 1.922669 -0.1061756 294      170.5
## 4 s3296 Acer_rubrum    0 91.24  19.7 1.922669 -0.1061756 294      170.5
## 5 s3297 Acer_rubrum    0 90.04  56.6 1.922669 -0.1061756 294      170.5
## 6 s3299 Acer_rubrum   15 81.87  87.0 1.899665  0.1736423 294      170.5
##   disp.mode pa
## 1      Wind  0
## 2      Wind  1
## 3      Wind  1
## 4      Wind  0
## 5      Wind  0
## 6      Wind  1
# phy-LMM
test1 = phyr::pglmm(freq ~ 1 + shade + (1|sp__) + (1|site) + (1|sp__@site), 
                    data = dat, family = "gaussian", REML = FALSE,
                    cov_ranef = list(sp = phylotree))
## Warning: Drop species from the phylogeny that are not in the variable sp
test1
## Linear mixed model fit by maximum likelihood
## 
## Call:freq ~ 1 + shade
## 
## logLik    AIC    BIC 
## -463.3  940.6  956.5 
## 
## Random effects:
##              Variance   Std.Dev
## 1|sp        7.345e-01 0.8570105
## 1|sp__      1.800e-04 0.0134157
## 1|site      1.035e-07 0.0003217
## 1|sp__@site 2.138e-05 0.0046238
## residual    3.261e+00 1.8058430
## 
## Fixed effects:
##                  Value  Std.Error  Zscore   Pvalue    
## (Intercept) -0.1911039  0.3920853 -0.4874 0.625972    
## shade        0.0226917  0.0067263  3.3736 0.000742 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# phy-GLMM
test2 = phyr::pglmm(pa ~ 1 + shade + (1|sp__) + (1|site) + (1|sp__@site), 
                    data = dat, family = "binomial", REML = FALSE,
                    cov_ranef = list(sp = phylotree))
## Warning: Drop species from the phylogeny that are not in the variable sp
test2
## Generalized linear mixed model for binomial data fit by maximum likelihood
## 
## Call:pa ~ 1 + shade
## 
## 
## Random effects:
##              Variance  Std.Dev
## 1|sp        1.786e-06 0.001336
## 1|sp__      4.441e-01 0.666389
## 1|site      4.496e-06 0.002120
## 1|sp__@site 8.689e-06 0.002948
## 
## Fixed effects:
##                  Value  Std.Error  Zscore    Pvalue    
## (Intercept) -2.0835724  0.5744500 -3.6271 0.0002867 ***
## shade        0.0165916  0.0087165  1.9035 0.0569784 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# bipartite
tree_site = ape::rtree(n = n_distinct(dat$site), tip.label = sort(unique(dat$site)))
z_bipartite = phyr::pglmm(freq ~ 1 + shade + (1|sp__) + (1|site__) + 
                            (1|sp__@site) + (1|sp@site__) + (1|sp__@site__), 
                          data = dat, family = "gaussian",REML = TRUE,
                          cov_ranef = list(sp = phylotree, site = tree_site))
## Warning: Drop species from the phylogeny that are not in the variable sp
z_bipartite
## Linear mixed model fit by restricted maximum likelihood
## 
## Call:freq ~ 1 + shade
## 
## logLik    AIC    BIC 
## -466.0  952.1  974.8 
## 
## Random effects:
##                Variance  Std.Dev
## 1|sp          1.648e-02 0.128377
## 1|sp__        1.173e+00 1.082923
## 1|site        2.792e-02 0.167098
## 1|site__      8.659e-03 0.093052
## 1|sp__@site   1.965e+00 1.401671
## 1|sp@site__   7.968e-02 0.282273
## 1|sp__@site__ 8.041e-05 0.008967
## residual      9.625e-01 0.981064
## 
## Fixed effects:
##                 Value Std.Error  Zscore Pvalue
## (Intercept) -0.127328  0.815075 -0.1562 0.8759
## shade        0.019393  0.011889  1.6311 0.1029

Licenses

Licensed under the GPL-3 license.

Contributing

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

Code of conduct

Please note that the 'phyr' project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Help Manual

Help pageTopics
Not in%nin%
Create phylogenetic var-cov matrix based on phylogeny and community dataalign_comm_V
Generic method to output bootstrap confidence intervals from an object.boot_ci
Example community datacomm_a
Example community datacomm_b
Correlations among multiple variates with phylogenetic signalboot_ci.cor_phylo cor_phylo print.cor_phylo
Example environmental dataenvi
Family Objects for communityPGLMM objectsfamily.communityPGLMM
Fitted values for communityPGLMMfitted.communityPGLMM
Extract fixed-effects estimatesfixed.effects fixef fixef.communityPGLMM
'get_design_matrix' gets design matrix for gaussian, binomial, and poisson modelsget_design_matrix
Match phylogeny with community datamatch_comm_tree
Extracting the Model Frame from a communityPGLMM Model objectmodel.frame.communityPGLMM
Number of Observation in a communityPGLMM Modelnobs.communityPGLMM
Phylogeny and community data from an Oldfield ecosystem in Southern Ontario, Canadaoldfield
pairwise site phylogenetic community dissimilarity (PCD) within a communitypcd
Predicted PCD with species poolpcd_pred
Phylogenetic Generalized Linear Mixed Model for Community DatacommunityPGLMM pglmm
Phylogenetic Generalized Linear Mixed Model for Comparative Datapglmm_compare
'pglmm_matrix_structure' produces the entire covariance matrix structure (V) when you specify random effects.communityPGLMM.matrix.structure pglmm_matrix_structure
Visualize random terms of communityPGLMMscommunityPGLMM.plot.re communityPGLMM.show.re pglmm_plot_ranef pglmm_plot_re
Predicted values of PGLMMcommunityPGLMM.predicted.values pglmm_predicted_values
'pglmm_profile_LRT' tests statistical significance of the phylogenetic random effect of binomial models on species slopes using a likelihood ratio test.communityPGLMM.profile.LRT pglmm_profile_LRT
Example phylogenyphylotree
plot_bayes genericplot_bayes
Plot the original dataset and predicted values (optional)plot_bayes.communityPGLMM plot_data
Predict Function for communityPGLMM Model Objectspredict.communityPGLMM
Prepare data for 'pglmm'prep_dat_pglmm
Print summary information of fitted modelprint.communityPGLMM
Print summary information of fitted modelprint.pglmm_compare
Phylogenetic Species Diversity Metricspsc psd pse psr psv psv.spp
Extract random-effects estimatesrandom.effects ranef ranef.communityPGLMM
Refit bootstrap replicates that failed to converge in a call to 'cor_phylo'print.cp_refits refit_boots
Residuals of communityPGLMM objectsresiduals.communityPGLMM
Remove site that has no observations of any speciesrm_site_noobs
Remove species that not observed in any siterm_sp_noobs
Simulate from a communityPGLMM objectsimulate.communityPGLMM
Summary information of fitted modelsummary.communityPGLMM
Summary information of fitted pglmm_compare modelsummary.pglmm_compare
Example species traits datatraits
Create phylogenetic var-cov matrixvcv2