Skip to content
Snippets Groups Projects
Commit 68c97cbe authored by dj44vuri's avatar dj44vuri
Browse files

now implemented a safer way to identify beta parameters

parent 55ebf814
No related branches found
No related tags found
No related merge requests found
...@@ -24,13 +24,31 @@ sim_choice <- function(designfile, no_sim = 10, respondents = 330, u ,designtype ...@@ -24,13 +24,31 @@ sim_choice <- function(designfile, no_sim = 10, respondents = 330, u ,designtype
# Create a lookup table for bcoeff names # Create a lookup table for bcoeff names
bcoeff_lookup <- tibble::tibble( bcoeff_lookup <- tibble::tibble(
original = names(bcoeff), 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 # Replace all underscores in bcoeff names with an empty string
names(bcoeff) <- bcoeff_lookup$modified 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 #### #### Function to simulate and estimate ####
......
...@@ -18,7 +18,7 @@ sim_all( ...@@ -18,7 +18,7 @@ sim_all(
estimate = TRUE, estimate = TRUE,
chunks = 1, chunks = 1,
utility_transform_type = "simple", utility_transform_type = "simple",
reshape_type = "tidyr" reshape_type = "auto"
) )
} }
\arguments{ \arguments{
...@@ -46,7 +46,7 @@ sim_all( ...@@ -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{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{ \value{
A list, with all information on the simulation. This list an be easily processed by the user and in the rmarkdown template. A list, with all information on the simulation. This list an be easily processed by the user and in the rmarkdown template.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment