Newly tracked file
R/PD_ses.R
created.
Other files ignored by Codecov
man/PD_ses.Rd
is new.
NAMESPACE
has changed.
1 | + | rt <- function (phy) { |
|
2 | + | phy$tip.label <- phy$tip.label[sample(length(phy$tip.label))] |
|
3 | + | return(phy) |
|
4 | + | } |
|
5 | + | ||
6 | + | #' Phylogenetic diversity standardized for species richness |
|
7 | + | #' |
|
8 | + | #' This function computes the standard effect size of PD by correcting for |
|
9 | + | #' changes in species richness. The novelty of this function is its ability |
|
10 | + | #' to utilize sparse community matrix making it possible to |
|
11 | + | #' efficiently randomize very large community matrices spanning thousands of |
|
12 | + | #' taxa and sites. |
|
13 | + | #' |
|
14 | + | #' @param x a (sparse) community matrix, i.e. an object of class matrix or Matrix. |
|
15 | + | #' @param phy a phylogenetic tree (object of class phylo). |
|
16 | + | #' @param reps Number of replications. |
|
17 | + | #' @param model The null model for separating patterns from processes and |
|
18 | + | #' for contrasting against alternative hypotheses. Available null models |
|
19 | + | #' include: |
|
20 | + | #' \itemize{ |
|
21 | + | #' \item \dQuote{tipshuffle}: shuffles tip labels multiple times. |
|
22 | + | #' \item \dQuote{rowwise}: shuffles sites (i.e., varying richness) and |
|
23 | + | #' keeping species occurrence frequency constant. |
|
24 | + | #' \item \dQuote{colwise}: shuffles species occurrence frequency and |
|
25 | + | #' keeping site richness constant.} |
|
26 | + | #' |
|
27 | + | #' @param \dots Further arguments passed to or from other methods. |
|
28 | + | #' @rdname PD_ses |
|
29 | + | #' @importFrom stats sd |
|
30 | + | #' @importFrom ape keep.tip |
|
31 | + | #' @references |
|
32 | + | #' Proches, S., Wilson, J.R.U. & Cowling, R.M. (2006) How much evolutionary |
|
33 | + | #' history in a 10 x 10m plot? \emph{Proceedings of Royal Society B} |
|
34 | + | #' \strong{273}: 1143-1148. |
|
35 | + | #' @examples |
|
36 | + | #' library(ape) |
|
37 | + | #' library(Matrix) |
|
38 | + | #' tree <- read.tree(text ="((t1:1,t2:1)N2:1,(t3:1,t4:1)N3:1)N1;") |
|
39 | + | #' com <- sparseMatrix(c(1,3,4,1,4,5,1,2,3,4,5,6,3,4,6), |
|
40 | + | #' c(1,1,1,2,2,2,3,3,3,3,3,3,4,4,4),x=1, |
|
41 | + | #' dimnames = list(paste0("g", 1:6), tree$tip.label)) |
|
42 | + | #' |
|
43 | + | #' PD_ses(com, tree, model="rowwise") |
|
44 | + | #' |
|
45 | + | #' @return A data frame of results for each community or grid cell |
|
46 | + | #' \itemize{ |
|
47 | + | #' \item grids: Site identity |
|
48 | + | #' \item richness: Number of taxa in community |
|
49 | + | #' \item PD_obs: Observed PD in community |
|
50 | + | #' \item pd_rand_mean: Mean PD in null communities |
|
51 | + | #' \item pd_rand_sd: Standard deviation of PD in null communities |
|
52 | + | #' \item pd_obs_rank: Rank of observed PD vs. null communities |
|
53 | + | #' \item pd_obs_z: Standardized effect size of PD vs. null communities \eqn{= (PD_obs - pd_rand_mean) / pd_rand_sd} |
|
54 | + | #' \item pd_obs_p: P-value (quantile) of observed PD vs. null communities \eqn{= mpd_obs_rank / iter + 1} |
|
55 | + | #' \item reps: Number of replicates |
|
56 | + | #' } |
|
57 | + | #' @export |
|
58 | + | PD_ses <- function(x, phy, |
|
59 | + | model = c("tipshuffle", "rowwise", "colwise"), |
|
60 | + | reps = 1000, ...) { |
|
61 | + | ||
62 | + | colnames(x) <- gsub(" ", "_", colnames(x)) |
|
63 | + | p <- keep.tip(phy, intersect(phy$tip.label, colnames(x))) |
|
64 | + | x <- x[, intersect(p$tip.label, colnames(x))] |
|
65 | + | ||
66 | + | PD_obs <- PD(x, p) |
|
67 | + | pd.rand <- switch(model, |
|
68 | + | tipshuffle = lapply(seq_len(reps), function(i) |
|
69 | + | PD(x, rt(p))), |
|
70 | + | rowwise = lapply(seq_len(reps), function(i) |
|
71 | + | PD(x[sample(nrow(x)),], p)), |
|
72 | + | colwise = lapply(seq_len(reps), function(i) |
|
73 | + | PD(x[,sample(ncol(x))], p))) |
|
74 | + | ||
75 | + | y <- do.call(rbind, pd.rand) |
|
76 | + | pd_rand_mean <- apply(X = y, MARGIN = 2, FUN = mean, na.rm = TRUE) |
|
77 | + | pd_rand_sd <- apply(X = y, MARGIN = 2, FUN = sd, na.rm = TRUE) |
|
78 | + | pd_obs_z <- (PD_obs - pd_rand_mean)/pd_rand_sd |
|
79 | + | pd_obs_rank <- apply(X = rbind(PD_obs, y), MARGIN = 2, FUN = rank)[1, ] |
|
80 | + | pd_obs_rank <- ifelse(is.na(pd_rand_mean), NA, pd_obs_rank) |
|
81 | + | ||
82 | + | m <- data.frame(grids=rownames(x), PD_obs, pd_rand_mean, pd_rand_sd, |
|
83 | + | pd_obs_rank, pd_obs_z, |
|
84 | + | pd_obs_p = pd_obs_rank/(reps + 1), reps = reps, |
|
85 | + | row.names = row.names(x)) |
|
86 | + | ||
87 | + | ||
88 | + | z <- data.frame(table(sparse2long(x)$grids)) |
|
89 | + | names(z) <- c("grids", "richness") |
|
90 | + | res <- Reduce(function(x, y) merge(x, y, by="grids",all=TRUE) , list(z, m)) |
|
91 | + | res |
|
92 | + | } |
Files | Coverage |
---|---|
R | 8.11% |
Project Totals (36 files) | 8.11% |