#' Marginal and conditional r-squared for lme objects
#'
#' This method extracts the variance for fixed and random effects,
#' as well as residuals, and calls \code{\link{.rsquared.glmm}}
#'
#' @param mdl an lme model (usually fit using \code{\link{nlme::lme}})
#' @export
#' @examples
#'
r.squared.lme <- function(mdl){
# Get design matrix of fixed effects from model
Fmat <- model.matrix(eval(mdl$call$fixed)[-2], mdl$data)
# Get variance of fixed effects by multiplying coefficients by design matrix
VarF <- var(as.vector(nlme::fixef(mdl) %*% t(Fmat)))
# Get variance of random effects by extracting variance components
VarRand <- sum(suppressWarnings(as.numeric(nlme::VarCorr(mdl)
[rownames(nlme::VarCorr(mdl)) != "Residual",
1])), na.rm=T)
# Get residual variance
VarResid <- as.numeric(nlme::VarCorr(mdl)[rownames(nlme::VarCorr(mdl))=="Residual", 1])
# Call the internal function to do the pseudo r-squared calculations
.rsquared.glmm(VarF, VarRand, VarResid, family = "gaussian", link = "identity",
mdl.aic = AIC(update(mdl, method="ML")),
mdl.class = class(mdl))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.