Skip to contents
library(risk3r)

N <- 10000

predicted <- runif(N)

actual <- rbinom(N, size = 1, prob = predicted)

metrics(actual, predicted)
#>  Creating woe binning ...
#> # A tibble: 1 × 4
#>      ks   auc    iv  gini
#>   <dbl> <dbl> <dbl> <dbl>
#> 1 0.509 0.835  1.77 0.671

Remove NA if are present.

predicted2 <- predicted
predicted2[sample(c(TRUE, FALSE), size = N, prob = c(1, 99), replace = TRUE)] <- NA

metrics(actual, predicted2)
#> 92 of 10000 'predicted' values are NAs, they will be ignorated
#>  Creating woe binning ...
#> # A tibble: 1 × 4
#>      ks   auc    iv  gini
#>   <dbl> <dbl> <dbl> <dbl>
#> 1 0.509 0.828  1.75 0.655

Using a glm model

daux <- data.frame(actual = actual, predicted = predicted)

m <- glm(actual ~ predicted, family = binomial, data = daux)

model_metrics(m)
#>  Creating woe binning ...
#> # A tibble: 1 × 4
#>      ks   auc    iv  gini
#>   <dbl> <dbl> <dbl> <dbl>
#> 1 0.509 0.835  1.76 0.671

Works with new data.

new_daux <- head(daux, 1000)

model_metrics(m, newdata = new_daux)
#>  Creating woe binning ...
#> # A tibble: 1 × 4
#>      ks   auc    iv  gini
#>   <dbl> <dbl> <dbl> <dbl>
#> 1 0.490 0.820  1.76 0.641