Skip to content

Commit 464698b

Browse files
authored
Errors in using r2_kullback (#686)
1 parent 41d60d0 commit 464698b

File tree

7 files changed

+32
-3
lines changed

7 files changed

+32
-3
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: performance
33
Title: Assessment of Regression Models Performance
4-
Version: 0.10.8.15
4+
Version: 0.10.8.16
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ S3method(r2_coxsnell,survreg)
429429
S3method(r2_coxsnell,svycoxph)
430430
S3method(r2_coxsnell,truncreg)
431431
S3method(r2_efron,default)
432+
S3method(r2_kullback,default)
433+
S3method(r2_kullback,glm)
432434
S3method(r2_loo_posterior,BFBayesFactor)
433435
S3method(r2_loo_posterior,brmsfit)
434436
S3method(r2_loo_posterior,stanmvreg)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
`check_collinearity()` and `check_outliers()` for models with non-numeric
2929
response variables.
3030

31+
* `r2_kullback()` now gives an informative error for non-supported models.
32+
3133
## Bug fixes
3234

3335
* Fixed issue in `binned_residuals()` for models with binary outcome, where

R/r2_kl.R

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#' @param model A generalized linear model.
88
#' @param adjust Logical, if `TRUE` (the default), the adjusted R2 value is
99
#' returned.
10+
#' @param ... Additional arguments. Currently not used.
1011
#'
1112
#' @return A named vector with the R2 value.
1213
#'
@@ -19,7 +20,13 @@
1920
#' 77: 329-342.
2021
#'
2122
#' @export
22-
r2_kullback <- function(model, adjust = TRUE) {
23+
r2_kullback <- function(model, ...) {
24+
UseMethod("r2_kullback")
25+
}
26+
27+
#' @rdname r2_kullback
28+
#' @export
29+
r2_kullback.glm <- function(model, adjust = TRUE, ...) {
2330
if (adjust) {
2431
adj <- model$df.null / model$df.residual
2532
} else {
@@ -31,3 +38,8 @@ r2_kullback <- function(model, adjust = TRUE) {
3138
names(klr2) <- "Kullback-Leibler R2"
3239
klr2
3340
}
41+
42+
#' @export
43+
r2_kullback.default <- function(model, ...) {
44+
insight::format_error("This function only works for objects of class `glm`.")
45+
}

inst/WORDLIST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ poisson
296296
preprint
297297
priori
298298
pscl
299+
quantreg
299300
quared
300301
quartile
301302
quartiles

man/r2_kullback.Rd

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-r2_kullback.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@ test_that("r2_kullback", {
33
expect_equal(r2_kullback(model), c(`Kullback-Leibler R2` = 0.3834), tolerance = 1e-3)
44
expect_equal(r2_kullback(model, adjust = FALSE), c(`Kullback-Leibler R2` = 0.4232), tolerance = 1e-3)
55
})
6+
7+
test_that("r2_kullback errors for non-supported", {
8+
skip_if_not_installed("pscl")
9+
data("bioChemists", package = "pscl")
10+
model <- pscl::zeroinfl(art ~ . | 1, data = bioChemists, dist = "negbin")
11+
expect_error(r2_kullback(model), regex = "This function only works")
12+
})

0 commit comments

Comments
 (0)