inst/examples/assisted/plot_b08selected_specification.R

#!/usr/bin/env Rscript

# b08selected_specification. Estimate one model from the complete catalog.
#
# The configuration identifier below is copied from the native Pareto/glossary
# output. BIOGEME.from_configuration() resolves it in native Biogeme; R only
# constructs and compiles the complete expression tree.

library(rbiogeme)

# prepare_swissmetro_example() is defined in ../swissmetro/example_utils.R.
# It handles paths and output setup only. The complete model specification is
# kept in this script so it can be read and run independently.
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_b08everything_model <- function(database) {
  # Native read_data() creates COMMUTERS from PURPOSE == 1.
  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"
  )

  # Parameter names, starting values, 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
  }

  # A shared controller keeps the three travel-time catalogs synchronized.
  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)
  nested_existing <- nested_log_probability(
    utilities,
    availability,
    nested_nests(
      c(1, 2, 3),
      list(nested_nest(mu_existing, c(1, 3), name = "Existing"))
    ),
    choice
  )
  mu_public <- biogeme_beta("mu_public", start = 1, lower = 1, upper = 10)
  nested_public <- nested_log_probability(
    utilities,
    availability,
    nested_nests(
      c(1, 2, 3),
      list(nested_nest(mu_public, c(1, 2), name = "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,
    availability = availability,
    control = biogeme_control(
    output_directory = prepared$output,
      generate_html = FALSE,
      generate_yaml = FALSE,
      save_iterations = FALSE
    )
  )
}

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

# The assisted examples match native read_data(): remove only CHOICE == 0.
database <- swissmetro_data(prepared$data, filter_purpose = FALSE)
model <- build_b08everything_model(database)

# This exact identifier is the native Biogeme configuration selected in the
# Python example. Names and ordering are part of the equivalence contract.
specification_id <- paste0(
  "asc:GA-LUGGAGE;",
  "b_cost_gen_altspec:generic;",
  "b_time:FIRST;",
  "b_time_gen_altspec:generic;",
  "model_catalog:logit;",
  "train_tt_catalog:power"
)
fit <- estimate_configuration(
  model,
  configuration_id = specification_id,
  model_name = "my_favorite_model",
  control = model$control
)

cat("Selected specification: ", specification_id, "\n", sep = "")
print(fit)

# Native get_pandas_estimated_parameters() is represented here as an ordinary
# R data frame after native estimation has returned serialized result fields.
standard_errors <- fit$standard_errors
t_statistics <- fit$t_statistics
p_values <- fit$p_values
if (is.null(standard_errors)) {
  standard_errors <- rep(NA_real_, length(fit$beta_names))
  t_statistics <- standard_errors
  p_values <- standard_errors
}
estimated_parameters <- data.frame(
  Value = unname(fit$beta_values),
  `Std err` = unname(standard_errors),
  `t-test` = unname(t_statistics),
  `p-value` = unname(p_values),
  row.names = fit$beta_names,
  check.names = FALSE
)
print(estimated_parameters)

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.