From 68c97cbe6ffa9cb1cc7022b7b5b0b859e77f2455 Mon Sep 17 00:00:00 2001 From: Julian Sagebiel <julian.sagebiel@idiv.de> Date: Tue, 31 Dec 2024 00:52:07 +0100 Subject: [PATCH] now implemented a safer way to identify beta parameters --- R/sim_choice.R | 20 +++++++++++++++++++- man/sim_all.Rd | 4 ++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/R/sim_choice.R b/R/sim_choice.R index 15870e2..3fdd273 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 7522c92..4be6d8d 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. -- GitLab