1

我的数据是一个物种共现矩阵,我想生成随机矩阵以测试共现模式。

我为此类分析找到的唯一函数是 R 包 picante 中的 randomizeMatrix 函数。它工作得很好,但是此函数中可用的空模型类型的数量是有限的。

目前实现的空模型(null.model的参数):频率(保持物种出现频率),丰富度(保持样本物种丰富度),独立交换和试验交换

有谁知道其他函数或对此函数的修改,这将允许我测试其他空模型,例如等概率或比例列总和。

这是我使用该功能的方式

> test <- matrix(c(1,1,0,1,0,1,0,0,1,0,0,1,0,1,0,0),nrow=4,ncol=4)
> test
     [,1] [,2] [,3] [,4]
[1,]    1    0    1    0
[2,]    1    1    0    1
[3,]    0    0    0    0
[4,]    1    0    1    0
> randomizeMatrix(test,null.model = "richness",iterations = 1000)
     [,1] [,2] [,3] [,4]
[1,]    1    1    0    0
[2,]    1    1    0    1
[3,]    0    0    0    0
[4,]    0    1    0    1
> randomizeMatrix(test,null.model = "independentswap",iterations = 1000)
     [,1] [,2] [,3] [,4]
[1,]    1    0    1    0
[2,]    1    1    0    1
[3,]    0    0    0    0
[4,]    1    0    1    0
>

我在一个循环中运行该函数以获得多次迭代

先感谢您

4

1 回答 1

0

我编写了一个函数来生成一个空的随机矩阵,其中每一列的概率都不同。这是针对物种出现矩阵的。

nullMatrix <- function(nrows, ncols, prob1) {
matrixVector <- c()
for(i in 1:ncols){
    columnVector <- sample(c(0,1), nrows, replace = T, prob = c((1-prob1[i]), prob1[i]))
    matrixVector <- c(matrixVector, columnVector)
}
matrix(matrixVector, nrow = nrows, ncol = ncols)
}

这会生成一个指定的 n 行和列的矩阵以及每行为 1 的概率向量。

我使用以下代码对其进行了测试。

#generate probability vectore for column matrix, each 10 rows the probability increases by .1
p1 <- c(rep(.1, 10), rep(.2, 10), rep(.3, 10), rep(.4, 10), rep(.5, 10), rep(.6, 10), rep(.7, 10), rep(.8, 10), rep(.9, 10), rep(1, 10) )
#generate the matrix with the nullMatrix function
m1 <- nullMatrix(100, 100, p1)
#The average of every ten rows should roughly = .1, .2, ..., 1
sum(m1[,c(1:10)])/1000 #Should ~.1
sum(m1[,c(11:20)])/1000 #Should ~.2
sum(m1[,c(21:30)])/1000 #Should ~.3
sum(m1[,c(31:40)])/1000 #Should ~.4
sum(m1[,c(41:50)])/1000 #Should ~.5
sum(m1[,c(51:60)])/1000 #Should ~.6
sum(m1[,c(61:70)])/1000 #Should ~.7
sum(m1[,c(71:80)])/1000 #Should ~.8
sum(m1[,c(81:90)])/1000 #Should ~.9
sum(m1[,c(91:100)])/1000 #Should ~1

我的谈话有点晚了,但希望这对某人有帮助。

于 2014-11-06T16:08:50.323 回答