Skip to content

Instantly share code, notes, and snippets.

@BERENZ
Created November 22, 2015 21:03
Show Gist options
  • Save BERENZ/e9b581a4b7160357934e to your computer and use it in GitHub Desktop.
Save BERENZ/e9b581a4b7160357934e to your computer and use it in GitHub Desktop.
Variable importance for Mixed Models
calc.relip.mm <- function(model,type='lmg') {
if (!isLMM(model) & !isGLMM(model)) {
stop('Currently supports only lmer/glmer objects', call. = FALSE)
}
require(lme4)
X <- getME(model,'X')
X <- X[,-1]
Y <- getME(model,'y')
s_resid <- sigma(model)
s_effect <- getME(model,'theta')*s_resid
s2 <- sum(s_resid^2,s_effect^2)
V <- Diagonal(x = s2,n=nrow(X))
YX <- cbind(Y,X)
cov_XY <- solve( t(YX) %*% solve(V) %*% as.matrix(YX))
colnames(cov_XY) <- rownames(cov_XY) <- colnames(YX)
importances <- calc.relimp(as.matrix(cov_XY),rela=T,type=type)
return(importances)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment