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.