Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullmod 1rowcol #180

Merged
merged 3 commits into from
Jun 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions R/make.commsim.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ function(method)
"quasiswap" = commsim(method="quasiswap", binary=TRUE, isSeq=FALSE,
mode="integer",
fun=function(x, n, nr, nc, rs, cs, rf, cf, s, fill, thin) {
if (nr < 2L || nc < 2)
stop("needs at least 2 items")
out <- array(unlist(r2dtable(n, rs, cs)), c(nr, nc, n))
storage.mode(out) <- "integer"
for (k in seq_len(n))
Expand Down Expand Up @@ -122,6 +124,8 @@ function(method)
"backtrack" = commsim(method="backtrack", binary=TRUE, isSeq=FALSE,
mode="integer",
fun=function(x, n, nr, nc, rs, cs, rf, cf, s, fill, thin) {
if (nr < 2L || nc < 2)
stop("needs at least 2 items")
btrfun <- function() {
all <- matrix(as.integer(1:(nr * nc)), nrow = nr, ncol = nc)
out <- matrix(0L, nrow = nr, ncol = nc)
Expand Down Expand Up @@ -171,13 +175,17 @@ function(method)
"r2dtable" = commsim(method="r2dtable", binary=FALSE, isSeq=FALSE,
mode="integer",
fun=function(x, n, nr, nc, cs, rs, rf, cf, s, fill, thin) {
if (nr < 2L || nc < 2)
stop("needs at least 2 items")
out <- array(unlist(r2dtable(n, rs, cs)), c(nr, nc, n))
storage.mode(out) <- "integer"
out
}),
"swap_count" = commsim(method="swap_count", binary=FALSE, isSeq=TRUE,
mode="integer",
fun=function(x, n, nr, nc, cs, rs, rf, cf, s, fill, thin) {
if (nr < 2L || nc < 2)
stop("needs at least 2 items")
out <- array(0L, c(nr, nc, n))
out[,,1] <- .C("swapcount",
m = x, nr, nc, thin, PACKAGE = "vegan")$m
Expand All @@ -189,6 +197,8 @@ function(method)
"quasiswap_count" = commsim(method="quasiswap_count", binary=FALSE, isSeq=FALSE,
mode="integer",
fun=function(x, n, nr, nc, cs, rs, rf, cf, s, fill, thin) {
if (nr < 2L || nc < 2)
stop("needs at least 2 items")
out <- array(unlist(r2dtable(n, rs, cs)), c(nr, nc, n))
storage.mode(out) <- "integer"
for (k in seq_len(n))
Expand All @@ -199,6 +209,8 @@ function(method)
"swsh_samp" = commsim(method="swsh_samp", binary=FALSE, isSeq=FALSE,
mode="double",
fun=function(x, n, nr, nc, cs, rs, rf, cf, s, fill, thin) {
if (nr < 2L || nc < 2)
stop("needs at least 2 items")
nz <- x[x > 0]
out <- array(unlist(r2dtable(fill, rf, cf)), c(nr, nc, n))
storage.mode(out) <- "double"
Expand All @@ -213,6 +225,8 @@ function(method)
"swsh_both" = commsim(method="swsh_both", binary=FALSE, isSeq=FALSE,
mode="integer",
fun=function(x, n, nr, nc, cs, rs, rf, cf, s, fill, thin) {
if (nr < 2L || nc < 2)
stop("needs at least 2 items")
indshuffle <- function(x) {
drop(rmultinom(1, sum(x), rep(1, length(x))))
}
Expand All @@ -230,6 +244,8 @@ function(method)
"swsh_samp_r" = commsim(method="swsh_samp_r", binary=FALSE, isSeq=FALSE,
mode="double",
fun=function(x, n, nr, nc, cs, rs, rf, cf, s, fill, thin) {
if (nr < 2L || nc < 2)
stop("needs at least 2 items")
out <- array(unlist(r2dtable(fill, rf, cf)), c(nr, nc, n))
storage.mode(out) <- "double"
I <- seq_len(nr)
Expand All @@ -250,6 +266,8 @@ function(method)
"swsh_samp_c" = commsim(method="swsh_samp_c", binary=FALSE, isSeq=FALSE,
mode="double",
fun=function(x, n, nr, nc, cs, rs, rf, cf, s, fill, thin) {
if (nr < 2L || nc < 2)
stop("needs at least 2 items")
out <- array(unlist(r2dtable(fill, rf, cf)), c(nr, nc, n))
storage.mode(out) <- "double"
J <- seq_len(nc)
Expand All @@ -270,6 +288,8 @@ function(method)
"swsh_both_r" = commsim(method="swsh_both_r", binary=FALSE, isSeq=FALSE,
mode="integer",
fun=function(x, n, nr, nc, cs, rs, rf, cf, s, fill, thin) {
if (nr < 2L || nc < 2)
stop("needs at least 2 items")
indshuffle <- function(x) {
drop(rmultinom(1, sum(x), rep(1, length(x))))
}
Expand All @@ -293,6 +313,8 @@ function(method)
"swsh_both_c" = commsim(method="swsh_both_c", binary=FALSE, isSeq=FALSE,
mode="integer",
fun=function(x, n, nr, nc, cs, rs, rf, cf, s, fill, thin) {
if (nr < 2L || nc < 2)
stop("needs at least 2 items")
indshuffle <- function(x) {
drop(rmultinom(1, sum(x), rep(1, length(x))))
}
Expand Down Expand Up @@ -351,7 +373,8 @@ function(method)
J <- seq_len(nc)
for (k in seq_len(n))
for (j in J)
out[, j, k] <- sample(x[,j])
out[, j, k] <- if (nr < 2)
x[,j] else sample(x[,j])
out
}),
"r0_samp" = commsim(method="r0_samp", binary=FALSE, isSeq=FALSE,
Expand All @@ -361,7 +384,8 @@ function(method)
I <- seq_len(nr)
for (k in seq_len(n))
for (i in I)
out[i, , k] <- sample(x[i,])
out[i, , k] <- if (nc < 2)
x[i,] else sample(x[i,])
out
}),
"r00_ind" = commsim(method="r00_ind", binary=FALSE, isSeq=FALSE,
Expand Down Expand Up @@ -426,8 +450,11 @@ function(method)
J <- seq_len(nc)
for (k in seq_len(n))
for (j in J) {
out[,j,k][x[,j] > 0] <- indshuffle(x[,j][x[,j] > 0] - 1L) + 1L
out[,j,k] <- sample(out[,j,k])
if (sum(x[,j]) > 0) {
out[,j,k][x[,j] > 0] <- indshuffle(x[,j][x[,j] > 0] - 1L) + 1L
out[,j,k] <- if (nr < 2)
out[,j,k] else sample(out[,j,k])
}
}
out
}),
Expand All @@ -441,8 +468,11 @@ function(method)
I <- seq_len(nr)
for (k in seq_len(n))
for (i in I) {
out[i,,k][x[i,] > 0] <- indshuffle(x[i,][x[i,] > 0] - 1L) + 1L
out[i,,k] <- sample(out[i,,k])
if (sum(x[i,]) > 0) {
out[i,,k][x[i,] > 0] <- indshuffle(x[i,][x[i,] > 0] - 1L) + 1L
out[i,,k] <- if (nc < 2)
out[i,,k] else sample(out[i,,k])
}
}
out
})
Expand Down
10 changes: 8 additions & 2 deletions man/nullmodel.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,15 @@ Jari Oksanen and Peter Solymos
\code{\link{commsim}}, \code{\link{make.commsim}},
\code{\link{permatfull}}, \code{\link{permatswap}}
}
\note{
Care must be taken when the input matrix only contains a single
row or column. Such input is invalid for swapping and hypergeometric
distribution (calling \code{\link{r2dtable}}) based algorithms.
This also applies to cases when the input is stratified into subsets.
}
\examples{
set.seed(123)
x <- matrix(rbinom(12*10, 1, 0.5)*rpois(12*10, 3), 12, 10)
data(mite)
x <- as.matrix(mite)[1:12, 21:30]

## non-sequential nullmodel
(nm <- nullmodel(x, "r00"))
Expand Down