I would like to plot a KDE from KernelDensity onto the frequency histogram of the corresponding raw data. Is this possible? Can you provide example code, if so?
Thanks!
Joffre
2 Answers
0
Hi Joffre,
It is possible to overlay a histogram onto a KDE of the corresponding raw data. The easiest way to do this is to:
- Plot the KDE using the kernelDensity function.
- Add overlay the histogram using the plotAddHistF function.
/*
** Loadd data
*/
fname = getGAUSSHome("examples/winevolatileacidity.csv");
dataset = loadd(fname);
// Example One:
// Full sample normal density
struct kernelDensityResults krslt1;
krslt1 = kernelDensity(dataset[., "volatile acidity"]);
// Add histogram of raw data
// using frequencies with 50 bins
plotAddHistF(dataset[., "volatile acidity"], 50);
Note that this will plot the histogram on the same y-axis as the KDE. For better appearance, you can use a plotControl
structure to plot the histogram on the right y-axis:
/*
** Loadd data
*/
fname = getGAUSSHome("examples/winevolatileacidity.csv");
dataset = loadd(fname);
// Full sample normal density
struct kernelDensityResults krslt1;
krslt1 = kernelDensity(dataset[., "volatile acidity"]);
// Use plot control structure to plot
// new graph on right axis
struct plotControl ctl;
ctl = plotGetDefaults("bar");
// Specify to plot histogram
// on right y-axis
plotSetWhichYAxis(&ctl, "right");
// Add histogram of raw data
// using frequency with 100 bins
plotAddHist(ctl, dataset[., "volatile acidity"], 50);
0
Eric, thank you! This is very helpful, particularly the second example. That solves the messy scaling issue across the empirical distribution and the kernel function.
Joffre
Your Answer
2 Answers
Hi Joffre,
It is possible to overlay a histogram onto a KDE of the corresponding raw data. The easiest way to do this is to:
- Plot the KDE using the kernelDensity function.
- Add overlay the histogram using the plotAddHistF function.
/*
** Loadd data
*/
fname = getGAUSSHome("examples/winevolatileacidity.csv");
dataset = loadd(fname);
// Example One:
// Full sample normal density
struct kernelDensityResults krslt1;
krslt1 = kernelDensity(dataset[., "volatile acidity"]);
// Add histogram of raw data
// using frequencies with 50 bins
plotAddHistF(dataset[., "volatile acidity"], 50);
Note that this will plot the histogram on the same y-axis as the KDE. For better appearance, you can use a plotControl
structure to plot the histogram on the right y-axis:
/*
** Loadd data
*/
fname = getGAUSSHome("examples/winevolatileacidity.csv");
dataset = loadd(fname);
// Full sample normal density
struct kernelDensityResults krslt1;
krslt1 = kernelDensity(dataset[., "volatile acidity"]);
// Use plot control structure to plot
// new graph on right axis
struct plotControl ctl;
ctl = plotGetDefaults("bar");
// Specify to plot histogram
// on right y-axis
plotSetWhichYAxis(&ctl, "right");
// Add histogram of raw data
// using frequency with 100 bins
plotAddHist(ctl, dataset[., "volatile acidity"], 50);
Eric, thank you! This is very helpful, particularly the second example. That solves the messy scaling issue across the empirical distribution and the kernel function.
Joffre