[R] rootogram for normal distributions

Deepayan Sarkar deepayan.sarkar at gmail.com
Mon Jan 17 06:06:54 CET 2011

On Sun, Jan 16, 2011 at 11:58 AM, Hugo Mildenberger
<Hugo.Mildenberger at web.de> wrote:
> Thank you very much for your qualified answers, and also for the
> link to the Tukey paper. I appreciate Tukey's writings very much.

Yes, thanks to Hadley for the nice reference, I hadn't seen it before.

> Looking at the lattice code (below), a possible implementation might
> involve  binning, not so?
> I see a problematic part here:
>   xx <- sort(unique(x))
> Unique certainly works well with Poisson distributed data, but is
> essentially a no-op when confronted with continous floating-point
> numbers.

True, but as Achim said, rootogram() is intended to work with data
arising from discrete distributions, not continuous ones. I see now
that this is not as explicit as it could be in the help page (although
"frequency distribution" gives a hint), which I will try to improve.

I don't think automatic handling of continuous distributions is simple
(because it is not clear how you would specify the reference
distribution). However, a little preliminary work will get you close
with the current implementation:

xnorm <- rnorm(1000)

## 'discretize' by binning and replacing data by bin midpoints
h <- hist(xnorm, plot = FALSE) # add arguments for more control
xdisc <- with(h, rep(mids, counts))

## Option 1: Assume bin probabilities proportional to dnorm()
norm.factor <- sum(dnorm(h$mids, mean(xnorm), sd(xnorm)))

rootogram(~ xdisc,
          dfun = function(x) {
              dnorm(x, mean(xnorm), sd(xnorm)) / norm.factor

## Option 2: Compute probabilities explicitly using pnorm()

## pdisc <- diff(pnorm(h$breaks)) ## or estimated:
pdisc <- diff(pnorm(h$breaks, mean = mean(xnorm), sd = sd(xnorm)))
pdisc <- pdisc / sum(pdisc)

rootogram(~ xdisc,
          dfun = function(x) {
              f <- factor(x, levels = h$mids)


More information about the R-help mailing list