Reputation: 1828
I have two vectors S and V, and using the function kde2d
, I get the following plot of their joint density:
Using this data, is it possible to obtain an empirical estimate of the joint probability, in the form P(S[i],V[j]) ?
In the question How to find/estimate probability density function from density function in R it is suggested we use approxfun
to get the height of a value in a 1D KDE plot. Is there a way to extend this idea to 2 dimensions?
Upvotes: 3
Views: 5377
Reputation: 44330
One approach would be to use bilinear interpolation of the grid 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)
Upvotes: 3