inst/examples/assisted/plot_b03alt_spec.R

#!/usr/bin/env Rscript

# b03alt_spec. Generic versus alternative-specific time and cost coefficients.
#
# generic_alt_specific_catalogs() creates the same native two-branch catalogs
# as Biogeme's helper. R keeps the utilities and likelihood visible; native
# Biogeme resolves the four combinations, estimates them, and creates reports.

library(rbiogeme)

# prepare_swissmetro_example() is defined in ../swissmetro/example_utils.R.
# It handles input paths and output setup only; all parameters and utilities
# for this example are specified in this file.
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_b03alt_spec_model <- function(database) {
  # Parameter names and starts are unchanged from native b03alt_spec.
  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)

  # Each returned list contains one catalog for Train, Swissmetro, and Car.
  # The generic/altspec controller is shared across alternatives for each
  # coefficient, but the time and cost controllers are independent.
  time_catalogs <- generic_alt_specific_catalogs(
    generic_name = "b_time",
    beta_parameters = list(b_time),
    alternatives = c("train", "swissmetro", "car")
  )[[1L]]
  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_train +
      time_catalogs$train * variable("TRAIN_TT_SCALED") +
      cost_catalogs$train * variable("TRAIN_COST_SCALED"),
    `2` = time_catalogs$swissmetro * variable("SM_TT_SCALED") +
      cost_catalogs$swissmetro * variable("SM_COST_SCALED"),
    `3` = asc_car +
      time_catalogs$car * variable("CAR_TT_SCALED") +
      cost_catalogs$car * variable("CAR_CO_SCALED")
  )
  log_probability <- logit_log_probability(
    utilities = utilities,
    availability = list(
      `1` = variable("TRAIN_AV_SP"),
      `2` = variable("SM_AV"),
      `3` = variable("CAR_AV_SP")
    ),
    alternative = variable("CHOICE")
  )
  biogeme_model(
    database = database,
    formula = log_probability,
    control = biogeme_control(
    output_directory = prepared$output,
      model_name = "b01alt_spec",
      generate_html = FALSE,
      generate_yaml = FALSE,
      save_iterations = FALSE
    )
  )
}

prepared <- prepare_swissmetro_example(
  commandArgs(trailingOnly = TRUE),
  default_model = "b01alt_spec"
)
database <- swissmetro_data(prepared$data, filter_purpose = FALSE)
model <- build_b03alt_spec_model(database)

fit <- estimate_catalog(
  model,
  model_name = "b01alt_spec",
  control = model$control,
  force = TRUE
)

cat("A total of ", length(fit$results), " models have been estimated.\n", sep = "")
for (configuration in names(fit$results)) {
  result <- fit$results[[configuration]]
  cat(
    configuration,
    ": LL=",
    formatC(result$final_log_likelihood, digits = 2, format = "f"),
    " K=",
    length(result$beta_names),
    "\n",
    sep = ""
  )
}
print(fit$summary)
for (name in names(fit$description)) {
  if (!identical(name, unname(fit$description[[name]]))) {
    cat(name, "\t", fit$description[[name]], "\n", sep = "")
  }
}
cat("Non dominated models:\n")
for (configuration in fit$non_dominated) cat(configuration, "\n", sep = "")
print(fit$non_dominated_summary)
cat(fit$latex, "\n")

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.