Skip to content

Commit

Permalink
performance::r2_nakagawa() and r.squaredGLMM() give different values …
Browse files Browse the repository at this point in the history
…for Gaussian glmmTMB models without random effects

Fixes #652
  • Loading branch information
strengejacke committed Nov 21, 2023
1 parent 18827e8 commit ea28c89
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: performance
Title: Assessment of Regression Models Performance
Version: 0.10.8.1
Version: 0.10.8.2
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
28 changes: 25 additions & 3 deletions R/r2.R
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,6 @@ r2.merMod <- function(model, ci = NULL, tolerance = 1e-5, ...) {
r2_nakagawa(model, ci = ci, tolerance = tolerance, ...)
}

#' @export
r2.glmmTMB <- r2.merMod

#' @export
r2.cpglmm <- r2.merMod

Expand All @@ -491,6 +488,19 @@ r2.MixMod <- r2.merMod
#' @export
r2.rlmerMod <- r2.merMod

#' @export
r2.glmmTMB <- function(model, ci = NULL, tolerance = 1e-5, ...) {
if (insight::is_mixed_model(model)) {
r2_nakagawa(model, ci = ci, tolerance = tolerance, ...)
} else {
mi <- insight::model_info(model, verbose = FALSE)
if (mi$is_linear) {
.r2_lm_manual(model)
} else {
insight::format_error("`r2()` does not support models of class `glmmTMB` without random effects and this link-function.") # nolint
}
}
}

#' @export
r2.wbm <- function(model, tolerance = 1e-5, ...) {
Expand Down Expand Up @@ -840,3 +850,15 @@ r2.DirichletRegModel <- function(model, ...) {
}
ci
}


.r2_lm_manual <- function(model) {
r <- stats::residuals(model)
f <- stats::fitted(model)
rss <- sum(r^2)
mss <- sum((f - mean(f, na.rm = TRUE))^2)

Check warning on line 859 in R/r2.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/r2.R,line=859,col=44,[trailing_whitespace_linter] Trailing whitespace is superfluous.
out <- list(R2 = mss / (mss + rss))
names(out$R2) <- "R2"
attr(out, "model_type") <- "Linear"
structure(class = "r2_generic", out)
}

0 comments on commit ea28c89

Please sign in to comment.