/
null_dispersion_field_distribution.R
79 lines (75 loc) · 3.2 KB
/
null_dispersion_field_distribution.R
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
#' null_dispersion_field_distribution: Null distribution of the
#' dispersion field
#' @description null_dispersion_field_distribution estimates a
#' random distribution of the dispersion field values.
#' @param pam A Presence-Absence-Matrix of matrix class or sparse matrix.
#' @param n_iter Number of iterations to obtain the distribution.
#' @param parallel If TRUE the computations will be performed in parallel.
#' @param n_cores Number of cores for the parallel computation.
#' @param randal Randomization algorithm applied to the PAM.
#' Possible choices "curveball" and "fastball".
#' @importFrom Rdpack reprompt
#' @importFrom Rcpp sourceCpp
#' @useDynLib bamm
#' @return A data matrix of size nrow(pam) X n_iter with dispersion
#' field values.
#' @details
#' Estimates a random distribution of the dispersion field values. To obtain
#' random values it uses the function code {\link[bamm]{permute_pam}}
#' at each step of the iterations. Randomization of the PAM can be
#' performed using the "fastball" (Godard and Neal, 2022) and the
#' "curveball" (Strona et al., 2014) algorithms. The implementation
#' of the "fastball" in C++ is provided in
#' \url{https://github.com/zpneal/fastball/blob/main/fastball.cpp}
#'
#' @references
#' \insertRef{Soberon2015}{bamm}
#'
#' \insertRef{Strona2014}{bamm}
#'
#' \insertRef{Gordard2022}{bamm}
#'
#' @author Luis Osorio-Olvera & Jorge Soberón
#' @examples
#' set.seed(111)
#' pam <- matrix(rbinom(100,1,0.3),nrow = 10,ncol = 10)
#' dfield_rand <- bamm::null_dispersion_field_distribution(pam,n_iter=10,
#' parallel=FALSE,
#' n_cores = 2)
#' head(dfield_rand)
#' @export
null_dispersion_field_distribution <- function(pam,n_iter=10,randal="fastball",
parallel=TRUE,n_cores=2){
if(is.data.frame(pam))
pam <- data.matrix(pam)
if(!is.matrix(pam))
stop("m should be a matrix or a data.frame")
sniter <- 1:n_iter
if(parallel){
oplan <- plan(tweak(multisession, workers = n_cores))
} else{
oplan <- plan(sequential)
}
on.exit(plan(oplan), add = TRUE)
nms <-paste0("dfrand_",sniter)
distfield_rand <- sniter %>% furrr::future_map_dfc(function(x){
ppam <- bamm::permute_pam(m=pam,as_sparse=TRUE,randal = randal)
distfield <-bamm::pam2bioindex(pam=ppam,
biodiv_index = "dispersion_field",
as_sparse = FALSE)
y <- data.frame(dfield =distfield@dispersion_field)
return(y)
},.progress = TRUE,.options = furrr::furrr_options(seed = NULL))
plan(sequential)
distfield_rand <- data.matrix(distfield_rand)
colnames(distfield_rand) <- nms
if(randal == "fastball"){
message("This function uses the code from:\n",
"https://github.com/zpneal/fastball/blob/main/fastball.cpp\n",
"Please when using the function for publications cite:\n
Godard K, Neal ZP (2022). 'fastball: a fast algorithm to randomly
sample bipartite graphs with fixed degree sequences'.
Journal of Complex Networks, 10(6), cnac049.")
}
return(distfield_rand)
}