From 3985394a0fa0c06e0a78d202e9b2da658eb0d640 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 16 Feb 2024 21:00:19 +0100 Subject: [PATCH] Errors in using r2_kullback Fixes #667 --- DESCRIPTION | 2 +- NAMESPACE | 2 ++ NEWS.md | 2 ++ R/r2_kl.R | 14 +++++++++++++- man/r2_kullback.Rd | 7 ++++++- 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index cdd668003..70adc6648 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", diff --git a/NAMESPACE b/NAMESPACE index 710374ed7..b483c40ad 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/NEWS.md b/NEWS.md index 8327f127b..68d6fe215 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/r2_kl.R b/R/r2_kl.R index b5c1bb649..553e338e4 100644 --- a/R/r2_kl.R +++ b/R/r2_kl.R @@ -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. #' @@ -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 { @@ -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`.") +} diff --git a/man/r2_kullback.Rd b/man/r2_kullback.Rd index d2980d18e..ae0f9dd55 100644 --- a/man/r2_kullback.Rd +++ b/man/r2_kullback.Rd @@ -2,13 +2,18 @@ % Please edit documentation in R/r2_kl.R \name{r2_kullback} \alias{r2_kullback} +\alias{r2_kullback.glm} \title{Kullback-Leibler R2} \usage{ -r2_kullback(model, adjust = TRUE) +r2_kullback(model, ...) + +\method{r2_kullback}{glm}(model, adjust = TRUE, ...) } \arguments{ \item{model}{A generalized linear model.} +\item{...}{Additional arguments. Currently not used.} + \item{adjust}{Logical, if \code{TRUE} (the default), the adjusted R2 value is returned.} }