R/get_icc.R

Defines functions get_icc

Documented in get_icc

#' get_icc() it reurns the intra class correlation of an mlm model
#'
#' @param mlm_model it requires an mlm_model, generated by lme4. If this is a null, is the percentage of variance between clusters.
#'
#' @return a numeric object.
#'
#' @details if the mlm_model is a null model it returns the ICC. If is a mlm_model with covariates, it returns the conditional variance between clusters.
#'
#' @examples
#'
#' lme4::lmer(y ~ 1 + (1|id_j), data = clustered_data, REML = FALSE) %>%
#' get_icc(.)
#'
#'
#' @export
get_icc <- function(x){
var_com <- broom.mixed::tidy(x) %>%
           dplyr::filter(effect == 'ran_pars') %>%
           dplyr::mutate(var = estimate^2) %>%
           dplyr::mutate(per = var/sum(var))

icc <- as.numeric(dplyr::select(var_com, per)[1,])

return(icc)
}
dacarras/r4sda documentation built on Nov. 9, 2023, 10:17 a.m.