R/check_model_diagnostics.R

Defines functions .sigma_glmmTMB_nonmixed .quasipoisson_dispersion .fitted_rows .expected_variance .use_simulated_residuals .binomial_fitted .model_diagnostic_overdispersion .model_diagnostic_homogeneity .model_diagnostic_ncv .glmmTMB_variance .glmmTMB_estimated_X .glmmTMB_dispersion_unsupported .glmmTMB_influence_unsupported .influence_glmmTMB .model_diagnostic_outlier .model_diagnostic_normality .model_diagnostic_ranef_qq .model_diagnostic_qq .model_diagnostic_vif

# prepare data for VIF plot ----------------------------------

.model_diagnostic_vif <- function(model, verbose = TRUE) {
  out <- check_collinearity(model, verbose = verbose)
  dat <- insight::compact_list(attr(out, "data"))
  if (is.null(dat)) {
    return(NULL)
  }
  dat$group <- "low"
  dat$group[dat$VIF >= 5 & dat$VIF < 10] <- "moderate"
  dat$group[dat$VIF >= 10] <- "high"

  dat <- datawizard::data_select(
    dat,
    select = c(x = "Term", y = "VIF", facet = "Component", group = "group", df = "df"),
    verbose = FALSE
  )

  if (insight::n_unique(dat$facet) <= 1) {
    dat$facet <- NULL
  }

  attr(dat, "CI") <- attributes(out)$CI
  dat
}


# prepare data for QQ plot ----------------------------------

.model_diagnostic_qq <- function(model, model_info = NULL, verbose = TRUE) {
  if (
    inherits(model, c("lme", "lmerMod", "merMod", "afex_aov", "BFBayesFactor", "gam"))
  ) {
    res_ <- stats::residuals(model)
  } else if (inherits(model, "geeglm")) {
    res_ <- stats::residuals(model, type = "pearson")
  } else if (inherits(model, "glmmTMB")) {
    res_ <- stats::residuals(model, type = "deviance")
  } else if (inherits(model, "glm")) {
    res_ <- .safe(abs(stats::rstandard(model, type = "deviance")))
  } else {
    res_ <- .safe(stats::rstudent(model))
    if (is.null(res_)) {
      res_ <- .safe(stats::residuals(model))
    }
  }

  if (is.null(res_) || all(is.na(res_))) {
    if (verbose) {
      if (is.null(model_info$family)) {
        fam <- "model"
      } else {
        fam <- paste0("`", model_info$family, "`")
      }
      insight::format_alert(
        paste(
          sprintf(
            "QQ plot could not be created. Cannot extract residuals from objects of class `%s`.",
            class(model)[1]
          ),
          sprintf(
            "Maybe the model class or the %s family does not support the computation of (deviance) residuals?",
            fam
          )
        )
      )
    }
    return(NULL)
  }

  if (
    inherits(model, c("glm", "glmerMod")) ||
      (inherits(model, "glmmTMB") && isFALSE(model_info$is_linear))
  ) {
    fitted_ <- stats::qnorm((stats::ppoints(length(res_)) + 1) / 2)
  } else {
    fitted_ <- stats::fitted(model)
  }

  # validation check, sometimes either residuals or fitted can contain NA, see #488
  if (anyNA(res_) || anyNA(fitted_)) {
    # drop NA and make sure both fitted and residuals match
    non_na <- !is.na(fitted_) & !is.na(res_)
    fitted_ <- fitted_[non_na]
    res_ <- res_[non_na]
  }

  res_ <- sort(res_, na.last = NA)
  fitted_ <- sort(fitted_, na.last = NA)

  data.frame(x = fitted_, y = res_)
}


# prepare data for random effects QQ plot ----------------------------------

