Nothing
#!/usr/bin/env Rscript
# b18a. Ordinal logit model
#
# This example mirrors plot_b18a_ordinal_logit.py. It is intentionally an
# ordinal-logit illustration: the Swissmetro choice codes are not intrinsically
# ordered, but the native example treats 1 -> 2 -> 3 as ordered categories.
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. The --data, --python, and
# --output options work from any current working directory.
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_b18a_ordinal_logit_model <- function(database) {
# Parameter names, starts, and bounds match the native Python example.
b_time <- biogeme_beta("b_time", start = 0)
b_cost <- biogeme_beta("b_cost", start = 0)
tau1 <- biogeme_beta("tau1", start = -1, upper = 0)
delta2 <- biogeme_beta("delta2", start = 2, lower = 0)
# The second cutpoint is defined symbolically. OrderedLogLogit receives the
# latent index, cutpoints, observed response, category labels, and optional
# neutral labels; all of these remain native Biogeme expression nodes.
tau2 <- tau1 + delta2
utility <- b_time * variable("TRAIN_TT_SCALED") +
b_cost * variable("TRAIN_COST_SCALED")
log_probability <- ordered_logit_log_probability(
eta = utility,
cutpoints = list(tau1, tau2),
alternative = variable("CHOICE"),
categories = c(1, 2, 3),
neutral_labels = numeric()
)
# biogeme_model() is the generic formula interface. The bridge compiles the
# complete ordered-logit graph once, before native estimation begins.
biogeme_model(
database = database,
formula = log_probability,
control = biogeme_control(
output_directory = prepared$output,
model_name = "b18a_ordinal_logit",
generate_html = TRUE,
generate_yaml = FALSE,
save_iterations = FALSE
)
)
}
prepared <- prepare_swissmetro_example(
commandArgs(trailingOnly = TRUE),
default_model = "b18a_ordinal_logit"
)
# Always estimate afresh. Remove only exact b18a artifacts so an old YAML or
# iteration file cannot silently supply the estimates.
stale_files <- c(
"b18a_ordinal_logit.yaml",
"__b18a_ordinal_logit.iter",
"b18a_ordinal_logit.html"
)
stale_files <- file.path(prepared$output, stale_files)
stale_files <- stale_files[file.exists(stale_files)]
if (length(stale_files) > 0L) unlink(stale_files, force = TRUE)
database <- swissmetro_data(prepared$data)
model <- build_b18a_ordinal_logit_model(database)
# Native Biogeme performs the ordered probabilities, derivatives, optimization,
# and reporting; no R callback is used in those evaluations.
fit <- estimate(
model,
model_name = "b18a_ordinal_logit",
control = model$control
)
print(summary(fit))
print(coef(fit))
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.