Nothing
#!/usr/bin/env Rscript
# b26. Bayesian triangular mixture with panel data.
#
# The random time coefficient and all alternative-specific constants are
# shared across each individual's trajectory. Native Bayesian panel handling
# prepares the individual draws and aggregates the likelihood by ID.
library(rbiogeme)
# prepare_swissmetro_example() is defined in ../swissmetro/example_utils.R.
# It reads --data, --python, and --output and configures the native bridge.
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_b26triangular_panel_mixture_model <- function(database) {
positive_lower_bound <- 1e-5
# Parameter names, starts, and the fixed Swissmetro ASC match native Python.
b_time <- biogeme_beta("b_time", start = 0)
b_time_s <- biogeme_beta("b_time_s", start = 1, lower = positive_lower_bound)
b_time_rnd <- distributed_parameter(
"b_time_rnd",
b_time + b_time_s * draw("b_time_rnd_err_term", "TRIANGULAR")
)
b_cost <- biogeme_beta("b_cost", start = 0)
asc_car <- biogeme_beta("asc_car", start = 0)
asc_car_s <- biogeme_beta("asc_car_s", start = 1)
asc_car_rnd <- distributed_parameter(
"asc_car_rnd",
asc_car + asc_car_s * draw("asc_car_eps", "TRIANGULAR")
)
asc_train <- biogeme_beta("asc_train", start = 0)
asc_train_s <- biogeme_beta("asc_train_s", start = 1)
asc_train_rnd <- distributed_parameter(
"asc_train_rnd",
asc_train + asc_train_s * draw("asc_train_eps", "TRIANGULAR")
)
asc_sm <- biogeme_beta("asc_sm", start = 0, fixed = TRUE)
asc_sm_s <- biogeme_beta("asc_sm_s", start = 1)
asc_sm_rnd <- distributed_parameter(
"asc_sm_rnd",
asc_sm + asc_sm_s * draw("asc_sm_eps", "TRIANGULAR")
)
utilities <- list(
`1` = asc_train_rnd + b_time_rnd * variable("TRAIN_TT_SCALED") +
b_cost * variable("TRAIN_COST_SCALED"),
`2` = asc_sm_rnd + b_time_rnd * variable("SM_TT_SCALED") +
b_cost * variable("SM_COST_SCALED"),
`3` = asc_car_rnd + 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 formula is per observation. Native Bayesian panel estimation performs
# the individual-draw preparation and panel aggregation after compilation.
biogeme_model(
database = database,
formula = logit_log_probability(
utilities = utilities,
availability = availability,
alternative = variable("CHOICE")
),
control = biogeme_control(
output_directory = prepared$output,
model_name = "b26triangular_panel",
generate_html = TRUE,
generate_yaml = TRUE,
generate_netcdf = TRUE
)
)
}
prepared <- prepare_swissmetro_example(
commandArgs(trailingOnly = TRUE),
default_model = "b26triangular_panel"
)
unlink(file.path(prepared$output, c(
"b26triangular_panel.yaml",
"b26triangular_panel.nc",
"b26triangular_panel.html",
"__b26triangular_panel.iter"
)), force = TRUE)
# panel = TRUE declares ID and validates that each person's observations are
# contiguous before native Bayesian panel estimation starts.
database <- swissmetro_data(prepared$data, panel = TRUE)
model <- build_b26triangular_panel_mixture_model(database)
fit <- bayesian_estimate(
model,
model_name = "b26triangular_panel",
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.