.model_diagnostic_ranef_qq <- function(
  model,
  level = 0.95,
  model_info = NULL,
  verbose = TRUE
) {
  # check if we have mixed model
  if (is.null(model_info) || !model_info$is_mixed) {
    return(NULL)
  }

  insight::check_if_installed("lme4")

  ## TODO: check if we can simplify code by using following:
  ## as.data.frame(lme4::ranef(model, condVar = TRUE), stringsAsFactors = FALSE)

  tryCatch(
    if (inherits(model, "glmmTMB")) {
      var_attr <- "condVar"
      re <- .collapse_cond(lme4::ranef(model, condVar = TRUE))
    } else {
      var_attr <- "postVar"
      re <- lme4::ranef(model, condVar = TRUE)
    },
    error = function(e) {
      NULL
    }
  )

  se <- tryCatch(
    suppressWarnings(lapply(re, function(.x) {
      pv <- attr(.x, var_attr, exact = TRUE)
      unlist(lapply(
        if (is.list(pv)) {
          pv
        } else {
          list(pv)
        },
        function(.pv) {
          cols <- seq_len(dim(.pv)[1])
          unlist(lapply(cols, function(.y) sqrt(.pv[.y, .y, ])))
        }
      ))
    })),
    error = function(e) {
      NULL
    }
  )

  if (is.null(se)) {
    if (verbose) {
      insight::format_alert(
        "Could not compute standard errors from random effects for diagnostic plot."
      )
    }
    return(NULL)
  }

  Map(
    function(.re, .se) {
      ord <- unlist(lapply(.re, order)) +
        rep((0:(ncol(.re) - 1)) * nrow(.re), each = nrow(.re))

      df.y <- unlist(.re)[ord]
      df.ci <- stats::qnorm((1 + level) / 2) * .se[ord]

      data.frame(
        x = rep(stats::qnorm(stats::ppoints(nrow(.re))), ncol(.re)),
        y = df.y,
        conf.low = df.y - df.ci,
        conf.high = df.y + df.ci,
        facet = gl(ncol(.re), nrow(.re), labels = names(.re)),
        stringsAsFactors = FALSE,
        row.names = NULL
      )
    },
    re,
    se
  )
}


# prepare data for normality of residuals plot ----------------------------------

.model_diagnostic_normality <- function(model, verbose = TRUE) {
  r <- try(as.numeric(stats::residuals(model)), silent = TRUE)

  if (inherits(r, "try-error")) {
    insight::format_alert(sprintf(
      "Non-normality of residuals could not be computed. Cannot extract residuals from objects of class '%s'.",
      class(model)[1]
    ))
    return(NULL)
  }

  dat <- as.data.frame(bayestestR::estimate_density(r))
  dat$curve <- stats::dnorm(
    seq(min(dat$x), max(dat$x), length.out = nrow(dat)),
    mean(r),
    stats::sd(r)
  )
  dat
}


# prepare data for influential obs plot ----------------------------------

.model_diagnostic_outlier <- function(model, threshold = NULL) {
  # use the threshold from `check_outliers()`, so that the contour lines
  # match the flagged outliers
  if (!is.null(threshold)) {
    cook_levels <- threshold
  } else if (inherits(model, "lm", which = TRUE) == 1) {
    s <- summary(model)
    cook_levels <- round(stats::qf(0.5, s$fstatistic[2], s$fstatistic[3]), 2)
  } else {
    cook_levels <- c(0.5, 1)
  }

  if (inherits(model, "glmmTMB")) {
    influence_glmmTMB <- .influence_glmmTMB(model)
    if (is.null(influence_glmmTMB)) {
      return(NULL)
    }
    # one row per fitted observation, also with `na.action = na.exclude`. A
    # data frame keeps the column name `Predicted` of the other models.
    predicted <- data.frame(Predicted = influence_glmmTMB$fitted)
    model_resid <- influence_glmmTMB$residuals
    n_params <- influence_glmmTMB$n_params
    hat_values <- influence_glmmTMB$hat
    cooks_distance <- influence_glmmTMB$cooks_distance
    std_resid <- influence_glmmTMB$std_resid
  } else {
    model_resid <- as.numeric(insight::get_residuals(model))
    predicted <- insight::get_predicted(model, ci = NULL)
    n_params <- tryCatch(model$rank, error = function(e) insight::n_parameters(model))

    infl <- stats::influence(model, do.coef = FALSE)
    hat_values <- infl$hat
    cooks_distance <- stats::cooks.distance(model, infl)

    std_resid <- tryCatch(
      stats::rstandard(model, infl),
      error = function(e) {
        # no rstandard() method (e.g., lmerMod): standardize Pearson residuals,
        # so that the Cook's distance contours match `cooks.distance()`
        .safe(
          as.numeric(
            stats::residuals(model, type = "pearson") /
              (stats::sigma(model) * sqrt(1 - infl$hat))
          ),
          model_resid
        )
      }
    )
  }

  plot_data <- data.frame(
    Hat = hat_values,
    Cooks_Distance = cooks_distance,
    Fitted = predicted,
    Residuals = model_resid,
    Std_Residuals = std_resid,
    stringsAsFactors = FALSE
  )
  plot_data$Index <- seq_len(nrow(plot_data))
  plot_data$Influential <- "OK"
  plot_data$Influential[
    abs(plot_data$Cooks_Distance) >= max(cook_levels)
  ] <- "Influential"

  attr(plot_data, "cook_levels") <- cook_levels
  attr(plot_data, "n_params") <- n_params
  plot_data
}


