title: Getting values from kernel density estimation in R
body: <p>I am trying to get density estimates for the log of stock prices in R.  I know I can plot it using <code>plot(density(x))</code>.  However, I actually want values for the function.</p>
  
  <p>I'm trying to implement the kernel density estimation formula.  Here's what I have so far:</p>
  
  <pre><code>a &lt;- read.csv("boi_new.csv", header=FALSE)
  S = a[,3] # takes column of increments in stock prices
  dS=S[!is.na(S)] # omits first empty field
  
  N = length(dS)                  # Sample size
  rseed = 0                       # Random seed
  x = rep(c(1:5),N/5)             # Inputted data
  
  set.seed(rseed)   # Sets random seed for reproducibility
  
  QL &lt;- function(dS){
      h = density(dS)$bandwidth
      r = log(dS^2)
      f = 0*x
      for(i in 1:N){
          f[i] = 1/(N*h) * sum(dnorm((x-r[i])/h))
      }
      return(f)
  }
  
  QL(dS)
  </code></pre>
  
  <p>Any help would be much appreciated.  Been at this for days!</p>