[R] Lattice Histogram Scaling

Roger Koenker rkoenker at illinois.edu
Mon Aug 14 23:41:06 CEST 2017


I am trying to do some comparisons of density estimators using lattice.
The code below attempts to plot the same histogram in each panel and
then overplots a kernel estimate with different bandwidths. Finding
packet.number() was a bit arduous, but seems to do what I want.  My
concern now is that close examination of the 4 histograms reveals that
they are different even though they use the same data, and use the
same binning.  Can someone explain this, or better yet suggest a fix?
Admittedly, the kernel estimates are rather silly, they are just standing
in for something else that I would like to think is less silly.

Many thanks,
Roger


require(REBayes)  # Source for the velo data
require(lattice)
x <- velo
x <- x[!(x == 0)]
bandwidths <- (1:4)*10
lp <- histogram(~x|bw, nint = 100, border = grey(.9), col = grey(.9), 
		type = "density", panel = function(x, bw = bandwidths, ...){
	    panel.histogram(x, ...)
	    f <- density(x, bw[packet.number()])
	    panel.lines(f$x, f$y, col = "blue", lwd = 1.5)
	    })
print(lp)


url:    www.econ.uiuc.edu/~roger            Roger Koenker
email    rkoenker at uiuc.edu            Department of Economics
vox:     217-333-4558                University of Illinois
fax:       217-244-6678                Urbana, IL 61801



More information about the R-help mailing list