# glmmTMB has no influence() or hatvalues() method. The hat values are the
# diagonal of W^(1/2) A H^-1 A' W^(1/2), with A = [X, Z], W the GLM weight
# a * (dmu/deta)^2 / (phi * V(mu)) with a the prior weights, and
# H = A' W A + G, where G is zero for the fixed effects and the inverse
# random-effects covariance for the random effects (after Lee & Nelder 1996,
# p. 631, with phi moved into W and their diagonal U generalized to G).
# With L L' equal to that covariance and B = [W^(1/2) Z L, W^(1/2) X], they
# are the diagonal of B (B'B + D)^-1 B', with D the identity for the random
# effects and zero for the fixed effects, which also works when a variance
# is 0. A sparse Cholesky factor of B'B + D keeps this fast for many
# random-effect levels. Cook's distance is the one-step formula that
# `cooks.distance()` uses for glm, so that `check_model()` needs no refits.
# Returns NULL for models that `.glmmTMB_influence_unsupported()` names.
.influence_glmmTMB <- function(model) {
  if (!is.null(.glmmTMB_influence_unsupported(model))) {
    return(NULL)
  }
  insight::check_if_installed("Matrix")
  n_obs <- nrow(glmmTMB::getME(model, "X"))

  # with `na.action = na.exclude`, predictions and residuals have NA rows for
  # the observations that the model did not use
  na_action <- attr(stats::model.frame(model), "na.action")
  fitted_rows <- function(x) {
    x <- as.vector(x)
    if (length(x) == n_obs + length(na_action) && length(na_action) > 0) {
      x <- x[-na_action]
    }
    x
  }

  X <- .glmmTMB_estimated_X(model)

  fam <- stats::family(model)
  eta <- fitted_rows(stats::predict(model, type = "link"))
  mu <- fam$linkinv(eta)
  # for binomial models, the prior weights include the number of trials
  prior_weights <- fitted_rows(stats::weights(model))
  if (!length(prior_weights)) {
    prior_weights <- rep(1, n_obs)
  }
  variance <- .glmmTMB_variance(model, mu)
  w <- prior_weights * fam$mu.eta(eta)^2 / variance

  B <- Matrix::Matrix(X, sparse = TRUE)
  n_re <- 0
  re_struc <- model$modelInfo$reStruc$condReStruc
  if (length(re_struc)) {
    vc <- glmmTMB::VarCorr(model)$cond
    roots <- lapply(seq_along(re_struc), function(i) {
      s <- matrix(unclass(vc[[i]]), nrow = nrow(vc[[i]]))
      e <- eigen(s, symmetric = TRUE)
      root <- e$vectors %*% diag(sqrt(pmax(e$values, 0)), nrow = nrow(s))
      Matrix::kronecker(
        Matrix::Diagonal(re_struc[[i]]$blockReps),
        Matrix::Matrix(root, sparse = TRUE)
      )
    })
    ZL <- glmmTMB::getME(model, "Z") %*% Matrix::bdiag(roots)
    n_re <- ncol(ZL)
    B <- cbind(ZL, B)
  }
  B <- Matrix::Diagonal(x = sqrt(w)) %*% B
  penalized <- Matrix::crossprod(B) +
    Matrix::Diagonal(x = rep(c(1, 0), c(n_re, ncol(X))))
  # a fill-reducing permutation limits the fill-in of the factor, for example
  # when the random-effect terms are not ordered by their number of levels.
  # Heavily crossed random effects can still give a dense factor.
  chol_factor <- Matrix::Cholesky(
    Matrix::forceSymmetric(penalized),
    perm = TRUE,
    LDL = FALSE
  )
  solved <- Matrix::solve(
    chol_factor,
    Matrix::solve(chol_factor, Matrix::t(B), system = "P"),
    system = "L"
  )
  hat_values <- as.vector(Matrix::colSums(solved^2))

  n_params <- ncol(X)
  model_resid <- fitted_rows(stats::residuals(model, type = "response"))
  pearson <- model_resid * sqrt(prior_weights / variance)
  list(
    hat = hat_values,
    cooks_distance = pearson^2 * hat_values / (n_params * (1 - hat_values)^2),
    std_resid = pearson / sqrt(1 - hat_values),
    residuals = model_resid,
    fitted = mu,
    n_params = n_params
  )
}


