From 7b2815e55a61818d098c738f11aa9ad99fffb55a Mon Sep 17 00:00:00 2001
From: Julian Sagebiel <julian.sagebiel@idiv.de>
Date: Tue, 31 Dec 2024 01:24:44 +0100
Subject: [PATCH] functionalized transformation of names

---
 R/sim_all.R                |  8 ++++++-
 R/sim_choice.R             | 11 +++-------
 R/utils.R                  | 43 ++++++++++++++++++++++++++++++++++++++
 man/modify_bcoeff_names.Rd | 25 ++++++++++++++++++++++
 4 files changed, 78 insertions(+), 9 deletions(-)
 create mode 100644 man/modify_bcoeff_names.Rd

diff --git a/R/sim_all.R b/R/sim_all.R
index c2ff269..c312e4e 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 3fdd273..9d8c25d 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 89114de..2fc4054 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 0000000..b39eaed
--- /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}
-- 
GitLab