0
$\begingroup$

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:

set.seed(12345)
data<-rlnorm(100,0,0.5)
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?

added

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

set.seed(12345)
x<-rlnorm(100,0,0.5)
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)
  return(g)
}

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.

$\endgroup$
2
  • $\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.