psv
library(phyr)
# simulate data
= 500
nspp = 100
nsite = ape::rtree(n = nspp)
tree_sim = matrix(rbinom(nspp * nsite, size = 1, prob = 0.6),
comm_sim nrow = nsite, ncol = nspp)
row.names(comm_sim) = paste0("site_", 1:nsite)
colnames(comm_sim) = paste0("t", 1:nspp)
= comm_sim[, tree_sim$tip.label]
comm_sim # about 40 times faster
::benchmark(
rbenchmark"picante" = {picante::psv(comm_sim, tree_sim)},
"phyr R" = {phyr::psv(comm_sim, tree_sim, cpp = FALSE)},
"phyr c++" = {phyr::psv(comm_sim, tree_sim, cpp = TRUE)},
replications = 10,
columns = c("test", "replications", "elapsed",
"relative", "user.self", "sys.self"))
#> test replications elapsed relative user.self sys.self
#> 3 phyr c++ 10 0.339 1.000 0.298 0.030
#> 2 phyr R 10 3.287 9.696 2.907 0.303
#> 1 picante 10 16.265 47.979 14.824 0.795
pse
= matrix(rpois(nspp * nsite, 3), nrow = nsite, ncol = nspp)
comm_sim row.names(comm_sim) = paste0("site_", 1:nsite)
colnames(comm_sim) = paste0("t", 1:nspp)
= comm_sim[, tree_sim$tip.label]
comm_sim # about 2-3 times faster
::benchmark(
rbenchmark"picante" = {picante::pse(comm_sim, tree_sim)},
"phyr R" = {phyr::pse(comm_sim, tree_sim, cpp = FALSE)},
"phyr c++" = {phyr::pse(comm_sim, tree_sim, cpp = TRUE)},
replications = 20,
columns = c("test", "replications", "elapsed",
"relative", "user.self", "sys.self"))
#> test replications elapsed relative user.self sys.self
#> 3 phyr c++ 20 1.456 1.000 1.329 0.105
#> 2 phyr R 20 4.233 2.907 3.453 0.555
#> 1 picante 20 3.858 2.650 3.319 0.475
pcd
# pcd is about 20 times faster
::benchmark(
rbenchmark"phyr" = {phyr::pcd(comm = comm_a, tree = phylotree, reps = 1000, verbose = FALSE)},
"picante" = {picante::pcd(comm = comm_a, tree = phylotree, reps = 1000)},
replications = 10,
columns = c("test", "replications", "elapsed",
"relative", "user.self", "sys.self"))
#> test replications elapsed relative user.self sys.self
#> 1 phyr 10 0.214 1.000 0.192 0.012
#> 2 picante 10 4.516 21.103 4.043 0.074
pglmm
)To compare the cpp version and R version, and the version from the pez
package.
library(dplyr)
= comm_a
comm $site = row.names(comm)
comm= tidyr::gather(comm, key = "sp", value = "freq", -site) %>%
dat left_join(envi, by = "site") %>%
left_join(traits, by = "sp")
$pa = as.numeric(dat$freq > 0)
dat# data prep for pez::communityPGLMM, not necessary for phyr::pglmm
= arrange(dat, site, sp)
dat = filter(dat, sp %in% sample(unique(dat$sp), 10),
dat %in% sample(unique(dat$site), 5))
site = n_distinct(dat$sp)
nspp = n_distinct(dat$site)
nsite
$site = as.factor(dat$site)
dat$sp = as.factor(dat$sp)
dat
= ape::drop.tip(phylotree, setdiff(phylotree$tip.label, unique(dat$sp)))
tree <- ape::vcv(tree)
Vphy <- Vphy/max(Vphy)
Vphy <- Vphy/exp(determinant(Vphy)$modulus[1]/nspp)
Vphy = Vphy[levels(dat$sp), levels(dat$sp)]
Vphy
# prepare random effects
<- list(1, site = dat$site, covar = diag(nsite))
re.site <- list(1, sp = dat$sp, covar = diag(nspp))
re.sp <- list(1, sp = dat$sp, covar = Vphy)
re.sp.phy # sp is nested within site
<- list(1, sp = dat$sp, covar = Vphy, site = dat$site)
re.nested.phy <- list(1, sp = dat$sp, covar = solve(Vphy), site = dat$site) # equal to sp__@site
re.nested.rep # can be named
= list(re.sp = re.sp, re.sp.phy = re.sp.phy, re.nested.phy = re.nested.phy, re.site = re.site)
re
# about 4-10 times faster for a small dataset
::benchmark(
rbenchmark"phyr c++ bobyqa" = {phyr::pglmm(freq ~ 1 + shade + (1|sp__) + (1|site) + (1|sp__@site),
cov_ranef = list(sp = phylotree), REML = FALSE,
dat, cpp = TRUE, optimizer = "bobyqa")},
"phyr c++ Nelder-Mead" = {phyr::pglmm(freq ~ 1 + shade + (1|sp__) + (1|site) + (1|sp__@site),
cov_ranef = list(sp = phylotree), REML = FALSE,
dat, cpp = TRUE, optimizer = "Nelder-Mead")},
"phyr R Nelder-Mead" = {phyr::pglmm(freq ~ 1 + shade + (1|sp__) + (1|site) + (1|sp__@site),
cov_ranef = list(sp = phylotree), REML = FALSE,
dat, cpp = FALSE, optimizer = "Nelder-Mead")},
"pez R Nelder-Mead" = {pez::communityPGLMM(freq ~ 1 + shade, data = dat, sp = dat$sp, site = dat$site,
random.effects = re, REML = FALSE)},
replications = 5,
columns = c("test", "replications", "elapsed",
"relative", "user.self", "sys.self")
)#> test replications elapsed relative user.self sys.self
#> 4 pez R Nelder-Mead 5 32.214 88.989 30.821 0.374
#> 1 phyr c++ bobyqa 5 0.362 1.000 0.342 0.006
#> 2 phyr c++ Nelder-Mead 5 1.156 3.193 1.115 0.015
#> 3 phyr R Nelder-Mead 5 33.281 91.936 31.198 0.480
# about 6 times faster for a small dataset
::benchmark(
rbenchmark"phyr c++ bobyqa" = {phyr::pglmm(pa ~ 1 + shade + (1|sp__) + (1|site) + (1|sp__@site),
family = "binomial", cov_ranef = list(sp = phylotree), REML = FALSE,
dat, cpp = TRUE, optimizer = "bobyqa")},
"phyr c++ Nelder-Mead" = {phyr::pglmm(pa ~ 1 + shade + (1|sp__) + (1|site) + (1|sp__@site),
family = "binomial", cov_ranef = list(sp = phylotree), REML = FALSE,
dat, cpp = TRUE, optimizer = "Nelder-Mead")},
"phyr R Nelder-Mead" = {phyr::pglmm(pa ~ 1 + shade + (1|sp__) + (1|site) + (1|sp__@site),
family = "binomial", cov_ranef = list(sp = phylotree), REML = FALSE,
dat, cpp = FALSE, optimizer = "Nelder-Mead")},
"pez R Nelder-Mead" = {pez::communityPGLMM(pa ~ 1 + shade, data = dat, sp = dat$sp,
family = "binomial", site = dat$site,
random.effects = re, REML = FALSE)},
replications = 5,
columns = c("test", "replications", "elapsed",
"relative", "user.self", "sys.self")
)#> test replications elapsed relative user.self sys.self
#> 4 pez R Nelder-Mead 5 49.296 42.314 45.731 0.604
#> 1 phyr c++ bobyqa 5 1.840 1.579 1.750 0.033
#> 2 phyr c++ Nelder-Mead 5 1.165 1.000 1.093 0.021
#> 3 phyr R Nelder-Mead 5 24.355 20.906 23.024 0.317
cor_phylo()
library(ape)
# Set up parameter values for simulating data
<- 50
n <- rcoal(n, tip.label = 1:n)
phy <- paste0("par", 1:2)
trt_names
<- matrix(c(1, 0.7, 0.7, 1), nrow = 2, ncol = 2)
R <- c(0.3, 0.95)
d <- 1
B2
<- c(0.2, 1)
Se <- matrix(Se, nrow = n, ncol = 2, byrow = TRUE)
M colnames(M) <- trt_names
# Set up needed matrices for the simulations
<- length(d)
p
<- stree(n)
star $edge.length <- array(1, dim = c(n, 1))
star$tip.label <- phy$tip.label
star
<- vcv(phy)
Vphy <- Vphy/max(Vphy)
Vphy <- Vphy/exp(determinant(Vphy)$modulus[1]/n)
Vphy
<- matrix(1, nrow = n, ncol = 1) %*% diag(Vphy) - Vphy
tau <- matrix(0, nrow = p * n, ncol = p * n)
C for (i in 1:p) for (j in 1:p) {
<- (d[i]^tau * (d[j]^t(tau)) * (1 - (d[i] * d[j])^Vphy))/(1 - d[i] * d[j])
Cd * (i - 1) + 1):(i * n), (n * (j - 1) + 1):(j * n)] <- R[i, j] * Cd
C[(n
}<- matrix(M^2, ncol = 1)
MM <- C + diag(as.numeric(MM))
V
<- t(chol(V))
iD
<- iD %*% rnorm(2 * n)
XX <- matrix(XX, n, p)
X colnames(X) <- trt_names
rownames(X) <- phy$tip.label
rownames(M) <- phy$tip.label
<- list(cbind(rnorm(n, mean = 2, sd = 10)))
U names(U) <- trt_names[2]
2] <- X[,2] + B2[1] * U[[1]][,1] - B2[1] * mean(U[[1]][,1])
X[,
<- cor_phylo(variates = X,
z covariates = U,
meas_errors = M,
phy = phy,
species = phy$tip.label)
<- list(NULL, matrix(rnorm(n, mean = 2, sd = 10), nrow = n, ncol = 1))
U2 rownames(U2[[2]]) <- phy$tip.label
colnames(U2[[2]]) <- "par2"
= X
X2 2] <- X2[,2] + B2[1] * U2[[2]][,1] - B2[1] * mean(U2[[2]][,1])
X2[,
<- corphylo(X = X2, SeM = M, U = U2, phy = phy, method = "Nelder-Mead")
z_r
::benchmark(
rbenchmark"cor_phylo" = {cor_phylo(variates = X, covariates = U, meas_errors = M,
phy = phy, species = phy$tip.label)},
"corphylo" = {corphylo(X = X2, SeM = M, U = U2, phy = phy, method = "Nelder-Mead")},
replications = 5,
columns = c("test", "replications", "elapsed",
"relative", "user.self", "sys.self")
)#> test replications elapsed relative user.self sys.self
#> 1 cor_phylo 5 4.511 1.000 4.329 0.062
#> 2 corphylo 5 16.190 3.589 13.863 1.369