inst/examples/bayesian_swissmetro/plot_b05_normal_mixture.R

#!/usr/bin/env Rscript

# b05. Bayesian mixture of logit models with a normally distributed time
# coefficient.
#
# This script is self-contained: the symbolic parameter, draw, distributed
# parameter, utilities, availability, and conditional log-likelihood are all
# specified below. Native Python Biogeme performs the Bayesian sampling.

library(rbiogeme)

# prepare_swissmetro_example() is defined in ../swissmetro/example_utils.R.
# It reads --data, --python, and --output, configures native Biogeme, and
# prepares the Swissmetro data without hiding this model specification.
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_b05_normal_mixture_model <- function(database) {
  positive_lower_bound <- 1e-5

  # biogeme_beta() creates a symbolic native Beta. The fifth Python Beta
  # argument (status = 1) is represented by fixed = TRUE in R.
  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, upper = 0)
  b_time <- biogeme_beta("b_time", start = 0, upper = 0)
  b_time_s <- biogeme_beta(
    "b_time_s",
    start = 10,
    lower = positive_lower_bound
  )

  # draw() maps to native Draws. distributed_parameter() maps to native
  # DistributedParameter and keeps b_time_rnd visible in Bayesian output.
  b_time_eps <- draw("b_time_eps", "NORMAL")
  b_time_rnd <- distributed_parameter(
    "b_time_rnd",
    b_time + b_time_s * b_time_eps
  )

  utilities <- list(
    `1` = asc_train + b_time_rnd * variable("TRAIN_TT_SCALED") +
      b_cost * variable("TRAIN_COST_SCALED"),
    `2` = asc_sm + b_time_rnd * variable("SM_TT_SCALED") +
      b_cost * variable("SM_COST_SCALED"),
    `3` = asc_car + b_time_rnd * 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")
  )

  # Bayesian estimation samples b_time_rnd explicitly, so the likelihood is
  # the conditional native logit log-likelihood; no R integration callback is
  # involved.
  biogeme_model(
    database = database,
    formula = logit_log_probability(
      utilities = utilities,
      availability = availability,
      alternative = variable("CHOICE")
    ),
    control = biogeme_control(
    output_directory = prepared$output,
      model_name = "b05_normal_mixture",
      user_notes = paste(
        "Example of Bayesian estimation of a mixture of logit models",
        "with three alternatives"
      ),
      generate_html = TRUE,
      generate_yaml = TRUE,
      generate_netcdf = TRUE
    )
  )
}

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

# Bayesian estimation always starts a fresh native run. Remove only the exact
# outputs for this example so old YAML, NetCDF, HTML, or iteration files cannot
# be reused silently.
unlink(file.path(prepared$output, c(
  "b05_normal_mixture.yaml",
  "b05_normal_mixture.nc",
  "b05_normal_mixture.html",
  "__b05_normal_mixture.iter"
)), force = TRUE)

database <- swissmetro_data(prepared$data)
model <- build_b05_normal_mixture_model(database)
fit <- bayesian_estimate(model, model_name = "b05_normal_mixture", control = model$control)

print(summary(fit))
print(coef(fit))
print(bayesian_stored_variables(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.