The weighted.residuals() function has been under development recently: the current version is

## see PR#7961, https://stat.ethz.ch/pipermail/r-devel/2011-January/059642.html ## (Note, Jan 2026: PR# 7961 suggested use of deviance residuals but then dfbeta et al. are
## not one-step approximations to leave-one-out coeff.)
weighted.residuals <- function(obj, drop0 = TRUE)
{
#    w <- weights(obj, type="working")
#    r <- residuals(obj, type="working")
    w <- naresid(obj$na.action, obj$weights)
    r <- naresid(obj$na.action, obj$residuals)
    if (!is.null(w)) r <- r * sqrt(w)
    if (inherits(obj, "glm")) w <- weights(obj, "prior")
    if(drop0 && !is.null(w)) {
        if(is.matrix(r)) r[w != 0, , drop = FALSE] # e.g. mlm fit
        else r[w != 0]
    } else r
}

The previous version was, as per https://bugs.r-project.org/show_bug.cgi?id=7961,


## see PR#7961
weighted.residuals <- function(obj, drop0 = TRUE)
{
    w <- weights(obj)
    r <- residuals(obj, type="deviance")
    if(drop0 && !is.null(w)) r[w != 0] else r
}

The problem with the current version is that it only works with list-like objects that have $na.action, $weights, and $residuals components. Among other things

* lme4's merMod objects, which are objects with an S4 class, throw an error (because you can't access an S4 object element with $; even if you used getElement() you'd get an error because those slots don't exist). * glmmTMB objects return NULL (they're list-like but don't have the appropriate elements).

A robust solution would use na.action(), residuals(), weights() accessor methods. However, we can't count on working weights being the default type, and some residuals.* or weights.* methods might not have type="working" allowed (an intermediate version of weighted.residuals() used weights(., type = "working"); lme4 throws an error for type = "working" with GLMMs ... glmmTMB takes a type argument but ignores it ...

Another solution to this would be to make weighted.residuals() an S3 method and let other packages provide a method if they liked ...

   Thoughts welcome.

  Ben Bolker

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to