I have experemental data that not contain negative values (and theoretically can not contain negative values). When I do kernel density estimates of probability distribution function in R I get function which start from negative values. Something like that:

plot(density(data, kernel="gaussian"))

I think if i take kernel like "lognormal" (which itself can not be negative), I get what I need. But such kernels no in density function. How to be with such data?


I use function from this question (I have no zeros in data):

hist(x, freq=F, xlim=c(-0.3,4))
lines(density(x, kernel="gaussian"), col="blue")

logdensity <- function (x, bw = "SJ") 
  y <- log(x)
  g <- density(y, bw = bw, n = 1001)
  xgrid <- exp(g$x)
  g$y <- c(0, g$y/xgrid)
  g$x <- c(0, xgrid)

fit <- logdensity(x)
lines(fit$x,fit$y, col="red")

left tail density function (blue line) starts from negative, left tail of new function (red line) start later then zero (with lag). But I ned start KDE line directly after zero, this agrees with nature of the data.

  • $\begingroup$ It took me a while to find that one (I knew I had seen it somewhere - it is missing a crucial tag though!) I'm thinking that will solve your problem, but if not just clarify how this is a different situation. $\endgroup$
    – Andy W
    Apr 18 '13 at 19:18
  • $\begingroup$ That approach @AndyW points to (which is what I usually do in similar situations) effectively does more smoothing in the tail where you need it and guarantees the smooth estimate is on the correct domain. $\endgroup$
    – Glen_b
    Apr 19 '13 at 0:00

Browse other questions tagged or ask your own question.