Getting values from kernel density estimation in R

夙愿已清 提交于 2019-12-02 21:04:29

You can pull the values directly from the density function:

x = rnorm(100)
d = density(x, from=-5, to = 5, n = 1000)
d$x
d$y

Alternatively, if you really want to write your own kernel density function, here's some code to get you started:

  1. Set the points z and x range:

    z = c(-2, -1, 2)
    x = seq(-5, 5, 0.01)
    
  2. Now we'll add the points to a graph

    plot(0, 0, xlim=c(-5, 5), ylim=c(-0.02, 0.8), 
         pch=NA, ylab="", xlab="z")
    for(i in 1:length(z)) {
       points(z[i], 0, pch="X", col=2)
    }
     abline(h=0)
    
  3. Put Normal density's around each point:

    ## Now we combine the kernels,
    x_total = numeric(length(x))
    for(i in 1:length(x_total)) {
      for(j in 1:length(z)) {
        x_total[i] = x_total[i] + 
          dnorm(x[i], z[j], sd=1)
      }
    }
    

    and add the curves to the plot:

    lines(x, x_total, col=4, lty=2)
    
  4. Finally, calculate the complete estimate:

    ## Just as a histogram is the sum of the boxes, 
    ## the kernel density estimate is just the sum of the bumps. 
    ## All that's left to do, is ensure that the estimate has the
    ## correct area, i.e. in this case we divide by $n=3$:
    
    plot(x, x_total/3, 
           xlim=c(-5, 5), ylim=c(-0.02, 0.8), 
           ylab="", xlab="z", type="l")
    abline(h=0)
    

    This corresponds to

    density(z, adjust=1, bw=1)
    

The plots above give:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!