我估計了一個物種在網格化景觀中的散佈概率,給定了散佈核心(距離函數)和最大散佈距離。我試圖計算如公式1中所述的面積 - 面積擴散概率。 8的this (open access) paper。這涉及四倍積分,分別評估源和目標單元中源點和目標點的每種可能組合的擴散函數的值。與adaptIntegrate的積分不一致性
我已經與adaptIntegrate
從cubature
封裝中實現這一點,如下,源小區A,目標小區B,和簡化的分散內核其中分散爲1時,點間距離> 1.25,否則爲0。這在下面以圖形示出,其中單元B的紅色區域不可到達,因爲單元A中的點不在1.25的距離內。
library(cubature)
f <- function(xmin, xmax, ymin, ymax) {
adaptIntegrate(function(x) {
r <- sqrt((x[3] - x[1])^2 + (x[4] - x[2])^2)
ifelse(r > 1.25, 0, 1)
},
lowerLimit=c(-0.5, -0.5, xmin, ymin),
upperLimit=c(0.5, 0.5, xmax, ymax),
maxEval=1e5)
}
f(xmin=1.5, xmax=2.5, ymin=-0.5, ymax=0.5)
# $integral
# [1] 0.01949567
#
# $error
# [1] 0.001225998
#
# $functionEvaluations
# [1] 100035
#
# $returnCode
# [1] 0
我得到一個不同的整體考慮靶細胞時,C,被放置在相同的距離,但是上面,而不是小區A的權
f(xmin=-0.5, xmax=0.5, ymin=1.5, ymax=2.5)
# $integral
# [1] 0.01016105
#
# $error
# [1] 0.0241325
#
# $functionEvaluations
# [1] 100035
#
# $returnCode
# [1] 0
爲什麼這些積分如此不同(0.01949567
vs 0.01016105
)?我有錯誤地編碼了嗎?改變容差和最大數量的評估似乎沒有太大的區別。或者,是否有更好的方法來編碼這種類型的問題的解決方案?
我意識到關於一般方法的問題可能更適合於stats.stackexchange.com,但是我已經在這裏發佈,因爲我懷疑可能有一些我忽略了編碼本身。
編輯: 對於
A -> B
情況下,嵌套
integrate
返回類似於第一
adaptIntegrate
溶液中的溶液。對於
A -> C
的情況,它返回
Error in integrate(function(ky) { : the integral is probably divergent
。
g <- function(Bx, By, Ax, Ay) {
r <- sqrt((Ax - Bx)^2 + (Ay - By)^2)
ifelse(r > 1.25, 0, 1)
}
integrate(function(Ay) {
sapply(Ay, function(Ay) {
integrate(function(Ax) {
sapply(Ax, function(Ax) {
integrate(function(By) {
sapply(By, function(By) {
integrate(function(Bx) g(Bx, By, Ax, Ay), 1.5, 2.5)$value # Bx
})
}, -0.5, 0.5)$value # By
})
}, -0.5, 0.5)$value # Ax
})
}, -0.5, 0.5)$value # Ay
# [1] 0.019593
謝謝@Julius--它看起來像蒙特卡洛整合可能是一條路。介意告訴我你是如何保持「r」值的? (我可以將它們轉換爲文件,但有沒有更快的方法?)如果其他人對「adaptIntegrate」差異的原因有更深入的瞭解,我會將問題保持更長時間。 – jbaums
@jbaums,當然,我把'rs < - numeric(100035); cnt < - 1「在函數之前並且rs [cnt] << - r;函數內部的cnt << - cnt + 1'。 – Julius