# “Negative density” for non-negative variables [closed]

Having an integer positive variable (number of days) in an experiment, I got negative values for the kernel density plots using R. I have read other posts relating to this topic. They admitted that the experimental biases, zero values and values very close to zero are the reasons for this. Can you suggest a link which shows the values close to zero will end up to negative density and why is this so?

Next question is that I have only two observations (out of 5000 observations) with zero value. And also there are not many observations with value close to zero as well. However, the density plot in R starts from minus 100. How can I explain it?

I will use this link to solve the explanation problem but I also like to know more about it.

Good methods for density plots of non-negative variables in R?

• Can you clarify your question (perhaps add some example data, or a figure)? A kernel density just draws a symmetrical density (usually normal) around each point & sums them. If you have any points at 0, half of those densities will be negative. What more do you need to know? – gung - Reinstate Monica Jul 27 '14 at 14:42

Your description seems to be confusing two different things. Can you show an example of what you're talking about?

(It may not be a good idea to use an ordinary kernel density estimate if your random variable is discrete.)

You can get negative $$x$$-values ending up with some positive density from a kernel density estimate,

simply because of the way KDEs work (and there are ways to do something about that if that's a problem - e.g. see the discussion here).

However, negative density (density<0) would be when the density is below the x-axis, not to the left of the y-axis. That doesn't happen with a correctly implemented kernel density estimate, as long as the kernel itself is non-negative.

The reason why you get negative values getting positive density follows straight from the way a kernel density estimate works.

A KDE replaces each observation $$x_i$$ by a little hill of probability density, of area 1/n (the kernel, centered at $$x_i$$), and the density estimate at a point $$x_0$$ is the sum of all the little density-hills (each of which is centered at one of the data points), evaluated at $$x_0$$.

See the description and diagrams here for further explanation of what goes on.

Now when any data point is close to 0, some of the little hill around it will be below zero. That is, some of the density will be at negative values of $$x$$.

If you have an exact 0, half the contribution to the density estimate will be at negative $$x$$'s.

Can you suggest a link which shows the values close to zero will end up to negative density and why is this so?

Sure, if you mean "positive density at negative $$x$$", try here.

However, the density plot in R starts from minus 100.

What's the bandwidth?

Can you show us, rather than tell us? Can you give some small sample of data with the same issues you're describing?