From 5d1709984c06b6784f92d7f80767dfffe4945876 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 16 Feb 2024 20:35:35 +0100 Subject: [PATCH 1/4] support for quantile regression and mixed effects quantile regression Fixes #671 --- NAMESPACE | 3 +++ NEWS.md | 2 ++ R/check_outliers.R | 9 +++++++++ 3 files changed, 14 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index 970895f14..710374ed7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -89,6 +89,9 @@ S3method(check_outliers,metagen) S3method(check_outliers,numeric) S3method(check_outliers,rma) S3method(check_outliers,rma.uni) +S3method(check_outliers,rq) +S3method(check_outliers,rqs) +S3method(check_outliers,rqss) S3method(check_overdispersion,default) S3method(check_overdispersion,fixest) S3method(check_overdispersion,fixest_multi) diff --git a/NEWS.md b/NEWS.md index 2a8b4ecb9..bcabe1a82 100644 --- a/NEWS.md +++ b/NEWS.md @@ -39,6 +39,8 @@ * Fixed edge cases in `check_collinearity()` and `check_outliers()` for models with response variables of classes `Date`, `POSIXct`, `POSIXlt` or `difftime`. +* Fixed issue with `check_model()` for models of package *quantreg*. + # performance 0.10.8 ## Changes diff --git a/R/check_outliers.R b/R/check_outliers.R index 574201454..2017d4cc5 100644 --- a/R/check_outliers.R +++ b/R/check_outliers.R @@ -1987,3 +1987,12 @@ check_outliers.lmrob <- check_outliers.glmmTMB #' @export check_outliers.glmrob <- check_outliers.glmmTMB + +#' @export +check_outliers.rq <- check_outliers.glmmTMB + +#' @export +check_outliers.rqs <- check_outliers.glmmTMB + +#' @export +check_outliers.rqss <- check_outliers.glmmTMB From 12b2b9e649f65fd8c835ee7e3d40228802110d99 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 16 Feb 2024 20:40:53 +0100 Subject: [PATCH 2/4] tests --- DESCRIPTION | 3 ++- tests/testthat/test-check_model.R | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 142767fa3..cdd668003 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: performance Title: Assessment of Regression Models Performance -Version: 0.10.8.13 +Version: 0.10.8.15 Authors@R: c(person(given = "Daniel", family = "Lüdecke", @@ -128,6 +128,7 @@ Suggests: patchwork, pscl, psych, + quantreg, qqplotr (>= 0.0.6), randomForest, rempsyc, diff --git a/tests/testthat/test-check_model.R b/tests/testthat/test-check_model.R index cc512ed78..4a3d23703 100644 --- a/tests/testthat/test-check_model.R +++ b/tests/testthat/test-check_model.R @@ -43,3 +43,11 @@ test_that("`check_model()` for invalid models", { m1 <- lm(y ~ 1, data = dd) expect_error(check_model(m1)) }) + +test_that("`check_model()` works for quantreg", { + skip_if_not_installed("quantreg") + data(engel, package = "quantreg") + qm <- quantreg::rq(foodexp ~ income, data = engel) + x <- check_model(qm, verbose = FALSE) + expect_s3_class(x, "check_model") +}) From e6346e3c05fcae8d2a057c1e87165f48a955c882 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 16 Feb 2024 20:44:06 +0100 Subject: [PATCH 3/4] remove non working examples --- R/model_performance.bayesian.R | 8 +------- R/r2_bayes.R | 20 +------------------- man/model_performance.stanreg.Rd | 8 +------- man/r2_bayes.Rd | 20 +------------------- 4 files changed, 4 insertions(+), 52 deletions(-) diff --git a/R/model_performance.bayesian.R b/R/model_performance.bayesian.R index fd796fb60..2ba4e5130 100644 --- a/R/model_performance.bayesian.R +++ b/R/model_performance.bayesian.R @@ -40,7 +40,7 @@ #' #' - **PCP**: percentage of correct predictions, see [performance_pcp()]. #' -#' @examplesIf require("rstanarm") && require("rstantools") && require("BayesFactor") +#' @examplesIf require("rstanarm") && require("rstantools") #' \donttest{ #' model <- suppressWarnings(rstanarm::stan_glm( #' mpg ~ wt + cyl, @@ -59,12 +59,6 @@ #' refresh = 0 #' )) #' model_performance(model) -#' -#' model <- BayesFactor::generalTestBF(carb ~ am + mpg, mtcars) -#' -#' model_performance(model) -#' model_performance(model[3]) -#' model_performance(model, average = TRUE) #' } #' @seealso [r2_bayes] #' @references Gelman, A., Goodrich, B., Gabry, J., and Vehtari, A. (2018). diff --git a/R/r2_bayes.R b/R/r2_bayes.R index 71a44ef43..ec98f754b 100644 --- a/R/r2_bayes.R +++ b/R/r2_bayes.R @@ -30,7 +30,7 @@ #' `r2_posterior()` is the actual workhorse for `r2_bayes()` and #' returns a posterior sample of Bayesian R2 values. #' -#' @examplesIf require("rstanarm") && require("rstantools") && require("BayesFactor") && require("brms") +#' @examplesIf require("rstanarm") && require("rstantools") && require("brms") #' library(performance) #' \donttest{ #' model <- suppressWarnings(rstanarm::stan_glm( @@ -53,24 +53,6 @@ #' r2_bayes(model) #' } #' -#' BFM <- BayesFactor::generalTestBF(mpg ~ qsec + gear, data = mtcars, progress = FALSE) -#' FM <- BayesFactor::lmBF(mpg ~ qsec + gear, data = mtcars) -#' -#' r2_bayes(FM) -#' r2_bayes(BFM[3]) -#' r2_bayes(BFM, average = TRUE) # across all models -#' -#' # with random effects: -#' mtcars$gear <- factor(mtcars$gear) -#' model <- BayesFactor::lmBF( -#' mpg ~ hp + cyl + gear + gear:wt, -#' mtcars, -#' progress = FALSE, -#' whichRandom = c("gear", "gear:wt") -#' ) -#' -#' r2_bayes(model) -#' #' \donttest{ #' model <- suppressWarnings(brms::brm( #' mpg ~ wt + cyl, diff --git a/man/model_performance.stanreg.Rd b/man/model_performance.stanreg.Rd index bbd82bc53..622c5b18a 100644 --- a/man/model_performance.stanreg.Rd +++ b/man/model_performance.stanreg.Rd @@ -60,7 +60,7 @@ values mean better fit. See \code{?loo::waic}. } } \examples{ -\dontshow{if (require("rstanarm") && require("rstantools") && require("BayesFactor")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (require("rstanarm") && require("rstantools")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \donttest{ model <- suppressWarnings(rstanarm::stan_glm( mpg ~ wt + cyl, @@ -79,12 +79,6 @@ model <- suppressWarnings(rstanarm::stan_glmer( refresh = 0 )) model_performance(model) - -model <- BayesFactor::generalTestBF(carb ~ am + mpg, mtcars) - -model_performance(model) -model_performance(model[3]) -model_performance(model, average = TRUE) } \dontshow{\}) # examplesIf} } diff --git a/man/r2_bayes.Rd b/man/r2_bayes.Rd index edc78150e..67200fb7d 100644 --- a/man/r2_bayes.Rd +++ b/man/r2_bayes.Rd @@ -62,7 +62,7 @@ R2 takes both the fixed and random effects into account. returns a posterior sample of Bayesian R2 values. } \examples{ -\dontshow{if (require("rstanarm") && require("rstantools") && require("BayesFactor") && require("brms")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (require("rstanarm") && require("rstantools") && require("brms")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} library(performance) \donttest{ model <- suppressWarnings(rstanarm::stan_glm( @@ -85,24 +85,6 @@ model <- suppressWarnings(rstanarm::stan_lmer( r2_bayes(model) } -BFM <- BayesFactor::generalTestBF(mpg ~ qsec + gear, data = mtcars, progress = FALSE) -FM <- BayesFactor::lmBF(mpg ~ qsec + gear, data = mtcars) - -r2_bayes(FM) -r2_bayes(BFM[3]) -r2_bayes(BFM, average = TRUE) # across all models - -# with random effects: -mtcars$gear <- factor(mtcars$gear) -model <- BayesFactor::lmBF( - mpg ~ hp + cyl + gear + gear:wt, - mtcars, - progress = FALSE, - whichRandom = c("gear", "gear:wt") -) - -r2_bayes(model) - \donttest{ model <- suppressWarnings(brms::brm( mpg ~ wt + cyl, From bc9d2359bf467a8268d821dba61912ce3925aca7 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 16 Feb 2024 20:44:48 +0100 Subject: [PATCH 4/4] remove BF --- vignettes/r2.Rmd | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/vignettes/r2.Rmd b/vignettes/r2.Rmd index 843bd3412..e6ee63356 100644 --- a/vignettes/r2.Rmd +++ b/vignettes/r2.Rmd @@ -29,14 +29,11 @@ knitr::opts_chunk$set( ) options(digits = 2) -pkgs <- c( - "effectsize", "BayesFactor", "lme4", "rstanarm" -) +pkgs <- c("effectsize", "lme4", "rstanarm") successfully_loaded <- sapply(pkgs, requireNamespace, quietly = TRUE) if (all(successfully_loaded)) { library(performance) library(effectsize) - library(BayesFactor) library(lme4) library(rstanarm) } @@ -147,27 +144,6 @@ model <- stan_lmer(Petal.Length ~ Petal.Width + (1 | Species), data = iris, refr r2(model) ``` -Let's look at another regression analysis carried out with `{BayesFactor}` package. - -```{r, eval=successfully_loaded["BayesFactor"] && utils::packageVersion("BayesFactor") >= package_version("0.9.12-4.3")} -library(BayesFactor) -data(puzzles) - -m1 <- anovaBF(extra ~ group + ID, - data = sleep, - whichRandom = "ID", progress = FALSE -) - -r2(m1) - -m2 <- generalTestBF(RT ~ shape * color + ID, - data = puzzles, whichRandom = "ID", - neverExclude = "ID", progress = FALSE -) - -r2(m2) -``` - # Comparing change in R2 using Cohen's *f* Cohen's $f$ (of [ANOVA fame](https://easystats.github.io/effectsize/articles/anovaES.html)) can be used as a measure of effect size in the context of sequential multiple regression (i.e., [**nested models**](https://easystats.github.io/performance/reference/test_performance.html)).