#' Marginal and conditional r-squared for glmm given fixed and random variances
#'
#' This function is based on Nakagawa and Schielzeth (2013). It returns the marginal
#' and conditional r-squared, as well as the AIC for each glmm.
#' Users should call the higher-level generic "r.squared", or implement a method for the
#' corresponding class to get varF, varRand and the family from the specific object
#'
#' @param varF Variance of fixed effects
#' @param varRand Variance of random effects
#' @param varResid Residual variance. Only necessary for "gaussian" family
#' @param family family of the glmm (currently works with gaussian, binomial and poisson)
#' @param link model link function. Working links are: gaussian: "identity" (default);
#' binomial: "logit" (default), "probit"; poisson: "log" (default), "sqrt"
#' @param mdl.aic The model's AIC
#' @param mdl.class The name of the model's class
#' @param null.fixef Numeric vector containing the fixed effects of the null model.
#' Only necessary for "poisson" family
#' @return A data frame with "Class", "Family", "Marginal", "Conditional", and "AIC" columns
#' @export
#' @examples
#'
.rsquared.glmm <- function(varF, varRand, varResid = NULL, varDisp = NULL, family, link,
mdl.aic, mdl.class, null.fixef = NULL){
if(family == "gaussian"){
# Only works with identity link
if(link != "identity")
family_link.stop(family, link)
# Calculate marginal R-squared (fixed effects/total variance)
Rm <- varF/(varF+varRand+varResid)
# Calculate conditional R-squared (fixed effects+random effects/total variance)
Rc <- (varF+varRand)/(varF+varRand+varResid)
}
else if(family == "binomial"){
# Get the distribution-specific variance
if(link == "logit")
varDist <- (pi^2)/3
else if(link == "probit")
varDist <- 1
else
family_link.stop(family, link)
# Calculate marginal R-squared
Rm <- varF/(varF+varRand+varDist+varDisp)
# Calculate conditional R-squared (fixed effects+random effects/total variance)
Rc <- (varF+varRand)/(varF+varRand+varDist+varDisp)
}
else if(family == "poisson"){
# Get the distribution-specific variance
if(link == "log")
varDist <- log(1+1/exp(null.fixef))
else if(link == "sqrt")
varDist <- 0.25
else
family_link.stop(family, link)
# Calculate marginal R-squared
Rm <- varF/(varF+varRand+varDist+varDisp)
# Calculate conditional R-squared (fixed effects+random effects/total variance)
Rc <- (varF+varRand)/(varF+varRand+varDist+varDisp)
}
else
family_link.stop(family, link)
# Bind R^2s into a matrix and return with AIC values
data.frame(Class=mdl.class, Family = family, Link = link,
Marginal=Rm, Conditional=Rc, AIC=mdl.aic)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.