Skip to content
Snippets Groups Projects
glance_glmmtmb.R 1.65 KiB
# WARNING - Generated by {fusen} from /dev/flat_full.Rmd: do not edit by hand


#' Extracts the summary statistics from a list of glmmTMB models.
#'
#' This function takes a list of glmmTMB models and extracts the summary statistics (AIC, BIC, logLik, deviance,
#' df.resid, and dispersion) for each model and returns them as a single DataFrame.
#'
#' @param list_tmb A list of glmmTMB models or a unique glmmTMB obj model
#' @return A DataFrame with the summary statistics for all the glmmTMB models in the list.
#' @export
#' @importFrom stats setNames
#' @examples
#' data(mtcars)
#' models <-  fitModelParallel(Sepal.Length ~ Sepal.Width + Petal.Length, 
#'                            group_by = "Species",n.cores = 1, data = iris)
#' result <- glance_tmb(models)
glance_tmb <- function(list_tmb){
  if (identical(class(list_tmb), "glmmTMB")) return(getGlance(list_tmb))
  l_group <- attributes(list_tmb)$names
  l_glance <- lapply(stats::setNames(l_group, l_group), function(group) getGlance(list_tmb[[group]]))
  return(do.call("rbind", l_glance))
}


#' Extracts the summary statistics from a single glmmTMB model.
#'
#' This function takes a single glmmTMB model and extracts the summary statistics (AIC, BIC, logLik, deviance,
#' df.resid, and dispersion) from the model and returns them as a DataFrame.
#'
#' @param x A glmmTMB model.
#' @return A DataFrame with the summary statistics for the glmmTMB model.
#' @export
#'
#' @examples
#' data(mtcars)
#' model <- glmmTMB::glmmTMB(mpg ~ wt + (1|cyl), data = mtcars)
#' getGlance(model)
getGlance <- function(x){
  if (is.null(x)) return(NULL)
  ret <- data.frame(t(summary(x)$AICtab))
  ret$dispersion <- glmmTMB::sigma(x)
  ret
}