# The reason why `.influence_glmmTMB()` gives no hat values for a glmmTMB
# model, or NULL if it gives them. The supported families are those whose
# hat values can be checked against lme4, glm or glm.nb.
.glmmTMB_influence_unsupported <- function(model) {
  family_name <- stats::family(model)$family
  re_terms <- model$modelInfo$reStruc
  if (ncol(glmmTMB::getME(model, "Xzi")) > 0 || length(re_terms$ziReStruc)) {
    return("zero-inflation")
  }
  dispersion <- .glmmTMB_dispersion_unsupported(model, family_name)
  if (!is.null(dispersion)) {
    return(dispersion)
  }
  if (!family_name %in% c("gaussian", "poisson", "binomial", "Gamma", "nbinom2")) {
    return(paste0("the `", family_name, "` family"))
  }
  # Cook's distance divides by the number of estimated fixed effects
  if (ncol(.glmmTMB_estimated_X(model)) == 0) {
    return("no estimated fixed effects")
  }
  re_struc <- re_terms$condReStruc
  if (length(re_struc)) {
    vc <- glmmTMB::VarCorr(model)$cond
    block_sizes <- vapply(re_struc, function(i) i$blockSize, numeric(1))
    if (
      length(vc) != length(re_struc) || any(vapply(vc, nrow, numeric(1)) != block_sizes)
    ) {
      return("this random-effects structure")
    }
  }
  NULL
}


# Families with a dispersion parameter need an estimated dispersion from
# `dispformula = ~1`. For poisson and binomial models, the dispersion model
# has no columns.
.glmmTMB_dispersion_unsupported <- function(model, family_name) {
  x_disp <- glmmTMB::getME(model, "Xdisp")
  n_disp <- ncol(x_disp)
  has_dispersion <- family_name %in% c("gaussian", "Gamma", "nbinom2")
  if (
    n_disp > 1 ||
      (has_dispersion && n_disp != 1) ||
      (n_disp == 1 && !all(x_disp[, 1] == 1)) ||
      length(model$modelInfo$reStruc$dispReStruc)
  ) {
    return("a dispersion formula")
  }
  # `dispformula = ~0` and `map` fix the dispersion through `betadisp`
  if (has_dispersion && anyNA(model$obj$env$map[["betadisp"]])) {
    return("a fixed dispersion")
  }
  # `sigma()` ignores an offset in the dispersion formula
  if (any(model$obj$env$data$dispoffset != 0)) {
    return("a dispersion formula")
  }
  NULL
}


# The conditional fixed-effect model matrix with one column per estimated
# fixed effect. Fixed effects that `map` fixes are dropped, and fixed effects
# that `map` sets equal are one column. With `rank_check = "skip"`, aliased
# columns are dropped. `[[` avoids a partial match of `betadisp` or `betazi`.
.glmmTMB_estimated_X <- function(model) {
  X <- as.matrix(glmmTMB::getME(model, "X"))
  beta_map <- model$obj$env$map[["beta"]]
  if (!is.null(beta_map)) {
    estimated <- !is.na(beta_map)
    beta_level <- droplevels(beta_map[estimated])
    X <- X[, estimated, drop = FALSE] %*%
      (outer(as.integer(beta_level), seq_len(nlevels(beta_level)), "==") * 1)
  }
  if (ncol(X) > 0) {
    qr_X <- qr(X)
    X <- X[, qr_X$pivot[seq_len(qr_X$rank)], drop = FALSE]
  }
  X
}


