@@ -161,27 +161,28 @@ simulate_utility_based_choices <- function(design, priors) {
161161 # Create optimization environment using the existing function
162162 opt_env <- setup_optimization_environment(
163163 profiles = profiles ,
164- method = " random" , # Hard-code this so that the obsID vectors are correct
165- time_start = Sys.time(), # Not important for choice simulation
164+ method = " random" ,
165+ time_start = Sys.time(),
166166 n_alts = design_params $ n_alts ,
167167 n_q = design_params $ n_q ,
168168 n_resp = design_params $ n_resp ,
169169 n_blocks = design_params $ n_blocks ,
170- n_cores = 1 , # Not used for choice simulation
171- n_start = 1 , # Not used for choice simulation
172- max_iter = 1 , # Not used for choice simulation
173- priors = priors , # The new priors for choice simulation
170+ n_cores = 1 ,
171+ n_start = 1 ,
172+ max_iter = 1 ,
173+ priors = priors ,
174174 no_choice = design_params $ no_choice ,
175175 label = design_params $ label ,
176- balance_by = NULL , # Not used for choice simulation
177- remove_dominant = FALSE , # Not needed for choice simulation
178- dominance_types = NULL , # Not needed for choice simulation
179- dominance_threshold = 0.8 , # Not needed for choice simulation
180- max_dominance_attempts = 1 , # Not needed for choice simulation
181- randomize_questions = TRUE , # Not used for choice simulation
182- randomize_alts = TRUE , # Not used for choice simulation
183- include_probs = FALSE , # Not used for choice simulation
184- 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"
185186 )
186187
187188 # Get design matrix from the design object
@@ -207,7 +208,8 @@ get_design_matrix_from_design_object <- function(design, opt_env) {
207208 # Get the regular profiles (excluding no-choice if present)
208209 regular_design <- design
209210 if (opt_env $ no_choice ) {
210- regular_design <- design [design $ profileID != 0 , ]
211+ no_choice_id <- opt_env $ n $ profiles + 1
212+ regular_design <- design [design $ profileID != no_choice_id , ]
211213 }
212214
213215 # Determine matrix dimensions
@@ -220,16 +222,16 @@ get_design_matrix_from_design_object <- function(design, opt_env) {
220222 # Fill matrix from profileID data
221223 for (obs in 1 : n_questions ) {
222224 obs_rows <- regular_design [regular_design $ obsID == obs , ]
223- obs_rows <- obs_rows [order(obs_rows $ altID ), ] # Ensure proper order
225+ obs_rows <- obs_rows [order(obs_rows $ altID ), ]
224226
225- if (nrow(obs_rows ) == n_alts ) {
226- design_matrix [obs , ] <- obs_rows $ profileID
227- } else {
227+ if (nrow(obs_rows ) != n_alts ) {
228228 stop(sprintf(
229- " Inconsistent number of alternatives in observation %d" ,
230- obs
229+ " Inconsistent number of alternatives in observation %d: expected %d, got %d " ,
230+ obs , n_alts , nrow( obs_rows )
231231 ))
232232 }
233+
234+ design_matrix [obs , ] <- obs_rows $ profileID
233235 }
234236
235237 return (design_matrix )
0 commit comments