jhelvy / cbcTools

An R package with tools for designing choice based conjoint (cbc) survey experiments and conducting power analyses
https://jhelvy.github.io/cbcTools/
Other
6 stars 5 forks source link

Error with cbc_design when keep_db_error = TRUE #36

Open keikeisusu opened 1 month ago

keikeisusu commented 1 month ago

Hi! I'm trying to make a series of designs with cbc_design to pick ones with small DB-error. But when keep_db_error = TRUE, the function returns the error as below:

Error in `[.data.frame`(as.data.frame(design), , c(metaNames, varNames)) : undefined columns selected

Does anyone know how to deal with this?

set.seed(123)

prof_3233 <- cbc_profiles(
  A = c("A1", "A2", "A3"),
  B = c("B1", "B2"),
  C = c("C1", "C2", "C3"),
  D = c(3000, 3500, 4000)
)

cea_des <- cbc_design(
  profiles = prof_3233,
  n_resp = 100,
  n_alts = 2,
  n_q = 10,
  method = "CEA",
  priors = list(A = c(0, 0), B = 0, C = c(0, 0), D = 0), 
  parallel = TRUE,
  keep_db_error = TRUE
)

Thanks in advance!

mmardehali commented 1 month ago

I think I know why this is happening, but the line that may raise that error seems to be left out of the script here. I think you may be trying to read/print the DB-error without identifying the proper location to look for the value. To access the DB-error value, try the following: DBerr <- as.numeric(cea_des$db_err)

This will store the error value in DBerr which you can then print, round, etc. For simplicity, I would also recommend rounding the error: RoundedDBerror <- round(Dberr, 2)

I'm using version 0.4.0 of cbcTools, which at the time I designed my DCE was the most recent version. If you are using a different version, your results may vary. That said, when I ran the script you provided here, no errors were raised and everything worked as expected.

keikeisusu commented 1 month ago

Thanks, mmardehali!

I'm using ver. 0.5.2 . The returned object from cbc_design is a data.frame, not a list, regardless of the value for keep_db_error. I tried running the same code as above with v 0.4.0, as you said, it worked. In addition, the object is indeed a list.

Although I've not scrutinized the source code here, it seems the lines 871-873 don't function.