From d1ede6a9737bd4160eaafa4c32b83a100939fcf1 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 3 Oct 2023 12:04:28 +0200 Subject: [PATCH 01/25] Support for nestedLogit models --- DESCRIPTION | 3 ++- NAMESPACE | 1 + R/model_performance.lm.R | 11 +++++++++++ tests/testthat/test-nestedLogit.R | 17 +++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-nestedLogit.R diff --git a/DESCRIPTION b/DESCRIPTION index 19bada6a3..49ad2e6e4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: performance Title: Assessment of Regression Models Performance -Version: 0.10.5.5 +Version: 0.10.5.6 Authors@R: c(person(given = "Daniel", family = "Lüdecke", @@ -117,6 +117,7 @@ Suggests: mgcv, mlogit, multimode, + nestedLogit, nlme, nonnest2, ordinal, diff --git a/NAMESPACE b/NAMESPACE index b5c3ee92b..4d6136b6a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -180,6 +180,7 @@ S3method(model_performance,model_fit) S3method(model_performance,multinom) S3method(model_performance,negbinirr) S3method(model_performance,negbinmfx) +S3method(model_performance,nestedLogit) S3method(model_performance,plm) S3method(model_performance,poissonirr) S3method(model_performance,poissonmfx) diff --git a/R/model_performance.lm.R b/R/model_performance.lm.R index e55253125..d8e7d76eb 100644 --- a/R/model_performance.lm.R +++ b/R/model_performance.lm.R @@ -253,6 +253,17 @@ model_performance.zeroinfl <- model_performance.lm #' @export model_performance.zerotrunc <- model_performance.lm +#' @export +model_performance.nestedLogit <- function(model, metrics = "all", verbose = TRUE, ...) { + mp <- lapply(model$models, model_performance.lm, metrics = metrics, verbose = verbose, ...) + out <- cbind( + data.frame(Response = names(mp), stringsAsFactors = FALSE), + do.call(rbind, mp) + ) + row.names(out) <- NULL + class(out) <- unique(c("performance_model", class(out))) + out +} diff --git a/tests/testthat/test-nestedLogit.R b/tests/testthat/test-nestedLogit.R new file mode 100644 index 000000000..73fd093f8 --- /dev/null +++ b/tests/testthat/test-nestedLogit.R @@ -0,0 +1,17 @@ +skip_if_not_installed("nestedLogit") +skip_if_not_installed("carData") + +data("Womenlf", package = "carData") +comparisons <- nestedLogit::logits( + work = nestedLogit::dichotomy("not.work", working = c("parttime", "fulltime")), + full = nestedLogit::dichotomy("parttime", "fulltime") +) +mnl <- nestedLogit::nestedLogit( + partic ~ hincome + children, + dichotomies = comparisons, + data = Womenlf +) + +test_that("model_performance", { + expect_snapshot(model_performance(mnl)) +}) From c336ece6410ea570706ae19e787455115fa51dde Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 3 Oct 2023 12:06:51 +0200 Subject: [PATCH 02/25] add test --- tests/testthat/_snaps/nestedLogit.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 tests/testthat/_snaps/nestedLogit.md diff --git a/tests/testthat/_snaps/nestedLogit.md b/tests/testthat/_snaps/nestedLogit.md new file mode 100644 index 000000000..907ae3fa5 --- /dev/null +++ b/tests/testthat/_snaps/nestedLogit.md @@ -0,0 +1,12 @@ +# model_performance + + Code + model_performance(mnl) + Output + # Indices of model performance + + Response | AIC | BIC | RMSE | Sigma + -------------------------------------------- + work | 325.733 | 336.449 | 0.456 | 1.000 + full | 110.495 | 118.541 | 0.398 | 1.000 + From 210761a4618541f814d8b427247e92197a52d686 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 3 Oct 2023 12:07:22 +0200 Subject: [PATCH 03/25] news --- NEWS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index 47fad4e59..06eee8f47 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # performance (development version) +## General + +* Support for `nestedLogit` models. + ## Changes to functions * `check_outliers()` for method `"ics"` now detects number of available cores From f6e8544d14da89f0a2a419aa33cf5dcde24b781d Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 3 Oct 2023 12:15:37 +0200 Subject: [PATCH 04/25] lintr --- R/model_performance.lm.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/model_performance.lm.R b/R/model_performance.lm.R index d8e7d76eb..f03ee726e 100644 --- a/R/model_performance.lm.R +++ b/R/model_performance.lm.R @@ -125,10 +125,10 @@ model_performance.lm <- function(model, metrics = "all", verbose = TRUE, ...) { if (("LOGLOSS" %in% toupper(metrics)) && isTRUE(info$is_binomial)) { out$Log_loss <- .safe({ .logloss <- performance_logloss(model, verbose = verbose) - if (!is.na(.logloss)) { - .logloss - } else { + if (is.na(.logloss)) { NULL + } else { + .logloss } }) } From 2d4a3fbb2f07f7492e5f9545917d698b891587b0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 3 Oct 2023 20:43:32 +0200 Subject: [PATCH 05/25] minor --- R/r2.R | 6 +++++- tests/testthat/test-nestedLogit.R | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/R/r2.R b/R/r2.R index 26a16f9e6..c0a53af64 100644 --- a/R/r2.R +++ b/R/r2.R @@ -291,7 +291,7 @@ r2.glm <- function(model, ci = NULL, verbose = TRUE, ...) { if (info$family %in% c("gaussian", "inverse.gaussian")) { out <- r2.default(model, ...) } else if (info$is_logit && info$is_bernoulli) { - out <- list("R2_Tjur" = r2_tjur(model, ...)) + out <- list("R2_Tjur" = r2_tjur(model, model_info = info, ...)) attr(out, "model_type") <- "Logistic" names(out$R2_Tjur) <- "Tjur's R2" class(out) <- c("r2_pseudo", class(out)) @@ -312,6 +312,10 @@ r2.glm <- function(model, ci = NULL, verbose = TRUE, ...) { #' @export r2.glmx <- r2.glm +#' @export +r2.nestedLogit <- function(model, ci = NULL, verbose = TRUE, ...) { + lapply(r2, model$models, ci = ci, verbose = verbose, ...) +} diff --git a/tests/testthat/test-nestedLogit.R b/tests/testthat/test-nestedLogit.R index 73fd093f8..beaa701d4 100644 --- a/tests/testthat/test-nestedLogit.R +++ b/tests/testthat/test-nestedLogit.R @@ -1,3 +1,4 @@ +skip_on_os(c("mac", "linux")) skip_if_not_installed("nestedLogit") skip_if_not_installed("carData") From 07d5b3a334a1e9ac8ef467f447ee937bdda90131 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 10:37:04 +0200 Subject: [PATCH 06/25] add r2-support --- NAMESPACE | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index 4d6136b6a..e573d5ce0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -372,6 +372,7 @@ S3method(r2,model_fit) S3method(r2,multinom) S3method(r2,negbinirr) S3method(r2,negbinmfx) +S3method(r2,nestedLogit) S3method(r2,ols) S3method(r2,phylolm) S3method(r2,plm) @@ -414,6 +415,7 @@ S3method(r2_coxsnell,mclogit) S3method(r2_coxsnell,multinom) S3method(r2_coxsnell,negbinirr) S3method(r2_coxsnell,negbinmfx) +S3method(r2_coxsnell,nestedLogit) S3method(r2_coxsnell,poissonirr) S3method(r2_coxsnell,poissonmfx) S3method(r2_coxsnell,polr) @@ -469,6 +471,7 @@ S3method(r2_nagelkerke,mclogit) S3method(r2_nagelkerke,multinom) S3method(r2_nagelkerke,negbinirr) S3method(r2_nagelkerke,negbinmfx) +S3method(r2_nagelkerke,nestedLogit) S3method(r2_nagelkerke,poissonirr) S3method(r2_nagelkerke,poissonmfx) S3method(r2_nagelkerke,polr) @@ -480,6 +483,8 @@ S3method(r2_posterior,BFBayesFactor) S3method(r2_posterior,brmsfit) S3method(r2_posterior,stanmvreg) S3method(r2_posterior,stanreg) +S3method(r2_tjur,default) +S3method(r2_tjur,nestedLogit) S3method(residuals,BFBayesFactor) S3method(residuals,check_normality_numeric) S3method(residuals,iv_robust) From c1cc3dfe81120e3d6b68e17be063370ea7666e3f Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 10:37:17 +0200 Subject: [PATCH 07/25] r2 --- R/model_performance.lm.R | 5 +++ R/r2.R | 19 +++++------ R/r2_coxsnell.R | 20 ++++++++++++ R/r2_nagelkerke.R | 20 ++++++++++++ R/r2_tjur.R | 32 +++++++++++++++++++ tests/testthat/_snaps/nestedLogit.md | 8 ++--- tests/testthat/test-nestedLogit.R | 47 ++++++++++++++++++++++++++++ 7 files changed, 135 insertions(+), 16 deletions(-) diff --git a/R/model_performance.lm.R b/R/model_performance.lm.R index f03ee726e..e8e5b07cf 100644 --- a/R/model_performance.lm.R +++ b/R/model_performance.lm.R @@ -260,6 +260,11 @@ model_performance.nestedLogit <- function(model, metrics = "all", verbose = TRUE data.frame(Response = names(mp), stringsAsFactors = FALSE), do.call(rbind, mp) ) + # need to handle R2 separately + if (any(c("ALL", "R2") %in% toupper(metrics))) { + out$R2 <- unlist(r2_tjur(model)) + } + row.names(out) <- NULL class(out) <- unique(c("performance_model", class(out))) out diff --git a/R/r2.R b/R/r2.R index c0a53af64..0e9760bba 100644 --- a/R/r2.R +++ b/R/r2.R @@ -49,11 +49,8 @@ r2 <- function(model, ...) { } - - # Default models ----------------------------------------------- - #' @rdname r2 #' @export r2.default <- function(model, ci = NULL, verbose = TRUE, ...) { @@ -115,6 +112,8 @@ r2.lm <- function(model, ci = NULL, ...) { #' @export r2.phylolm <- r2.lm +# helper ------------- + .r2_lm <- function(model_summary, ci = NULL) { out <- list( R2 = model_summary$r.squared, @@ -140,7 +139,6 @@ r2.phylolm <- r2.lm } - #' @export r2.summary.lm <- function(model, ci = NULL, ...) { if (!is.null(ci) && !is.na(ci)) { @@ -150,7 +148,6 @@ r2.summary.lm <- function(model, ci = NULL, ...) { } - #' @export r2.systemfit <- function(model, ...) { out <- lapply(summary(model)$eq, function(model_summary) { @@ -198,8 +195,6 @@ r2.ols <- function(model, ...) { structure(class = "r2_generic", out) } - - #' @export r2.lrm <- r2.ols @@ -207,7 +202,6 @@ r2.lrm <- r2.ols r2.cph <- r2.ols - #' @export r2.mhurdle <- function(model, ...) { resp <- insight::get_response(model, verbose = FALSE) @@ -230,7 +224,6 @@ r2.mhurdle <- function(model, ...) { } - #' @export r2.aov <- function(model, ci = NULL, ...) { if (!is.null(ci) && !is.na(ci)) { @@ -252,7 +245,6 @@ r2.aov <- function(model, ci = NULL, ...) { } - #' @export r2.mlm <- function(model, ...) { model_summary <- summary(model) @@ -276,7 +268,6 @@ r2.mlm <- function(model, ...) { } - #' @export r2.glm <- function(model, ci = NULL, verbose = TRUE, ...) { if (!is.null(ci) && !is.na(ci)) { @@ -312,9 +303,13 @@ r2.glm <- function(model, ci = NULL, verbose = TRUE, ...) { #' @export r2.glmx <- r2.glm + #' @export r2.nestedLogit <- function(model, ci = NULL, verbose = TRUE, ...) { - lapply(r2, model$models, ci = ci, verbose = verbose, ...) + out <- list("R2_Tjur" = r2_tjur(model, ...)) + attr(out, "model_type") <- "Logistic" + class(out) <- c("r2_pseudo", class(out)) + out } diff --git a/R/r2_coxsnell.R b/R/r2_coxsnell.R index c151136d4..cdb73dea7 100644 --- a/R/r2_coxsnell.R +++ b/R/r2_coxsnell.R @@ -88,6 +88,26 @@ r2_coxsnell.glm <- function(model, verbose = TRUE, ...) { #' @export r2_coxsnell.BBreg <- r2_coxsnell.glm + +#' @export +r2_coxsnell.nestedLogit <- function(model, ...) { + n <- insight::n_obs(model, disaggregate = TRUE) + stats::setNames( + lapply(names(model$models), function(i) { + m <- model$models[[i]] + # if no deviance, return NA + if (is.null(m$deviance)) { + return(NA) + } + r2_coxsnell <- (1 - exp((m$deviance - m$null.deviance) / n[[i]])) + names(r2_coxsnell) <- "Cox & Snell's R2" + r2_coxsnell + }), + names(model$models) + ) +} + + #' @export r2_coxsnell.mclogit <- function(model, ...) { insight::check_if_installed("mclogit", reason = "to calculate R2") diff --git a/R/r2_nagelkerke.R b/R/r2_nagelkerke.R index bb6230f22..85bcc6e8f 100644 --- a/R/r2_nagelkerke.R +++ b/R/r2_nagelkerke.R @@ -77,6 +77,26 @@ r2_nagelkerke.glm <- function(model, verbose = TRUE, ...) { #' @export r2_nagelkerke.BBreg <- r2_nagelkerke.glm + +#' @export +r2_nagelkerke.nestedLogit <- function(model, ...) { + n <- insight::n_obs(model, disaggregate = TRUE) + stats::setNames( + lapply(names(model$models), function(i) { + m <- model$models[[i]] + # if no deviance, return NA + if (is.null(m$deviance)) { + return(NA) + } + r2_nagelkerke <- (1 - exp((m$deviance - m$null.deviance) / n[[i]])) / (1 - exp(-m$null.deviance / n[[i]])) + names(r2_nagelkerke) <- "Nagelkerke's R2" + r2_nagelkerke + }), + names(model$models) + ) +} + + #' @export r2_nagelkerke.bife <- function(model, ...) { r2_nagelkerke <- r2_coxsnell(model) / (1 - exp(-model$null_deviance / insight::n_obs(model))) diff --git a/R/r2_tjur.R b/R/r2_tjur.R index e0aa6117c..efdca98c2 100644 --- a/R/r2_tjur.R +++ b/R/r2_tjur.R @@ -23,6 +23,11 @@ #' #' @export r2_tjur <- function(model, ...) { + UseMethod("r2_tjur") +} + +#' @export +r2_tjur.default <- function(model, ...) { info <- list(...)$model_info if (is.null(info)) { info <- suppressWarnings(insight::model_info(model, verbose = FALSE)) @@ -50,3 +55,30 @@ r2_tjur <- function(model, ...) { names(tjur_d) <- "Tjur's R2" tjur_d } + +#' @export +r2_tjur.nestedLogit <- function(model, ...) { + resp <- insight::get_response(model, dichotomies = TRUE, verbose = FALSE) + + stats::setNames( + lapply(names(model$models), function(i) { + y <- resp[[i]] + m <- model$models[[i]] + pred <- stats::predict(m, type = "response") + # delete pred for cases with missing residuals + if (anyNA(stats::residuals(m))) { + pred <- pred[!is.na(stats::residuals(m))] + } + categories <- unique(y) + m1 <- mean(pred[which(y == categories[1])], na.rm = TRUE) + m2 <- mean(pred[which(y == categories[2])], na.rm = TRUE) + + tjur_d <- abs(m2 - m1) + + names(tjur_d) <- "Tjur's R2" + tjur_d + } + ), + names(model$models) + ) +} diff --git a/tests/testthat/_snaps/nestedLogit.md b/tests/testthat/_snaps/nestedLogit.md index 907ae3fa5..f5c9a0bdd 100644 --- a/tests/testthat/_snaps/nestedLogit.md +++ b/tests/testthat/_snaps/nestedLogit.md @@ -5,8 +5,8 @@ Output # Indices of model performance - Response | AIC | BIC | RMSE | Sigma - -------------------------------------------- - work | 325.733 | 336.449 | 0.456 | 1.000 - full | 110.495 | 118.541 | 0.398 | 1.000 + Response | AIC | BIC | RMSE | Sigma | R2 + ---------------------------------------------------- + work | 325.733 | 336.449 | 0.456 | 1.000 | 0.138 + full | 110.495 | 118.541 | 0.398 | 1.000 | 0.333 diff --git a/tests/testthat/test-nestedLogit.R b/tests/testthat/test-nestedLogit.R index beaa701d4..78a8db3c7 100644 --- a/tests/testthat/test-nestedLogit.R +++ b/tests/testthat/test-nestedLogit.R @@ -13,6 +13,53 @@ mnl <- nestedLogit::nestedLogit( data = Womenlf ) +test_that("r2", { + out <- r2(mnl) + expect_equal( + out, + list(R2_Tjur = list( + work = c(`Tjur's R2` = 0.137759452521642), + full = c(`Tjur's R2` = 0.332536937208286) + )), + ignore_attr = TRUE, + tolerance = 1e-4 + ) + + out <- r2_tjur(mnl) + expect_equal( + out, + list(R2_Tjur = list( + work = c(`Tjur's R2` = 0.137759452521642), + full = c(`Tjur's R2` = 0.332536937208286) + )), + ignore_attr = TRUE, + tolerance = 1e-4 + ) + + out <- r2_coxsnell(mnl) + expect_equal( + out, + list( + work = c(`Cox & Snell's R2` = 0.129313084315599), + full = c(`Cox & Snell's R2` = 0.308541455410686) + ), + ignore_attr = TRUE, + tolerance = 1e-4 + ) + + out <- r2_nagelkerke(mnl) + expect_equal( + out, + list( + work = c(`Nagelkerke's R2` = 0.174313365512442), + full = c(`Nagelkerke's R2` = 0.418511411473948) + ), + ignore_attr = TRUE, + tolerance = 1e-4 + ) +}) + + test_that("model_performance", { expect_snapshot(model_performance(mnl)) }) From c1455f90aeae91dfabab858d6772123f2738262b Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 10:47:38 +0200 Subject: [PATCH 08/25] styler --- R/r2_tjur.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/R/r2_tjur.R b/R/r2_tjur.R index efdca98c2..6a044e299 100644 --- a/R/r2_tjur.R +++ b/R/r2_tjur.R @@ -77,8 +77,7 @@ r2_tjur.nestedLogit <- function(model, ...) { names(tjur_d) <- "Tjur's R2" tjur_d - } - ), + }), names(model$models) ) } From e132f51c6c949780054094ff7411ed6416e380ba Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 12:18:58 +0200 Subject: [PATCH 09/25] use latest insight --- DESCRIPTION | 2 +- tests/testthat/test-nestedLogit.R | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 49ad2e6e4..933df479a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -150,4 +150,4 @@ Config/Needs/website: r-lib/pkgdown, easystats/easystatstemplate Config/rcmdcheck/ignore-inconsequential-notes: true -Remotes: easystats/see, easystats/parameters +Remotes: easystats/see, easystats/parameters, easystats/insight diff --git a/tests/testthat/test-nestedLogit.R b/tests/testthat/test-nestedLogit.R index 78a8db3c7..70c978fc0 100644 --- a/tests/testthat/test-nestedLogit.R +++ b/tests/testthat/test-nestedLogit.R @@ -1,4 +1,5 @@ skip_on_os(c("mac", "linux")) +skip_if(packageVersion("insight") <= "0.19.5.10") skip_if_not_installed("nestedLogit") skip_if_not_installed("carData") From f195fed49cba966699ce6e70608003084ed36702 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 12:23:38 +0200 Subject: [PATCH 10/25] lintr --- R/r2.R | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/R/r2.R b/R/r2.R index 0e9760bba..0f066a5be 100644 --- a/R/r2.R +++ b/R/r2.R @@ -170,11 +170,11 @@ r2.systemfit <- function(model, ...) { #' @export r2.lm_robust <- function(model, ...) { out <- list( - "R2" = tryCatch( + R2 = tryCatch( model[["r.squared"]], error = function(e) NULL ), - "R2_adjusted" = tryCatch( + R2_adjusted = tryCatch( model[["adj.r.squared"]], error = function(e) NULL ) @@ -282,7 +282,7 @@ r2.glm <- function(model, ci = NULL, verbose = TRUE, ...) { if (info$family %in% c("gaussian", "inverse.gaussian")) { out <- r2.default(model, ...) } else if (info$is_logit && info$is_bernoulli) { - out <- list("R2_Tjur" = r2_tjur(model, model_info = info, ...)) + out <- list(R2_Tjur = r2_tjur(model, model_info = info, ...)) attr(out, "model_type") <- "Logistic" names(out$R2_Tjur) <- "Tjur's R2" class(out) <- c("r2_pseudo", class(out)) @@ -292,7 +292,7 @@ r2.glm <- function(model, ci = NULL, verbose = TRUE, ...) { } out <- NULL } else { - out <- list("R2_Nagelkerke" = r2_nagelkerke(model, ...)) + out <- list(R2_Nagelkerke = r2_nagelkerke(model, ...)) names(out$R2_Nagelkerke) <- "Nagelkerke's R2" attr(out, "model_type") <- "Generalized Linear" class(out) <- c("r2_pseudo", class(out)) @@ -306,7 +306,7 @@ r2.glmx <- r2.glm #' @export r2.nestedLogit <- function(model, ci = NULL, verbose = TRUE, ...) { - out <- list("R2_Tjur" = r2_tjur(model, ...)) + out <- list(R2_Tjur = r2_tjur(model, ...)) attr(out, "model_type") <- "Logistic" class(out) <- c("r2_pseudo", class(out)) out @@ -357,7 +357,7 @@ r2.model_fit <- r2.logitmfx #' @export r2.BBreg <- function(model, ...) { - out <- list("R2_CoxSnell" = r2_coxsnell(model)) + out <- list(R2_CoxSnell = r2_coxsnell(model)) names(out$R2_CoxSnell) <- "Cox & Snell's R2" class(out) <- c("r2_pseudo", class(out)) out @@ -377,7 +377,7 @@ r2.bayesx <- r2.BBreg #' @export r2.censReg <- function(model, ...) { - out <- list("R2_Nagelkerke" = r2_nagelkerke(model)) + out <- list(R2_Nagelkerke = r2_nagelkerke(model)) names(out$R2_Nagelkerke) <- "Nagelkerke's R2" class(out) <- c("r2_pseudo", class(out)) out @@ -506,8 +506,8 @@ r2.wbm <- function(model, tolerance = 1e-5, ...) { names(r2_marginal) <- "Marginal R2" out <- list( - "R2_conditional" = r2_conditional, - "R2_marginal" = r2_marginal + R2_conditional = r2_conditional, + R2_marginal = r2_marginal ) attr(out, "model_type") <- "Fixed Effects" @@ -530,8 +530,8 @@ r2.sem <- function(model, ...) { structure( class = "r2_nakagawa", list( - "R2_conditional" = r2_conditional, - "R2_marginal" = r2_marginal + R2_conditional = r2_conditional, + R2_marginal = r2_marginal ) ) } @@ -693,7 +693,7 @@ r2.ivreg <- function(model, ...) { #' @export r2.bigglm <- function(model, ...) { - out <- list("R2_CoxSnell" = summary(model)$rsq) + out <- list(R2_CoxSnell = summary(model)$rsq) names(out$R2_CoxSnell) <- "Cox & Snell's R2" class(out) <- c("r2_pseudo", class(out)) out @@ -761,8 +761,8 @@ r2.Arima <- function(model, ...) { r2.plm <- function(model, ...) { model_summary <- summary(model) out <- list( - "R2" = c(`R2` = model_summary$r.squared[1]), - "R2_adjusted" = c(`adjusted R2` = model_summary$r.squared[2]) + R2 = c(`R2` = model_summary$r.squared[1]), + R2_adjusted = c(`adjusted R2` = model_summary$r.squared[2]) ) attr(out, "model_type") <- "Panel Data" @@ -778,8 +778,8 @@ r2.selection <- function(model, ...) { return(NULL) } out <- list( - "R2" = c(`R2` = model_summary$rSquared$R2), - "R2_adjusted" = c(`adjusted R2` = model_summary$rSquared$R2adj) + R2 = c(`R2` = model_summary$rSquared$R2), + R2_adjusted = c(`adjusted R2` = model_summary$rSquared$R2adj) ) attr(out, "model_type") <- "Tobit 2" From 96a544a6e726865c0ca78c66a86f3eef722e136d Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 12:23:59 +0200 Subject: [PATCH 11/25] lintr --- R/r2.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/r2.R b/R/r2.R index 0f066a5be..f3e2b7b6c 100644 --- a/R/r2.R +++ b/R/r2.R @@ -748,10 +748,10 @@ r2.mmclogit <- function(model, ...) { #' @export r2.Arima <- function(model, ...) { - if (!requireNamespace("forecast", quietly = TRUE)) { - list(R2 = NA) - } else { + if (requireNamespace("forecast", quietly = TRUE)) { list(R2 = stats::cor(stats::fitted(model), insight::get_data(model, verbose = FALSE))^2) + } else { + list(R2 = NA) } } From b6a118beb310ee886f700a38ca67e24869cbdb34 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 12:24:38 +0200 Subject: [PATCH 12/25] lintr --- R/r2.R | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/R/r2.R b/R/r2.R index f3e2b7b6c..486e8e559 100644 --- a/R/r2.R +++ b/R/r2.R @@ -562,12 +562,10 @@ r2.gam <- function(model, ...) { # gamlss inherits from gam, and summary.gamlss prints results automatically printout <- utils::capture.output(s <- summary(model)) # nolint - if (!is.null(s$r.sq)) { - list( - R2 = c(`Adjusted R2` = s$r.sq) - ) - } else { + if (is.null(s$r.sq)) { NextMethod() + } else { + list(R2 = c(`Adjusted R2` = s$r.sq)) } } From c36d0d375f7c467fe4f749a6d3cdcd603f55a585 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 12:28:44 +0200 Subject: [PATCH 13/25] add test --- tests/testthat/test-binned_residuals.R | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/testthat/test-binned_residuals.R diff --git a/tests/testthat/test-binned_residuals.R b/tests/testthat/test-binned_residuals.R new file mode 100644 index 000000000..972f030af --- /dev/null +++ b/tests/testthat/test-binned_residuals.R @@ -0,0 +1,19 @@ +test_that("binned_residuals", { + data(mtcars) + model <- glm(vs ~ wt + mpg, data = mtcars, family = "binomial") + result <- binned_residuals(model) + expect_named( + result, + c("xbar", "ybar", "n", "x.lo", "x.hi", "se", "ci_range", "CI_low", "CI_high", "group") + ) + expect_equal( + result$xbar, + c(0.03786, 0.09514, 0.25911, 0.47955, 0.71109, 0.97119), + tolerance = 1e-4 + ) + expect_equal( + result$ybar, + c(-0.03786, -0.09514, 0.07423, -0.07955, 0.28891, -0.13786), + tolerance = 1e-4 + ) +}) From 3f6c19279f9afc710a3c6813f175ab1787085e9e Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 12:31:47 +0200 Subject: [PATCH 14/25] test --- tests/testthat/test-binned_residuals.R | 50 ++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/testthat/test-binned_residuals.R b/tests/testthat/test-binned_residuals.R index 972f030af..4499441bd 100644 --- a/tests/testthat/test-binned_residuals.R +++ b/tests/testthat/test-binned_residuals.R @@ -17,3 +17,53 @@ test_that("binned_residuals", { tolerance = 1e-4 ) }) + + +test_that("binned_residuals, n_bins", { + data(mtcars) + model <- glm(vs ~ wt + mpg, data = mtcars, family = "binomial") + result <- binned_residuals(model, n_bins = 10) + expect_named( + result, + c("xbar", "ybar", "n", "x.lo", "x.hi", "se", "ci_range", "CI_low", "CI_high", "group") + ) + expect_equal( + result$xbar, + c( + 0.02373, 0.06301, 0.08441, 0.17907, 0.29225, 0.44073, 0.54951, + 0.69701, 0.9168, 0.99204 + ), + tolerance = 1e-4 + ) + expect_equal( + result$ybar, + c( + -0.02373, -0.06301, -0.08441, -0.17907, 0.20775, -0.1074, 0.11715, + 0.30299, -0.25014, 0.00796 + ), + tolerance = 1e-4 + ) +}) + + +test_that("binned_residuals, terms", { + data(mtcars) + model <- glm(vs ~ wt + mpg, data = mtcars, family = "binomial") + result <- binned_residuals(model, term = "mpg") + expect_named( + result, + c("xbar", "ybar", "n", "x.lo", "x.hi", "se", "ci_range", "CI_low", "CI_high", "group") + ) + expect_equal( + result$xbar, + c(12.62, 15.34, 18.1, 20.9, 22.875, 30.06667), + tolerance = 1e-4 + ) + expect_equal( + result$ybar, + c(-0.05435, -0.07866, 0.13925, -0.11861, 0.27763, -0.13786), + tolerance = 1e-4 + ) +}) + +dput(round(result$ybar, 5)) From ee95065e8a6eb8f3eeddb122feac95390b595d29 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 12:33:51 +0200 Subject: [PATCH 15/25] tests --- tests/testthat/test-binned_residuals.R | 2 -- tests/testthat/test-check_autocorrelation.R | 6 ++++++ 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/test-check_autocorrelation.R diff --git a/tests/testthat/test-binned_residuals.R b/tests/testthat/test-binned_residuals.R index 4499441bd..62958b519 100644 --- a/tests/testthat/test-binned_residuals.R +++ b/tests/testthat/test-binned_residuals.R @@ -65,5 +65,3 @@ test_that("binned_residuals, terms", { tolerance = 1e-4 ) }) - -dput(round(result$ybar, 5)) diff --git a/tests/testthat/test-check_autocorrelation.R b/tests/testthat/test-check_autocorrelation.R new file mode 100644 index 000000000..d33a97279 --- /dev/null +++ b/tests/testthat/test-check_autocorrelation.R @@ -0,0 +1,6 @@ +test_that("check_autocorrelation", { + data(mtcars) + m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars) + out <- check_autocorrelation(m) + expect_equal(out, 0.278, ignor_attr = TRUE, tolerance = 1e-3) +}) From 8467a762f8770d901c3fbcb4d7bc0748ab00bb40 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 12:37:58 +0200 Subject: [PATCH 16/25] test --- tests/testthat/test-check_distribution.R | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/testthat/test-check_distribution.R diff --git a/tests/testthat/test-check_distribution.R b/tests/testthat/test-check_distribution.R new file mode 100644 index 000000000..9226e6102 --- /dev/null +++ b/tests/testthat/test-check_distribution.R @@ -0,0 +1,34 @@ +test_that("check_distribution", { + skip_if_not_installed("lme4") + skip_if_not_installed("randomForest") + data(sleepstudy, package = "lme4") + model <<- lme4::lmer(Reaction ~ Days + (Days | Subject), sleepstudy) + out <- check_distribution(model) + + expect_identical( + out$Distribution, + c( + "bernoulli", "beta", "beta-binomial", "binomial", "cauchy", + "chi", "exponential", "F", "gamma", "half-cauchy", "inverse-gamma", + "lognormal", "neg. binomial (zero-infl.)", "negative binomial", + "normal", "pareto", "poisson", "poisson (zero-infl.)", "tweedie", + "uniform", "weibull" + ) + ) + expect_equal( + out$p_Residuals, + c( + 0, 0, 0, 0, 0.9375, 0, 0, 0, 0, 0, 0, 0.03125, 0, 0, 0, 0, + 0, 0, 0, 0, 0.03125 + ), + tolerance = 1e-4 + ) + expect_equal( + out$p_Response, + c( + 0, 0, 0.03125, 0, 0, 0, 0, 0, 0.4375, 0.03125, 0, 0.46875, + 0.03125, 0, 0, 0, 0, 0, 0, 0, 0 + ), + tolerance = 1e-4 + ) +}) From 2c3c00ab141427dbd04377bceb9352da1c57661e Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 12:39:11 +0200 Subject: [PATCH 17/25] add snapshot --- tests/testthat/_snaps/check_distribution.md | 21 +++++++++++++++++++++ tests/testthat/test-check_distribution.R | 2 ++ 2 files changed, 23 insertions(+) create mode 100644 tests/testthat/_snaps/check_distribution.md diff --git a/tests/testthat/_snaps/check_distribution.md b/tests/testthat/_snaps/check_distribution.md new file mode 100644 index 000000000..11187a23b --- /dev/null +++ b/tests/testthat/_snaps/check_distribution.md @@ -0,0 +1,21 @@ +# check_distribution + + Code + print(out) + Output + # Distribution of Model Family + + Predicted Distribution of Residuals + + Distribution Probability + cauchy 94% + lognormal 3% + weibull 3% + + Predicted Distribution of Response + + Distribution Probability + lognormal 47% + gamma 44% + beta-binomial 3% + diff --git a/tests/testthat/test-check_distribution.R b/tests/testthat/test-check_distribution.R index 9226e6102..e8ab2835f 100644 --- a/tests/testthat/test-check_distribution.R +++ b/tests/testthat/test-check_distribution.R @@ -31,4 +31,6 @@ test_that("check_distribution", { ), tolerance = 1e-4 ) + + expect_snapshot(print(out)) }) From 7caea04fb1f447b6cb6c176df422b21b5604ce31 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 12:42:16 +0200 Subject: [PATCH 18/25] add test --- tests/testthat/test-check_heterogeneity_bias.R | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 tests/testthat/test-check_heterogeneity_bias.R diff --git a/tests/testthat/test-check_heterogeneity_bias.R b/tests/testthat/test-check_heterogeneity_bias.R new file mode 100644 index 000000000..2ae8fead3 --- /dev/null +++ b/tests/testthat/test-check_heterogeneity_bias.R @@ -0,0 +1,8 @@ +test_that("check_heterogeneity_bias", { + data(iris) + set.seed(123) + iris$ID <- sample(1:4, nrow(iris), replace = TRUE) # fake-ID + out <- check_heterogeneity_bias(iris, select = c("Sepal.Length", "Petal.Length"), group = "ID") + expect_equal(out, c("Sepal.Length", "Petal.Length"), ignore_attr = TRUE) + expect_output(print(out), "Possible heterogeneity bias due to following predictors: Sepal\\.Length, Petal\\.Length") +}) From 6db512104e445f5e2874fe0546f33a3bc0a634a6 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 12:45:40 +0200 Subject: [PATCH 19/25] test --- tests/testthat/test-check_heterogeneity_bias.R | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/testthat/test-check_heterogeneity_bias.R b/tests/testthat/test-check_heterogeneity_bias.R index 2ae8fead3..3cfdec519 100644 --- a/tests/testthat/test-check_heterogeneity_bias.R +++ b/tests/testthat/test-check_heterogeneity_bias.R @@ -5,4 +5,19 @@ test_that("check_heterogeneity_bias", { out <- check_heterogeneity_bias(iris, select = c("Sepal.Length", "Petal.Length"), group = "ID") expect_equal(out, c("Sepal.Length", "Petal.Length"), ignore_attr = TRUE) expect_output(print(out), "Possible heterogeneity bias due to following predictors: Sepal\\.Length, Petal\\.Length") + + m <- lm(Sepal.Length ~ Petal.Length + Petal.Width + Species + ID, data = iris) + expect_error( + check_heterogeneity_bias(m, select = c("Sepal.Length", "Petal.Length"), group = "ID"), + regex = "no mixed model" + ) + + skip_if_not_installed("lme4") + m <- lme4::lmer(Sepal.Length ~ Petal.Length + Petal.Width + Species + (1 | ID), data = iris) + out <- check_heterogeneity_bias(m, select = c("Sepal.Length", "Petal.Length"), group = "ID") + expect_equal(out, c("Petal.Length", "Petal.Width", "Species"), ignore_attr = TRUE) + expect_output( + print(out), + "Possible heterogeneity bias due to following predictors: Petal\\.Length, Petal\\.Width, Species" + ) }) From 393d574edcbda9d225a692a278a6802eec3b2ec8 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 12:46:50 +0200 Subject: [PATCH 20/25] test --- tests/testthat/test-check_heterogeneity_bias.R | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/testthat/test-check_heterogeneity_bias.R b/tests/testthat/test-check_heterogeneity_bias.R index 3cfdec519..7abc6af30 100644 --- a/tests/testthat/test-check_heterogeneity_bias.R +++ b/tests/testthat/test-check_heterogeneity_bias.R @@ -6,6 +6,10 @@ test_that("check_heterogeneity_bias", { expect_equal(out, c("Sepal.Length", "Petal.Length"), ignore_attr = TRUE) expect_output(print(out), "Possible heterogeneity bias due to following predictors: Sepal\\.Length, Petal\\.Length") + out <- check_heterogeneity_bias(iris, select = ~ Sepal.Length + Petal.Length, group = ~ID) + expect_equal(out, c("Sepal.Length", "Petal.Length"), ignore_attr = TRUE) + expect_output(print(out), "Possible heterogeneity bias due to following predictors: Sepal\\.Length, Petal\\.Length") + m <- lm(Sepal.Length ~ Petal.Length + Petal.Width + Species + ID, data = iris) expect_error( check_heterogeneity_bias(m, select = c("Sepal.Length", "Petal.Length"), group = "ID"), @@ -20,4 +24,10 @@ test_that("check_heterogeneity_bias", { print(out), "Possible heterogeneity bias due to following predictors: Petal\\.Length, Petal\\.Width, Species" ) + out <- check_heterogeneity_bias(m, select = ~ Sepal.Length + Petal.Length, group = ~ID) + expect_equal(out, c("Petal.Length", "Petal.Width", "Species"), ignore_attr = TRUE) + expect_output( + print(out), + "Possible heterogeneity bias due to following predictors: Petal\\.Length, Petal\\.Width, Species" + ) }) From 21c62114ed06ee3a0b0af718932ba1123061cc7c Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 12:56:23 +0200 Subject: [PATCH 21/25] fix --- tests/testthat/test-binned_residuals.R | 2 +- tests/testthat/test-check_autocorrelation.R | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-binned_residuals.R b/tests/testthat/test-binned_residuals.R index 62958b519..4aa69e0ec 100644 --- a/tests/testthat/test-binned_residuals.R +++ b/tests/testthat/test-binned_residuals.R @@ -30,7 +30,7 @@ test_that("binned_residuals, n_bins", { expect_equal( result$xbar, c( - 0.02373, 0.06301, 0.08441, 0.17907, 0.29225, 0.44073, 0.54951, + 0.02373, 0.06301, 0.08441, 0.17907, 0.29225, 0.44073, 0.54951, 0.69701, 0.9168, 0.99204 ), tolerance = 1e-4 diff --git a/tests/testthat/test-check_autocorrelation.R b/tests/testthat/test-check_autocorrelation.R index d33a97279..b389f3985 100644 --- a/tests/testthat/test-check_autocorrelation.R +++ b/tests/testthat/test-check_autocorrelation.R @@ -1,6 +1,7 @@ test_that("check_autocorrelation", { data(mtcars) m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars) + set.seed(123) out <- check_autocorrelation(m) - expect_equal(out, 0.278, ignor_attr = TRUE, tolerance = 1e-3) + expect_equal(as.vector(out), 0.316, ignor_attr = TRUE, tolerance = 1e-2) }) From ec8d4dc76cb46aaf68f93f3491854e135892c11b Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 12:57:27 +0200 Subject: [PATCH 22/25] lintr --- R/r2.R | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/R/r2.R b/R/r2.R index 486e8e559..5b266efc7 100644 --- a/R/r2.R +++ b/R/r2.R @@ -600,7 +600,7 @@ r2.rma <- function(model, ...) { #' @export r2.feis <- function(model, ...) { out <- list( - R2 = c(`R2` = model$r2), + R2 = c(R2 = model$r2), R2_adjusted = c(`adjusted R2` = model$adj.r2) ) @@ -651,7 +651,7 @@ r2.fixest_multi <- function(model, ...) { r2.felm <- function(model, ...) { model_summary <- summary(model) out <- list( - R2 = c(`R2` = model_summary$r2), + R2 = c(R2 = model_summary$r2), R2_adjusted = c(`adjusted R2` = model_summary$r2adj) ) @@ -665,7 +665,7 @@ r2.felm <- function(model, ...) { #' @export r2.iv_robust <- function(model, ...) { out <- list( - R2 = c(`R2` = model$r.squared), + R2 = c(R2 = model$r.squared), R2_adjusted = c(`adjusted R2` = model$adj.r.squared) ) @@ -679,7 +679,7 @@ r2.iv_robust <- function(model, ...) { r2.ivreg <- function(model, ...) { model_summary <- summary(model) out <- list( - R2 = c(`R2` = model_summary$r.squared), + R2 = c(R2 = model_summary$r.squared), R2_adjusted = c(`adjusted R2` = model_summary$adj.r.squared) ) @@ -725,7 +725,7 @@ r2.biglm <- function(model, ...) { r2.lmrob <- function(model, ...) { model_summary <- summary(model) out <- list( - R2 = c(`R2` = model_summary$r.squared), + R2 = c(R2 = model_summary$r.squared), R2_adjusted = c(`adjusted R2` = model_summary$adj.r.squared) ) @@ -759,7 +759,7 @@ r2.Arima <- function(model, ...) { r2.plm <- function(model, ...) { model_summary <- summary(model) out <- list( - R2 = c(`R2` = model_summary$r.squared[1]), + R2 = c(R2 = model_summary$r.squared[1]), R2_adjusted = c(`adjusted R2` = model_summary$r.squared[2]) ) @@ -776,7 +776,7 @@ r2.selection <- function(model, ...) { return(NULL) } out <- list( - R2 = c(`R2` = model_summary$rSquared$R2), + R2 = c(R2 = model_summary$rSquared$R2), R2_adjusted = c(`adjusted R2` = model_summary$rSquared$R2adj) ) @@ -792,7 +792,7 @@ r2.svyglm <- function(model, ...) { rsq.adjust <- 1 - ((1 - rsq) * (model$df.null / model$df.residual)) out <- list( - R2 = c(`R2` = rsq), + R2 = c(R2 = rsq), R2_adjusted = c(`adjusted R2` = rsq.adjust) ) From 72ed16b09086df02d173e666e1860da00f3e3f05 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 13:06:24 +0200 Subject: [PATCH 23/25] remove invalid URL --- R/check_itemscale.R | 8 +++----- man/check_itemscale.Rd | 2 -- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/R/check_itemscale.R b/R/check_itemscale.R index 8d8b71082..5aa704618 100644 --- a/R/check_itemscale.R +++ b/R/check_itemscale.R @@ -40,8 +40,6 @@ #' - Briggs SR, Cheek JM (1986) The role of factor analysis in the development #' and evaluation of personality scales. Journal of Personality, 54(1), #' 106-148. doi: 10.1111/j.1467-6494.1986.tb00391.x -#' - Trochim WMK (2008) Types of Reliability. -#' ([web](https://conjointly.com/kb/types-of-reliability/)) #' #' @examplesIf require("parameters") && require("psych") #' # data generation from '?prcomp', slightly modified @@ -82,9 +80,9 @@ check_itemscale <- function(x) { Mean = vapply(items, mean, numeric(1), na.rm = TRUE), SD = vapply(items, stats::sd, numeric(1), na.rm = TRUE), Skewness = vapply(items, function(i) as.numeric(datawizard::skewness(i)), numeric(1)), - "Difficulty" = item_difficulty(items)$Difficulty, - "Discrimination" = .item_discr, - "alpha if deleted" = .item_alpha, + Difficulty = item_difficulty(items)$Difficulty, + Discrimination = .item_discr, + `alpha if deleted` = .item_alpha, stringsAsFactors = FALSE, check.names = FALSE ) diff --git a/man/check_itemscale.Rd b/man/check_itemscale.Rd index 7fa487ab5..7f790b1d2 100644 --- a/man/check_itemscale.Rd +++ b/man/check_itemscale.Rd @@ -61,7 +61,5 @@ check_itemscale(pca) \item Briggs SR, Cheek JM (1986) The role of factor analysis in the development and evaluation of personality scales. Journal of Personality, 54(1), 106-148. doi: 10.1111/j.1467-6494.1986.tb00391.x -\item Trochim WMK (2008) Types of Reliability. -(\href{https://conjointly.com/kb/types-of-reliability/}{web}) } } From 22f1ea99d8f796dadd3c6470ad9263d672f4c99b Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 13:19:51 +0200 Subject: [PATCH 24/25] lintr --- R/r2.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/r2.R b/R/r2.R index 5b266efc7..c844f22cd 100644 --- a/R/r2.R +++ b/R/r2.R @@ -804,7 +804,7 @@ r2.svyglm <- function(model, ...) { #' @export r2.vglm <- function(model, ...) { - out <- list("R2_McKelvey" = r2_mckelvey(model)) + out <- list(R2_McKelvey = r2_mckelvey(model)) names(out$R2_McKelvey) <- "McKelvey's R2" class(out) <- c("r2_pseudo", class(out)) out @@ -817,7 +817,7 @@ r2.vgam <- r2.vglm #' @export r2.DirichletRegModel <- function(model, ...) { - out <- list("R2_Nagelkerke" = r2_nagelkerke(model)) + out <- list(R2_Nagelkerke = r2_nagelkerke(model)) names(out$R2_Nagelkerke) <- "Nagelkerke's R2" class(out) <- c("r2_pseudo", class(out)) out From b2e1a6e5dd4001f105ee2ea21ba1770ce25df0d0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 13:23:12 +0200 Subject: [PATCH 25/25] fix test --- tests/testthat/test-nestedLogit.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-nestedLogit.R b/tests/testthat/test-nestedLogit.R index 70c978fc0..47a852b04 100644 --- a/tests/testthat/test-nestedLogit.R +++ b/tests/testthat/test-nestedLogit.R @@ -29,10 +29,10 @@ test_that("r2", { out <- r2_tjur(mnl) expect_equal( out, - list(R2_Tjur = list( + list( work = c(`Tjur's R2` = 0.137759452521642), full = c(`Tjur's R2` = 0.332536937208286) - )), + ), ignore_attr = TRUE, tolerance = 1e-4 )