traets / idefix

R-package to create efficient choice designs for the MNL model, and adaptive designs for the MMNL model.
21 stars 5 forks source link

Continuous variable choice set doesn't appear to be correct #14

Open UrbanFercecNchain opened 7 months ago

UrbanFercecNchain commented 7 months ago

Dear Developers,

I am conducting a DCE experiment with 5 variables for which I would like to use your software to optimize the choice sets. For the first trial design, I have no priors, which means that values for all variable levels should be somewhat uniformly present in the result set. This appears to be the case for all discrete variables but isn't for my value of continuous variable. Specifically, in about 90% of all alternatives I get the lowest number while the highest is not even present, which to me seems like an error. I am providing you with my code below.

# Load necessary library
library(idefix)

# Set random seed for reproducibility
set.seed(123)

# Define levels for each attribute
cost_levels <- c("1.8€", "2.5€", "3.3€", "4.0", "4.5")
taste_levels <- c("Povprečen", "Dober", "Vrhunski")
environment_levels <- c("Slab", "Povprečen", "Dober", "Izjemen")
type_levels <- c("Klasična", "Posebna")
standard_levels <- c("Običajen", "Trajnostni")
code <- c("C", "D", "D", "D", "D")
alts <- 3
c_lvls <- list(c(1.8, 2.5, 3.3, 4.0, 4.5))
alt_cte <- c(0, 0, 1)

# Use 'Profiles' function to generate candidate profiles
profiles <- Profiles(lvls = c(5, 3, 4, 2, 2),
                     coding = code,
                     c.lvls = c_lvls)

# Assuming 'mu' and 'Sigma' are defined based on your prior beliefs or non-informative priors
mu <- rep(0, 9) #no prior beliefs
Sigma <- diag(rep(1, length(mu))) # Assuming independence and same variance

# Generate par.draws
draws <- MASS::mvrnorm(n = 500, mu = mu, Sigma = Sigma)

# Generate design using 'Modfed' function
design <- Modfed(cand.set = profiles,
                 n.sets = 12, # Number of choice sets to generate
                 n.alts = alts, # Including the "I want none" option
                 alt.cte = alt_cte, # Assuming the last alternative is "I want none"
                 par.draws = list(draws[, 1], draws[, 2:9]),
                 no.choice = TRUE)

# The 'design' object now contains your experimental design
print(design)

#Decode
lvls <- list(cost_levels, taste_levels, environment_levels, type_levels, standard_levels)
decode <- Decode(des = design$design,
                 lvl.names = lvls,
                 coding = code,
                 n.alts = alts,
                 c.lvls = c_lvls,
                 alt.cte = alt_cte,
                 no.choice = 3
                  )
print(decode)

If I run this, I get 1.8€ in 20 of alternatives and 3.3€ in 4. However, is it more then possible that I have made an error somewhere and that your software is working perfectly. That being said, I would kindly ask you for your help.

KilianHTT commented 1 week ago

Hello, I encounter the same issue. I'm also working with prices (continuous) and a no-choice alternative.

I even tried with the CEA function and with increased "n.set" just to be sure. But I never get a choice set including the third and fourth level for this continuous attribute.

Here is my code :

# 1. Define attribute levels and coding type for each attribute
# In this example, three attributes are defined (AOC, Production Method, and Price)
levels <- c(2, 3, 4)          # AOC; Production Method (Conv, HVE, AB), and Price (€4, €6.25, €8.5, €10.75)
code <- c("D", "D", "C")      # Use dummy coding for the first two attributes and continuous coding for price
n.sets <- 12 # Multiple of attribute levels!
alt.cte <- c(0, 0, 1) # Alternative-specific constant (ASC) for the no-choice alternative
n.alts <- 3
lev_price <- list(c(4, 6.25, 8.5, 10.75))

# 2. Generate the list of candidate profiles
# This step generates all possible combinations of attribute levels.
cand.set <- Profiles(lvls = levels, coding = code, c.lvls = lev_price) # 24 possible combinations = 24 candidate sets

# 3. Define prior parameters for preference coefficients
mu <- c(0, 0.02, 0.87, 0.96, -0.18)  # Mean of preference coefficient distributions for priors
# First coefficient corresponds to ASC for the no-choice alternative
sigma <- diag(length(mu))          # Covariance matrix (diagonal) for coefficients

# Generate draws from the prior distribution for Bayesian design
set.seed(123)  # For reproducibility
par.draws <- MASS::mvrnorm(n = 200, mu = mu, Sigma = sigma)

# Adjust the structure of the parameter draws
par.draws <- list(par.draws[, 1], par.draws[, 2:5])

# 4. Create an optimal (D-efficient) design using the idefix package
# Use the modified Federov algorithm to generate a design minimizing D(B)-error.
design <- Modfed(cand.set = cand.set, n.sets = n.sets, n.alts = n.alts, par.draws = par.draws, no.choice = TRUE, alt.cte = alt.cte, best = TRUE, parallel = TRUE)

# Alternative approach: Create a CEA (Choice Experiment Algorithm) design
design <- CEA(lvls = levels, coding = code, par.draws = par.draws, n.alts = n.alts, n.sets = n.sets, parallel = TRUE, c.lvls = lev_price, alt.cte = alt.cte, no.choice = TRUE)

I also may have done something wrong.

Thanks in advance!

Nb: When this attribute is dummy coded I get all its level attributes. The issue might be related to the "continuous" type of coding.