rootogram            package:latticeExtra            R Documentation

_T_r_e_l_l_i_s _D_i_s_p_l_a_y_s _o_f _T_u_k_e_y'_s _H_a_n_g_i_n_g _R_o_o_t_o_g_r_a_m_s

_D_e_s_c_r_i_p_t_i_o_n:

     Displays hanging rootograms.

_U_s_a_g_e:

     rootogram(x, ...)

     ## S3 method for class 'formula':
     rootogram(x, data = parent.frame(),
               ylab = expression(sqrt(P(X == x))),
               prepanel = prepanel.rootogram,
               panel = panel.rootogram,
               ...)

     prepanel.rootogram(x, y = table(x),
                        dfun = NULL,
                        transformation = sqrt,
                        hang = TRUE,
                        ...)

     panel.rootogram(x, y = table(x),
                     dfun = NULL,
                     col = plot.line$col,
                     lty = plot.line$lty,
                     lwd = plot.line$lwd,
                     alpha = plot.line$alpha,
                     transformation = sqrt,
                     hang = TRUE,
                     ...)

_A_r_g_u_m_e_n_t_s:

    x, y: For 'rootogram', 'x' is the object on which method dispatch
          is carried out.  For the '"formula"' method, 'x' is a formula
          describing the form of conditioning plot.  The formula can be
          either of the form '~x' or of the form 'y~x'.  In the first
          case, 'x' is assumed to be a vector of raw observations, and
          an observed frequency distribution is computed from it.  In
          the second case, 'x' is assumed to be unique values and 'y'
          the corresponding frequencies.  In either case, further
          conditioning variables are allowed.

          A similar interpretation holds for 'x' and 'y' in
          'prepanel.rootogram' and 'panel.rootogram'. 

    data: For the '"formula"' method, a data frame containing values
          for any variables in the formula, as well as those in
          'groups' and 'subset' if applicable ('groups' is currently
          ignored by the default panel function).  By default the
          environment where the function was called from is used.  

    dfun: a probability mass function, to be evaluated at unique x
          values 

prepanel, panel: panel and prepanel function used to create the
          display.  

    ylab: the y-axis label; typically a character string or an
          expression. 

col, lty, lwd, alpha: graphical parameters 

transformation: a vectorized function.  Relative frequencies (observed)
          and theoretical probabilities ('dfun') are transformed by
          this function before being plotted. 

    hang: logical, whether lines representing observed relative
          freuqncies should "hang" from the curve representing the
          theoretical probabilities. 

     ...: extra arguments, passed on as appropriate.  Standard lattice
          arguments as well as arguments to 'panel.rootogram' can be
          supplied directly in the high level 'rootogram' call. 

_D_e_t_a_i_l_s:

     This function implements Tukey's hanging rootograms.  Also
     consider the 'rootogram' function in the 'vcd' package, especially
     if the number of unique values is small.

_V_a_l_u_e:

     'rootogram' produces an object of class '"trellis"'. The 'update'
     method can be used to update components of the object and the
     'print' method (usually called by default) will plot it on an
     appropriate plotting device.

_A_u_t_h_o_r(_s):

     Deepayan Sarkar deepayan.sarkar@gmail.com

_S_e_e _A_l_s_o:

     'xyplot'

_E_x_a_m_p_l_e_s:

     library(lattice)

     x <- rpois(1000, lambda = 50)

     p <- rootogram(~x, dfun = function(x) dpois(x, lambda = 50))
     p

     lambdav <- c(30, 40, 50, 60, 70)

     update(p[rep(1, length(lambdav))],
            aspect = "xy",
            panel = function(x, ...) {
                panel.rootogram(x,
                                dfun = function(x)
                                dpois(x, lambda = lambdav[panel.number()]))
            })

     lambdav <- c(46, 48, 50, 52, 54)

     update(p[rep(1, length(lambdav))],
            aspect = "xy",
            prepanel = function(x, ...) {
                tmp <-
                    lapply(lambdav,
                           function(lambda) {
                               prepanel.rootogram(x,
                                                  dfun = function(x)
                                                  dpois(x, lambda = lambda))
                           })
                list(xlim = range(sapply(tmp, "[[", "xlim")),
                     ylim = range(sapply(tmp, "[[", "ylim")),
                     dx = do.call("c", lapply(tmp, "[[", "dx")),
                     dy = do.call("c", lapply(tmp, "[[", "dy")))
            },
            panel = function(x, ...) {
                panel.rootogram(x,
                                dfun = function(x)
                                dpois(x, lambda = lambdav[panel.number()]))
                grid::grid.text(bquote(Poisson(lambda == .(foo)),
                                       where = list(foo = lambdav[panel.number()])),
                                y = 0.15,
                                gp = grid::gpar(cex = 1.5))
            },
            xlab = "",
            sub = "Random sample from Poisson(50)")

