inst/examples/swissmetro/plot_b24_halton_mixture.R

#!/usr/bin/env Rscript

# b24. Mixture of logit with Halton draws
#
# This example mirrors plot_b24_halton_mixture.py. The random time coefficient
# is normally distributed and integrated with native Monte Carlo integration
# using Halton draws in base 5.

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_b24_halton_mixture_model <- function(database) {
  # These definitions match the native Beta names, starts, and fixed flags.
  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)

  # biogeme_draws() supplies draw metadata while its expression form remains
  # a native Draws node. The bridge does not run an R callback for draws.
  b_time_draws <- biogeme_draws(
    "b_time_rnd",
    draw_type = "NORMAL_HALTON5",
    number_of_draws = 10000,
    seed = 1223
  )
  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))

  # The complete expression is compiled once; native Biogeme performs the
  # integration, optimization, derivatives, and reporting.
  biogeme_model(
    database = database,
    formula = log_probability,
    draws = b_time_draws,
    control = biogeme_control(
    output_directory = prepared$output,
      model_name = "b24_halton_mixture",
      number_of_draws = 10000,
      seed = 1223,
      analytical_hessian_mode = "automatic",
      user_notes = paste0(
        "Example of a mixture of logit models with three alternatives, ",
        "approximated using Monte-Carlo integration with Halton draws."
      ),
      generate_html = FALSE,
      generate_yaml = FALSE,
      save_iterations = FALSE
    )
  )
}

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

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