-
Notifications
You must be signed in to change notification settings - Fork 10
/
README.Rmd
99 lines (75 loc) · 4.68 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "##",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
<!-- [![Travis build status](https://travis-ci.org/daijiang/phyr.svg?branch=master)](https://travis-ci.org/daijiang/phyr) [![Coverage status](https://codecov.io/gh/daijiang/phyr/branch/master/graph/badge.svg)](https://codecov.io/gh/daijiang/phyr) -->
# phyr <img src="man/figures/logo.png" align="right" height="138" />
<!-- badges: start -->
[![R-CMD-check](https://github.com/daijiang/phyr/workflows/R-CMD-check/badge.svg)](https://github.com/daijiang/phyr/actions)
[![Codecov test coverage](https://codecov.io/gh/daijiang/phyr/branch/master/graph/badge.svg)](https://app.codecov.io/gh/daijiang/phyr?branch=master)
<!-- badges: end -->
# Installation
To install this package:
```{r eval = F}
options(repos = c(
phyr = 'https://daijiang.r-universe.dev',
CRAN = 'https://cloud.r-project.org'))
install.packages('phyr')
# or
devtools::install_github("daijiang/phyr")
```
# 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` 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](https://leanpub.com/correlateddata).
```{r message=FALSE}
library(phyr)
```
```{r eval = T}
library(dplyr)
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)
# 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))
test1
# 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))
test2
# 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))
z_bipartite
```
# Licenses
Licensed under the [GPL-3 license](https://www.gnu.org/licenses/gpl-3.0.en.html).
# Contributing
Contributions are welcome. You can provide comments and feedback or ask questions by filing an issue on Github [here](https://github.com/daijiang/phyr/issues) or making pull requests.
# Code of conduct
Please note that the 'phyr' project is released with a [Contributor Code of Conduct](https://github.com/daijiang/phyr/blob/master/CODE_OF_CONDUCT.md). By contributing to this project, you agree to abide by its terms.