# Var(y | random effects) for a prior weight of 1, for the families in
# `.glmmTMB_influence_unsupported()`
.glmmTMB_variance <- function(model, mu) {
  switch(
    stats::family(model)$family,
    gaussian = rep(stats::sigma(model)^2, length(mu)),
    Gamma = stats::sigma(model)^2 * mu^2,
    poisson = mu,
    binomial = mu * (1 - mu),
    nbinom2 = mu * (1 + mu / stats::sigma(model))
  )
}


# prepare data for non-constant variance plot ----------------------------------

.model_diagnostic_ncv <- function(model, verbose = TRUE) {
  ncv <- tryCatch(
    data.frame(
      x = as.numeric(stats::fitted(model)),
      y = as.numeric(stats::residuals(model))
    ),
    error = function(e) {
      NULL
    }
  )

  if (is.null(ncv)) {
    if (verbose) {
      insight::format_alert(sprintf(
        "Non-constant error variance could not be computed. Cannot extract residuals from objects of class '%s'.",
        class(model)[1]
      ))
    }
    return(NULL)
  }

  ncv
}


# prepare data for homogeneity of variance plot ----------------------------------

.model_diagnostic_homogeneity <- function(model, verbose = TRUE) {
  faminfo <- insight::model_info(model)
  r <- tryCatch(
    if (inherits(model, "merMod")) {
      stats::residuals(model, scaled = TRUE)
    } else if (inherits(model, "gam")) {
      stats::residuals(model, type = "scaled.pearson")
    } else if (inherits(model, c("glmmTMB", "MixMod"))) {
      ## Pearson residuals are scaled by the family's variance function V(mu_i),
      ## which varies across observations. The fallback below divides by a single
      ## scalar, which is only correct when V() does not depend on mu (e.g.
      ## gaussian). For non-mixed binomial/poisson models `.sigma_glmmTMB_nonmixed()`
      ## returns 1, i.e. no standardization at all.
      r_pearson <- .safe(stats::residuals(model, type = "pearson"))
      if (is.null(r_pearson) || all(is.na(r_pearson))) {
        residual_sigma <- if (faminfo$is_mixed) {
          sqrt(insight::get_variance_residual(model))
        } else {
          .sigma_glmmTMB_nonmixed(model, faminfo)
        }
        stats::residuals(model, type = "response") / residual_sigma
      } else {
        r_pearson
      }
    } else if (inherits(model, "glm")) {
      ## TODO: check if we can / should use deviance residuals (as for QQ plots) here as well?
      stats::rstandard(model, type = "pearson")
    } else {
      stats::rstandard(model)
    },
    error = function(e) {
      NULL
    }
  )

  if (is.null(r)) {
    if (verbose) {
      insight::format_alert(sprintf(
        "Homogeneity of variance could not be computed. Cannot extract residual variance from objects of class '%s'.",
        class(model)[1]
      ))
    }
    return(NULL)
  }

  data.frame(
    x = stats::fitted(model),
    y = sqrt(abs(r))
  )
}


