Solved – Plotting density in two different ways gives wildly different looking curves

data visualizationdensity functionr

I have a very strange problem. I don't think it's a bug because I've tried it a few ways with similar results.

I have three sets of data that are all related to each other. When I make density plots of them individually (with the sm package), like so:

sm.density(sim$V2)

I get this curve:

One of the curves individually

And the other two curves look very similar. But! When I plot them all onto one graph for comparison, I get very different results:

All the curves together

The first curve I showed you has turned into one of those shorter, jagged curves in this plot. So, I can't figure out how the first smooth curve turns into something so jagged, with apparently an extra peak appearing as well.

Does anyone have any idea what might account for this? Could there be some difference in how the density is being calculated?

Best Answer

The sm.density function picks a smoothing parameter for you, unless you specify one. It seems like it chose different smoothing parameters for the different variables.

Try setting h to some fixed value. e.g.

sm.density(sim$v2, h = 1)

for each graph