@@ -46,6 +46,9 @@ cbc_choices <- function(design, priors = NULL) {
4646 stop(" design must be a cbc_design object created by cbc_design()" )
4747 }
4848
49+ # Store original encoding
50+ original_encoding <- attr(design , " encoding" ) %|| % " standard"
51+
4952 if (is.null(priors )) {
5053 # Simulate random choices
5154 result <- simulate_random_choices(design )
@@ -66,8 +69,8 @@ cbc_choices <- function(design, priors = NULL) {
6669 priors_used <- TRUE
6770 }
6871
69- # Preserve encoding attributes
70- attr(result , " is_dummy_coded " ) <- attr( design , " is_dummy_coded " )
72+ # Preserve encoding and categorical structure attributes
73+ attr(result , " encoding " ) <- original_encoding
7174 attr(result , " categorical_structure" ) <- attr(
7275 design ,
7376 " categorical_structure"
@@ -158,27 +161,28 @@ simulate_utility_based_choices <- function(design, priors) {
158161 # Create optimization environment using the existing function
159162 opt_env <- setup_optimization_environment(
160163 profiles = profiles ,
161- method = " random" , # Hard-code this so that the obsID vectors are correct
162- time_start = Sys.time(), # Not important for choice simulation
164+ method = " random" ,
165+ time_start = Sys.time(),
163166 n_alts = design_params $ n_alts ,
164167 n_q = design_params $ n_q ,
165168 n_resp = design_params $ n_resp ,
166169 n_blocks = design_params $ n_blocks ,
167- n_cores = 1 , # Not used for choice simulation
168- n_start = 1 , # Not used for choice simulation
169- max_iter = 1 , # Not used for choice simulation
170- priors = priors , # The new priors for choice simulation
170+ n_cores = 1 ,
171+ n_start = 1 ,
172+ max_iter = 1 ,
173+ priors = priors ,
171174 no_choice = design_params $ no_choice ,
172175 label = design_params $ label ,
173- balance_by = NULL , # Not used for choice simulation
174- remove_dominant = FALSE , # Not needed for choice simulation
175- dominance_types = NULL , # Not needed for choice simulation
176- dominance_threshold = 0.8 , # Not needed for choice simulation
177- max_dominance_attempts = 1 , # Not needed for choice simulation
178- randomize_questions = TRUE , # Not used for choice simulation
179- randomize_alts = TRUE , # Not used for choice simulation
180- include_probs = FALSE , # Not used for choice simulation
181- use_idefix = FALSE # Not used for choice simulation
176+ balance_by = NULL ,
177+ remove_dominant = FALSE ,
178+ dominance_types = NULL ,
179+ dominance_threshold = 0.8 ,
180+ max_dominance_attempts = 1 ,
181+ randomize_questions = TRUE ,
182+ randomize_alts = TRUE ,
183+ include_probs = FALSE ,
184+ use_idefix = FALSE ,
185+ coding = design_params $ coding %|| % " standard"
182186 )
183187
184188 # Get design matrix from the design object
@@ -217,16 +221,16 @@ get_design_matrix_from_design_object <- function(design, opt_env) {
217221 # Fill matrix from profileID data
218222 for (obs in 1 : n_questions ) {
219223 obs_rows <- regular_design [regular_design $ obsID == obs , ]
220- obs_rows <- obs_rows [order(obs_rows $ altID ), ] # Ensure proper order
224+ obs_rows <- obs_rows [order(obs_rows $ altID ), ]
221225
222- if (nrow(obs_rows ) == n_alts ) {
223- design_matrix [obs , ] <- obs_rows $ profileID
224- } else {
226+ if (nrow(obs_rows ) != n_alts ) {
225227 stop(sprintf(
226- " Inconsistent number of alternatives in observation %d" ,
227- obs
228+ " Inconsistent number of alternatives in observation %d: expected %d, got %d " ,
229+ obs , n_alts , nrow( obs_rows )
228230 ))
229231 }
232+
233+ design_matrix [obs , ] <- obs_rows $ profileID
230234 }
231235
232236 return (design_matrix )
0 commit comments