I'm working with the "geyser" data set from the MASS package and comparing kernel density estimates of the np package.

My problem is to understand the density estimate using least squares cross-validation and the Epanechnikov kernel:


enter image description here

For the Gaussian kernel it seems to be fine:


enter image description here

Or if I use the Epanechnikov kernel and maximum likelihood cv:


Is it my fault or is it a problem in the package?

Edit: If I use Mathematica for the Epanechnikov kernel and least squares cv it is working:

d = SmoothKernelDistribution[data, bw = "LeastSquaresCrossValidation", ker = "Epanechnikov"]
Plot[{PDF[d, x], {x, 20,110}]

1 Answer 1



This is explained in the FAQ:

I use plot() (npplot()) to plot, say, a density and the resulting plot looks like an inverted density rather than a density

This can occur when the datadriven bandwidth is dramatically undersmoothed. Data-driven (i.e., automatic) bandwidth selection procedures are not guaranteed always to produce good results due to perhaps the presence of outliers or the rounding/discretization of continuous data, among others. By default, npplot() takes the two extremes of the data (minimum, maximum i.e., actual data points) then creates an equally spaced grid of evaluation data (i.e., not actual data points in general) and computes the density for these points. Since the bandwidth is extremely small, the density estimate at these evaluation points is correctly zero, while those for the sample realizations (in this case only two, the min and max) are non-zero, hence we get two peaks at the edges of the plot and a flat bowl equal to zero everywhere else. This can also happen when your data is heavily discretized and you treat it as continuous. In such cases, treating the data as ordered may result in more sensible estimates

As suggested treating the data as ordered, works:

                bwmethod="cv.ls", ckertype="epanechnikov", ckerorder=2)

enter image description here

It also succeeds with higher kernel orders, such as with ckerorder=4 in this example:

enter image description here

  • $\begingroup$ Thanks, but why does it work with the Gaussian kernel or in Mathematica? $\endgroup$
    – TMoek
    Oct 25, 2015 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.