在 R 中创建 2D 箱 [英] Creating 2D bins in R

查看:25
本文介绍了在 R 中创建 2D 箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中有坐标数据,我想确定我的点所在位置的分布.点的整个空间是一个边长为100的正方形.

I have coordinate data in R, and I would like to determine a distribution of where my points lie. The entire space of points is a square of side length 100.

我想将点分配给正方形上的不同部分,例如四舍五入到最接近的 5.我见过使用 cutfindinterval 的例子,但我不知道在创建 2d bin 时如何使用它.

I'd like to assign points to different segments on the square, for example rounded to the nearest 5. I've seen examples using cut and findinterval but i'm not sure how to use this when creating a 2d bin.

实际上,我想要做的是平滑分布,这样网格的相邻区域之间就不会出现巨大的跳跃.

Actually, what I want to be able to do is smooth the distribution so there are not huge jumps in between neighboring regions of the grid.

例如(这只是为了说明问题):

For example (this is just meant to illustrate the problem):

set.seed(1)
x <- runif(2000, 0, 100)
y <- runif(2000, 0, 100)
plot(y~x)
points( x = 21, y = 70, col = 'red', cex = 2, bg = 'red')

红点显然位于一个偶然没有很多其他点的区域,所以这里的密度会比邻近区域的密度有一个跳跃,我希望能够平滑它

the red point is clearly in a region that by chance hasn't had many other points, so the density here would be a jump from the density of the neighbouring regions, I'd like to be able to smooth this out

推荐答案

您可以使用 ash 库中的 bin2 函数获取分箱数据.

You can get the binned data using the bin2 function in the ash library.

关于红点周围区域数据稀疏的问题,一种可能的解决方案是平均平移直方图.它在多次移动直方图并平均分箱计数后对您的数据进行分箱.这减轻了bin原点的问题.例如,想象一下如果红点位于 bin 的左上角或 bin 的右下角,则包含红点的 bin 中的点数会如何变化.

Regarding the problem of the sparsity of data in the region around the red point, one possible solution is with the average shifted histogram. It bins your data after shifting the histogram several times and averaging the bin counts. This alleviates the problem of the bin origin. e.g., imagine how the number of points in the bin containing the red point changes if the red point is the topleft of the bin or the bottom right of the bin.

library(ash)
bins <- bin2(cbind(x,y))
f <- ash2(bins, m = c(10,10))

image(f$x,f$y,f$z)
contour(f$x,f$y,f$z,add=TRUE)

如果你想要更平滑的 bin,你可以尝试增加参数 m,它是一个长度为 2 的向量,控制每个变量的平滑参数.

If you would like smoother bins, you could try increasing the argument m, which is a vector of length 2 controlling the smoothing parameters along each variable.

f2 <- ash2(bins, m = c(10,10))
image(f2$x, f2$y, f2$z)
contour(f2$x,f2$y,f2$z,add=TRUE)

比较 ff2

分箱算法是用fortran实现的,速度非常快.

The binning algorithm is implemented in fortran and is very fast.

这篇关于在 R 中创建 2D 箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