Understand the idea
Picture each value as a small heap of sand shaped like a bell, then add all the heaps together: the result is a density curve. Where many values sit close together the heaps stack into a peak; where values are rare the curve stays low. It is a smoothed histogram, with no bins to choose.
The height is not a count. The curve is scaled so that the whole area under it is 1, and the share of values between two points is the area under the curve between them. With groups, each group's curve has area 1, so a small group is drawn as tall as a large one: compare where the curves sit and their shapes, not their sizes.
The bandwidth is how wide each little bell is. A wide one blurs detail away, so two real peaks can merge into one; a narrow one turns ordinary scatter into bumps that are not really there. On penguin body mass the default bandwidth gives one peak and half of it gives two. So a bump is a hint to check, not a finding: try a second bandwidth, and look at the rug of values or at a histogram.
Two or more peaks often mean that two groups are mixed in one column (species, machines, shifts): draw one curve per group to see. The curve also spills a little past the smallest and largest values, even below zero for a column that cannot be negative; that is the blur, not data. With fewer than about 30 values the shape is unreliable.
Practitioner explanation
KernelStats computes R's density() with its defaults: a Gaussian kernel evaluated at 512 points from the minimum minus 3 bandwidths to the maximum plus 3 bandwidths, so the curve matches R's. The bandwidth rule is Silverman (bw.nrd0, 0.9 min(s, IQR/1.34) n^(−1/5), R's default) or Scott (bw.nrd, the same with 1.06), times the Bandwidth multiplier (R's adjust). Both rules are tuned to a roughly normal shape and oversmooth data with several peaks, which is what the multiplier is for; plug-in or cross-validated bandwidths (bw.SJ, bw.ucv) are not offered. There is no boundary correction, so a positive column gets some density below zero, as in R.
Each group gets its own bandwidth, in its own color (at most 12 groups). The Peaks count on the card counts only clear peaks, a local maximum at least 10% as high as the tallest with a prominence of at least 5% of it, so small wiggles are not counted: on penguin body mass at multiplier 0.5 the card reports peaks near 3,693 and 4,697 g, while R's curve also has a minor bump near 5,500 g. Peak at is the highest point of the curve, a smoothed mode. The rug draws at most 2,000 ticks; the curve uses every value. The plot is descriptive: no distribution is fitted and nothing is tested.
When to use it
Consider it when
You want the shape of one numeric column (one peak or two, skew, tails) without choosing histogram bins, or to compare the shapes of several groups on one axis.
Use caution when
Very few values (under about 30), a count or rating column with only a few distinct values (use a bar chart or a histogram), when you need counts rather than shares, or as a test of shape (use the Normality Test).
Formulas and symbols
Kernel density estimate
x_i the values, n their number, h the bandwidth, φ the standard normal density (the Gaussian kernel).
Silverman's bandwidth (R's bw.nrd0), with the multiplier
s the standard deviation, IQR the interquartile range; Scott's rule (bw.nrd) uses 1.06 in place of 0.9.
Alternatives
- Histogram (Histogram). You want counts per range, or to check with actual counts whether a bump in the curve holds up.
- Boxplot (Boxplot). Many groups to compare compactly by median, quartiles and outliers.
- Q-Q Plot (Q-Q Plot). The question is whether the tails match a particular distribution.
Worked example
Scenario
Density plots of penguin body mass (342 values; 2 blank rows left out): the default, then the bandwidth multiplier 0.5, then one curve per species (Scott, multiplier 0.5).
Result
Default (Silverman, bandwidth 224.7 g): one peak, at about 3,653 g. Multiplier 0.5 (bandwidth 112.3 g): two peaks, at about 3,693 g and 4,697 g. By species: Adelie one peak (about 3,478 g, n = 151), Chinstrap one peak (about 3,697 g, n = 68), Gentoo two peaks (about 4,745 g and 5,516 g, n = 123).
Interpretation
The default curve smooths over a second cluster that a narrower bandwidth shows. Drawn by species, the heavier cluster is the Gentoo penguins, whose own curve again has two peaks worth checking against another column.
Dataset: penguins.csv
Common misconceptions
Misconception
The density curve has two bumps, so there are two groups in the data.
Correction
Bumps depend on the bandwidth: a narrow one makes bumps out of noise and a wide one hides real ones. Check a bump with a second bandwidth and a histogram, and look for a column that explains it by drawing one curve per group.
Misconception
Group A's curve is higher, so group A has more values there.
Correction
Each group's curve has area 1, so heights compare shares within each group, not numbers of values: a group of 20 is drawn as tall as a group of 2,000. The table gives each group's n.
Misconception
The density curve is the distribution my data follow.
Correction
It is these values blurred by the bandwidth, not a fitted model. To fit and compare named distributions, use Distribution Fitting.
References
- Density Estimation for Statistics and Data Analysis, Silverman, B. W. (1986).
