# Estimate PDF from only positive data [duplicate]

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?

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.

• 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. Apr 18 '13 at 19:18
• 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. Apr 19 '13 at 0:00