tests/testthat/test-get_accuracy.R

# Mock data for testing
mock_out_acc <- data.frame(
  alpha_1 = c(NA, NA, "2"),
  alpha_2 = c("1", "1", "1"),
  alpha_3 = c("1", "2", "2")
)
class(mock_out_acc) <- c("outbreaker_chains", "data.frame")

true_tree_acc <- data.frame(
  from = c(NA, "1", "1"),
  to = c("1", "2", "3"),
  stringsAsFactors = FALSE
)

test_that("get_accuracy calculates correctly", {
  expected_accuracies <- c(3/3, 2/3, 1/3)

  acc <- get_accuracy(mock_out_acc, true_tree_acc)
  expect_equal(acc, expected_accuracies)
  expect_length(acc, nrow(mock_out_acc))
})

test_that("get_accuracy handles fully incorrect tree", {
  mock_out_incorrect <- data.frame(alpha_1 = "2", alpha_2 = "1")
  class(mock_out_incorrect) <- c("outbreaker_chains", "data.frame")
  true_tree_simple <- data.frame(from = c(NA, "1"), to = c("1", "2"), stringsAsFactors = FALSE)
  # Posterior tree 1: 2->1, 1->2. True tree: NA->1, 1->2. Correct: 1 (1->2)
  acc <- get_accuracy(mock_out_incorrect, true_tree_simple)
  expect_equal(acc, c(1/2))
})

Try the o2ools package in your browser

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

o2ools documentation built on June 8, 2025, 10:18 a.m.