inst/examples/swissmetro/plot_b25_triangular_mixture.R

#!/usr/bin/env Rscript

# b25. Triangular mixture of logit
#
# This example mirrors plot_b25_triangular_mixture.py. The random time
# coefficient uses a user-defined triangular distribution. The distribution
# is identified by the bridge-owned TRIANGULAR generator, so no R callback is
# evaluated during Biogeme integration or estimation.

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. It does not define the model.
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_b25_triangular_mixture_model <- function(database) {
  # Parameter names, starts, and fixed flags match the native example.
  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_cost <- biogeme_beta("b_cost", start = 0)
  b_time <- biogeme_beta("b_time", start = 0)
  b_time_s <- biogeme_beta("b_time_s", start = 1)

  # The bridge maps generator = "TRIANGULAR" to native
  # np.random.triangular(-1, 0, 1, (sample_size, number_of_draws)).
  b_time_draws <- biogeme_draws(
    "b_time_rnd",
    draw_type = "TRIANGULAR",
    number_of_draws = 10000,
    seed = 1223,
    generator = "TRIANGULAR"
  )
  b_time_rnd <- b_time + b_time_s * b_time_draws

  v_train <- asc_train + b_time_rnd * variable("TRAIN_TT_SCALED") +
    b_cost * variable("TRAIN_COST_SCALED")
  v_swissmetro <- asc_sm + b_time_rnd * variable("SM_TT_SCALED") +
    b_cost * variable("SM_COST_SCALED")
  v_car <- asc_car + b_time_rnd * variable("CAR_TT_SCALED") +
    b_cost * variable("CAR_CO_SCALED")
  conditional_probability <- logit_probability(
    utilities = list(`1` = v_train, `2` = v_swissmetro, `3` = v_car),
    availability = list(
      `1` = variable("TRAIN_AV_SP"),
      `2` = variable("SM_AV"),
      `3` = variable("CAR_AV_SP")
    ),
    alternative = variable("CHOICE")
  )
  log_probability <- log(monte_carlo(conditional_probability))

  biogeme_model(
    database = database,
    formula = log_probability,
    draws = b_time_draws,
    control = biogeme_control(
    output_directory = prepared$output,
      model_name = "b25_triangular_mixture",
      number_of_draws = 10000,
      seed = 1223,
      analytical_hessian_mode = "automatic",
      generate_html = FALSE,
      generate_yaml = FALSE,
      save_iterations = FALSE
    )
  )
}

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

database <- swissmetro_data(prepared$data)
model <- build_b25_triangular_mixture_model(database)
fit <- estimate(
  model,
  model_name = "b25_triangular_mixture",
  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.