0

From the following code in the R documentation link I would like to figure out how to extract a single value ("density") for each of the original points of a data frame This, so that I can use the new column for further applications that i have in mind with "point" density in 3D space.

I looked at this SO issue: link, but that code give a matrix of 51 layers.. and i'm stuck how to get 150 point values instead.

In my case i'm concerned about the trivariate example that uses:

fhat <- kde(x=iris[,1:3])

This is the code, which gives a nice plot, but i'm failing to get a single value for each of the 150 rows/points in the iris data set

library(ks)
library(MASS)
data(iris)

## univariate example
fhat <- kde(x=iris[,2])
plot(fhat, cont=50, col.cont="blue", cont.lwd=2, xlab="Sepal length")

## bivariate example
fhat <- kde(x=iris[,2:3])
plot(fhat, display="filled.contour2", cont=seq(10,90,by=10))
plot(fhat, display="persp", thin=3, border=1, col="white")

## trivariate example
fhat <- kde(x=iris[,2:4])
plot(fhat, drawpoints=TRUE)
Mark
  • 2,789
  • 1
  • 26
  • 66
  • thanks, corrected the errors! – Mark Nov 25 '17 at 16:21
  • The `ks` package has a few private methods that generate the plots. So, the code in them shows how to get to the the plot values. You can see those functions by entering `ks:::plotkde.1d` (and `.2d` and `3d` variants) in an R console. You can also `str(fhat)` to see the resultant computed data. See if any of those lead to answer paths. If they don't or they're confusing, add the confusing/falling short of goal bits of code to the question and it's likely a fruitful path can be identified. – hrbrmstr Nov 25 '17 at 16:25
  • what I intend to do is to use the density value per point to set the color of points in plotly scatter3d (possibly by binning values in limited amount of density categories) so that a cloud of points can be for instance be colored light red to very dark red based on denisty. Plotly doesn't have a way to do that by itself in these plots so I have a bypass in mind, but need to have a column with density values first then. I looked at the codes you mentioned already, but couldn't make sense of it – Mark Nov 25 '17 at 16:32

1 Answers1

2

I am assuming You are referring to kde() from the ks package?

If so, use the argument eval.points to compute the density for a vector/matrix of points using the estimate:

## univariate example

library(ks)

fhat <- kde(x = iris[,2])
plot(fhat, cont=50, col.cont="blue", cont.lwd=2, xlab="Sepal length")

estimate <- kde(iris[,2], eval.points = iris[,2])$estimate
points(iris[,2], estimate)

enter image description here

Works analogously for higher input dimensions.

Martin C. Arnold
  • 9,483
  • 1
  • 14
  • 22
  • I need it for the 3d version, but tested it, and yes it works! I didn't realize it could be done so easily. kde(iris[,1:3], eval.points = iris[,1:3])$estimate Thank you M.A. – Mark Nov 25 '17 at 16:36
  • only discovery now is that kde is pretty slow. 40.000 points takes 1112.87 sec. If anyone reading this happens to know a faster package feel free to leave a comment – Mark Nov 25 '17 at 18:00