0
$\begingroup$

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

$\endgroup$
  • $\begingroup$ It's not clear how far your problem is about writing good code or solving a statistical problem. A problem can be both, but questions focusing on code are usually off-topic here. On the statistical side at least two questions are crucial: Do your values actually include zero ("non-negative" implies that is possible)? If so, logarithmic transformation can't work. Do your data include frequency spikes? If so, kernel density estimation can't remove them but at most reduce their amplitude. Users are often disappointed with kernel estimates because they expect magic. $\endgroup$ – Nick Cox Sep 18 '14 at 10:28
  • $\begingroup$ Thanks; you make it clear that do you have zeroes, but I don't think I can add much, except a warning. As 0 is, as it were, close to "very small" people often try log (very small) for log 0 but that usually creates outliers and doesn't make density estimation easier. Suppose your possible values are 0, 0.01, 0.02, ... and you try log10(value + 0.00001) then your values are now close to -5, -1, -0.7, ...). The principle doesn't depend on the base, but the size of the effect depends on the constant. In this example log10(value + 0.5) is a conservative change, contrary to common intuition. $\endgroup$ – Nick Cox Sep 18 '14 at 16:36

Browse other questions tagged or ask your own question.