Search code examples
rdistributionkernel-density

Kernel smoothing function with cumulative distribution


I am trying to do a cumulative distribution. I know the matlab script to do it but I can't figure out how to do it in R

Here is my matlab code :

[p2,xp2,up2] = ksdensity(X(:,2),xi,'Kernel','epanechnikov','function','cdf','Support',[m(2),M(2)]);

Any idea ?


Solution

  • Ok, I managed to it with density function and a little sum. Here is my code for those who would like :

    dens = density(X[,1], kernel = 'epanechnikov', from = m[1], to = M[1])
    x = matrix(unlist(dens[1]))
    y = matrix(unlist(dens[2]))
    c1 <- cdf(x,y)
    plot(x, c1)
    
    cdf <- function(x,y) {
      S <- matrix(0, length(y), 1)
      S[1] <- y[1]
      for(k in seq(2, length(y))) {
        S[k] <- S[k-1] + y[k]
      }
      return(S)
    }