R - Get co-local probabilities from a 2D kernel density estimate
I have two vectors S and V and using a function kde2d
I get the following plot of their joint density:
and V
" data-src="/img/f654241c025914a38ab47065a2ed09d4.jpg" class=" lazyloaded" src="https://fooobar.com//img/f654241c025914a38ab47065a2ed09d4.jpg">
Using these data, is it possible to obtain an empirical estimate of the joint probability in the form of P (S [i], V [j])?
In the question How to find / estimate a probability density function from a density function in R, it is proposed to use it approxfun
to get the height of a value in 1D KDE. Is there a way to extend this idea to 2 dimensions?
source to share
One approach is to use bilinear grid interpolation returned by kde2d
:
library(fields)
points <- data.frame(x=0:2, y=c(0, 5, 5))
interp.surface(k, points)
# [1] 0.066104795 0.040191482 0.001943069
Data:
library(MASS)
set.seed(144)
x <- rnorm(1000)
y <- 5*x + rnorm(1000)
k <- kde2d(x, y)
source to share