diff --git a/R/sim_choice.R b/R/sim_choice.R index 15870e27ec846a892d3e1a60ae3485883028e8a9..3fdd273f9da4e4b6aa6ea314d10eef1e0f755c5a 100644 --- a/R/sim_choice.R +++ b/R/sim_choice.R @@ -24,13 +24,31 @@ sim_choice <- function(designfile, no_sim = 10, respondents = 330, u ,designtype # Create a lookup table for bcoeff names bcoeff_lookup <- tibble::tibble( original = names(bcoeff), - modified = stringr::str_replace_all(names(bcoeff), "_", "") + modified = stringr::str_replace_all(names(bcoeff), "[._]", "") ) # Replace all underscores in bcoeff names with an empty string names(bcoeff) <- bcoeff_lookup$modified + u <- purrr::map(u, function(utility_group) { + purrr::map(utility_group, function(utility) { + # Convert the RHS of the formula to a single string + rhs_string <- paste(deparse(formula.tools::rhs(utility)), collapse = " ") + + # Replace coefficient names in the RHS string + modified_rhs <- stringr::str_replace_all( + rhs_string, + stats::setNames( + bcoeff_lookup$modified, + paste0("(?<![a-zA-Z0-9._])", bcoeff_lookup$original, "(?![a-zA-Z0-9._])") + ) + ) + + # Recreate the formula with the modified RHS + formula(paste(as.character(formula.tools::lhs(utility)), "~", modified_rhs)) + }) + }) #### Function to simulate and estimate #### diff --git a/man/sim_all.Rd b/man/sim_all.Rd index 7522c92e5e2e0963837727e28186b82d46e91364..4be6d8d0ee7c2c14ea40f0a9b5a686b58042af6f 100644 --- a/man/sim_all.Rd +++ b/man/sim_all.Rd @@ -18,7 +18,7 @@ sim_all( estimate = TRUE, chunks = 1, utility_transform_type = "simple", - reshape_type = "tidyr" + reshape_type = "auto" ) } \arguments{ @@ -46,7 +46,7 @@ sim_all( \item{utility_transform_type}{How the utility function you entered is transformed to the utility function required for mixl. You can use the classic way (simple) where parameters have to start with "b" and variables with "alt" or the more flexible (but potentially error prone) way (exact) where parameters and variables are matched exactly what how the are called in the dataset and in the bcoeff list. Default is "simple". In the long run, simple will be deleted, as exact should be downwards compatible.} -\item{reshape_type}{Must be either "stats" to use the reshape from the stats package or tidyr to use pivot longer. Default is tidyr. Only change it once you face an error at this position and you may be lucky that it works then.} +\item{reshape_type}{Must be "auto", "stats" to use the reshape from the stats package or tidyr to use pivot longer. Default is auto and should not bother you. Only change it once you face an error at this position and you may be lucky that it works then.} } \value{ A list, with all information on the simulation. This list an be easily processed by the user and in the rmarkdown template.