Nothing
native_swissmetro_b11c <- function(data) {
expressions <- reticulate::import("biogeme.expressions", convert = FALSE)
database_module <- reticulate::import("biogeme.database", convert = FALSE)
biogeme_module <- reticulate::import("biogeme.biogeme", convert = FALSE)
models <- reticulate::import("biogeme.models", convert = FALSE)
nests_module <- reticulate::import("biogeme.nests", convert = FALSE)
bridge <- rbiogeme:::biogeme_bridge()
database <- database_module$Database(
"swissmetro_native_b11c",
reticulate::r_to_py(data)
)
variable <- expressions$Variable
purpose <- variable("PURPOSE")
choice <- variable("CHOICE")
database$remove(((purpose != 1) * (purpose != 3) + (choice == 0)) > 0)
ga <- variable("GA")
sp <- variable("SP")
sm_cost <- database$define_variable("SM_COST", variable("SM_CO") * (ga == 0))
train_cost <- database$define_variable("TRAIN_COST", variable("TRAIN_CO") * (ga == 0))
car_av_sp <- database$define_variable("CAR_AV_SP", variable("CAR_AV") * (sp != 0))
train_av_sp <- database$define_variable("TRAIN_AV_SP", variable("TRAIN_AV") * (sp != 0))
train_tt_scaled <- database$define_variable("TRAIN_TT_SCALED", variable("TRAIN_TT") / 100)
train_cost_scaled <- database$define_variable("TRAIN_COST_SCALED", train_cost / 100)
sm_tt_scaled <- database$define_variable("SM_TT_SCALED", variable("SM_TT") / 100)
sm_cost_scaled <- database$define_variable("SM_COST_SCALED", sm_cost / 100)
car_tt_scaled <- database$define_variable("CAR_TT_SCALED", variable("CAR_TT") / 100)
car_co_scaled <- database$define_variable("CAR_CO_SCALED", variable("CAR_CO") / 100)
beta <- expressions$Beta
asc_car <- beta("asc_car", 0, NULL, NULL, 0)
asc_train <- beta("asc_train", 0, NULL, NULL, 0)
asc_sm <- beta("asc_sm", 0, NULL, NULL, 1)
b_time <- beta("b_time", 0, NULL, NULL, 0)
b_cost <- beta("b_cost", 0, NULL, NULL, 0)
existing_nest_parameter <- beta("existing_nest_parameter", 1, 1, 5, 0)
public_nest_parameter <- beta("public_nest_parameter", 1, 1, 5, 0)
alpha_existing <- beta("alpha_existing", 0.5, 0, 1, 0)
alpha_public <- 1 - alpha_existing
utilities <- reticulate::dict(
`1` = asc_train + b_time * train_tt_scaled + b_cost * train_cost_scaled,
`2` = asc_sm + b_time * sm_tt_scaled + b_cost * sm_cost_scaled,
`3` = asc_car + b_time * car_tt_scaled + b_cost * car_co_scaled
)
availability <- reticulate::dict(
`1` = train_av_sp,
`2` = variable("SM_AV"),
`3` = car_av_sp
)
nest_existing <- nests_module$OneNestForCrossNestedLogit(
nest_param = existing_nest_parameter,
dict_of_alpha = reticulate::dict(`1` = alpha_existing, `3` = 1.0),
name = "existing"
)
nest_public <- nests_module$OneNestForCrossNestedLogit(
nest_param = public_nest_parameter,
dict_of_alpha = reticulate::dict(`1` = alpha_public, `2` = 1.0),
name = "public"
)
nests <- nests_module$NestsForCrossNestedLogit(
choice_set = reticulate::r_to_py(as.integer(c(1L, 2L, 3L))),
tuple_of_nests = reticulate::tuple(nest_existing, nest_public)
)
log_probability <- models$logcnl(utilities, availability, nests, choice)
biogeme <- biogeme_module$BIOGEME(
database,
log_probability,
generate_html = FALSE,
generate_yaml = FALSE,
save_iterations = FALSE
)
biogeme$model_name <- "b11c_cnl_sparse"
results <- biogeme$estimate()
list(
results = reticulate::py_to_r(bridge$extract_estimation_results(results)),
number_of_rows = nrow(reticulate::py_to_r(database$dataframe))
)
}
test_that("b11c sparse CNL matches native Biogeme and dense semantics", {
skip_if_not(
identical(Sys.getenv("RBIOGEME_RUN_INTEGRATION"), "1"),
"Set RBIOGEME_RUN_INTEGRATION=1 to run full Swissmetro equivalence tests"
)
skip_if_not(
rbiogeme_test_configure_python(),
"Set RBIOGEME_PYTHON to a compatible native Biogeme interpreter"
)
data_path <- rbiogeme_test_swissmetro_path()
skip_if(
!nzchar(data_path),
"Set RBIOGEME_SWISSMETRO_DATA to the Swissmetro .dat file"
)
data <- read.delim(data_path, check.names = FALSE, stringsAsFactors = FALSE)
database <- swissmetro_data(data)
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)
existing_nest_parameter <- biogeme_beta("existing_nest_parameter", start = 1, lower = 1, upper = 5)
public_nest_parameter <- biogeme_beta("public_nest_parameter", start = 1, lower = 1, upper = 5)
alpha_existing <- biogeme_beta("alpha_existing", start = 0.5, lower = 0, upper = 1)
alpha_public <- 1 - alpha_existing
utilities <- 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")
)
availability <- list(
`1` = variable("TRAIN_AV_SP"),
`2` = variable("SM_AV"),
`3` = variable("CAR_AV_SP")
)
sparse_nests <- cross_nested_nests(
choice_set = c(1L, 2L, 3L),
sparse = TRUE,
nests = list(
cross_nested_nest(existing_nest_parameter, list(`1` = alpha_existing, `3` = 1), name = "existing"),
cross_nested_nest(public_nest_parameter, list(`1` = alpha_public, `2` = 1), name = "public")
)
)
dense_nests <- cross_nested_nests(
choice_set = c(1L, 2L, 3L),
nests = list(
cross_nested_nest(existing_nest_parameter, list(`1` = alpha_existing, `2` = 0, `3` = 1), name = "existing"),
cross_nested_nest(public_nest_parameter, list(`1` = alpha_public, `2` = 1, `3` = 0), name = "public")
)
)
model <- cross_nested_logit_model(
database = database,
choice = "CHOICE",
utilities = utilities,
availability = availability,
nests = sparse_nests,
control = biogeme_control(
model_name = "b11c_cnl_sparse",
generate_html = FALSE,
generate_yaml = FALSE,
save_iterations = FALSE
)
)
report <- cross_nested_sparsity_report(sparse_nests)
expect_equal(report$stored_memberships, c(2L, 2L))
expect_equal(report$active_memberships, c(2L, 2L))
expect_equal(report$omitted_memberships, c(1L, 1L))
expect_equal(report$density, c(2 / 3, 2 / 3))
temporary_directory <- tempfile("rbiogeme-b11c-")
dir.create(temporary_directory, recursive = TRUE)
original_directory <- getwd()
setwd(temporary_directory)
on.exit(setwd(original_directory), add = TRUE)
r_fit <- estimate(model, model_name = "b11c_cnl_sparse", control = model$control)
native <- native_swissmetro_b11c(data)
expect_equal(nobs(r_fit), native$number_of_rows)
expect_identical(r_fit$beta_names, native$results$beta_names)
expect_equal(unname(coef(r_fit)), native$results$beta_values, tolerance = 1e-8)
expect_equal(as.numeric(logLik(r_fit)), native$results$final_log_likelihood, tolerance = 1e-8)
native_beta_values <- as.numeric(native$results$beta_values)
names(native_beta_values) <- native$results$beta_names
sparse_log <- cross_nested_log_probability(
utilities, availability, sparse_nests, variable("CHOICE")
)
dense_log <- cross_nested_log_probability(
utilities, availability, dense_nests, variable("CHOICE")
)
sparse_model <- biogeme_model(database = database, formula = sparse_log)
dense_model <- biogeme_model(database = database, formula = dense_log)
sparse_values <- simulate(
sparse_model,
expressions = list(contribution = sparse_log),
beta = native_beta_values,
control = biogeme_control(model_name = "b11c_sparse_contributions")
)
dense_values <- simulate(
dense_model,
expressions = list(contribution = dense_log),
beta = native_beta_values,
control = biogeme_control(model_name = "b11c_dense_contributions")
)
expect_equal(
sparse_values$values$contribution,
dense_values$values$contribution,
tolerance = 1e-12
)
})
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.