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

R: Sample a matrix for cells close to a specified position

我正在尝试使用半随机 selection 方法寻找收集蜗牛的地点。我在我想收集蜗牛的区域周围设置了一个 10km2 的网格,它被分成 10,000 个 10m2 的单元格。我想将 R 中的这个网格随机分配到 select 200 个现场站点。

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

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

sample(dat, size = 200)

但是,我想偏向采样以选择更靠近单个位置的单元格(代表更靠近研究站的站点)。用图片更容易解释;

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

我知道我可以使用 sample 中的 prob 参数指定抽样概率,但我不知道如何创建二维概率矩阵。如有任何帮助,我将不胜感激,我不想手动完成此操作。

可能有比这更好的方法,但一个快速的方法是使用分布在 x 轴和 y 轴上随机采样(我使用正态 - 钟形分布,但你真的可以使用任何分布) .诀窍是使分布的均值成为研究站的位置。您可以通过改变分布的标准差来改变对研究站的偏见。 然后使用随机 selected 位置作为你的 x 和 y 坐标 select 位置。

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

我将对 9 x 6 网格(54 个单元格)执行此操作,这样可以更容易地看到发生了什么,并且仅对这 54 个单元格中的 5 个进行采样。您可以将其修改为 100 x 100 网格,从 10,000 个单元格中抽取 200 个样本。

# Number of rows and columns of the grid (modify these as required)
nx <- 9 # rows
ny <- 6 # columns

# Create coordinate matrix
x <- rep(1:nx, each=ny);x
y <- rep(1:ny, nx);y 
xy <- cbind(x, y); xy

# Where is the station? (edit: not snails nest)
Station <- rbind(c(x=3, y=2)) # Change as required

# Determine distance from each grid location to the station
library(SpatialTools)
D <- dist2(xy, Station)

来自 dist2

的帮助页面

dist2 takes the matrices of coordinates coords1 and coords2 and returns the inter-Euclidean distances between coordinates.

我们可以使用 image 函数将其可视化。

XY <- (matrix(D, nr=nx, byrow=TRUE))
image(XY) # axes are scaled to 0-1

# Create a scaling function - scales x to lie in [0-1)
scale_prop <- function(x, m=0)
  (x - min(x)) / (m + max(x) - min(x))

# Add the coordinates to the grid
text(x=scale_prop(xy[,1]), y=scale_prop(xy[,2]), labels=paste(xy[,1],xy[,2],sep=","))

较浅的色调表示网格更靠近 (3,2) 的车站。

# Sampling probabilities will be proportional to the distance from the station, which are scaled to lie between [0 - 1). We don't want a 1 for the maximum distance (m=1).
prob <- 1 - scale_prop(D, m=1); range (prob)

# Sample from the grid using given probabilities
sam <- sample(1:nrow(xy), size = 5, prob=prob) # Change size as required.
xy[sam,] # Thse are your (**MY!**) 5 samples
     x y
[1,] 4 4
[2,] 7 1
[3,] 3 2
[4,] 5 1
[5,] 5 3

为确认样本概率正确,您可以模拟多个样本并查看哪些坐标被采样最多。

snail.sam <- function(nsamples) {
  sam <- sample(1:nrow(xy), size = nsamples, prob=prob)
  apply(xy[sam,], 1, function(x) paste(x[1], x[2], sep=","))
}

SAMPLES <- replicate(10000, snail.sam(5))

tab <- table(SAMPLES)
cols <- colorRampPalette(c("lightblue", "darkblue"))(max(tab))
barplot(table(SAMPLES), horiz=TRUE, las=1, cex.names=0.5,
        col=cols[tab])


如果使用 100 x 100 网格并且站点位于坐标 (60,70),则图像将如下所示,采样网格显示为黑点:

这些点倾向于位于靠近站点的位置,尽管采样的可变性可能使这一点难以看清。如果你想给车站附近的网格甚至更多权重,那么你可以重新调整概率,我认为可以这样做,以节省旅行成本,但这些权重需要在估计整个地区的蜗牛数量时将其纳入分析。在这里,我将概率立方化,这样您就可以看到会发生什么。

sam <- sample(1:nrow(xy), size = 200, prob=prob^3)

现在点靠近车站的趋势更加明显。