Nothing
#!/usr/bin/env Rscript
# b07. Bayesian discrete mixture of two logit models.
#
# Class 1 has no time coefficient. Class 2 estimates a common time
# coefficient. The class probabilities are normalized symbolically in R and
# the complete mixture log-likelihood is compiled to native Biogeme.
library(rbiogeme)
# prepare_swissmetro_example() is defined in ../swissmetro/example_utils.R.
# It makes this script independent of the current working directory and
# documents the --data, --python, and --output command-line interface.
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_b07_discrete_mixture_model <- function(database) {
# Parameter definitions preserve native names, starts, bounds, and status.
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 = 0)
b_cost <- biogeme_beta("b_cost", start = 0)
prob_class1 <- biogeme_beta(
"prob_class1",
start = 0.5,
lower = 0,
upper = 1
)
prob_class2 <- 1 - prob_class1
availability <- list(
`1` = variable("TRAIN_AV_SP"),
`2` = variable("SM_AV"),
`3` = variable("CAR_AV_SP")
)
choice <- variable("CHOICE")
# Class 1 utilities omit travel time, exactly as in the native example.
utilities_class_1 <- list(
`1` = asc_train + b_cost * variable("TRAIN_COST_SCALED"),
`2` = asc_sm + b_cost * variable("SM_COST_SCALED"),
`3` = asc_car + b_cost * variable("CAR_CO_SCALED")
)
utilities_class_2 <- list(
`1` = asc_train + b_time * variable("TRAIN_TT_SCALED") +
b_cost * variable("TRAIN_COST_SCALED"),
`2` = asc_sm + 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")
)
# logit_probability() is the native probability expression. The weighted
# sum is then logged symbolically; Biogeme evaluates the whole tree.
probability_class_1 <- logit_probability(
utilities_class_1,
availability,
alternative = choice
)
probability_class_2 <- logit_probability(
utilities_class_2,
availability,
alternative = choice
)
log_probability <- log(
prob_class1 * probability_class_1 + prob_class2 * probability_class_2
)
biogeme_model(
database = database,
formula = log_probability,
control = biogeme_control(
output_directory = prepared$output,
model_name = "b07_discrete_mixture",
generate_html = TRUE,
generate_yaml = TRUE,
generate_netcdf = TRUE
)
)
}
prepared <- prepare_swissmetro_example(
commandArgs(trailingOnly = TRUE),
default_model = "b07_discrete_mixture"
)
# Do not allow a previous Bayesian result or iteration file to be reused.
unlink(file.path(prepared$output, c(
"b07_discrete_mixture.yaml",
"b07_discrete_mixture.nc",
"b07_discrete_mixture.html",
"__b07_discrete_mixture.iter"
)), force = TRUE)
database <- swissmetro_data(prepared$data)
model <- build_b07_discrete_mixture_model(database)
fit <- bayesian_estimate(model, model_name = "b07_discrete_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.