Skip to content
Prev 45243 / 63424 Next

nobs() with glm(family="poisson")

Le mercredi 27 f?vrier 2013 ? 18:59 -0500, John Fox a ?crit :
Thanks, that sounds like a good summary of the situation. There are two
legitimate use cases and definitions of number of observations, yet only
one nobs() and one BIC() function.

Indeed software can hardly find out which situation applies, except
maybe if it is possible to consider (as I suggested above) that
log-linear models are always fitted to tabular data (several
observations per cell), while poisson regressions are fitted to data
frames (one observation per row). If this is right, then a possible
solution would be to define nobs.glm() like this:

nobs.glm <- function(object, ...) {
    w <- object$prior.weights

    if(is.matrix(object$data)) {
        if (!is.null(w)) sum(object$data[w != 0], na.rm=TRUE)
        else sum(object$data, na.rm=TRUE)
    }
    else {
        if (!is.null(w)) sum(w != 0) else length(object$residuals)
    }
}

This would just require glm() to call as.data.frame(data) when passed a
table.


(loglin() could be considered the most natural way of fitting log-linear
models, but glm() is very useful too since it supports the quasipoisson
family, and the negative binomial via glm.nb(); finally, glm() handles
structural zeros better than loglin().)


Regards