help for a loop procedure
On Thu, Jan 27, 2011 at 05:30:15PM +0100, Petr Savicky wrote:
On Thu, Jan 27, 2011 at 11:30:37AM +0100, Serena Corezzola wrote:
Hello everybody! I?m trying to define the optimal number of surveys to detect the highest number of species within a monitoring season/session.
[...]
This can be partially automatized as follows
UM <- as.matrix(U)
A <- rbind(
c(1, 0, 0),
c(0, 1, 0),
c(0, 0, 1),
c(1, 1, 0),
c(1, 0, 1),
c(0, 1, 1),
c(1, 1, 1))
rownam <- rep("U", times=nrow(A))
for (i in 1:3) {
rownam[A[, i] == 1] <- paste(rownam[A[, i] == 1], i, sep="")
}
dimnames(A) <- list(rownam, NULL)
C <- A %*% UM
C
Aadi Aagl Apap Aage Bdia Beup Crub Carc Cpam
U1 0 0 0 0 7 0 5 0 1
U2 0 0 0 0 4 2 1 0 0
U3 0 0 0 0 0 0 0 0 14
U12 0 0 0 0 11 2 6 0 1
U13 0 0 0 0 7 0 5 0 15
U23 0 0 0 0 4 2 1 0 14
U123 0 0 0 0 11 2 6 0 15
rowSums(C != 0)
U1 U2 U3 U12 U13 U23 U123
3 3 1 4 3 4 4
Now I need to do this with 10 and 32 sample events??.: (
Hello.
In a previous email, i suggested the code above. However, it may
be used only for a fixed matrix U. For testing the procedure for
a larger matrix U, matrix A should be generated differently. For a
fixed k, A should have choose(nrow(U), k) rows, nrow(U) columns and
its rows should be all 0,1-vectors with k ones. The following code
may be used, although better ways of computing A probably exist.
n <- nrow(U)
k <- 2
cmb <- combn(n, k)
A <- matrix(0, nrow=ncol(cmb), ncol=n)
ind <- cbind(1:nrow(A), 0L)
for (i in seq.int(length=k)) {
ind[, 2] <- cmb[i, ]
A[ind] <- 1
}
A
[,1] [,2] [,3]
[1,] 1 1 0
[2,] 1 0 1
[3,] 0 1 1
C <- A %*% as.matrix(U)
rowSums(C != 0)
[1] 4 3 4
This output corresponds to U12, U13, U23.
If n = 32, then the above may be used for computing the required
counts exactly for a few small values of k. For k up to 10, an
approximation may be more suitable. For example, simulation may
be used, where random subsets are generated using sample(n, k).
Petr Savicky.