Skip to content

Commit

Permalink
lintr
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Nov 22, 2023
1 parent 281d4fd commit 472b39e
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions R/r2_nagelkerke.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,23 @@ r2_nagelkerke.glm <- function(model, verbose = TRUE, ...) {
if (is.null(info)) {
info <- suppressWarnings(insight::model_info(model, verbose = FALSE))
}

if (info$is_binomial && !info$is_bernoulli && class(model)[1] == "glm") {
if (verbose) {
insight::format_warning("Can't calculate accurate R2 for binomial models that are not Bernoulli models.")
}
return(NULL)
} else {
r2cox <- r2_coxsnell(model)
if (is.na(r2cox) || is.null(r2cox)) {
return(NULL)
}
r2_nagelkerke <- r2cox / (1 - exp(-model$null.deviance / insight::n_obs(model, disaggregate = TRUE)))
names(r2_nagelkerke) <- "Nagelkerke's R2"
r2_nagelkerke
}

r2cox <- r2_coxsnell(model)

if (is.na(r2cox) || is.null(r2cox)) {
return(NULL)
}

r2_nagelkerke <- r2cox / (1 - exp(-model$null.deviance / insight::n_obs(model, disaggregate = TRUE)))
names(r2_nagelkerke) <- "Nagelkerke's R2"
r2_nagelkerke
}

#' @export
Expand All @@ -84,26 +87,31 @@ r2_nagelkerke.glmmTMB <- function(model, verbose = TRUE, ...) {
if (is.null(info)) {
info <- suppressWarnings(insight::model_info(model, verbose = FALSE))
}

if (info$is_binomial && !info$is_bernoulli) {
if (verbose) {
insight::format_warning("Can't calculate accurate R2 for binomial models that are not Bernoulli models.")
}
return(NULL)
} else {
dev <- stats::deviance(model)
# if no deviance, return NA
if (is.null(dev)) {
return(NULL)
}
null_dev <- stats::deviance(insight::null_model(model))
r2cox <- (1 - exp((dev - null_dev) / insight::n_obs(model, disaggregate = TRUE)))
if (is.na(r2cox) || is.null(r2cox)) {
return(NULL)
}
r2_nagelkerke <- r2cox / (1 - exp(-null_dev / insight::n_obs(model, disaggregate = TRUE)))
names(r2_nagelkerke) <- "Nagelkerke's R2"
r2_nagelkerke
}

dev <- stats::deviance(model)

# if no deviance, return NA
if (is.null(dev)) {
return(NULL)
}

null_dev <- stats::deviance(insight::null_model(model))
r2cox <- (1 - exp((dev - null_dev) / insight::n_obs(model, disaggregate = TRUE)))

if (is.na(r2cox) || is.null(r2cox)) {
return(NULL)
}

r2_nagelkerke <- r2cox / (1 - exp(-null_dev / insight::n_obs(model, disaggregate = TRUE)))
names(r2_nagelkerke) <- "Nagelkerke's R2"
r2_nagelkerke
}


Expand Down

0 comments on commit 472b39e

Please sign in to comment.