inst/examples/assisted/plot_b06everything.R

#!/usr/bin/env Rscript

# b06everything. Demonstrate the native limit on exhaustive catalog search.
#
# This is the complete specification from everything_spec.py. It combines 3
# model structures, 3 travel-time forms, 2 cost structures, 2 time structures,
# 4 constant segmentations, and 3 time segmentations: 432 specifications.
# Native Biogeme deliberately raises a ValueOutOfRange error when an exhaustive
# estimate_catalog() is requested above its configured limit.

library(rbiogeme)

# prepare_swissmetro_example() is defined in ../swissmetro/example_utils.R.
# It handles paths and output setup only. The complete expression tree is
# written below so this example remains self-contained for R users.
script_path <- commandArgs(trailingOnly = FALSE)
script_path <- sub("^--file=", "", script_path[startsWith(script_path, "--file=")][[1L]])
source(file.path(dirname(normalizePath(script_path)), "..", "swissmetro", "example_utils.R"))

build_b06everything_model <- function(database) {
  # Native read_data() creates COMMUTERS from PURPOSE == 1. Defining it as a
  # Biogeme expression keeps the transformation in the native database.
  database <- biogeme_database_define_variable(
    database,
    "COMMUTERS",
    variable("PURPOSE") == 1
  )
  segmentation_ga <- biogeme_database_segmentation(
    database,
    "GA",
    c(`0` = "noGA", `1` = "GA"),
    reference = "noGA"
  )
  segmentation_luggage <- biogeme_database_segmentation(
    database,
    "LUGGAGE",
    c(`0` = "no_lugg", `1` = "one_lugg", `3` = "several_lugg"),
    reference = "no_lugg"
  )
  segmentation_first <- biogeme_database_segmentation(
    database,
    "FIRST",
    c(`0` = "2nd_class", `1` = "1st_class"),
    reference = "2nd_class"
  )
  segmentation_purpose <- biogeme_database_segmentation(
    database,
    "COMMUTERS",
    c(`0` = "non_commuters", `1` = "commuters"),
    reference = "non_commuters"
  )

  # These names, starts, and bounds match everything_spec.py.
  asc_car <- biogeme_beta("asc_car", start = 0)
  asc_train <- biogeme_beta("asc_train", start = 0)
  b_time <- biogeme_beta("b_time", start = 0)
  b_cost <- biogeme_beta("b_cost", start = 0)
  lambda_travel_time <- biogeme_beta(
    "lambda_travel_time",
    start = 1,
    lower = -10,
    upper = 10
  )
  square_tt_coef <- biogeme_beta("square_tt_coef", start = 0)
  cube_tt_coef <- biogeme_beta("cube_tt_coef", start = 0)

  power_series <- function(the_variable) {
    the_variable + square_tt_coef * the_variable^2 +
      cube_tt_coef * the_variable * the_variable^3
  }
  time_controller <- catalog_controller(
    "train_tt_catalog",
    c("linear", "boxcox", "power")
  )
  train_tt_catalog <- catalog(
    "train_tt_catalog",
    list(
      linear = variable("TRAIN_TT_SCALED"),
      boxcox = boxcox(variable("TRAIN_TT_SCALED"), lambda_travel_time),
      power = power_series(variable("TRAIN_TT_SCALED"))
    ),
    controller = time_controller
  )
  sm_tt_catalog <- catalog(
    "sm_tt_catalog",
    list(
      linear = variable("SM_TT_SCALED"),
      boxcox = boxcox(variable("SM_TT_SCALED"), lambda_travel_time),
      power = power_series(variable("SM_TT_SCALED"))
    ),
    controller = time_controller
  )
  car_tt_catalog <- catalog(
    "car_tt_catalog",
    list(
      linear = variable("CAR_TT_SCALED"),
      boxcox = boxcox(variable("CAR_TT_SCALED"), lambda_travel_time),
      power = power_series(variable("CAR_TT_SCALED"))
    ),
    controller = time_controller
  )

  asc_catalogs <- segmentation_catalogs(
    generic_name = "asc",
    beta_parameters = list(asc_train, asc_car),
    potential_segmentations = list(segmentation_ga, segmentation_luggage),
    maximum_number = 2
  )
  b_time_catalogs <- generic_alt_specific_catalogs(
    generic_name = "b_time",
    beta_parameters = list(b_time),
    alternatives = c("train", "swissmetro", "car"),
    potential_segmentations = list(segmentation_first, segmentation_purpose),
    maximum_number = 1
  )[[1L]]
  b_cost_catalogs <- generic_alt_specific_catalogs(
    generic_name = "b_cost",
    beta_parameters = list(b_cost),
    alternatives = c("train", "swissmetro", "car")
  )[[1L]]

  utilities <- list(
    `1` = asc_catalogs[[1L]] +
      b_time_catalogs$train * train_tt_catalog +
      b_cost_catalogs$train * variable("TRAIN_COST_SCALED"),
    `2` = b_time_catalogs$swissmetro * sm_tt_catalog +
      b_cost_catalogs$swissmetro * variable("SM_COST_SCALED"),
    `3` = asc_catalogs[[2L]] +
      b_time_catalogs$car * car_tt_catalog +
      b_cost_catalogs$car * variable("CAR_CO_SCALED")
  )
  availability <- list(
    `1` = variable("TRAIN_AV_SP"),
    `2` = variable("SM_AV"),
    `3` = variable("CAR_AV_SP")
  )
  choice <- variable("CHOICE")
  logit <- logit_log_probability(utilities, availability, choice)

  mu_existing <- biogeme_beta("mu_existing", start = 1, lower = 1, upper = 10)
  existing <- nested_nest(mu_existing, c(1, 3), name = "Existing")
  nested_existing <- nested_log_probability(
    utilities,
    availability,
    nested_nests(c(1, 2, 3), list(existing)),
    choice
  )
  mu_public <- biogeme_beta("mu_public", start = 1, lower = 1, upper = 10)
  public <- nested_nest(mu_public, c(1, 2), name = "Public")
  nested_public <- nested_log_probability(
    utilities,
    availability,
    nested_nests(c(1, 2, 3), list(public)),
    choice
  )
  model_catalog <- catalog(
    "model_catalog",
    list(
      logit = logit,
      `nested existing` = nested_existing,
      `nested public` = nested_public
    )
  )
  biogeme_model(
    database = database,
    formula = model_catalog,
    control = biogeme_control(
    output_directory = prepared$output,
      generate_html = FALSE,
      generate_yaml = FALSE
    )
  )
}

prepared <- prepare_swissmetro_example(
  commandArgs(trailingOnly = TRUE),
  default_model = "b06everything"
)

# Native read_data() removes only CHOICE == 0 for these assisted examples.
database <- swissmetro_data(prepared$data, filter_purpose = FALSE)
model <- build_b06everything_model(database)
cat("Native catalog count: ", count_number_of_specifications(model), "\n", sep = "")

# Native Biogeme rejects exhaustive estimation above its configured limit. The
# error is displayed, matching the try/except in plot_b06everything.py.
tryCatch(
  estimate_catalog(
    model,
    model_name = "b06everything",
    control = model$control,
    force = TRUE
  ),
  error = function(error) {
    cat(conditionMessage(error), "\n", sep = "")
  }
)

invisible(model)

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.