.model_diagnostic_overdispersion <- function(model, residual_type = NULL, ...) {
  faminfo <- insight::model_info(model)
  d <- NULL

  if (.use_simulated_residuals(model, residual_type, faminfo)) {
    trials <- NULL
    response_scale <- 1
    d <- .safe({
      simres <- simulate_residuals(model, ...)
      predicted <- .fitted_rows(simres$fittedPredictedResponse, model)
      if (identical(faminfo$family, "binomial")) {
        model_frame <- stats::model.frame(model)
        response <- stats::model.response(model_frame)
        matrix_response <- is.matrix(response) && ncol(response) == 2
        trials <- if (matrix_response) {
          rowSums(response)
        } else {
          1
        }
        weights <- stats::model.weights(model_frame)
        if (!is.null(weights)) {
          if (matrix_response) {
            response_scale <- weights
          } else {
            trials <- weights
          }
        }
        # DHARMa versions differ on whether fittedPredictedResponse is a
        # proportion or a count; getFitted() consistently returns proportions.
        predicted <- .binomial_fitted(model) * trials * response_scale
      }
      # Use response-scale residuals (observed - predicted) so that Res2 and V
      # are on the same scale as the family variance formula.
      raw_res <- simres$observedResponse - predicted
      data.frame(
        Predicted = predicted,
        Residuals = raw_res,
        Res2 = raw_res^2
      )
    })
    if (!is.null(d)) {
      d$V <- .expected_variance(
        model,
        faminfo,
        d,
        trials = trials,
        response_scale = response_scale
      )
      # Pearson-like standardized residuals: divide by sqrt of expected variance.
      # Use 1e-6 floor to avoid division by near-zero values when predicted
      # means are very small, while keeping the value practically interpretable.
      d$StdRes <- d$Residuals / sqrt(pmax(d$V, 1e-6))
      return(d)
    }
  }

  # data for poisson models
  if (faminfo$is_poisson && !faminfo$is_zero_inflated) {
    d <- data.frame(
      Predicted = .fitted_rows(stats::predict(model, type = "response"), model)
    )
    d$Residuals <- insight::get_response(model) - as.vector(d$Predicted)
    d$Res2 <- d$Residuals^2
    d$V <- .expected_variance(model, faminfo, d)
    d$StdRes <- .fitted_rows(insight::get_residuals(model, type = "pearson"), model)
    if (!is.null(d$StdRes)) {
      d$StdRes <- d$StdRes / sqrt(.quasipoisson_dispersion(model, faminfo))
    }
  }

  # data for negative binomial models
  if (faminfo$is_negbin && !faminfo$is_zero_inflated) {
    d <- data.frame(
      Predicted = .fitted_rows(stats::predict(model, type = "response"), model)
    )
    d$Residuals <- insight::get_response(model) - as.vector(d$Predicted)
    d$Res2 <- d$Residuals^2
    d$V <- .expected_variance(model, faminfo, d)
    d$StdRes <- .fitted_rows(insight::get_residuals(model, type = "pearson"), model)
  }

  # data for zero-inflated poisson models
  if (faminfo$is_poisson && faminfo$is_zero_inflated) {
    d <- data.frame(
      Predicted = .fitted_rows(stats::predict(model, type = "response"), model)
    )
    d$Residuals <- insight::get_response(model) - as.vector(d$Predicted)
    d$Res2 <- d$Residuals^2
    if (inherits(model, "glmmTMB")) {
      ptype <- "zprob"
    } else {
      ptype <- "zero"
    }
    d$Prob <- .fitted_rows(stats::predict(model, type = ptype), model)
    d$V <- .expected_variance(model, faminfo, d)
    d$StdRes <- .fitted_rows(insight::get_residuals(model, type = "pearson"), model)
  }

  # data for zero-inflated negative binomial models
  if (faminfo$is_negbin && faminfo$is_zero_inflated && !faminfo$is_dispersion) {
    d <- data.frame(
      Predicted = .fitted_rows(stats::predict(model, type = "response"), model)
    )
    d$Residuals <- insight::get_response(model) - as.vector(d$Predicted)
    d$Res2 <- d$Residuals^2
    d$V <- .expected_variance(model, faminfo, d)
    d$StdRes <- .fitted_rows(insight::get_residuals(model, type = "pearson"), model)
  }

  # data for zero-inflated negative binomial models with dispersion
  if (faminfo$is_negbin && faminfo$is_zero_inflated && faminfo$is_dispersion) {
    d <- data.frame(
      Predicted = .fitted_rows(stats::predict(model, type = "response"), model)
    )
    d$Residuals <- insight::get_response(model) - as.vector(d$Predicted)
    d$Res2 <- d$Residuals^2
    d$V <- .expected_variance(model, faminfo, d)
    d$StdRes <- .fitted_rows(insight::get_residuals(model, type = "pearson"), model)
  }

  # fallback for remaining models
  if (is.null(d)) {
    d <- data.frame(
      Predicted = .fitted_rows(stats::predict(model, type = "response"), model)
    )
    d$Residuals <- .fitted_rows(insight::get_residuals(model), model)
    d$Res2 <- d$Residuals^2
    d$V <- .expected_variance(model, faminfo, d)
    d$StdRes <- d$Residuals / sqrt(pmax(d$V, 1e-6))
  }

  d
}


