From 5c176180d51d8033b87c20f2b033b709f2f6b25e Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 5 Feb 2024 08:34:44 +0100 Subject: [PATCH] glmmTMB shouldn't use qqhalfnormal https://github.com/easystats/performance/issues/654 --- R/plot.check_model.R | 4 +++- R/plot.check_normality.R | 13 +++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/R/plot.check_model.R b/R/plot.check_model.R index 14e0a4930..97acb31a7 100644 --- a/R/plot.check_model.R +++ b/R/plot.check_model.R @@ -39,6 +39,7 @@ plot.see_check_model <- function(x, model_info <- attr(x, "model_info") overdisp_type <- attr(x, "overdisp_type") plot_type <- attr(x, "type") + model_class <- attr(x, "model_class") if (missing(type) && !is.null(plot_type) && plot_type %in% c("density", "discrete_dots", "discrete_interval", "discrete_both")) { type <- plot_type @@ -174,7 +175,8 @@ plot.see_check_model <- function(x, colors = colors, dot_alpha_level = dot_alpha_level, show_dots = TRUE, # qq-plots w/o dots makes no sense - model_info = model_info + model_info = model_info, + model_class = model_class ) } diff --git a/R/plot.check_normality.R b/R/plot.check_normality.R index 8aae47298..3747fe6c1 100644 --- a/R/plot.check_normality.R +++ b/R/plot.check_normality.R @@ -70,9 +70,12 @@ plot.see_check_normality <- function(x, } else { if (type == "qq") { # nolint model_info <- attributes(x)$model_info - if (inherits(model, c("lme", "lmerMod", "merMod", "glmmTMB", "afex_aov", "BFBayesFactor"))) { + if (inherits(model, c("lme", "lmerMod", "merMod", "afex_aov", "BFBayesFactor"))) { res_ <- suppressMessages(sort(stats::residuals(model), na.last = NA)) dat <- stats::na.omit(data.frame(y = res_)) + } else if (inherits(model, "glmmTMB")) { + res_ <- abs(stats::residuals(model, type = "deviance")) + dat <- stats::na.omit(data.frame(y = res_)) } else if (inherits(model, "glm")) { res_ <- abs(stats::rstandard(model, type = "deviance")) fitted_ <- stats::qnorm((stats::ppoints(length(res_)) + 1) / 2)[order(order(res_))] @@ -90,7 +93,8 @@ plot.see_check_normality <- function(x, detrend = detrend, dot_alpha_level = dot_alpha, model_info = model_info, - method = method + method = method, + model_class = class(model)[1] ) } else if (type == "density") { r <- suppressMessages(stats::residuals(model)) @@ -166,10 +170,11 @@ plot.see_check_normality <- function(x, colors = unname(social_colors(c("green", "blue", "red"))), dot_alpha_level = 0.8, show_dots = TRUE, - model_info = NULL) { + model_info = NULL, + model_class = NULL) { qhalfnorm <- function(p) stats::qnorm((p + 1) / 2) # qq-halfnorm for GLM - if (isTRUE(model_info$is_binomial) || isTRUE(model_info$is_count)) { + if (!identical(model_class, "glmmTMB") && (isTRUE(model_info$is_binomial) || isTRUE(model_info$is_count))) { gg_init <- ggplot2::ggplot(x, ggplot2::aes(x = .data$x, y = .data$y)) qq_stuff <- list( ggplot2::geom_point(