tests/testthat/test-metrics.R

actual <- c(1, 1, 1, 1, 1, 1, 0, 0, 0, 0)
predicted <- c(1, 1, 1, 1, 0, 0, 1, 0, 0, 0)

test_that("robust metrics reproduce their classical limits", {
  expect_equal(robMCC(actual, predicted, d = 0), MCC(actual, predicted))
  expect_equal(robKappa(actual, predicted, d = 0), Kappa(actual, predicted))
  expect_equal(
    robFScore(actual, predicted, beta = 1.5, d0 = 0),
    FScore(actual, predicted, beta = 1.5)
  )
  expect_equal(
    robFScore2(actual, predicted, d1 = 2.25, d0 = 0, c = 0),
    FScore(actual, predicted, beta = 1.5)
  )
  expect_equal(
    robFScore2(actual, predicted, d1 = 2.25, d0 = 0.1, c = 0),
    robFScore(actual, predicted, beta = 1.5, d0 = 0.1)
  )
})

test_that("label and confusion-matrix inputs agree", {
  expect_equal(FScore(actual, predicted, beta = 1.5),
               FScore(TP = 4, FN = 2, FP = 1, TN = 3, beta = 1.5))
  expect_equal(MCC(actual, predicted), MCC(TP = 4, FN = 2, FP = 1, TN = 3))
  expect_equal(robFScore(actual, predicted, beta = 1.5, d0 = 0.1),
               robFScore(TP = 4, FN = 2, FP = 1, TN = 3, beta = 1.5, d0 = 0.1))
  expect_equal(robMCC(actual, predicted, d = 0.1),
               robMCC(TP = 4, FN = 2, FP = 1, TN = 3, d = 0.1))
  expect_equal(Kappa(actual, predicted), Kappa(TP = 4, FN = 2, FP = 1, TN = 3))
  expect_equal(robKappa(actual, predicted, d = 0.1),
               robKappa(TP = 4, FN = 2, FP = 1, TN = 3, d = 0.1))
  expect_equal(Kappa(TP = 4, FN = 2, FP = 1, TN = 3), 0.4)
  expect_equal(robKappa(TP = 4, FN = 2, FP = 1, TN = 3, d = 0.1),
               0.4027777777777778)
})

test_that("metric parameter and label validation rejects invalid input", {
  expect_error(MCC(c(0, 2), c(0, 1)), "actual")
  expect_error(FScore(actual, predicted, beta = 0), "beta")
  expect_error(robMCC(actual, predicted, d = -0.1), "d")
  expect_error(robKappa(actual, predicted, d = -0.1), "d")
  expect_error(robFScore2(actual, predicted, d1 = -0.1), "d1")
  expect_error(robFScore2(actual, predicted, d0 = 0, c = 0.1), "d0")
  expect_no_error(robFScore2(actual, predicted, d1 = 0, d0 = 0.1, c = 0))
})

Try the RobustMetrics package in your browser

Any scripts or data that you put into this service are public.

RobustMetrics documentation built on Aug. 21, 2026, 5:17 p.m.