How can I get the value of a kernel density estimate at specific points?

前端 未结 2 689
我寻月下人不归
我寻月下人不归 2020-12-31 06:00

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

相关标签:
2条回答
  • 2020-12-31 06:47

    I eventually found the precise function I was looking for: interp.surface from the fields package. From the help text:

    Uses bilinear weights to interpolate values on a rectangular grid to arbitrary locations or to another grid.

    0 讨论(0)
  • 2020-12-31 07:03

    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()
    

    enter image description here

    Or, if I just change n 10^6 we get

    enter image description here

    0 讨论(0)
提交回复
热议问题