Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors in using r2_kullback #686

Merged
merged 3 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.15
Version: 0.10.8.16
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ S3method(r2_coxsnell,survreg)
S3method(r2_coxsnell,svycoxph)
S3method(r2_coxsnell,truncreg)
S3method(r2_efron,default)
S3method(r2_kullback,default)
S3method(r2_kullback,glm)
S3method(r2_loo_posterior,BFBayesFactor)
S3method(r2_loo_posterior,brmsfit)
S3method(r2_loo_posterior,stanmvreg)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
`check_collinearity()` and `check_outliers()` for models with non-numeric
response variables.

* `r2_kullback()` now gives an informative error for non-supported models.

## Bug fixes

* Fixed issue in `binned_residuals()` for models with binary outcome, where
Expand Down
14 changes: 13 additions & 1 deletion R/r2_kl.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#' @param model A generalized linear model.
#' @param adjust Logical, if `TRUE` (the default), the adjusted R2 value is
#' returned.
#' @param ... Additional arguments. Currently not used.
#'
#' @return A named vector with the R2 value.
#'
Expand All @@ -19,7 +20,13 @@
#' 77: 329-342.
#'
#' @export
r2_kullback <- function(model, adjust = TRUE) {
r2_kullback <- function(model, ...) {
UseMethod("r2_kullback")
}

#' @rdname r2_kullback
#' @export
r2_kullback.glm <- function(model, adjust = TRUE, ...) {
if (adjust) {
adj <- model$df.null / model$df.residual
} else {
Expand All @@ -31,3 +38,8 @@ r2_kullback <- function(model, adjust = TRUE) {
names(klr2) <- "Kullback-Leibler R2"
klr2
}

#' @export
r2_kullback.default <- function(model, ...) {
insight::format_error("This function only works for objects of class `glm`.")
}
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ poisson
preprint
priori
pscl
quantreg
quared
quartile
quartiles
Expand Down
7 changes: 6 additions & 1 deletion man/r2_kullback.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/testthat/test-r2_kullback.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ test_that("r2_kullback", {
expect_equal(r2_kullback(model), c(`Kullback-Leibler R2` = 0.3834), tolerance = 1e-3)
expect_equal(r2_kullback(model, adjust = FALSE), c(`Kullback-Leibler R2` = 0.4232), tolerance = 1e-3)
})

test_that("r2_kullback errors for non-supported", {
skip_if_not_installed("pscl")
data("bioChemists", package = "pscl")
model <- pscl::zeroinfl(art ~ . | 1, data = bioChemists, dist = "negbin")
expect_error(r2_kullback(model), regex = "This function only works")
})
Loading