commsim
function can be used to feed Null Model algorithms into
nullmodel
analysis.
The make.commsim
function returns various predefined algorithm types
(see Details).
These functions represent low level interface for community null model
infrastructure in commsim(method, fun, binary, isSeq, mode)
make.commsim(method)
## S3 method for class 'commsim':
print(x, ...)
"integer"
or "double"
.commsim
.commsim
with elements
corresponding to the arguments (method
, binary
,
isSeq
, mode
, fun
).If the input of make.comsimm
is a commsim
object,
it is returned without further evaluation. If this is not the case,
the character method
argument is matched against
predefined algorithm names. An error message is issued
if none such is found. If the method
argument is missing,
the function returns names of all currently available
null model algorithms as a character vector.
oecosimu
c0
) or row frequencies or species richness of each
site (r0
) and take into account commonness and rarity of
species (r1
, r2
). Algorithms swap
, tswap
,
quasiswap
and backtracking
preserve both row and column
frequencies. Two first of these are sequential but the two latter are
non-sequential and produce independent matrices. Basic algorithms are
reviewed by Wright et al. (1998)."r00"
: "r0", "r0_old"
:"r0"
and "r0_old"
implement the
same method, but use different random number sequences; use
"r0_old"
if you want to reproduce results in commsimulator
(now deprecated).}
"r1"
:
"r2"
:"c0"
:"swap"
:"tswap"
:"swap"
algorithm, but it tries a fixed
number of times and performs zero to many swaps at one step
(according the thin argument in later call). This
approach was suggested by
"quasiswap"
:
"backtracking"
:
r2dtable
uses standard Rfunction r2dtable
also
used for simulated $P$-values in chisq.test
.
Algorithm quasiswap_count
uses the same, but retains the
original fill. Typically this means increasing numbers of zero cells
and the result is zero-inflated with respect to r2dtable
. "r2dtable"
:r2dtable
.} "quasiswap_count"
:swap.web
function in the package
r2dtable
function retaining row and column sums. Then
the original matrix fill is reconstructed by sequential steps to
increase or decrease matrix fill in the random matrix. These steps
are based on swapping $2 \times 2$ submatrices (see
"swap_count"
algorithm for details) to maintain row and
column totals. }
swap
, but they
swap the largest permissible value. The models in this section all
maintain the fill and perform a quantitative swap only if this can be
done without changing the fill. Single step of swap often changes the
matrix very little. In particular, if cell counts are variable, high
values change very slowly. Checking the chain stability and
independence is even more crucial than in binary swap, and very strong
thin
ning is often needed. These models should never be used
without inspecting their properties for the current data.
"swap_count"
: "abuswap_r"
:permatswap
). The algorithm is similar to
swap_count
, but uses different swap value for each row of the
$2 \times 2$ submatrix. Each step changes the the
corresponding column sums, but honours matrix fill, row sums, and
row/column frequencies (Hardy 2008; randomization scheme 2x).}
"abuswap_c"
:permatswap
). The algorithm is similar as the
previous one, but operates on columns. 2 x 2 submatrices. Each step
changes the the corresponding row sums, but honours matrix fill,
column sums, and row/column frequencies (Hardy 2008; randomization
scheme 3x).}
swsh
methods) preserve
fill and column and row frequencies, and also either row or column
sums. The methods first perform a binary quasiswap
and then
shuffle original quantitative data to non-zero cells. The
samp
methods shuffle original non-zero cell values and can be
used also with non-integer data. The both
methods
redistribute individuals randomly among non-zero cells and can only
be used with integer data. The shuffling is either free over the
whole matrix, or within rows (r
methods) or within columns
(c
methods). Shuffling within a row preserves row sums, and
shuffling within a column preserves column sums."swsh_samp"
: "swsh_both"
:
"swsh_samp_r"
:
"swsh_samp_c"
:
"swsh_both_r"
:
"swsh_both_c"
:
r00
, r0
and c0
. The _ind
methods shuffle
individuals so that the grand sum, row sum or column sums are similar
as in the observed matrix. These methods are similar as
r2dtable
but with still slacker constraints on marginal
sums. The _samp
and _both
methods first perform the
correspongind binary model with similar restriction on marginal
frequencies, and then distribute quantitative values over non-zero
cells. The _samp
models shuffle original cell values and can
therefore handle also non-count real values. The _both
models
shuffle individuals among non-zero values. The shuffling is over the
whole matrix in r00_
, and within row in r0_
and within
column in c0_
in all cases."r00_ind"
: "r0_ind"
:
"c0_ind"
:
"r00_samp"
:mode = "double"
) matrices.
This algorithm keeps total sum constant,
cells of the matrix are shuffled.}
"r0_samp"
:mode = "double"
) matrices.
This algorithm keeps row sums constant,
cells within each row are shuffled.}
"c0_samp"
:mode = "double"
) matrices.
This algorithm keeps column sums constant,
cells within each column are shuffled.}
"r00_both"
:
"r0_both"
:
"c0_both"
:
fun
must return an array of dim(nr, nc, n)
,
and must take some of the following arguments:
x
:n
:nr
:nc
:rs
:cs
:rf
:cf
:s
:x
,}
fill
:thin
:...
:Gotelli, N.J. & Entsminger, N.J. (2003). Swap algorithms in null model analysis. Ecology 84, 532--535.
Hardy, O. J. (2008) Testing the spatial phylogenetic structure of local communities: statistical performances of different null models and test statistics on a locally neutral community. Journal of Ecology 96, 914--926.
Jonsson, B.G. (2001) A null model for randomization tests of nestedness in species assemblages. Oecologia 127, 309--313.
Patefield, W. M. (1981) Algorithm AS159. An efficient method of generating r x c tables with given row and column totals. Applied Statistics 30, 91--97.
Wright, D.H., Patterson, B.D., Mikkelson, G.M., Cutler, A. & Atmar, W. (1998). A comparative analysis of nested subset patterns of species composition. Oecologia 113, 1--20.
permatfull
, permatswap
for
alternative specification of quantitative null models. Function
oecosimu
gives a higher-level interface for applying null
models in hypothesis testing and analysis of models. Function
nullmodel
and simulate.nullmodel
are used to
generate arrays of simulated null model matrices.## write the r00 algorithm
f <- function(x, n, ...)
array(replicate(n, sample(x)), c(dim(x), n))
(cs <- commsim("r00", fun=f, binary=TRUE,
isSeq=FALSE, mode="integer"))
## retrieving the sequential swap algorithm
(cs <- make.commsim("swap"))
## feeding a commsim object as argument
make.commsim(cs)
## structural constraints
diagfun <- function(x, y) {
c(sum = sum(y) == sum(x),
fill = sum(y > 0) == sum(x > 0),
rowSums = all(rowSums(y) == rowSums(x)),
colSums = all(colSums(y) == colSums(x)),
rowFreq = all(rowSums(y > 0) == rowSums(x > 0)),
colFreq = all(colSums(y > 0) == colSums(x > 0)))
}
evalfun <- function(meth, x, n) {
m <- nullmodel(x, meth)
y <- simulate(m, nsim=n)
out <- rowMeans(sapply(1:dim(y)[3],
function(i) diagfun(attr(y, "data"), y[,,i])))
z <- as.numeric(c(attr(y, "binary"), attr(y, "isSeq"),
attr(y, "mode") == "double"))
names(z) <- c("binary", "isSeq", "double")
c(z, out)
}
x <- matrix(rbinom(10*12, 1, 0.5)*rpois(10*12, 3), 12, 10)
algos <- make.commsim()
a <- t(sapply(algos, evalfun, x=x, n=10))
print(as.table(ifelse(a==1,1,0)), zero.print = ".")
Run the code above in your browser using DataLab