inst/examples/assisted/plot_b00logit.R

#!/usr/bin/env Rscript

# b00logit. Baseline Swissmetro logit model.
#
# This is the first estimation example in the native assisted folder. The
# complete utility and likelihood specification is deliberately visible here.
# rbiogeme creates symbolic expressions in R, then compiles the complete tree
# once; native Python Biogeme performs the likelihood, derivatives, and
# optimization.

library(rbiogeme)

# prepare_swissmetro_example() is defined in ../swissmetro/example_utils.R.
# It parses --data, --python, and --output, configures the Python bridge, and
# creates a dedicated output directory. The model specification is not hidden
# in that helper.
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_b00logit_model <- function(database) {
  # These names and the four free parameters match the native b00logit.py
  # example. There is no ASC for Swissmetro, so it is the reference utility.
  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)

  # Arithmetic on Biogeme expressions constructs a symbolic utility tree.
  utilities <- list(
    `1` = asc_train + b_time * variable("TRAIN_TT_SCALED") +
      b_cost * variable("TRAIN_COST_SCALED"),
    `2` = b_time * variable("SM_TT_SCALED") +
      b_cost * variable("SM_COST_SCALED"),
    `3` = asc_car + b_time * variable("CAR_TT_SCALED") +
      b_cost * variable("CAR_CO_SCALED")
  )

  # The names 1, 2, and 3 are the native alternative codes. Availability is
  # also symbolic and is evaluated by native Biogeme for each observation.
  logit_model(
    database = database,
    choice = "CHOICE",
    utilities = utilities,
    availability = list(
      `1` = variable("TRAIN_AV_SP"),
      `2` = variable("SM_AV"),
      `3` = variable("CAR_AV_SP")
    )
  )
}

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

# Native assisted read_data() removes only CHOICE == 0. The explicit
# filter_purpose = FALSE documents that distinction from the other Swissmetro
# examples, whose shared helper also filters PURPOSE to 1 and 3.
database <- swissmetro_data(prepared$data, filter_purpose = FALSE)
model <- build_b00logit_model(database)

# Disable reports and iteration files for a clean, repeatable example run.
# estimate() always starts native Biogeme estimation; no previous YAML file is
# loaded or recycled.
fit <- estimate(
  model,
  model_name = "b00logit",
  control = biogeme_control(
    output_directory = prepared$output,
    generate_html = FALSE,
    generate_yaml = FALSE,
    save_iterations = FALSE
  )
)

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.