tests/testthat/test-user-experience.R

test_that("validate_model performs native specification validation", {
  skip_if_not(rbiogeme_test_configure_python(), "A compatible Python Biogeme environment is not available")

  database <- biogeme_database(
    "validation_demo",
    data.frame(choice = c(1, 2, 1, 2), x = c(1, 2, 3, 4))
  )
  model <- logit_model(
    database,
    choice = "choice",
    utilities = list(`1` = 0, `2` = biogeme_beta("b", start = 0) * variable("x"))
  )

  validation <- validate_model(model)

  expect_s3_class(validation, "biogeme_model_validation")
  expect_true(validation$valid)
  expect_identical(validation$database_name, "validation_demo")
  expect_identical(validation$number_of_rows, 4L)
  expect_identical(validation$number_of_parameters, 1L)
  expect_true(validation$contains_log_likelihood)
  expect_true("log_like" %in% validation$formula_names)
})

test_that("biogeme_check reports a ready native environment", {
  skip_if_not(rbiogeme_test_configure_python(), "A compatible Python Biogeme environment is not available")

  check <- biogeme_check(verbose = FALSE)

  expect_s3_class(check, "biogeme_check")
  expect_true(check$ready)
  expect_true(is.data.frame(check$issues))
  expect_true(all(c("check", "status", "message", "action") %in% names(check$issues)))
  expect_true(all(check$issues$status == "OK"))
})

test_that("biogeme_setup provides the same ready status", {
  skip_if_not(rbiogeme_test_configure_python(), "A compatible Python Biogeme environment is not available")

  setup <- biogeme_setup(verbose = FALSE)

  expect_s3_class(setup, "biogeme_check")
  expect_true(setup$ready)
})

test_that("predict evaluates native probabilities and scenario data", {
  skip_if_not(rbiogeme_test_configure_python(), "A compatible Python Biogeme environment is not available")

  database <- biogeme_database(
    "prediction_demo",
    data.frame(choice = c(1, 2, 1, 2), x = c(1, 2, 3, 4))
  )
  model <- logit_model(
    database,
    choice = "choice",
    utilities = list(`1` = 0, `2` = biogeme_beta("b", start = 0) * variable("x"))
  )
  fit <- structure(
    list(beta_values = c(b = 1), beta_names = "b", model = model),
    class = c("biogeme_fit", "biogeme_result")
  )

  predicted <- predict(fit, newdata = data.frame(x = c(1, 2, 3)))

  expect_true(is.data.frame(predicted))
  expect_identical(names(predicted), c("1", "2"))
  expect_equal(nrow(predicted), 3L)
  expect_equal(rowSums(predicted), rep(1, 3), tolerance = 1e-12)
  expect_true(all(diff(predicted$`2`) > 0))
})

Try the rbiogeme package in your browser

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

rbiogeme documentation built on Sept. 29, 2026, 5:09 p.m.