inst/examples/swissmetro/plot_b11a_cnl.R

#!/usr/bin/env Rscript

# b11a. Cross-nested logit
#
# This example mirrors plot_b11a_cnl.py. Train is shared between an existing
# modes nest and a public transport nest; Car belongs only to existing modes,
# and Swissmetro belongs only to public transport.

library(rbiogeme)

# prepare_swissmetro_example() is defined in example_utils.R. It parses the
# command line, validates the data/Python paths, configures the bridge, reads
# the data, and creates a fresh output directory. The --data, --python, and
# --output options work from any current working directory.
script_path <- commandArgs(trailingOnly = FALSE)
script_path <- sub("^--file=", "", script_path[startsWith(script_path, "--file=")][[1L]])
source(file.path(dirname(normalizePath(script_path)), "example_utils.R"))

build_b11a_cnl_model <- function(database) {
  # These parameter names, starts, bounds, and fixed ASC match native Python.
  asc_car <- biogeme_beta("asc_car", start = 0)
  asc_train <- biogeme_beta("asc_train", start = 0)
  asc_sm <- biogeme_beta("asc_sm", start = 0, fixed = TRUE)
  b_time_swissmetro <- biogeme_beta("b_time_swissmetro", start = 0)
  b_time_train <- biogeme_beta("b_time_train", start = 0)
  b_time_car <- biogeme_beta("b_time_car", start = 0)
  b_cost <- biogeme_beta("b_cost", start = 0)
  b_headway_swissmetro <- biogeme_beta("b_headway_swissmetro", start = 0)
  b_headway_train <- biogeme_beta("b_headway_train", start = 0)
  ga_train <- biogeme_beta("ga_train", start = 0)
  ga_swissmetro <- biogeme_beta("ga_swissmetro", start = 0)
  existing_nest_parameter <- biogeme_beta(
    "existing_nest_parameter",
    start = 1,
    lower = 1,
    upper = 5
  )
  public_nest_parameter <- biogeme_beta(
    "public_nest_parameter",
    start = 1,
    lower = 1,
    upper = 5
  )
  alpha_existing <- biogeme_beta(
    "alpha_existing",
    start = 0.5,
    lower = 0,
    upper = 1
  )
  alpha_public <- 1 - alpha_existing

  utilities <- list(
    `1` = asc_train + b_time_train * variable("TRAIN_TT_SCALED") +
      b_cost * variable("TRAIN_COST_SCALED") +
      b_headway_train * variable("TRAIN_HE") + ga_train * variable("GA"),
    `2` = asc_sm + b_time_swissmetro * variable("SM_TT_SCALED") +
      b_cost * variable("SM_COST_SCALED") +
      b_headway_swissmetro * variable("SM_HE") +
      ga_swissmetro * variable("GA"),
    `3` = asc_car + b_time_car * variable("CAR_TT_SCALED") +
      b_cost * variable("CAR_CO_SCALED")
  )
  availability <- list(
    `1` = variable("TRAIN_AV_SP"),
    `2` = variable("SM_AV"),
    `3` = variable("CAR_AV_SP")
  )

  # cross_nested_nest() maps every alternative to an allocation expression.
  # The two allocations for Train sum symbolically to one.
  nests <- cross_nested_nests(
    choice_set = c(1L, 2L, 3L),
    nests = list(
      cross_nested_nest(
        nest_parameter = existing_nest_parameter,
        allocation = list(`1` = alpha_existing, `2` = 0, `3` = 1),
        name = "existing"
      ),
      cross_nested_nest(
        nest_parameter = public_nest_parameter,
        allocation = list(`1` = alpha_public, `2` = 1, `3` = 0),
        name = "public"
      )
    )
  )

  # cross_nested_logit_model() compiles native models.logcnl; all allocation
  # and utility expressions are compiled once before estimation.
  cross_nested_logit_model(
    database = database,
    choice = "CHOICE",
    utilities = utilities,
    availability = availability,
    nests = nests,
    control = biogeme_control(
    output_directory = prepared$output,
      model_name = "b11a_cnl",
      generate_html = TRUE,
      generate_yaml = FALSE,
      save_iterations = FALSE
    )
  )
}

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

# Always estimate from the expression tree. Remove only exact b11a artifacts so
# an old YAML or iteration file cannot silently be recycled.
stale_files <- c(
  "b11a_cnl.yaml",
  "__b11a_cnl.iter",
  "b11a_cnl.html"
)
stale_files <- file.path(prepared$output, stale_files)
stale_files <- stale_files[file.exists(stale_files)]
if (length(stale_files) > 0L) unlink(stale_files, force = TRUE)

database <- swissmetro_data(prepared$data)
model <- build_b11a_cnl_model(database)

# estimate() delegates the CNL likelihood, derivatives, optimization, and
# reporting to native Biogeme.
fit <- estimate(
  model,
  model_name = "b11a_cnl",
  control = model$control
)

print(summary(fit))
print(coef(fit))
invisible(fit)

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.