Skip to content

Instantly share code, notes, and snippets.

Created February 17, 2014 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9046934 to your computer and use it in GitHub Desktop.
Save anonymous/9046934 to your computer and use it in GitHub Desktop.
Attempt to produce Huber-White standard errors for Poisson glmer() fits
# Score residuals
modelRes <- function(model) residuals(model, "pearson")^2 / residuals(model, "working")
# Working residuals
# modelRes <- function(model) residuals(model, "working")
estfunGlmm <- function(x, ...)
{
xmat <- model.matrix(x)
xmat <- naresid(na.action(x), xmat)
if(any(alias <- is.na(fixef(x)))) xmat <- xmat[, !alias, drop = FALSE]
wres <- as.vector(modelRes(x)) * weights(x, "working")
rval <- wres * xmat
return(rval)
}
breadGlmm <- function(x, ...)
{
sx <- summary(x)
wres <- as.vector(modelRes(x)) * weights(x, "working")
return(as.matrix(vcov(x) * as.vector(length(wres))))
}
sandwichGlmm <- function(x, Meat, ...)
{
Bread <- breadGlmm(x)
n <- nrow(estfunGlmm(x))
return(1/n * (Bread %*% Meat %*% Bread))
}
robustSEglmm <- function(model, cluster){
M <- length(unique(cluster))
N <- length(cluster)
K <- length(fixef(model))
dfc <- (M/(M - 1)) * ((N - 1)/(N - K))
uj <- apply(estfunGlmm(model), 2, function(x) tapply(x, cluster, sum));
rcse.cov <- dfc * sandwichGlmm(model, Meat = crossprod(uj)/N)
return(as.matrix(sqrt(diag(rcse.cov))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment