Nothing
#!/usr/bin/env Rscript
# b06. Bayesian mixture of logit models with a symmetric uniform time
# coefficient.
#
# The complete model is specified in this file. R builds only neutral symbolic
# expressions; native Python Biogeme performs the Bayesian estimation.
library(rbiogeme)
# prepare_swissmetro_example() is defined in ../swissmetro/example_utils.R.
# It handles --data, --python, and --output and prepares the input database.
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_b06_unif_mixture_model <- function(database) {
# These names, starts, bounds, and fixed ASC match the native Python file.
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)
b_time <- biogeme_beta("b_time", start = 0)
b_time_s <- biogeme_beta("b_time_s", start = 1)
# UNIFORMSYM is the native symmetric uniform draw type. The wrapper stores
# the resulting individual-level coefficient as b_time_rnd.
b_time_rnd <- distributed_parameter(
"b_time_rnd",
b_time + b_time_s * draw("b_time_eps", "UNIFORMSYM")
)
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")
)
# The Bayesian model uses the conditional logit likelihood because the
# random coefficient is sampled explicitly by native Biogeme.
biogeme_model(
database = database,
formula = logit_log_probability(
utilities = utilities,
availability = availability,
alternative = variable("CHOICE")
),
control = biogeme_control(
output_directory = prepared$output,
model_name = "b06_unif_mixture",
generate_html = TRUE,
generate_yaml = TRUE,
generate_netcdf = TRUE
)
)
}
prepared <- prepare_swissmetro_example(
commandArgs(trailingOnly = TRUE),
default_model = "b06_unif_mixture"
)
# Force a new native Bayesian run for this model and output directory.
unlink(file.path(prepared$output, c(
"b06_unif_mixture.yaml",
"b06_unif_mixture.nc",
"b06_unif_mixture.html",
"__b06_unif_mixture.iter"
)), force = TRUE)
database <- swissmetro_data(prepared$data)
model <- build_b06_unif_mixture_model(database)
fit <- bayesian_estimate(model, model_name = "b06_unif_mixture", 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.