Nothing
#!/usr/bin/env Rscript
# b08. Bayesian multinomial logit with a common Box--Cox time transformation.
#
# The expression tree below is complete. boxcox() maps directly to native
# Biogeme's BoxCox expression, including its behavior near lambda = 0.
library(rbiogeme)
# prepare_swissmetro_example() is defined in ../swissmetro/example_utils.R.
# It reads the data and Python executable options and creates the output path.
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_b08_boxcox_model <- function(database) {
# These starts, bounds, and fixed status values match Bayesian Python b08.
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_time <- biogeme_beta("b_time", start = -1.675, upper = 0)
b_cost <- biogeme_beta("b_cost", start = -1.079, upper = 0)
boxcox_parameter <- biogeme_beta(
"boxcox_parameter",
start = 1,
lower = -2,
upper = 2
)
train_time <- boxcox(variable("TRAIN_TT_SCALED"), boxcox_parameter)
sm_time <- boxcox(variable("SM_TT_SCALED"), boxcox_parameter)
car_time <- boxcox(variable("CAR_TT_SCALED"), boxcox_parameter)
utilities <- list(
`1` = asc_train + b_time * train_time +
b_cost * variable("TRAIN_COST_SCALED"),
`2` = asc_sm + b_time * sm_time +
b_cost * variable("SM_COST_SCALED"),
`3` = asc_car + b_time * car_time +
b_cost * variable("CAR_CO_SCALED")
)
availability <- list(
`1` = variable("TRAIN_AV_SP"),
`2` = variable("SM_AV"),
`3` = variable("CAR_AV_SP")
)
# The Python example uses 10,000 posterior draws and 10,000 warmup draws.
log_probability <- logit_log_probability(
utilities,
availability,
alternative = variable("CHOICE")
)
biogeme_model(
database = database,
formula = log_probability,
control = biogeme_control(
output_directory = prepared$output,
model_name = "b08_boxcox",
bayesian_draws = 10000,
warmup = 10000,
generate_html = TRUE,
generate_yaml = TRUE,
generate_netcdf = TRUE
)
)
}
prepared <- prepare_swissmetro_example(
commandArgs(trailingOnly = TRUE),
default_model = "b08_boxcox"
)
# Start a fresh native run; exact old outputs cannot be silently recycled.
unlink(file.path(prepared$output, c(
"b08_boxcox.yaml",
"b08_boxcox.nc",
"b08_boxcox.html",
"__b08_boxcox.iter"
)), force = TRUE)
database <- swissmetro_data(prepared$data)
model <- build_b08_boxcox_model(database)
fit <- bayesian_estimate(model, model_name = "b08_boxcox", control = model$control)
print(summary(fit))
print(coef(fit))
print(bayesian_stored_variables(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.