R/getVC.R

Defines functions getVC

Documented in getVC

#' Fixed effect variance/sd estimates from linear mixed model
#'
#' @param model lmer fitted model.
#' @param round Rounding for numbers (default 2).
#'
#' @return A formatted data frame of random effect variance/sd estimates.
#' @export
#'
#' @examples
#' set.seed(234)
#' n1 <- 3 # groups
#' n2 <- 3 # observations per groups
#' dat <- data.frame(
#'   group = rep(c(LETTERS[1:n1]), each = n2),
#'   x1 = sample(1:5, n1 * n2, replace = TRUE),
#'   x2 = sample(1:5, n1 * n2, replace = TRUE),
#'   x3 = sample(1:5, n1 * n2, replace = TRUE)
#' )
#' library(lme4)
#' getVC(lmer(x1 ~ x2 + x3 + (x3 | group),
#'   data = dat, REML = FALSE,
#'   control = lmerControl(
#'     optimizer = "bobyqa",
#'     optCtrl = list(maxfun = 2e5)
#'   )
#' ))
getVC <- function(model, round = 2) {
  VC <- as.data.frame(lme4::VarCorr(model))
  VC <- cbind(VC[, c(1:3)],
    sdcor = round(VC[, 5], round),
    vcov = round(VC[, 4], round)
  )
  return(VC)
}
vjilmari/vjihelpers documentation built on May 23, 2022, 6:43 p.m.