I’m struggling to find a way to represent a kernel density estimation with a nonnegative random variable. I have read a couple of articles tackling this issue; however I couldn’t implement it in R. I also tried a log transformation and a Box-Cox transformation without success. With the log transformation I found serious problem at the 0 border.
This is my script:
K<-function(x){
return(1/sqrt(2*pi)*exp(-x^2)/2)
};
x<-seq(min(y),max(y),0.001);x;
nucleo2<-function(x,h,y){
nx=length(x)
n=length(y)
fhat=rowMeans(K(outer(x,y,"-")/h))/h
return(fhat)
};
ind0<-(y==0);
ind0;
y[ind0]=0.000001;
bw.SJ(log(y));
windows();
hist(y,freq=FALSE,breaks="Sturges", main="",xlab="King's inbreeding coefficient",ylab="Density");
rug(y);
ftx<-nucleo2(log(x),h=0.3,log(y))/x;
lines(ftx~x,col=4,lwd=2)
However, this script is not very useful; I found many problems at the border. Someone could help me!!?? Thanks a lot