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
5 stars 5 forks source link

Undefined columns selected error #9

Closed veronicadiaz176 closed 1 year ago

veronicadiaz176 commented 1 year ago

I have been encountering an error when trying to run the code provided for generating a Bayesian D-efficient design. The error message that I receive is as follows: "Error in [.data.frame(des, c("profileID", varnames)) : undefined columns selected."

I was wondering if you could help me with this issue. Is there a way to fix this error or a workaround that I can use to generate a Bayesian D-efficient design with the CBCTools package?

Thanks!

jhelvy commented 1 year ago

Thanks for posting this!

Could you provide some code that produces this error so I can reproduce it?

veronicadiaz176 commented 1 year ago

Sure, I'd be happy to help you reproduce the error. However, I should mention that I was just following your example, so I may have made a mistake along the way. In any case, here is the code that I used:

library(cbcTools)

profiles <- cbc_profiles( price = c(1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5), freshness = c('Poor', 'Average', 'Excellent'), type = list( "Fuji" = list( price = c(2, 2.5, 3) ), "Gala" = list( price = c(1, 1.5, 2) ), "Honeycrisp" = list( price = c(2.5, 3, 3.5, 4, 4.5, 5), freshness = c("Average", "Excellent") ) ) )

Bayesian D-efficient designs

design_db_eff <- cbc_design( profiles = profiles, n_resp = 900, # Number of respondents n_alts = 3, # Number of alternatives per question n_q = 6, # Number of questions per respondent priors = list( price = c(-0.1), type = c(0.1, 0.2), freshness = c(0.1, 0.2) ) )

Thanks!

jhelvy commented 1 year ago

Yes I can reproduce the error with this code. It seems the error occurs when using a restricted subset of profiles, as I do in this example. There appear to be a few other issues with the code when trying to obtain a Bayesian D-efficient design, as noted in issue #10. In addressing them, I will probably also address this issue.

jhelvy commented 1 year ago

I believe this is now fixed. Could you try installing the version on this branch with this code:

remotes::install_github("jhelvy/cbcTools@dupe-checks")

Then you can run your test code again with:

library(cbcTools)

profiles <- cbc_profiles(
  price = c(1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5),
  freshness = c('Poor', 'Average', 'Excellent'),
  type = list(
    "Fuji" = list(
      price = c(2, 2.5, 3)
    ),
    "Gala" = list(
      price = c(1, 1.5, 2)
    ),
    "Honeycrisp" = list(
      price = c(2.5, 3, 3.5, 4, 4.5, 5),
      freshness = c("Average", "Excellent")
    )
  )
)

#Bayesian D-efficient designs
design_db_eff <- cbc_design(
  profiles  = profiles,
  n_resp    = 900, # Number of respondents
  n_alts    = 3, # Number of alternatives per question
  n_q       = 6, # Number of questions per respondent
  priors = list(
    price     = c(-0.1),
    type      = c(0.1, 0.2),
    freshness = c(0.1, 0.2)
  )
)

The issue was fundamentally the same as the root issue in #10.

veronicadiaz176 commented 1 year ago

Thanks for letting me know that the issue has been fixed. I tried the new code and it works perfectly fine now.