Nothing
#!/usr/bin/env Rscript
# b01model. Estimate three alternative Swissmetro choice-model structures.
#
# The catalog contains one logit and two nested-logit likelihood expressions.
# R defines those symbolic expressions, while native Python Biogeme enumerates
# the catalog, estimates every configuration, and compiles the result tables.
library(rbiogeme)
# prepare_swissmetro_example() is defined in ../swissmetro/example_utils.R.
# It handles data/Python paths and output location; the model is specified in
# this script so it remains readable and self-contained.
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_b01model_model <- function(database) {
# Parameter names, starts, bounds, and utility coding match native b01model.
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)
utilities <- list(
`1` = asc_train + b_time * variable("TRAIN_TT_SCALED") +
b_cost * variable("TRAIN_COST_SCALED"),
`2` = b_time * variable("SM_TT_SCALED") +
b_cost * variable("SM_COST_SCALED"),
`3` = asc_car + b_time * 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")
)
choice <- variable("CHOICE")
logit <- logit_log_probability(utilities, availability, choice)
# A native OneNestForNestedLogit is represented by nested_nest().
mu_existing <- biogeme_beta("mu_existing", start = 1, lower = 1, upper = 10)
existing <- nested_nest(mu_existing, c(1, 3), name = "Existing")
existing_nests <- nested_nests(c(1, 2, 3), list(existing))
nested_existing <- nested_log_probability(
utilities, availability, existing_nests, choice
)
mu_public <- biogeme_beta("mu_public", start = 1, lower = 1, upper = 10)
public <- nested_nest(mu_public, c(1, 2), name = "Public")
public_nests <- nested_nests(c(1, 2, 3), list(public))
nested_public <- nested_log_probability(
utilities, availability, public_nests, choice
)
# The catalog controller is created and resolved by native Biogeme. The
# branch names are preserved exactly for reports and configuration IDs.
model_catalog <- catalog(
"model_catalog",
list(
logit = logit,
`nested existing` = nested_existing,
`nested public` = nested_public
)
)
biogeme_model(
database = database,
formula = model_catalog,
control = biogeme_control(
output_directory = prepared$output,
model_name = "b01model",
generate_html = FALSE,
generate_yaml = FALSE,
save_iterations = FALSE
)
)
}
prepared <- prepare_swissmetro_example(
commandArgs(trailingOnly = TRUE),
default_model = "b01model"
)
# Native biogeme.data.swissmetro.read_data() removes only CHOICE == 0.
database <- swissmetro_data(prepared$data, filter_purpose = FALSE)
model <- build_b01model_model(database)
# force = TRUE makes native Biogeme estimate every configuration afresh.
fit <- estimate_catalog(
model,
model_name = "b01model",
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)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.