plot - implementation of transformed KDE in R -
i following steps @ end of this post implement transformed kernel density estimate (kde) on bounded support [0,+inf[
. use transformation trick avoid boundary bias of traditional kde on bounded support (in case, near zero). basically, kde allocates weights observations not exist (outside support), severely underestimates pdf @ boundary (as shows on figure below).
1) regular approach (we observe undesirable boundary bias of kde near zero)
# sample exponential distribution obs=rexp(5e2) hist(obs,freq=false) k=density(obs) lines(k$x,k$y)
2) transformation approach
# 1) log transform obs pseudo.obs=log(obs) # 2) estimate density of pseudo obs kde pseudo.k=density(pseudo.obs,n=length(obs)) # 3) estimate density of original obs t.density=pseudo.k$y/obs # plot estimation lines(obs,t.density)
instead of getting similar blue line below should
i use kde on stupidity without using transformation, because unbounded. here code works:
# before same # 2) estimate density of pseudo obs kde pseudo.k=approxfun(density(pseudo.obs)) # 3) estimate density of original obs seq=seq(min(obs),max(obs),length.out=500) t.density=as.numeric(vector(length=length(seq))) (i in 1:length(seq)){ x=seq[i] t.density[i]=pseudo.k(log(x))/x } # plot result lines(seq,t.density,col="red")
Comments
Post a Comment