Compute fraction of variance attributable to each variable in regression model. Also interpretable as the intra-class correlation after correcting for all other variables in the model.
Details
The coefficient of determination (i.e. R^2) is 1 - [Residuals fraction]. This matches performance::r2_nakagawa() and performance::r2_mckelvey(), except these use the "lognormal" method.
For count models, the distributional variance is a function of the mean count rate. Following Eqn 5.8 of Nakagawa, et al. 2017, this can be estimated using parameters of a model including only intercept and random effect terms. But this requires refitting the model dropping the rest of the fixed effects. Instead, computing the mean of the observed counts is a fast approximation.
References
Nakagawa, Johnson, Schielzeth. 2017. The coefficient of determination R2 and intra-class correlation coefficient from generalized linear mixed-effects models revisited and expanded. J. R. Soc. Interface 14: 20170213. doi:10.1098/rsif.2017.0213
Nakagawa, Shinichi, and Holger Schielzeth. "A general and simple method for obtaining R2 from generalized linear mixed‐effects models." Methods in ecology and evolution 4, no. 2 (2013): 133-142. doi:10.1111/j.2041-210x.2012.00261.x
Examples
library(fastglmm)
library(lme4)
set.seed(1)
sleepstudy$V <- rnorm(nrow(sleepstudy))
# prepare response, design and random effect
Y <- matrix(sleepstudy$Reaction, nrow=1)
rownames(Y) <- "Reaction"
colnames(Y) <- rownames(sleepstudy)
design <- model.matrix(~ Days, sleepstudy)
Z <- preprocess_indicator(sleepstudy$Subject)
fit <- glmmFitResponses(Y, design, Z, family=gaussian())
fit$design <- design
fit$formula <- ~ Days + (1|Subject)
varpart(fit)
#> Days Subject Residuals
#> Reaction 0.2845826 0.4055011 0.3099164