R:对靠近指定位置的单元采样矩阵

问题描述 投票:1回答:1

我正在尝试使用半随机选择方法来寻找收集蜗牛的网站。我在要收集蜗牛的区域周围设置了一个10km 2网格,该网格被分解为10,000个10m 2单元。我想在R中随机选择200个现场站点。

在R中随机采样矩阵很容易;

dat <- matrix(1:10000, nrow = 100)

sample(dat, size = 200)

但是,我想使采样偏向于选择更靠近单个位置的细胞(代表更靠近研究站的位置)。用图像解释起来更容易;

enter image description here

带有叉号的黄色单元格代表我要采样的位置。灰色阴影是在sample函数中选择一个单元的可能性,而较暗的单元则更有可能被采样。

我知道我可以使用prob中的sample参数指定采样概率,但是我不知道如何创建2D概率矩阵。任何帮助将不胜感激,我不想手工做。

r matrix sample
1个回答
0
投票

[可能有比这更好的方法,但是一种快速的方法是使用分布在x和y轴上随机采样(我使用正态-钟形分布,但您也可以使用任何正态分布)。技巧是使分布的平均值成为研究站的位置。您可以通过更改分布的标准偏差来更改对研究工作站的偏见。然后使用随机选择的位置作为x和y坐标来选择位置。

dat <- matrix(1:10000, nrow = 100)
#randomly selected a position for the research station
rs <- c(80,30) 
# you can change the sd to change the bias
x <- round(rnorm(400,mean = rs[1], sd = 10)) 
y <- round(rnorm(400, mean = rs[2], sd = 10))  
position <- rep(NA, 200)
j = 1
i = 1

# as some of the numbers sampled can be outside of the area you want I oversampled # and then only selected the first 200 that were in the area of interest. 
while (j <= 200) {
  if(x[i] > 0 & x[i] < 100 &  y[i] > 0 & y [i]< 100){
    position[j] <- dat[x[i],y[i]]
    j = j +1
  }
  i = i +1
}

绘制结果:

plot(x,y, pch = 19)
points(x =80,y = 30, col = "red", pch = 19) # position of the station

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.