Skip to content

Commit

Permalink
[BUG-FIX] plot()-method for ROC() and prROC() 🔨
Browse files Browse the repository at this point in the history
* The plots are now correctly displaying lines when `panels = FALSE`
* NEWS.md updated accordingly.
  • Loading branch information
serkor1 committed Dec 8, 2024
1 parent d3c7949 commit 8f31040
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 41 deletions.
44 changes: 44 additions & 0 deletions NEWS.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,50 @@ SLmetrics::accuracy(
)
```

## Bug-fixes

* The `plot.prROC()`- and `plot.ROC()`-functions now adds a line to the plot when `panels = FALSE`. See Issue https://github.com/serkor1/SLmetrics/issues/9.

```{r}
# 1) define actual
# classes
actual <- factor(
sample(letters[1:2], size = 100, replace = TRUE)
)
# 2) define response
# probabilities
response <- runif(100)
# 3) calculate
# ROC and prROC
# 3.1) ROC
roc <- SLmetrics::ROC(
actual,
response
)
# 3.2) prROC
prroc <- SLmetrics::prROC(
actual,
response
)
# 4) plot with panels
# FALSE
par(mfrow = c(1,2))
plot(
roc,
panels = FALSE
)
plot(
prroc,
panels = FALSE
)
```



# Version 0.1-0
Expand Down
80 changes: 67 additions & 13 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

``` r
# 1) define factors
actual <- factor(c("yes", "no"))
actual <- factor(c("no", "yes"))
predicted <- factor(c(NA, "no"))

# 2) accuracy with na.rm = TRUE
Expand All @@ -36,7 +36,7 @@ SLmetrics::accuracy(
)
```

#> [1] 1
#> [1] 0

``` r
# 2) accuracy with na.rm = FALSE
Expand All @@ -49,6 +49,60 @@ SLmetrics::accuracy(

#> [1] NaN

## Bug-fixes

- The `plot.prROC()`- and `plot.ROC()`-functions now adds a line to
the plot when `panels = FALSE`. See Issue
<https://github.com/serkor1/SLmetrics/issues/9>.

<!-- end list -->

``` r
# 1) define actual
# classes
actual <- factor(
sample(letters[1:2], size = 100, replace = TRUE)
)

# 2) define response
# probabilities
response <- runif(100)

# 3) calculate
# ROC and prROC

# 3.1) ROC
roc <- SLmetrics::ROC(
actual,
response
)

# 3.2) prROC
prroc <- SLmetrics::prROC(
actual,
response
)

# 4) plot with panels
# FALSE
par(mfrow = c(1,2))
plot(
roc,
panels = FALSE
)
```

![](NEWS_files/figure-gfm/unnamed-chunk-2-1.png)<!-- -->

``` r
plot(
prroc,
panels = FALSE
)
```

![](NEWS_files/figure-gfm/unnamed-chunk-2-2.png)<!-- -->

# Version 0.1-0

## General
Expand All @@ -71,7 +125,7 @@ print(
)
```

#> [1] b a b b a c b c c a
#> [1] a a a b c a b c c a
#> Levels: a b c

``` r
Expand All @@ -83,7 +137,7 @@ print(
)
```

#> [1] c c a b a b c c a c
#> [1] c c b b a a b b c b
#> Levels: a b c

``` r
Expand All @@ -101,16 +155,16 @@ summary(
#> Confusion Matrix (3 x 3)
#> ================================================================================
#> a b c
#> a 1 0 2
#> b 1 1 2
#> a 1 2 2
#> b 0 2 0
#> c 1 1 1
#> ================================================================================
#> Overall Statistics (micro average)
#> - Accuracy: 0.30
#> - Balanced Accuracy: 0.31
#> - Sensitivity: 0.30
#> - Specificity: 0.65
#> - Precision: 0.30
#> - Accuracy: 0.40
#> - Balanced Accuracy: 0.51
#> - Sensitivity: 0.40
#> - Specificity: 0.70
#> - Precision: 0.40

``` r
# 2) calculate false positive
Expand All @@ -121,7 +175,7 @@ SLmetrics::fpr(
```

#> a b c
#> 0.2857143 0.1666667 0.5714286
#> 0.2000000 0.3750000 0.2857143

### Supervised regression metrics

Expand All @@ -142,4 +196,4 @@ SLmetrics::huberloss(
)
```

#> [1] 0.394088
#> [1] 0.4757045
Binary file added NEWS_files/figure-gfm/unnamed-chunk-2-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added NEWS_files/figure-gfm/unnamed-chunk-2-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 8 additions & 7 deletions R/S3_PrecisionRecallCurve.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ plot.prROC <- function(
# All common options for the
# plot goes her
pformula <- precision ~ recall
groups <-unique(x$label)
groups <- x$label
xlab <- "Recall"
ylab <- "Precision"
main <- "Precision-Recall Curve"
Expand All @@ -141,12 +141,13 @@ plot.prROC <- function(
}

roc_plot(
formula = pformula,
groups = groups,
xlab = xlab,
ylab = ylab,
main = main,
DT = x,
formula = pformula,
groups = groups,
xlab = xlab,
ylab = ylab,
main = main,
DT = x,
add_poly = panels,
...
)

Expand Down
15 changes: 8 additions & 7 deletions R/S3_RecieverOperatorCharacteristics.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ plot.ROC <- function(
# All common options for the
# plot goes her
pformula <- tpr ~ fpr
groups <- unique(x$label)
groups <- x$label
xlab <- "False Positive Rate (FPR)"
ylab <- "True Positive Rate (TPR)"
main <- "Reciever Operator Characteristics"
Expand All @@ -150,12 +150,13 @@ plot.ROC <- function(


roc_plot(
formula = pformula,
groups = groups,
xlab = xlab,
ylab = ylab,
main = main,
DT = x,
formula = pformula,
groups = groups,
xlab = xlab,
ylab = ylab,
main = main,
DT = x,
add_poly = panels,
...
)

Expand Down
34 changes: 20 additions & 14 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ roc_plot <- function(
xlab = NULL,
ylab = NULL,
main = NULL,
add_poly = FALSE,
...) {


Expand All @@ -42,25 +43,30 @@ roc_plot <- function(
lwd = 2,
auto.key = list(
space = "bottom",
columns = length(unique(DT$label))
columns = length(unique(DT$label)),
between.columns = 1
),
xlim = c(0, 1),
ylim = c(0, 1),
panel = function(x, y, ...) {

# 1) vertices for the polygon
poly_x <- c(0, x, 1)
poly_y <- c(0, y, 0)

# 1.1) plot the polygon
lattice::panel.polygon(
poly_x,
poly_y,
col = grDevices::adjustcolor(
col = "lightblue",
alpha.f = 0.4),
border = NA
)
if (add_poly) {

# 1) vertices for the polygon
poly_x <- c(0, x, 1)
poly_y <- c(0, y, 0)

# 1.1) plot the polygon
lattice::panel.polygon(
poly_x,
poly_y,
col = grDevices::adjustcolor(
col = "lightblue",
alpha.f = 0.4),
border = NA
)

}

# 2) xyplot
lattice::panel.xyplot(
Expand Down

0 comments on commit 8f31040

Please sign in to comment.