簡體   English   中英

R 中的累積曲線(非素食者)

[英]Accumulation curves in R (not in Vegan)

我想創建累積曲線,特別是公制累積曲線,使用引導和 for 循環。 我有興趣對示例數據集中的圖的總數進行采樣(替換),從 1 開始一直到總數( n =1 ... max n )。 每個將被采樣 1000 次。

我不相信像 Vegan 這樣的軟件包會對此有所幫助,因為我不是在尋找物種積累曲線,而是需要根據豐度數據和植物物種的保守系數來計算指標(如果有,請糾正我我錯了!)。

我的示例數據集是一個矩陣,包含圖、植物物種名稱、豐度值和 c 值(保守系數):

https://docs.google.com/spreadsheets/d/1v-93sV4ANUXpObVbtixTo2ZQjiOKemvQ_cfubZPq9L4/edit?usp=sharing

對於每個第n個樣本的 1000 次迭代中的每一次,我需要構建一個矩陣來保存 1000 次迭代結果,其中包含物種名稱、豐度和 c 值,然后從該樣本中消除任何重復的物種。 對於每次迭代,我必須計算植被指標。 重要的是我不計算整個 1000 次迭代的度量,而是計算每個單獨的迭代。

我會重復n +1 直到最大n 最后,理想情況下,我會將這些結果輸入到我的最終結果矩陣中,行為n … max n ,以及 1000 列,其中包含 1000 次迭代中每一次的計算度量。 然后我將跨迭代求平均值,然后根據這些平均值創建我想要的指標的累積曲線。

認為有用的代碼包含在下面,以及不同的示例數據集,包括我有興趣計算的指標。

https://docs.google.com/spreadsheets/d/1GcH2aq3qZzgTv2YkN-uMpnShblgsuKxAPYKH_mLbbh8/edit?usp=sharing


d<-Example2
d<-data.matrix(d)

MEANC<-function(x){
  mean(x, na.rm=TRUE)
}


FQI<-function(x){
  mean(x, na.rm=TRUE)*sqrt(sum(!is.na(x)))
}

RICH<-function(x){
  totalsprich<-sum(x)
  sum(x!=0, na.rm=TRUE)
}

shannon <- function(x){
  totalCov <- sum(x, na.rm=TRUE)
  (sum(x / totalCov * log(x / totalCov), na.rm=TRUE)) * -1

}

#for this particular example, the only two functions (metrics) that will work will be RICH and shannon

nrep<-1000
totalQuads<-nrow(d)

bootResultSD<-data.frame(matrix(nrow=nrep, ncol=totalQuads) )

bootResultMean<-data.frame(matrix(nrow=nrep, ncol=totalQuads)  )

for(j in 1:totalQuads){
  for(i in 1:nrep){

    bootIndex<-sample(1:totalQuads, j, replace=FALSE)

    bootSample<-d[bootIndex, na.rm=TRUE, drop=FALSE]

    VALUES<-apply(bootSample, 1, shannon)
    bootResultSD[i, j]<-sd(VALUES, na.rm=TRUE)
    bootResultMean[i, j]<-mean(VALUES, na.rm=TRUE)
  }
}

VALUES
bootResultSD
bootResultMean

meanDATA <- apply(bootResultMean, 2, mean, na.rm=TRUE)
meanDATASD <- apply(bootResultSD[-1], 2, mean, na.rm=TRUE)

我之前創建的問題在於它是基於每個圖計算指標,而不是累積圖並根據每個累積樣本重新計算指標。 到目前為止,這是我根據上面的代碼得出的結論,但我認為這不是我需要的:


for(j in 1:totalQuads){
  for(i in 1:nrep){

    bootIndex<-sample(1:totalQuads, 10, replace=TRUE) 

    bootSample<-d[bootIndex, na.rm=TRUE, drop=FALSE]

    booted<-bootSample[!duplicated(bootSample[,2]),]

    bootResultSD[i, j]<-sd(booted, na.rm=TRUE)
    bootResultMean[i, j]<-mean(booted, na.rm=TRUE)

  }
}

我不知道如何超越這一點。 提前致謝!

更新:

我與一位同事合作,為我的上述問題找到了答案。 這是他創建的代碼。

#d<-data file 
nrep <- 1000

totalQuads <- length(unique(d$Plot))

boot.result.fqi <- matrix(nrow=nrep, ncol=totalQuads)
boot.result.meanc <- matrix(nrow=nrep, ncol=totalQuads)
boot.result.shannon <- matrix(nrow=nrep, ncol=totalQuads)
boot.result.rich <- matrix(nrow=nrep, ncol=totalQuads)

for(j in 1:totalQuads){

for(i in 1:nrep){

    bootIndex <- sample(1:totalQuads, j, replace=TRUE)

    for(k in 1:length(bootIndex)){

        if(k == 1){
            bootSample <- subset(d, Plot %in% bootIndex[k])
        } else {
            bootSample <- rbind(bootSample, subset(d, Plot %in% bootIndex[k]))
        }

    }
    # bootSample <- subset(d, Plot %in% bootIndex)

    bootSampleUniqSp <- unique(bootSample[c("Species", "C_Value")])

    ## Calculate and store the results

    #Richness
    boot.result.rich[i,j] <- nrow(bootSampleUniqSp)

    #Mean C
    if(boot.result.rich[i,j] > 0){
        boot.result.meanc[i,j] <- mean(bootSampleUniqSp$C_Value)
    } else {
        boot.result.meanc[i,j] <- 0
    # This is the rule I've set up for when there are no species in the quads. Change the rule as you like
    }

    #FQI
    boot.result.fqi[i,j] <- boot.result.meanc[i,j] * sqrt(boot.result.rich[i,j])

    #Shannon
    covers <- aggregate(bootSample$CoverA_1.4mplot, 
by=list(bootSample$Species), sum)
    # covers <- bootSample$CoverA_1.4mplot
    total.cov <- sum(covers$x)
    boot.result.shannon[i,j] <- (sum(covers$x / total.cov * 
log(covers$x / total.cov), na.rm=TRUE)) * -1

}

}

par(mfcol=c(2,2))
boxplot(boot.result.rich, main="Richness")
boxplot(boot.result.meanc, main="Mean C")
boxplot(boot.result.fqi, main="FQI")
boxplot(boot.result.shannon, main="Shannon's index")


# the means across number of quadrats
apply(boot.result.shannon, 2, mean)

summary.dfr <- data.frame(quads=1:totalQuads, 
richness=apply(boot.result.rich, 2, mean), 
meanc=apply(boot.result.meanc, 2, mean), 
fqi=apply(boot.result.fqi, 2, mean), 
shannon=apply(boot.result.shannon, 2, mean)
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM