基于概率值将矩阵转换为存在缺失矩阵

时间:2015-04-11 11:27:46

标签: r matrix

在物种(A-D)矩阵的遗址(1-4)中,填充每个物种在每个地点发生的概率:

,A,B,C,D
1,0.9,0.8,0.7,0.6
2,0.6,0.7,0.8,0.9
3,0.3,0.55,0.65,0.95
4,0.65,0.6,0.5,0.8
  • 第1站点共有4种观察物种,
  • 2号站点有3个观察到的物种,
  • 3号站点有2个观察到的物种和
  • 位点4仅有1个观察到的物种。

如何根据发生概率值和每个站点观察到的物种数量,将原始矩阵转换为存在/不存在(1/0)?

1 个答案:

答案 0 :(得分:2)

我猜你想要为每个网站抽取N种(没有替换),按给定的概率加权......

probs <- read.csv(text="
,A,B,C,D
1,0.9,0.8,0.7,0.6
2,0.6,0.7,0.8,0.9
3,0.3,0.55,0.65,0.95
4,0.65,0.6,0.5,0.8")

probs <- probs[,-1] ## drop useless first column
sp_site <- c(4,3,2,1)
nsp <- ncol(probs)
nsite <- nrow(probs)
presabs <- matrix(0,nsp,nsite)
set.seed(101)
for (i in 1:nsite) {
    presabs[i,sample(1:nsp,size=sp_site[i],
                     prob=probs[i,],replace=FALSE)] <- 1
}
presabs
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    1    1
## [2,]    1    0    1    1
## [3,]    0    1    0    1
## [4,]    1    0    0    0