Skip to content

Commit 46a0dae

Browse files
[docs] Details to performance_roc() (#777)
Co-authored-by: Daniel <[email protected]>
1 parent 014ee4a commit 46a0dae

8 files changed

+57
-4
lines changed

DESCRIPTION

+1-1
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.12.4.15
4+
Version: 0.12.4.16
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

NAMESPACE

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ S3method(as.data.frame,performance_score)
2525
S3method(as.data.frame,r2_bayes)
2626
S3method(as.data.frame,r2_loo)
2727
S3method(as.data.frame,r2_nakagawa)
28-
S3method(as.numeric,check_outliers)
28+
S3method(as.double,check_outliers)
29+
S3method(as.double,performance_roc)
2930
S3method(check_autocorrelation,default)
3031
S3method(check_collinearity,BFBayesFactor)
3132
S3method(check_collinearity,MixMod)

NEWS.md

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
* `r2()` and `r2_mcfadden()` now support beta-binomial (non-mixed) models from
2020
package *glmmTMB*.
2121

22+
* An `as.numeric()` resp. `as.double()` method for objects of class
23+
`performance_roc` was added.
24+
25+
* Improved documentation for `performance_roc()`.
26+
2227
## Bug fixes
2328

2429
* `check_outliers()` did not warn that no numeric variables were found when only

R/check_outliers.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ as.data.frame.check_outliers <- function(x, ...) {
599599
}
600600

601601
#' @export
602-
as.numeric.check_outliers <- function(x, ...) {
602+
as.double.check_outliers <- function(x, ...) {
603603
attributes(x)$data$Outlier
604604
}
605605

R/performance_roc.R

+17-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#'
4747
#' model <- glm(y ~ Sepal.Length + Sepal.Width, data = train_data, family = "binomial")
4848
#' as.data.frame(performance_roc(model, new_data = test_data))
49+
#' as.numeric(performance_roc(model))
4950
#'
5051
#' roc <- performance_roc(model, new_data = test_data)
5152
#' area_under_curve(roc$Specificity, roc$Sensitivity)
@@ -122,6 +123,21 @@ print.performance_roc <- function(x, ...) {
122123
}
123124

124125

126+
#' @export
127+
as.double.performance_roc <- function(x, ...) {
128+
if (length(unique(x$Model)) == 1) {
129+
auc <- bayestestR::area_under_curve(x$Specificity, x$Sensitivity)
130+
} else {
131+
dat <- split(x, f = x$Model)
132+
133+
auc <- numeric(length(dat))
134+
for (i in seq_along(dat)) {
135+
auc[i] <- bayestestR::area_under_curve(dat[[i]]$Specificity, dat[[i]]$Sensitivity)
136+
}
137+
}
138+
auc
139+
}
140+
125141

126142
# utilities ---------------------------
127143

@@ -181,5 +197,5 @@ print.performance_roc <- function(x, ...) {
181197
if (inherits(x, "model_fit")) {
182198
x <- x$fit
183199
}
184-
inherits(x, c("glm", "glmerMod", "logitor", "logitmfx", "probitmfx"))
200+
inherits(x, c("glm", "glmerMod", "logitor", "logitmfx", "probitmfx", "glmmTMB"))
185201
}

man/performance_roc.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-check_outliers.R

+7
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,10 @@ test_that("check_outliers with DHARMa", {
406406
)
407407
)
408408
})
409+
410+
411+
test_that("check_outliers with DHARMa", {
412+
data(mtcars)
413+
out <- check_outliers(mtcars$mpg, method = "zscore", threshold = 2)
414+
expect_equal(which(as.numeric(out) == 1), c(18, 20))
415+
})

tests/testthat/test-performance_roc.R

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
skip_if_not_installed("bayestestR")
2+
13
test_that("performance_roc", {
24
skip_if_not_installed("lme4")
35
m <- lme4::glmer(vs ~ mpg + (1 | gear), family = "binomial", data = mtcars)
@@ -14,6 +16,7 @@ test_that("performance_roc", {
1416
)
1517
})
1618

19+
1720
test_that("performance_roc", {
1821
set.seed(123)
1922
d <- iris[sample(1:nrow(iris), size = 50), ]
@@ -40,3 +43,23 @@ test_that("performance_roc", {
4043
tolerance = 1e-3
4144
)
4245
})
46+
47+
48+
test_that("performance_roc, as.numeric", {
49+
data(iris)
50+
set.seed(123)
51+
iris$y <- rbinom(nrow(iris), size = 1, .3)
52+
folds <- sample(nrow(iris), size = nrow(iris) / 8, replace = FALSE)
53+
test_data <- iris[folds, ]
54+
train_data <- iris[-folds, ]
55+
56+
model <- glm(y ~ Sepal.Length + Sepal.Width, data = train_data, family = "binomial")
57+
roc <- performance_roc(model)
58+
out <- as.numeric(roc)
59+
expect_equal(
60+
out,
61+
bayestestR::area_under_curve(roc$Specificity, roc$Sensitivity),
62+
tolerance = 1e-4,
63+
ignore_attr = TRUE
64+
)
65+
})

0 commit comments

Comments
 (0)