I am experimenting with ways to deal with overplotting in R, and one thing I want to try is to plot individual points but color them by the density of their neighborhood. In order to do this I would need to compute a 2D kernel density estimate at each point. However, it seems that the standard kernel density estimation functions are all grid-based. Is there a function for computing 2D kernel density estimates at specific points that I specify? I would imagine a function that takes x and y vectors as arguments and returns a vector of density estimates.
我正在尝试处理R中的过度绘图的方法,我想尝试的一件事是绘制单个点,但用邻域的密度对它们进行着色。为了做到这一点,我需要在每个点计算2D核密度估计。但是,似乎标准的核密度估计函数都是基于网格的。是否有在我指定的特定点计算2D内核密度估计值的函数?我想象一个函数将x和y向量作为参数并返回密度估计的向量。
2 个解决方案
#1
5
If I understand what you want to do, it could be achieved by fitting a smoothing model to the grid density estimate and then using that to predict the density at each point you are interested in. For example:
如果我理解你想要做什么,可以通过将平滑模型拟合到网格密度估计,然后使用它来预测您感兴趣的每个点的密度来实现。例如:
# Simulate some data and put in data frame DF
n <- 100
x <- rnorm(n)
y <- 3 + 2* x * rexp(n) + rnorm(n)
# add some outliers
y[sample(1:n,20)] <- rnorm(20,20,20)
DF <- data.frame(x,y)
# Calculate 2d density over a grid
library(MASS)
dens <- kde2d(x,y)
# create a new data frame of that 2d density grid
# (needs checking that I haven't stuffed up the order here of z?)
gr <- data.frame(with(dens, expand.grid(x,y)), as.vector(dens$z))
names(gr) <- c("xgr", "ygr", "zgr")
# Fit a model
mod <- loess(zgr~xgr*ygr, data=gr)
# Apply the model to the original data to estimate density at that point
DF$pointdens <- predict(mod, newdata=data.frame(xgr=x, ygr=y))
# Draw plot
library(ggplot2)
ggplot(DF, aes(x=x,y=y, color=pointdens)) + geom_point()
Or, if I just change n 10^6 we get
或者,如果我改变n 10 ^ 6,我们得到
#2
4
I eventually found the precise function I was looking for: interp.surface
from the fields
package. From the help text:
我最终找到了我正在寻找的精确功能:来自fields包的interp.surface。从帮助文本:
Uses bilinear weights to interpolate values on a rectangular grid to arbitrary locations or to another grid.
使用双线性权重将矩形网格上的值插值到任意位置或另一个网格。
#1
5
If I understand what you want to do, it could be achieved by fitting a smoothing model to the grid density estimate and then using that to predict the density at each point you are interested in. For example:
如果我理解你想要做什么,可以通过将平滑模型拟合到网格密度估计,然后使用它来预测您感兴趣的每个点的密度来实现。例如:
# Simulate some data and put in data frame DF
n <- 100
x <- rnorm(n)
y <- 3 + 2* x * rexp(n) + rnorm(n)
# add some outliers
y[sample(1:n,20)] <- rnorm(20,20,20)
DF <- data.frame(x,y)
# Calculate 2d density over a grid
library(MASS)
dens <- kde2d(x,y)
# create a new data frame of that 2d density grid
# (needs checking that I haven't stuffed up the order here of z?)
gr <- data.frame(with(dens, expand.grid(x,y)), as.vector(dens$z))
names(gr) <- c("xgr", "ygr", "zgr")
# Fit a model
mod <- loess(zgr~xgr*ygr, data=gr)
# Apply the model to the original data to estimate density at that point
DF$pointdens <- predict(mod, newdata=data.frame(xgr=x, ygr=y))
# Draw plot
library(ggplot2)
ggplot(DF, aes(x=x,y=y, color=pointdens)) + geom_point()
Or, if I just change n 10^6 we get
或者,如果我改变n 10 ^ 6,我们得到
#2
4
I eventually found the precise function I was looking for: interp.surface
from the fields
package. From the help text:
我最终找到了我正在寻找的精确功能:来自fields包的interp.surface。从帮助文本:
Uses bilinear weights to interpolate values on a rectangular grid to arbitrary locations or to another grid.
使用双线性权重将矩形网格上的值插值到任意位置或另一个网格。