I am following the steps at the end of this post to implement a transformed Kernel Density Estimate (KDE) on a bounded support [0,+inf[
. We use the transformation trick to avoid the boundary bias of the traditional KDE on bounded support (in that case, near zero). Basically, the KDE allocates weights to observations that do not exist (outside the support), so it severely underestimates the PDF at the boundary (as shows well on the figure below).
1) Regular approach (we observe the undesirable boundary bias of the KDE near zero)
# sample from exponential distribution
obs=rexp(5e2)
hist(obs,freq=FALSE)
k=density(obs)
lines(k$x,k$y)
2) Transformation approach
# 1) log transform the obs
pseudo.obs=log(obs)
# 2) estimate the density of the pseudo obs with KDE
pseudo.k=density(pseudo.obs,n=length(obs))
# 3) estimate the density of the original obs
t.density=pseudo.k$y/obs
# plot estimation
lines(obs,t.density)
Instead of getting something similar to the blue line below as I should