Skip to content
Snippets Groups Projects
Commit 7b2815e5 authored by dj44vuri's avatar dj44vuri
Browse files

functionalized transformation of names

parent 68c97cbe
Branches
No related tags found
No related merge requests found
...@@ -91,7 +91,7 @@ sim_all <- function(nosim = 2, ...@@ -91,7 +91,7 @@ sim_all <- function(nosim = 2,
stop("Values in 'bcoeff' must be numeric.") 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" # Extract coefficients from utility function starting with "b"
coeff_names_ul <- unique(unlist(lapply(u, function(u) { coeff_names_ul <- unique(unlist(lapply(u, function(u) {
formula_strings <- unlist(u) formula_strings <- unlist(u)
...@@ -132,6 +132,12 @@ sim_all <- function(nosim = 2, ...@@ -132,6 +132,12 @@ sim_all <- function(nosim = 2,
########## End Validation Tests ################# ########## End Validation Tests #################
################################################# #################################################
bcoeff_result <- modify_bcoeff_names(bcoeff)
bcoeff <- bcoeff_result$bcoeff
designfile <- list.files(designpath, full.names = T) 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 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
......
...@@ -21,14 +21,9 @@ sim_choice <- function(designfile, no_sim = 10, respondents = 330, u ,designtype ...@@ -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.") message("'simple' is deprecated and will be removed in the future. Use 'exact' instead.")
} }
# Create a lookup table for bcoeff names bcoeff_result <- modify_bcoeff_names(bcoeff)
bcoeff_lookup <- tibble::tibble( bcoeff <- bcoeff_result$bcoeff
original = names(bcoeff), bcoeff_lookup <- bcoeff_result$bcoeff_lookup
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) { u <- purrr::map(u, function(utility_group) {
......
...@@ -94,3 +94,46 @@ make_md <- function(f=file){ ...@@ -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
)
}
% 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}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment