[英]finding probability of area in kernel density estimation using kde2d
I'm having a very hard time to calculate the probability of a point being in a particular area in a kernel density estimation.我很难在核密度估计中计算某个点位于特定区域的概率。 This will be used to show the probabilty of an animal moving around in a specific area.这将用于显示动物在特定区域内移动的概率。
Here is my sample data:这是我的示例数据:
set.seed(123)
x <- runif(100,0,100)
y <- runif(100,0,100)
n <- 11
lims <- c(range(0,100), range(0,100))
f1 <- MASS::kde2d(x = x,y = y ,n = n, lims = lims)
Where f1$z holds the density estimations in a matrix.其中 f1$z 保存矩阵中的密度估计。 The corresponding plot is shown below:对应的图如下所示:
library('plot.matrix')
plot(f1$z)
Now, my goal is to find for example the probability of a point being in the cell surrounded in blue.现在,我的目标是找到例如一个点在蓝色包围的单元格中的概率。
I wonder, if this can be achieved be simply calculating:我想知道,这是否可以通过简单的计算来实现:
library(raster)
raster <- raster(f1)
df <- as.data.frame(raster,xy=T)
df$layer / sum(df$layer)
But I assume the solution must be to integrate somehow like described in here .但我认为解决方案必须像这里描述的那样以某种方式集成。
Thank you!谢谢!
The point surrounded in blue is the point f1$z[3, 2]
.蓝色包围的点是点f1$z[3, 2]
。 Multiply this value by the cell size as computed in the code you link to and have将此值乘以在您链接到的代码中计算出的单元格大小并具有
xlim <- range(f1$x)
ylim <- range(f1$y)
cell_size <- (diff(xlim) / n) * (diff(ylim) / n)
f1$z[3, 2] * cell_size
#[1] 0.003765805
To see that this will compute the probability over that cell, compute the density over all cells f1$z
.要查看这将计算该单元格的概率,请计算所有单元格f1$z
的密度。 It must be equal to 1.它必须等于 1。
norm <- sum(f1$z) * cell_size # normalizing constant
sum(f1$z)*cell_size/norm
#[1] 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.