.binomial_fitted <- function(model) {
  .fitted_rows(DHARMa::getFitted(model), model)
}


# helpers ----------------------------------

.use_simulated_residuals <- function(model, residual_type, faminfo) {
  # validate argument
  if (!is.null(residual_type)) {
    insight::validate_argument(residual_type, c("normal", "simulated"))
  }

  # For mixed models and glmmTMB models, use simulated residuals from DHARMa
  # for more accurate overdispersion visualization. Pearson residuals can be
  # misleading for these model types.

  # fmt: skip
  if (is.null(residual_type)) {
    if (inherits(model, "glm")) {
      simres <- faminfo$is_bernoulli ||
        faminfo$is_binomial ||
        (!faminfo$is_count && !faminfo$is_binomial) ||
        faminfo$is_negbin
    } else {
      simres <- isTRUE(faminfo$is_mixed) ||
        inherits(model, "glmmTMB") ||
        faminfo$is_negbin
    }
  } else {
    simres <- switch(residual_type, normal = FALSE, simulated = TRUE)
  }
  # catch models/families not supported by DHARMa - we need to add more
  # exceptions here as they appear, but for now, `check_model()` also
  # automatically falls back to normal Q-Q plot for all models not supported
  # by DHARMa
  not_supported <- c("fixest", "glmx")

  if (
    inherits(model, not_supported) ||
      faminfo$family %in% c("quasipoisson", "quasibinomial") ||
      !requireNamespace("DHARMa", quietly = TRUE)
  ) {
    simres <- FALSE
  }

  simres
}


.expected_variance <- function(model, faminfo, d, trials = NULL, response_scale = 1) {
  expected_var <- NULL

  # Helper: get per-observation dispersion (if dispersion model) or scalar sigma
  .get_disp <- function() {
    if (inherits(model, "cpglmm")) {
      model@phi
    } else if (isTRUE(faminfo$is_dispersion)) {
      .fitted_rows(stats::predict(model, type = "disp"), model)
    } else {
      insight::get_sigma(model)
    }
  }

  # Helper: get zero-inflation probability (glmmTMB uses "zprob", others "zero")
  .get_zprob <- function() {
    ptype <- if (inherits(model, "glmmTMB")) "zprob" else "zero"
    .fitted_rows(stats::predict(model, type = ptype), model)
  }

  # handle models without zero-inflation component
  if (!faminfo$is_zero_inflated) {
    if (faminfo$is_poisson) {
      # Poisson: V = mu; quasipoisson glm: V = phi * mu (prior weights are
      # not included)
      expected_var <- d$Predicted * .quasipoisson_dispersion(model, faminfo)
    } else if (isTRUE(faminfo$family == "nbinom1")) {
      # nbinom1: V = mu(1 + phi)  [phi=0 is Poisson limit; larger phi = more OD]
      expected_var <- d$Predicted * (1 + .get_disp())
    } else if (isTRUE(faminfo$family == "nbinom2")) {
      # nbinom2: V = mu(1 + mu/phi)  [phi->inf is Poisson limit]
      expected_var <- d$Predicted * (1 + d$Predicted / .get_disp())
    } else if (faminfo$is_negbin) {
      # General negbin (e.g., MASS::glm.nb): parameterized as nbinom2
      expected_var <- d$Predicted * (1 + d$Predicted / insight::get_sigma(model))
    } else if (faminfo$is_tweedie) {
      # Tweedie distribution: phi*mu^power
      if (inherits(model, "cpglmm")) {
        p <- model@p - 2
      } else {
        insight::check_if_installed("glmmTMB")
        p <- unname(unlist(glmmTMB::family_params(model)))
      }
      expected_var <- .get_disp() * d$Predicted^p
    }
  } else if (faminfo$is_poisson) {
    # Zero-inflated Poisson: V = mu(1-p)(1 + mu*p)
    d$Prob <- .get_zprob()
    expected_var <- d$Predicted * (1 - d$Prob) * (1 + d$Predicted * d$Prob)
  } else if (isTRUE(faminfo$family == "nbinom1")) {
    # Zero-inflated nbinom1
    disp <- .get_disp()
    d$Prob <- .get_zprob()
    expected_var <- d$Predicted *
      (1 + disp) *
      (1 - d$Prob) *
      (1 + d$Predicted * (1 + disp) * d$Prob)
  } else if (isTRUE(faminfo$family == "nbinom2")) {
    # Zero-inflated nbinom2
    disp <- .get_disp()
    d$Prob <- .get_zprob()
    expected_var <- d$Predicted *
      (1 + d$Predicted / disp) *
      (1 - d$Prob) *
      (1 + d$Predicted * (1 + d$Predicted / disp) * d$Prob)
  } else if (faminfo$is_negbin) {
    # General zero-inflated negbin
    disp <- insight::get_sigma(model)
    d$Prob <- .get_zprob()
    expected_var <- d$Predicted *
      (1 + d$Predicted / disp) *
      (1 - d$Prob) *
      (1 + d$Predicted * (1 + d$Predicted / disp) * d$Prob)
  }

  # fallback for all other families
  if (is.null(expected_var)) {
    if (!is.null(trials) && identical(faminfo$family, "binomial")) {
      # For success counts, Var(Y) = n * p * (1 - p).
      n <- trials * response_scale
      if (faminfo$is_zero_inflated) {
        prob <- .get_zprob()
        n <- n * (1 - prob)
      }
      p <- d$Predicted / ifelse(n > 0, n, 1)
      expected_var <- response_scale^2 * trials * stats::family(model)$variance(p)
      if (faminfo$is_zero_inflated) {
        expected_var <- (1 - prob) *
          expected_var +
          response_scale^2 * trials^2 * p^2 * prob * (1 - prob)
      }
    }
    # find required arguments
    formal_args <- if (is.null(expected_var)) {
      .safe(formals(stats::family(model)$variance))
    } else {
      NULL
    }
    # error when not supported
    if (!is.null(formal_args)) {
      fun_args <- list()
      if ("mu" %in% names(formal_args)) {
        fun_args$mu <- d$Predicted
      }
      if ("theta" %in% names(formal_args)) {
        fun_args$theta <- .get_disp()
      }
      if ("phi" %in% names(formal_args)) {
        fun_args$phi <- .get_disp()
      }
      expected_var <- .safe(do.call(stats::family(model)$variance, fun_args))
    }
  }

  if (is.null(expected_var)) {
    insight::format_error(paste0(
      "Models of class `",
      class(model)[1],
      "` or model family `",
      faminfo$family,
      "` are not yet supported by `check_overdispersion()`. ",
      "Please file an issue at {.url https://github.com/easystats/performance/issues/}."
    ))
  }

  expected_var
}


