r2glmm
r2glmm copied to clipboard
cmp_R2 for crossed random effect models?
cmp_R2 has the signature cmp_R2(c=C, x=X, SigHat=SigHat, beta=beta, obsperclust = obsperclust, nclusts = nclusts, method = 'sgv')
. I'm trying to apply it to a crossed random effects model fitted with gamm4
(I'm extracting the $mer
component of the fitted model, so it's a merMod
object).
I understand how to get the contrasts matrix; I'm getting SigHat
with the utility function below (extracted from the body of r2beta.lmerMod
). I'm a little confused by obsperclust
, nclusts
, which would only seem to be unambiguously defined for a model with a single grouping variable.
I know I can go look at the papers to try to figure it out (and I will), but was hoping for a hint/some guidance ...
extract.merMod.cov <- function(model) {
Z = lme4::getME(model, "Z")
s2e = lme4::getME(model, "sigma")^2
lam = lme4::getME(model, "Lambda")
lamt = lme4::getME(model, "Lambdat")
G = s2e * (lam %*% lamt)
SigHat = Z %*% (G %*% Matrix::t(Z))
Matrix::diag(SigHat) = Matrix::diag(SigHat) + s2e/model@resp$weights
return(SigHat)
}