我正在使用“用户功能指数指南”中的脚本来计算功能多样性指数。但是,该脚本充满了错误和问题。我已经设法让脚本运行但它产生了错误的答案。
它使用三个矩阵,如下所示:
丰度:
S1 S2 S3 S4 S5 S6 S7 S8 S9
Palm 6 3 0 1 0 16 0 2 3
Forest 2 0 2 1 2 1 3 0 2
minsp:
min
S1 25.3038
S2 19.5750
S3 60.5880
S4 16.2864
S5 46.1040
S6 10.9056
S7 8.7570
S8 2.1289
S9 4.1730
maxsp:
max
S1 44.7344
S2 22.6966
S3 75.1817
S4 17.8176
S5 50.7472
S6 33.3660
S7 14.3341
S8 3.3947
S9 10.2510
我认为它应该产生两个不同的数字,具体取决于两个网站Palm
和Forest
。相反,它产生两个完全相同的数字。我看过它,发现问题从这些代码行开始变为蒸汽:
Siteminsp <- abundance*minsp
Siteminsp <- minsp[apply(Siteminsp,1,function (x) sum(x,na.rm=T))!=0, ,drop = FALSE]
Sitemaxsp <- abundance*maxsp
Sitemaxsp <- maxsp[apply(Sitemaxsp,1,function (x) sum(x,na.rm=T))!=0, ,drop = FALSE]
而不是abundance*minsp
产生一个新的矩阵,其中每个物种的最小值乘以每个地点的物种丰度。该函数将第一物种丰度乘以第一物种最小值,然后将第一物种丰度乘以第二个最小值,然后将第二物种丰度乘以第三个值,依此类推。
我是否想念这里应该发生什么?
有人可以解释一下出了什么问题,因为我完全没有想法。如果它有用,这是整个脚本:
rownames(Abundance1) <- Abundance1[,1]
Abundance <- Abundance1[,-1]
Abundance <- Abundance[,order(colnames(Abundance))]
rownames(min1) <- min1[,1]
minsp <- min1[,-1, drop= FALSE]
minsp <- minsp[order(rownames(min1)), , drop = FALSE]
rownames(max1) <- max1[,1]
maxsp <- max1[,-1, drop=FALSE]
maxsp <- maxsp[order(rownames(max1)), , drop = FALSE]
globalFRI <- function(minsp,maxsp) {
deltaS <- list()
for (j in 1:ncol(minsp))
{
xx <- cbind(minsp[,j], maxsp[,j])
xx <- xx[apply(xx,1,function(z) sum(is.finite(z))==2),]
xx <- xx[order(xx[,1]),]
z <- c(0, nrow(xx))
i <- 1
b <- xx[1, 2]
while ( i < nrow(xx) )
{
if (b < xx[i+1,1]) z <- c(z, i)
b <- ifelse(b >= xx[i+1,2], b, xx[i+1,2])
i <- i+1
if (i==nrow(xx)) break
}
group <- factor(rep(1:(length(z)-1), diff(sort(z))))
deltaS[[j]] <- tapply(xx[,2], group, max) - tapply(xx[,1], group, min)
}
globalFRIs <- sapply(deltaS, sum)}
globalFRIAll <- globalFRI(minsp,maxsp)
Abundance2 <- Abundance
Abundance2[Abundance2 != 0] <- 1
abundance <- data.matrix(Abundance)
IndexFRIs <- function(Abundance,minsp,maxsp,globalFRI) {
Siteminsp <- abundance*minsp
Siteminsp <- minsp[apply(Siteminsp,1,function (x) sum(x,na.rm=T))!=0, ,drop = FALSE]
Sitemaxsp <- abundance*maxsp
Sitemaxsp <- maxsp[apply(Sitemaxsp,1,function (x) sum(x,na.rm=T))!=0, ,drop = FALSE]
deltaS <- list()
for (j in 1:ncol(Siteminsp))
{
xx <- cbind(Siteminsp[,j], Sitemaxsp[,j])
xx <- xx[apply(xx,1,function(z) sum(is.finite(z))==2),]
xx <- xx[order(xx[,1]),]
z <- c(0, nrow(xx))
i <- 1
b <- xx[1, 2]
while ( i < nrow(xx) )
{
if (b < xx[i+1,1]) z <- c(z, i)
b <- ifelse(b >= xx[i+1,2], b, xx[i+1,2])
i <- i+1
if (i==nrow(xx)) break
}
group <- factor(rep(1:(length(z)-1), diff(sort(z))))
deltaS[[j]] <- tapply(xx[,2], group, max) - tapply(xx[,1], group, min)
}
RI <- sapply(deltaS, sum)
FRI1 <- RI/globalFRI
FRIs <- mean(FRI1)
}
FRIs <- apply(Abundance2,1,IndexFRIs,minsp,maxsp,globalFRIAll)
dput(丰度):
structure(c(6L, 2L, 3L, 0L, 0L, 2L, 1L, 1L, 0L, 2L, 16L, 1L,
0L, 3L, 2L, 0L, 3L, 2L), .Dim = c(2L, 9L), .Dimnames = list(c("Palm",
"Forest"), c("S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8",
"S9")))
dput(minsp):
structure(list(min = c(25.3038, 19.575, 60.588, 16.2864, 46.104,
10.9056, 8.757, 2.1289, 4.173)), .Names = "min", row.names = c("S1",
"S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9"), class = "data.frame")
dput(maxsp):
structure(list(max = c(44.7344, 22.6966, 75.1817, 17.8176, 50.7472,
33.366, 14.3341, 3.3947, 10.251)), .Names = "max", row.names = c("S1",
"S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9"), class = "data.frame")
我认为丰富* minsp的输出应该是这样的:
Palm Forest
S1 151.8228 50.6076
S2 58.725 0
S3 0 121.176
S4 16.2864 16.2864
S5 92.208 0
S6 174.489 10.9056
S7 0 26.271
S8 4.2578 0
S9 8.346 12.519
答案 0 :(得分:1)
您实际上并不是在寻找矩阵乘法,而是将两个向量相乘:
> apply(abundance, 1, `*`, unlist(minsp))
# Palm Forest
#S1 151.8228 50.6076
#S2 58.7250 0.0000
#S3 0.0000 121.1760
#S4 16.2864 16.2864
#S5 0.0000 92.2080
#S6 174.4896 10.9056
#S7 0.0000 26.2710
#S8 4.2578 0.0000
#S9 12.5190 8.3460
甚至更好:
t(abundance) * unlist(minsp)