# with `na.action = na.exclude`, predictions and residuals are padded with NA,
# so we remove the rows that were not used to fit the model
.fitted_rows <- function(x, model) {
  # not `.safe()`, which lets errors through with `easystats_errors = TRUE`
  model_frame <- tryCatch(stats::model.frame(model), error = function(e) NULL)
  na_action <- attr(model_frame, "na.action", exact = TRUE)
  if (
    !is.null(x) &&
      inherits(na_action, "exclude") &&
      length(x) == nrow(model_frame) + length(na_action)
  ) {
    x <- x[-na_action]
  }
  x
}


# the estimated dispersion phi of a quasipoisson glm, and 1 for other models,
# or when phi cannot be estimated (for example, with no residual degrees of
# freedom) or is not positive
.quasipoisson_dispersion <- function(model, faminfo) {
  phi <- 1
  if (inherits(model, "glm") && identical(faminfo$family, "quasipoisson")) {
    sigma2 <- as.numeric(insight::get_sigma(model))^2
    if (length(sigma2) == 1 && is.finite(sigma2) && sigma2 > 0) {
      phi <- sigma2
    }
  }
  phi
}


.sigma_glmmTMB_nonmixed <- function(model, faminfo) {
  if (!is.na(match(faminfo$family, c("binomial", "poisson", "truncated_poisson")))) {
    return(1)
  }
  betad <- model$fit$par["betadisp"]
  switch(
    faminfo$family,
    gaussian = exp(0.5 * betad),
    Gamma = exp(-0.5 * betad),
    exp(betad)
  )
}

Try the performance package in your browser

Any scripts or data that you put into this service are public.

performance documentation built on Nov. 21, 2026, 1:09 a.m.