diff --git a/R/sim_all.R b/R/sim_all.R index c2ff269405b069ff31f42a7c4c499b175c9e8aab..c312e4e0f1f0bd71d987d27d8f69a269fce94e42 100644 --- a/R/sim_all.R +++ b/R/sim_all.R @@ -91,7 +91,7 @@ sim_all <- function(nosim = 2, stop("Values in 'bcoeff' must be numeric.") } - #### check that all the coefficients in utility function have a cooresponding value in bcoeff #### + #### check that all the coefficients in utility function have a corresponding value in bcoeff #### # Extract coefficients from utility function starting with "b" coeff_names_ul <- unique(unlist(lapply(u, function(u) { formula_strings <- unlist(u) @@ -132,6 +132,12 @@ sim_all <- function(nosim = 2, ########## End Validation Tests ################# ################################################# + + bcoeff_result <- modify_bcoeff_names(bcoeff) + bcoeff <- bcoeff_result$bcoeff + + + designfile <- list.files(designpath, full.names = T) designname <- stringr::str_remove_all(list.files(designpath, full.names = F), "(.ngd|_|.RDS)") ## Make sure designnames to not contain file ending and "_", as the may cause issues when replace diff --git a/R/sim_choice.R b/R/sim_choice.R index 3fdd273f9da4e4b6aa6ea314d10eef1e0f755c5a..9d8c25d1abc28e115c7bda753bc2fdda98aa5d68 100644 --- a/R/sim_choice.R +++ b/R/sim_choice.R @@ -21,14 +21,9 @@ sim_choice <- function(designfile, no_sim = 10, respondents = 330, u ,designtype message("'simple' is deprecated and will be removed in the future. Use 'exact' instead.") } - # Create a lookup table for bcoeff names - bcoeff_lookup <- tibble::tibble( - original = names(bcoeff), - modified = stringr::str_replace_all(names(bcoeff), "[._]", "") - ) - - # Replace all underscores in bcoeff names with an empty string - names(bcoeff) <- bcoeff_lookup$modified + bcoeff_result <- modify_bcoeff_names(bcoeff) + bcoeff <- bcoeff_result$bcoeff + bcoeff_lookup <- bcoeff_result$bcoeff_lookup u <- purrr::map(u, function(utility_group) { diff --git a/R/utils.R b/R/utils.R index 89114de955f11d013dc52b6b3d660120826a34bf..2fc4054369ed782649b1dbec25d01062a2f922c2 100644 --- a/R/utils.R +++ b/R/utils.R @@ -94,3 +94,46 @@ make_md <- function(f=file){ } + + +#' Modify bcoeff Names +#' +#' This function modifies the names of beta coefficients (`bcoeff`) by removing +#' dots and underscores to ensure consistent naming. If the names are already modified, +#' the function skips processing. +#' +#' @param bcoeff A named list of beta coefficients. The names of this list are +#' the coefficients used in the utility function. +#' +#' @return A list containing: +#' \describe{ +#' \item{bcoeff}{The modified `bcoeff` list with updated names.} +#' \item{bcoeff_lookup}{A tibble mapping the original names to the modified names.} +#' } +#' +#' @keywords internal +modify_bcoeff_names <- function(bcoeff) { + # Check if bcoeff names need modification + if (any(grepl("[._]", names(bcoeff)))) { + # Create a lookup table + bcoeff_lookup <- tibble::tibble( + original = names(bcoeff), + modified = stringr::str_replace_all(names(bcoeff), "[._]", "") + ) + + # Modify the names in the original bcoeff + names(bcoeff) <- bcoeff_lookup$modified + } else { + # No modification needed; create a trivial lookup table + bcoeff_lookup <- tibble::tibble( + original = names(bcoeff), + modified = names(bcoeff) + ) + } + + # Return both modified bcoeff and lookup table + list( + bcoeff = bcoeff, + bcoeff_lookup = bcoeff_lookup + ) +} diff --git a/man/modify_bcoeff_names.Rd b/man/modify_bcoeff_names.Rd new file mode 100644 index 0000000000000000000000000000000000000000..b39eaedb2cbb2ccd95f1c91064931383ae13f390 --- /dev/null +++ b/man/modify_bcoeff_names.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{modify_bcoeff_names} +\alias{modify_bcoeff_names} +\title{Modify bcoeff Names} +\usage{ +modify_bcoeff_names(bcoeff) +} +\arguments{ +\item{bcoeff}{A named list of beta coefficients. The names of this list are +the coefficients used in the utility function.} +} +\value{ +A list containing: +\describe{ +\item{bcoeff}{The modified \code{bcoeff} list with updated names.} +\item{bcoeff_lookup}{A tibble mapping the original names to the modified names.} +} +} +\description{ +This function modifies the names of beta coefficients (\code{bcoeff}) by removing +dots and underscores to ensure consistent naming. If the names are already modified, +the function skips processing. +} +\keyword{internal}