Important: Read this before posting to this forum

  1. This forum is for questions related to the use of Apollo. We will answer some general choice modelling questions too, where appropriate, and time permitting. We cannot answer questions about how to estimate choice models with other software packages.
  2. There is a very detailed manual for Apollo available at http://www.ApolloChoiceModelling.com/manual.html. This contains detailed descriptions of the various Apollo functions, and numerous examples are available at http://www.ApolloChoiceModelling.com/examples.html. In addition, help files are available for all functions, using e.g. ?apollo_mnl
  3. Before asking a question on the forum, users are kindly requested to follow these steps:
    1. Check that the same issue has not already been addressed in the forum - there is a search tool.
    2. Ensure that the correct syntax has been used. For any function, detailed instructions are available directly in Apollo, e.g. by using ?apollo_mnl for apollo_mnl
    3. Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
    4. Make sure that R is using the latest official release of Apollo.
  4. If the above steps do not resolve the issue, then users should follow these steps when posting a question:
    1. provide full details on the issue, including the entire code and output, including any error messages
    2. posts will not immediately appear on the forum, but will be checked by a moderator first. We check the forum at least twice a week. It may thus take a couple of days for your post to appear and before we reply. There is no need to submit the post multiple times.

Dummy and effect coding result for heterogeneity (sigma)

Ask questions about the results reported after estimation. If the output includes errors, please include your model code if possible.
Post Reply
apk0022
Posts: 7
Joined: 25 Oct 2024, 18:36

Dummy and effect coding result for heterogeneity (sigma)

Post by apk0022 »

Hi apollo team,
I have a couple of questions.

1. I ran a mixed logit model in Apollo (R) using seed in the control. The result for the significance of heterogeneity (nature of t-ratio for sd or sigma) for one attribute (with 3 levels) is not consistent across dummy and effect coding. It shows significant heterogeneity (sigma) for the dummy and not significant for effect coding. I tried altering the levels in the model, checked the data, removed opt-out, and estimated the model, but it did not help with the issue. The study has 2 unlabelled alternatives and an opt-out option.
I thought the underlying heterogeneity would be reflected no matter whichever coding style I chose. This is the issue with only 1 attribute (specificity).

I did get the answer from Michiel, mentioning, 'Dummy coding is interpreted to the base level, whereas effects coding is interpreted to the mean. While they describe identical behavior, the interpretation of the parameters, including any heterogeneity, is not directly comparable.'
But still the doubt is why the significance is seen with one type of coding (dummy) and not with the other (effect).

I would be really grateful for any advice I can get to understand and resolve this issue.

2. Also, when I compare the MNL result, the robust s.e in the result for dummy coding is almost double the one observed in the effect coding. Why is it so? Would the issue with the significance of sigma in mixed logit be related to these differences in s.e in the MNL model? Please help me in understanding this issue.

Please find the result below:
Dummy coding (MNL):

Code: Select all

Model run by DELL using Apollo 0.3.5 on R 4.4.1 for Windows.					
Please acknowledge the use of Apollo by citing Hess & Palma (2019)					
  DOI 10.1016/j.jocm.2019.100170					
  www.ApolloChoiceModelling.com					
					
Model name                                  : DCE3					
Model description                           : MNL model_optout					
Model run at                                : 2025-04-08 12:07:41.268232					
Estimation method                           : bgw					
Model diagnosis                             : Relative function convergence					
Optimisation diagnosis                      : Maximum found					
     hessian properties                     : Negative definite					
     maximum eigenvalue                     : -38.027638					
     reciprocal of condition number         : 0.0487212					
Number of individuals                       : 218					
Number of rows in database                  : 1962					
Number of modelled outcomes                 : 1962					
					
Number of cores used                        :  1 					
Model without mixing					
					
LL(start)                                   : -2155.48					
LL at equal shares, LL(0)                   : -2155.48					
LL at observed shares, LL(C)                : -2044.09					
LL(final)                                   : -1864.93					
Rho-squared vs equal shares                  :  0.1348 					
Adj.Rho-squared vs equal shares              :  0.1288 					
Rho-squared vs observed shares               :  0.0876 					
Adj.Rho-squared vs observed shares           :  0.0823 					
AIC                                         :  3755.86 					
BIC                                         :  3828.43 					
					
Estimated parameters                        : 13					
Time taken (hh:mm:ss)                       :  00:00:2.51 					
     pre-estimation                         :  00:00:0.55 					
     estimation                             :  00:00:0.18 					
     post-estimation                        :  00:00:1.78 					
Iterations                                  :  7  					
Unconstrained optimisation
Estimates:					
                         Estimate    Rob.s.e. Rob.t.rat.(0)					
asc_alt                0.000000          NA            NA					
asc_alt3              -0.717078     0.15487      -4.63007					
b_fasting_time_2    0.039763     0.08891       0.44724					
b_fasting_time_3    0.006364     0.08309       0.07659					
b_fasting_time_4    0.004219     0.09011       0.04682					
b_invasiveness_2    0.435067     0.06827       6.37302					
b_sensitivity_2     0.324704     0.08266       3.92833					
b_sensitivity_3     0.656409     0.09819       6.68527					
b_specificity_2     0.332911     0.06943       4.79477					
b_specificity_3     0.756286     0.08074       9.36653					
b_prediction_2     -0.375720     0.07169      -5.24121					
b_cost_2           -0.491114     0.09378      -5.23660					
b_cost_3           -0.856268     0.11842      -7.23055					
b_cost_4           -1.671068     0.14756     -11.32494					
Effect coding(MNL):

Code: Select all

Model name                                  : DCE3							
Model description                           : MNL model							
Model run at                                : 2025-04-08 11:58:59.885666							
Estimation method                           : bgw							
Model diagnosis                             : Relative function convergence							
Optimisation diagnosis                      : Maximum found							
     hessian properties                     : Negative definite							
     maximum eigenvalue                     : -193.531857							
     reciprocal of condition number         : 0.143077							
Number of individuals                       : 218							
Number of rows in database                  : 1962							
Number of modelled outcomes                 : 1962							
							
Number of cores used                        :  1 							
Model without mixing							
							
LL(start)                                   : -2155.48							
LL at equal shares, LL(0)                   : -2155.48							
LL at observed shares, LL(C)                : -2044.09							
LL(final)                                   : -1864.93							
Rho-squared vs equal shares                  :  0.1348 							
Adj.Rho-squared vs equal shares              :  0.1288 							
Rho-squared vs observed shares               :  0.0876 							
Adj.Rho-squared vs observed shares           :  0.0823 							
AIC                                         :  3755.86 							
BIC                                         :  3828.43 							
							
Estimated parameters                        : 13							
Time taken (hh:mm:ss)                       :  00:00:1.15 							
     pre-estimation                         :  00:00:0.51 							
     estimation                             :  00:00:0.14 							
     post-estimation                        :  00:00:0.5 							
Iterations                                  :  7  							

Unconstrained optimisation.									
									
Estimates:									
                Estimate        s.e.        t.rat.(0)    p(2-sided)    Rob.s.e.  Rob.t.rat.(0)  p(2-sided)									
asc_alt     0.694829     0.05944    11.68900     0.00000     0.11818       5.87961    4.112e-09									
asc_alt3    0.000000          NA          NA          NA          NA            NA          NA									
b_Fast_1   -0.012586     0.05670    -0.22196     0.82434     0.05373      -0.23424     0.81480									
b_Fast_2    0.027176     0.05431     0.50037     0.61682     0.05455       0.49815     0.61838									
b_Fast_3   -0.006222     0.05810    -0.10709     0.91472     0.05281      -0.11783     0.90620									
b_Inv_1    -0.217534     0.02937    -7.40614   1.301e-13     0.03413      -6.37302   1.853e-10									
b_Sen_1    -0.327038     0.04451    -7.34734   2.023e-13     0.05447      -6.00363   1.929e-09									
b_Sen_2    -0.002334     0.04325    -0.05396     0.95696     0.04282      -0.05450     0.95654									
b_Spe_1    -0.363066     0.04807    -7.55283   4.263e-14     0.04515      -8.04047   8.882e-16									
b_Spe_2    -0.030155     0.04139    -0.72855     0.46628     0.03618      -0.83342     0.40461									
b_Pre_1     0.187860     0.02965     6.33659   2.349e-10     0.03584       5.24121   1.595e-07									
b_Cost_1    0.754613     0.05678    13.28895     0.00000     0.07826       9.64268     0.00000									
b_Cost_2    0.263499     0.05328     4.94528   7.603e-07     0.05618       4.68999   2.732e-06									
b_Cost_3   -0.101656     0.05707    -1.78136     0.07485     0.05907      -1.72090     0.08527									
									
Running Delta method computation for user-defined function using robust standard errors								
Expression   Value   s.e. t-ratio (0)									
   b_Fast_4 -0.0084 0.0522       -0.16									
    b_Inv_2  0.2175 0.0341        6.37									
    b_Sen_3  0.3294 0.0526        6.26									
    b_Spe_3  0.3932 0.0433        9.08									
    b_Pre_2 -0.1879 0.0358       -5.24									
   b_Cost_4 -0.9165 0.0856      -10.71									
									
Thank you in advance for your response.

Regards,
Asmita
dpalma
Posts: 215
Joined: 24 Apr 2020, 17:54

Re: Dummy and effect coding result for heterogeneity (sigma)

Post by dpalma »

Hi Asmita,

Could you also post the code for each model? Looking at least to the definition of the utilities might help us understand things better.

Best wishes,
David
apk0022
Posts: 7
Joined: 25 Oct 2024, 18:36

Re: Dummy and effect coding result for heterogeneity (sigma)

Post by apk0022 »

Hi David,
Thank you for your response.

Here is the code for effect and dummy coding for MNL and mixed logit:

Code: Select all

# ################################################################# #
#### DUMMYYYY   - MNL                ####
# ################################################################# #
### Clear memory
rm(list = ls())
install.packages("apollo")
### Load Apollo library
library(apollo)
library(dplyr)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "DCE3",
  modelDescr      = "MNL model_optout",
  indivID         = "ID", 
  #nAlternatives   = 3,  # Number of alternatives
  #alternatives    = c(1, 2, 0),  # Define alternative IDs
  outputDirectory = "output"
)

# Load the data
data = read.csv("DCE3_after_removal_modified_withcovariates_1.csv",header=TRUE)
head(data)

# Dummy coding for all the attributes
# Fasting Time (Fast_A and Fast_B)
database <- data %>%
  mutate(Fast1_2 = ifelse(Fast1 == 2, 1, 0),
         Fast1_3 = ifelse(Fast1 == 3, 1, 0),
         Fast1_4 = ifelse(Fast1 == 4, 1, 0),
         Fast2_2 = ifelse(Fast2 == 2, 1, 0),
         Fast2_3 = ifelse(Fast2 == 3, 1, 0),
         Fast2_4 = ifelse(Fast2 == 4, 1, 0),
         
         # Invasiveness (Inv_A and Inv_B)
         Inv1_2 = ifelse(Inv1 == 2, 1, 0), 
         Inv2_2 = ifelse(Inv2 == 2, 1, 0),
         
         #Sen_A (Sen_A and Sen_B)
         
         Sen1_2 = ifelse(Sen1 == 2, 1, 0),
         Sen1_3 = ifelse(Sen1 == 3, 1, 0),
         Sen2_2 = ifelse(Sen2 == 2, 1, 0),
         Sen2_3 = ifelse(Sen2 == 3, 1, 0),
         
         # Specificity (Spe_A and Spe_B)
         
         Spe1_2 = ifelse(Spe1 == 2, 1, 0),
         Spe1_3 = ifelse(Spe1 == 3, 1, 0),
         Spe2_2 = ifelse(Spe2 == 2, 1, 0),
         Spe2_3 = ifelse(Spe2 == 3, 1, 0),
         
         # Prediction (Pre_A and Pre_B)
         
         Pre1_2 = ifelse(Pre1 == 2, 1, 0), 
         Pre2_2 = ifelse(Pre2 == 2, 1, 0),
         
         
         # Cost (Cost_A and Cost_B)
         
         Cost1_2 = ifelse(Cost1 == 2, 1, 0),
         Cost1_3 = ifelse(Cost1 == 3, 1, 0),
         Cost1_4 = ifelse(Cost1 == 4, 1, 0),
         Cost2_2 = ifelse(Cost2 == 2, 1, 0),
         Cost2_3 = ifelse(Cost2 == 3, 1, 0),
         Cost2_4 = ifelse(Cost2 == 4, 1, 0)
  )


# View the data to check the dummy-coded variables
head(database)

### Vector of parameters, including any that are kept fixed in estimation
# Initial parameter values for 2 alternatives and 6 attributes
# Example with corrected parameters
apollo_beta <- c(
  asc_alt = 0,               # Alternative 1 and 2 (ASC)
  asc_alt3 = 0,               # Alternative 3 (ASC_optout)
  
  # Fasting Time for alternative 1 and alternative 2 (4 levels)
  b_fasting_time_2 = 0,  
  b_fasting_time_3 = 0,  
  b_fasting_time_4 = 0,  

  # Invasiveness for alternative 1 and alternative 2
  b_invasiveness_2 = 0,  

  # Sensitivity for alternative 1 and alternative 2 (3 levels)
  b_sensitivity_2 = 0,  
  b_sensitivity_3 = 0,   
  
  
  # Specificity for alternative 1 and alternative 2 (3 levels)
  b_specificity_2 = 0,  
  b_specificity_3 = 0,  
 
  # Prediction for alternative 1 and alternative 2
  b_prediction_2 = 0,  

  # Cost for alternative 1 and alternative 2 (4 levels)
  b_cost_2 = 0,
  b_cost_3 = 0,
  b_cost_4 = 0
)

apollo_fixed <- c("asc_alt")


# ################################################################# #
#### GROUP AND VALIDATE INPUTS                                   ####
# ################################################################# #

apollo_inputs = apollo_validateInputs()

# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION                        ####
# ################################################################# #
apollo_probabilities <- function(apollo_beta, apollo_inputs, functionality = "estimate") {
  
  ### Attach inputs and detach after function exit
  apollo_attach(apollo_beta, apollo_inputs)
  on.exit(apollo_detach(apollo_beta, apollo_inputs))
  
  ### Create list of probabilities P
  P <- list()
  
  ### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
  V <- list(
    # Utility function for alternative 1
    alt1 = asc_alt + 
      b_fasting_time_2 * (Fast1_2) +
      b_fasting_time_3 * (Fast1_3) +
      b_fasting_time_4 * (Fast1_4) +
      b_invasiveness_2 * (Inv1_2) +
      b_sensitivity_2 * (Sen1_2) + 
      b_sensitivity_3 * (Sen1_3) +
      b_specificity_2 * (Spe1_2) + 
      b_specificity_3 * (Spe1_3) +
      b_prediction_2 * (Pre1_2) +
      b_cost_2 * (Cost1_2) +
      b_cost_3 * (Cost1_3) +
      b_cost_4 * (Cost1_4),
    
    # Utility function for alternative 2
    alt2 = asc_alt + 
      b_fasting_time_2 * (Fast2_2) +
      b_fasting_time_3 * (Fast2_3) +
      b_fasting_time_4 * (Fast2_4) +
      b_invasiveness_2 * (Inv2_2) +
      b_sensitivity_2 * (Sen2_2) + 
      b_sensitivity_3 * (Sen2_3) +
      b_specificity_2 * (Spe2_2) + 
      b_specificity_3 * (Spe2_3) +
      b_prediction_2 * (Pre2_2) +
      b_cost_2 * (Cost2_2) +
      b_cost_3 * (Cost2_3) +
      b_cost_4 * (Cost2_4),
    
    alt3 = asc_alt3
    
  )
  
  
  ### Define settings for MNL model component
  mnl_settings <- list(
    alternatives = c(alt1 = 1, alt2 = 2, alt3=0),
    avail        = 1,  # Assuming all alternatives are available
    choiceVar    = Choice,  # Ensure 'Choice' column exists in your data
    utilities    = V
  )
  
  ### Compute probabilities using MNL model
  P[["model"]] <- apollo_mnl(mnl_settings, functionality)
  
  ### Take product across observations for the same individual if needed (for panel data)
  P <- apollo_panelProd(P, apollo_inputs, functionality)
  
  ### Prepare and return outputs of the function (final preparation)
  P <- apollo_prepareProb(P, apollo_inputs, functionality)
  
  return(P)
}

# ################################################################# #
#### MODEL ESTIMATION                                            ####
# ################################################################# 

# Estimating the model
model <- apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
# ################################################################# #
#### MODEL OUTPUTS                                               ####
# ################################################################# #

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN)                               ----
# ----------------------------------------------------------------- #

apollo_modelOutput(model,modelOutput_settings = list(printClassical=FALSE))

apollo_saveOutput(model)

####Effect Coded#####
[code]### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
library(dplyr)
### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "DCE3",
  modelDescr      = "MNL model",
  indivID         = "ID", 
  nAlternatives   = 2,  # Number of alternatives
  alternatives    = c(1, 2),  # Define alternative IDs
  outputDirectory = "output"
)

# Load the data
database <- read.csv("DCE3_after_removal_modified_withcovariates_1.csv")
database

######
# Effect coding (Consistent)
database <- database %>%
  mutate(
    Fast1_1 = ifelse(Fast1 == 1, 1, ifelse(Fast1 == 4, -1, 0)),
    Fast1_2 = ifelse(Fast1 == 2, 1, ifelse(Fast1 == 4, -1, 0)),
    Fast1_3 = ifelse(Fast1 == 3, 1, ifelse(Fast1 == 4, -1, 0)),
    Fast2_1 = ifelse(Fast2 == 1, 1, ifelse(Fast2 == 4, -1, 0)),
    Fast2_2 = ifelse(Fast2 == 2, 1, ifelse(Fast2 == 4, -1, 0)),
    Fast2_3 = ifelse(Fast2 == 3, 1, ifelse(Fast2 == 4, -1, 0)),
    
    Inv1_1 = ifelse(Inv1 == 1, 1, -1),
    Inv2_1 = ifelse(Inv2 == 1, 1, -1),
    
    Sen1_1 = ifelse(Sen1 == 1, 1, ifelse(Sen1 == 3, -1, 0)),
    Sen1_2 = ifelse(Sen1 == 2, 1, ifelse(Sen1 == 3, -1, 0)),
    Sen2_1 = ifelse(Sen2 == 1, 1, ifelse(Sen2 == 3, -1, 0)),
    Sen2_2 = ifelse(Sen2 == 2, 1, ifelse(Sen2 == 3, -1, 0)),
    
    Spe1_1 = ifelse(Spe1 == 1, 1, ifelse(Spe1 == 3, -1, 0)),
    Spe1_2 = ifelse(Spe1 == 2, 1, ifelse(Spe1 == 3, -1, 0)),
    Spe2_1 = ifelse(Spe2 == 1, 1, ifelse(Spe2 == 3, -1, 0)),
    Spe2_2 = ifelse(Spe2 == 2, 1, ifelse(Spe2 == 3, -1, 0)),
    
    Pre1_1 = ifelse(Pre1 == 1, 1, ifelse(Pre1 == 2, -1, 0)),
    Pre2_1 = ifelse(Pre2 == 1, 1, ifelse(Pre2 == 2, -1, 0)),
    
    Cost1_1 = ifelse(Cost1 == 1, 1, ifelse(Cost1 == 4, -1, 0)),
    Cost1_2 = ifelse(Cost1 == 2, 1, ifelse(Cost1 == 4, -1, 0)),
    Cost1_3 = ifelse(Cost1 == 3, 1, ifelse(Cost1 == 4, -1, 0)),
    Cost2_1 = ifelse(Cost2 == 1, 1, ifelse(Cost2 == 4, -1, 0)),
    Cost2_2 = ifelse(Cost2 == 2, 1, ifelse(Cost2 == 4, -1, 0)),
    Cost2_3 = ifelse(Cost2 == 3, 1, ifelse(Cost2 == 4, -1, 0))
  )
view(database)
write.csv(database, "DCE3_effect_coded.csv", row.names = FALSE)

# Apollo setup (as before)
apollo_beta <- c(
  asc_alt = 0,
  asc_alt3 = 0,  # Opt-out ASC
  
  b_Fast_1 = 0,
  b_Fast_2 = 0,
  b_Fast_3 = 0,
  
  b_Inv_1 = 0,
  
  b_Sen_1 = 0,
  b_Sen_2 = 0,
  
  b_Spe_1 = 0,
  b_Spe_2 = 0,
  
  b_Pre_1 = 0,
  
  b_Cost_1 = 0,
  b_Cost_2 = 0,
  b_Cost_3 = 0
)

apollo_fixed <- c("asc_alt3") #Fixing opt-out ASC

# ################################################################# #
#### GROUP AND VALIDATE INPUTS                                   ####
# ################################################################# #

apollo_inputs = apollo_validateInputs()

# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION                        ####
# ################################################################# #


apollo_probabilities <- function(apollo_beta, apollo_inputs, functionality = "estimate") {
  
  ### Attach inputs and detach after function exit
  apollo_attach(apollo_beta, apollo_inputs)
  on.exit(apollo_detach(apollo_beta, apollo_inputs))
  
  ### Create list of probabilities P
  P <- list()
  
  ### Effects coding constraint
  b_Fast_4 = - b_Fast_1 - b_Fast_2- b_Fast_3
  b_Inv_2 =  - b_Inv_1
  b_Sen_3 =  - b_Sen_1 - b_Sen_2
  b_Spe_3 =  - b_Spe_1 - b_Spe_2
  b_Pre_2 =  - b_Pre_1
  b_Cost_4 = -b_Cost_1 -b_Cost_2 -b_Cost_3
  
  
  ### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
  
  V <- list(
    alt1 = asc_alt +
      b_Fast_1 * Fast1_1  + b_Fast_2 * Fast1_2 + b_Fast_3 * Fast1_3  + 
      b_Inv_1 * Inv1_1  + 
      b_Sen_1 * Sen1_1 + b_Sen_2 * Sen1_2 + 
      b_Spe_1 * Spe1_1 + b_Spe_2 * Spe1_2 +
      b_Pre_1 * Pre1_1 +
      b_Cost_1 * Cost1_1 + b_Cost_2 * Cost1_2 + b_Cost_3 * Cost1_3 ,
    
    alt2 = asc_alt +
      b_Fast_1 * Fast2_1 + b_Fast_2 * Fast2_2 + b_Fast_3 * Fast2_3 + 
      b_Inv_1 * Inv2_1  +
      b_Sen_1 * Sen2_1 + b_Sen_2 * Sen2_2 +
      b_Spe_1 * Spe2_1 + b_Spe_2 * Spe2_2 + 
      b_Pre_1 * Pre2_1 +
      b_Cost_1 * Cost2_1 + b_Cost_2 * Cost2_2 + b_Cost_3 * Cost2_3,
    
    alt3 = asc_alt3 # Opt-out utility
  )
  
  
  ### Define settings for MNL model component
  mnl_settings <- list(
    alternatives = c(alt1 = 1, alt2 = 2, alt3=0),
    avail        = 1,  # Assuming all alternatives are available
    choiceVar    = Choice,  # Ensure 'Choice' column exists in your data
    utilities    = V
  )
  
  ### Compute probabilities using MNL model
  P[["model"]] <- apollo_mnl(mnl_settings, functionality)
  
  ### Take product across observations for the same individual if needed (for panel data)
  P <- apollo_panelProd(P, apollo_inputs, functionality)
  
  ### Prepare and return outputs of the function (final preparation)
  P <- apollo_prepareProb(P, apollo_inputs, functionality)
  
  return(P)
}

# ################################################################# #
#### MODEL ESTIMATION                                            ####
# ################################################################# 

# Estimating the model
model <- apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
# ################################################################# #
#### MODEL OUTPUTS                                               ####
# ################################################################# #

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN)                               ----
# ----------------------------------------------------------------- #

apollo_modelOutput(model,modelOutput_settings = list(printClassical=TRUE, printPVal =2))

apollo_saveOutput(model)

# ################################################################# #
##### POST-PROCESSING                                            ####
# ################################################################# #

### Print outputs of additional diagnostics to new output file (remember to close file writing when complete)
apollo_sink()

### calculate value and standard error for base of effects coded parameter
apollo_deltaMethod(model,deltaMethod_settings = list(expression=c(b_Fast_4 = "- b_Fast_1 - b_Fast_2- b_Fast_3",
                                                                  b_Inv_2 =  "- b_Inv_1",
                                                                  b_Sen_3 =  "- b_Sen_1 - b_Sen_2",
                                                                  b_Spe_3 =  "- b_Spe_1 - b_Spe_2",
                                                                  b_Pre_2 =  "- b_Pre_1",
                                                                  b_Cost_4 = "-b_Cost_1 -b_Cost_2 -b_Cost_3"
)))

Mixed Logit model

Code: Select all

###EFFECT CODE- mixed logit####
#########OPTOUT#################################################
rm(list = ls())

### Load Apollo library
library(apollo)
library(tidyverse)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "MMNL_DCE3_uncorrelated_effect",
  modelDescr      = "Mixed logit model on DCE3 data",
  indivID         = "ID",  
  nCores          = 4,
  seed            = 2025,
  outputDirectory = "output"
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS                     ####
# ################################################################# #
### Loading data
database <- read.csv("DCE3_after_removal_modified_withoutcovariates_1.csv")

# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta <- c(asc =0,
  asc_3 = 0,
  mu_b_Fast_2= 0,
  mu_b_Fast_3= 0,
  mu_b_Fast_4= 0,
  mu_b_Inv_2 = 0,
  mu_b_Sen_2 = 0, 
  mu_b_Sen_3 = 0,
  mu_b_Spe_2 = 0, 
  mu_b_Spe_3 = 0,
  mu_b_Pre_2 = 0, 
  mu_b_Cost_2 = 0,
  mu_b_Cost_3 = 0,
  mu_b_Cost_4 = 0,
  
  # Standard deviation parameters (sigma) - small positive values 
  sigma_b_Fast_2     = 0,
  sigma_b_Fast_3     = 0,
  sigma_b_Fast_4     = 0,
  sigma_b_Inv_2      = 0,
  sigma_b_Sen_2      = 0, 
  sigma_b_Sen_3      = 0, 
  sigma_b_Spe_2      = 0, 
  sigma_b_Spe_3      = 0,
  sigma_b_Pre_2      = 0,
  sigma_b_Cost_2 = 0.01,
  sigma_b_Cost_3 = 0.01, 
  sigma_b_Cost_4 = 0.01
)
apollo_fixed <- c("asc")


# ################################################################# #
#### DEFINE RANDOM COMPONENTS                                    ####
# ################################################################# #

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "mlhs", #distribution is across people rather than across choice for the same person
  interNDraws    = 1000,
  interUnifDraws = c(),
  interNormDraws = c("draws_Fast_2","draws_Fast_3","draws_Fast_4",
                     "draws_Inv_2",
                     "draws_Sen_2","draws_Sen_3",
                     "draws_Spe_2","draws_Spe_3", 
                     "draws_Pre_2", 
                     "draws_Cost_2","draws_Cost_3","draws_Cost_4")
)


### Create random parameter

  apollo_randCoeff = function(apollo_beta, apollo_inputs){
    randcoeff = list()
    #(mean + std dev * std normal variable)
    randcoeff[["b_Fast_2"]] =  mu_b_Fast_2  + sigma_b_Fast_2 * draws_Fast_2
    randcoeff[["b_Fast_3"]] =  mu_b_Fast_3  + sigma_b_Fast_3 * draws_Fast_3
    randcoeff[["b_Fast_4"]] =  mu_b_Fast_4  + sigma_b_Fast_4 * draws_Fast_4 
   
  
  randcoeff[["b_Inv_2"]]  =  mu_b_Inv_2   + sigma_b_Inv_2  * draws_Inv_2
  
  
  randcoeff[["b_Sen_2"]]  =  mu_b_Sen_2   + sigma_b_Sen_2  * draws_Sen_2 
  randcoeff[["b_Sen_3"]]  =  mu_b_Sen_3   + sigma_b_Sen_3  * draws_Sen_3 
 
  
  randcoeff[["b_Spe_2"]]  =  mu_b_Spe_2   + sigma_b_Spe_2  * draws_Spe_2 
  randcoeff[["b_Spe_3"]]  =  mu_b_Spe_3   + sigma_b_Spe_3  * draws_Spe_3 

  
  randcoeff[["b_Pre_2"]]  =  mu_b_Pre_2   + sigma_b_Pre_2 * draws_Pre_2 
 
  
  randcoeff[["b_Cost_2"]] = mu_b_Cost_2  + sigma_b_Cost_2 * draws_Cost_2
  randcoeff[["b_Cost_3"]] = mu_b_Cost_3  + sigma_b_Cost_3 * draws_Cost_3
  randcoeff[["b_Cost_4"]] = mu_b_Cost_4  + sigma_b_Cost_4 * draws_Cost_4
  
  
  return(randcoeff)
}


# ################################################################# #
#### GROUP AND VALIDATE INPUTS                                   ####
# ################################################################# #

apollo_inputs = apollo_validateInputs()

# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION                        ####
# ################################################################# #

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
  
  ### Function initialisation: do not change the following three commands
  ### Attach inputs and detach after function exit
  apollo_attach(apollo_beta, apollo_inputs)
  on.exit(apollo_detach(apollo_beta, apollo_inputs))
  
  ### Create list of probabilities P
  P = list()
  ### Effects coding constraint
  b_Fast_1  = -b_Fast_2 -b_Fast_3 - b_Fast_4  
  b_Inv_1   = -b_Inv_2
  b_Sen_1   = -b_Sen_2 - b_Sen_3
  b_Spe_1   = -b_Spe_2- b_Spe_3
  b_Pre_1   = -b_Pre_2
  b_Cost_1  = -b_Cost_2 - b_Cost_3 - b_Cost_4
  
  ### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
  
  V <- list(
    alt1 = asc +
      b_Fast_1 * (Fast1 ==1) + b_Fast_2 * (Fast1==2) + b_Fast_3 * (Fast1 == 3) +  b_Fast_4 * (Fast1 == 4) +
      b_Inv_1 * (Inv1==1) + b_Inv_2* (Inv1==2)+
      b_Sen_1 * (Sen1 == 1) + b_Sen_2 * (Sen1==2) +  b_Sen_3 * (Sen1==3)+
      b_Spe_1 * (Spe1==1) + b_Spe_2 * (Spe1==2) + b_Spe_3 * (Spe1==3) +
      b_Pre_1 * (Pre1==1) + b_Pre_2 * (Pre1==2)+
      b_Cost_1 * (Cost1==1) + b_Cost_2 * (Cost1==2) + b_Cost_3 * (Cost1==3)+ b_Cost_4 * (Cost1==4),
    
    alt2 = asc +
      b_Fast_1 * (Fast2==1) + b_Fast_2 * (Fast2==2) + b_Fast_3 * (Fast2==3) + b_Fast_4 * (Fast2==4)+
      b_Inv_1 * (Inv2==1)  + b_Inv_2 * (Inv2==2) +
      b_Sen_1 * (Sen2==1) + b_Sen_2 * (Sen2==2)  + b_Sen_3 * (Sen2==3)+
      b_Spe_1 * (Spe2==1) + b_Spe_2 * (Spe2==2)  + b_Spe_3 * (Spe2==3)+
      b_Pre_1 * (Pre2==1)  +  b_Pre_2 * (Pre2==2)+
      b_Cost_1 * (Cost2==1) + b_Cost_2 * (Cost2==2) + b_Cost_3 * (Cost2==3) + b_Cost_4 * (Cost2==4),
    
    alt3 = asc_3
  )
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives  = c(alt1=1, alt2=2,alt3=0),
    avail         = list(alt1=1, alt2=1,alt3=1), 
    choiceVar     = Choice,
    utilities     = V
  )
  
  ### Compute probabilities using MNL model
  P[["model"]] = apollo_mnl(mnl_settings, functionality)
  
  
  ### Take product across observation for same individual(heterogeneity is across per person; how likely is the sequences of choices per person in each group)
  P = apollo_panelProd(P, apollo_inputs, functionality)
  
  ### Average across inter-individual draws
  P = apollo_avgInterDraws(P, apollo_inputs, functionality)
  
  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}

# ################################################################# #
#### MODEL ESTIMATION                                            ####
# ################################################################# #
model = apollo_estimate(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs,list(writeIter=FALSE,
                                                                                           computeVCov = TRUE, printClassical=TRUE ))

# ################################################################# #
#### MODEL OUTPUTS                                               ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN)                               ----
# ----------------------------------------------------------------- #
apollo_modelOutput (model, modelOutput_settings=list(printPVal = 2))

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name)               ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)

###POST PROCESSING#####
### Print outputs of additional diagnostics to new output file (remember to close file writing when complete)
apollo_sink()

### calculate value and standard error for base of effects coded parameter
delta<-apollo_deltaMethod(model,deltaMethod_settings = list(expression=c(
  mu_b_Fast_1    = "- mu_b_Fast_2 - mu_b_Fast_3 - mu_b_Fast_4",
  mu_b_Inv_1     =  "- mu_b_Inv_2",
  mu_b_Sen_1     =  "- mu_b_Sen_2 - mu_b_Sen_3",
  mu_b_Spe_1     =  "- mu_b_Spe_2 - mu_b_Spe_3",
  mu_b_Pre_1     =  "- mu_b_Pre_2",
  mu_b_Cost_1    = "-mu_b_Cost_2 -mu_b_Cost_3 -mu_b_Cost_4",
  sigma_b_Fast_1 = "- sigma_b_Fast_2 - sigma_b_Fast_3 - sigma_b_Fast_4",
  sigma_b_Inv_1  = "- sigma_b_Inv_2",
  sigma_b_Sen_1  = "- sigma_b_Sen_2 - sigma_b_Sen_3",
  sigma_b_Spe_1  = "- sigma_b_Spe_2 -sigma_b_Spe_3",
  sigma_b_Pre_1  = "- sigma_b_Pre_2",
  sigma_b_Cost_1 = "-sigma_b_Cost_2 -sigma_b_Cost_3 -sigma_b_Cost_4"
)))
print(delta)


# ----------------------------------------------------------------- #
#---- switch off writing to file                                 ----
# ----------------------------------------------------------------- #

apollo_sink()


######DUMMY -mixed logit#########
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS                       ####
# ################################################################# #

### Clear memory
rm(list = ls())

### Load Apollo library
library(apollo)
library(tidyverse)

### Initialise code
apollo_initialise()

### Set core controls

apollo_control = list(
  modelName       = "MMNL_DCE3_uncorrelated_dummy",
  modelDescr      = "Mixed logit model on DCE3 data, uncorrelated__Normal",
  indivID         = "ID",  
  nCores          = 4,
  seed            = 2025,
  outputDirectory = "output"
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS                     ####
# ################################################################# #

### Loading data
database = read.csv("DCE3_after_removal_modified_withoutcovariates_1.csv",header=TRUE)

# View the data to check the dummy-coded variables
head(database)

###################################################################
#### DEFINE MODEL PARAMETERS                                     ####
###################################################################
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc =0,
  asc_3             = 0,
  b_Fast_1          = 0,
  mu_b_Fast_2       = 0,
  mu_b_Fast_3       = 0,
  mu_b_Fast_4       = 0,
  b_Inv_1           = 0,
  mu_b_Inv_2        = 0,
  b_Sen_1           = 0,
  mu_b_Sen_2        = 0,
  mu_b_Sen_3        = 0,
  b_Spe_1           = 0,
  mu_b_Spe_2        = 0,
  mu_b_Spe_3        = 0,
  b_Pre_1           = 0,
  mu_b_Pre_2        = 0,
  b_Cost_1          = 0,
  mu_log_b_Cost_2   = 0, 
  mu_log_b_Cost_3   = 0, 
  mu_log_b_Cost_4   = 0, 
  
  
  sigma_b_Fast_2     = 0,
  sigma_b_Fast_3     = 0,
  sigma_b_Fast_4     = 0,
  sigma_b_Inv_2      = 0,
  sigma_b_Sen_2      = 0,
  sigma_b_Sen_3      = 0,
  sigma_b_Spe_2      = 0,
  sigma_b_Spe_3      = 0,
  sigma_b_Pre_2      = 0,
  sigma_log_b_Cost_2 = 0.01, 
  sigma_log_b_Cost_3 = 0.01, 
  sigma_log_b_Cost_4 = 0.01
)

### Vector with names (in quotes) of parameters to be kept fixed at their starting value in apollo_beta, use apollo_beta_fixed = c() if none
apollo_fixed = c("asc","b_Fast_1", "b_Inv_1", "b_Sen_1", "b_Spe_1", "b_Pre_1", "b_Cost_1")

# ################################################################# #
#### DEFINE RANDOM COMPONENTS                                    ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "mlhs", #distribution is across people rather than across choice for the same person
  interNDraws    = 1000, 
  interUnifDraws = c(),
  interNormDraws = c("draws_Fast_2","draws_Fast_3","draws_Fast_4",
                     "draws_Inv_2",
                     "draws_Sen_2","draws_Sen_3",
                     "draws_Spe_2","draws_Spe_3", 
                     "draws_Pre_2", 
                     "draws_Cost_2","draws_Cost_3", "draws_Cost_4"
  ),
  intraDrawsType = "mlhs",
  intraNDraws    = 0,
  intraUnifDraws = c(),
  intraNormDraws = c()
)


### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  #(mean + std dev * std normal variable)
  randcoeff[["b_Fast_2"]] =  mu_b_Fast_2  + sigma_b_Fast_2 * draws_Fast_2
  randcoeff[["b_Fast_3"]] =  mu_b_Fast_3  + sigma_b_Fast_3 * draws_Fast_3
  randcoeff[["b_Fast_4"]] =  mu_b_Fast_4  + sigma_b_Fast_4 * draws_Fast_4 
  
  randcoeff[["b_Inv_2"]]  =  mu_b_Inv_2   + sigma_b_Inv_2  * draws_Inv_2 
  
  randcoeff[["b_Sen_2"]]  =  mu_b_Sen_2   + sigma_b_Sen_2  * draws_Sen_2 
  randcoeff[["b_Sen_3"]]  =  mu_b_Sen_3   + sigma_b_Sen_3  * draws_Sen_3 
  
  randcoeff[["b_Spe_2"]]  =  mu_b_Spe_2   + sigma_b_Spe_2  * draws_Spe_2 
  randcoeff[["b_Spe_3"]]  =  mu_b_Spe_3   + sigma_b_Spe_3  * draws_Spe_3 
  
  randcoeff[["b_Pre_2"]]  =  mu_b_Pre_2   + sigma_b_Pre_2  * draws_Pre_2 
  
  randcoeff[["b_Cost_2"]] =  mu_log_b_Cost_2 + sigma_log_b_Cost_2*draws_Cost_2 
  randcoeff[["b_Cost_3"]] =  mu_log_b_Cost_3 + sigma_log_b_Cost_3*draws_Cost_3 
  randcoeff[["b_Cost_4"]] =  mu_log_b_Cost_4 + sigma_log_b_Cost_4*draws_Cost_4 
  return(randcoeff)
}

# ################################################################# #
#### GROUP AND VALIDATE INPUTS                                   ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()

# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION                        ####
# ################################################################# #
apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
  
  ### Function initialisation: do not change the following three commands
  ### Attach inputs and detach after function exit
  apollo_attach(apollo_beta, apollo_inputs)
  on.exit(apollo_detach(apollo_beta, apollo_inputs))
  
  ### Create list of probabilities P
  P = list()
  
  ### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
  V <- list(
    alt1 =  asc +
      b_Fast_1*(Fast1==1) + b_Fast_2*(Fast1==2) + b_Fast_3* (Fast1 == 3) + b_Fast_4 * (Fast1 == 4)+
      b_Inv_1*(Inv1==1)   + b_Inv_2 * (Inv1 ==2)+ 
      b_Sen_1*(Sen1==1)   + b_Sen_2 * (Sen1==2) + b_Sen_3 * (Sen1==3) +
      b_Spe_1*(Spe1==1)   + b_Spe_2 * (Spe1==2) + b_Spe_3 * (Spe1==3) + 
      b_Pre_1*(Pre1==1)   + b_Pre_2 * (Pre1==2) +
      b_Cost_1*(Cost1==1) + b_Cost_2* (Cost1==2)+ b_Cost_3* (Cost1==3) + b_Cost_4 * (Cost1==4) ,
    
    alt2 = asc +
      b_Fast_1*(Fast2==1) + b_Fast_2* (Fast2==2)+ b_Fast_3* (Fast2==3) + b_Fast_4 * (Fast2==4)+
      b_Inv_1*(Inv2==1)   + b_Inv_2 * (Inv2==2) +
      b_Sen_1*(Sen2==1)   + b_Sen_2 * (Sen2==2) + b_Sen_3 * (Sen2==3) +
      b_Spe_1*(Spe2==1)   + b_Spe_2 * (Spe2==2) + b_Spe_3 * (Spe2==3) + 
      b_Pre_1*(Pre2==1)   + b_Pre_2 * (Pre2==2) +
      b_Cost_1*(Cost2==1) + b_Cost_2* (Cost2==2)+ b_Cost_3* (Cost2==3) + b_Cost_4 * (Cost2==4),
    
    alt3 = asc_3
  )

  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives  = c(alt1=1, alt2=2, alt3=0),
    avail         = list(alt1=1, alt2=1, alt3=1), 
    choiceVar     = Choice,
    utilities     = V
  )
  
  ### Compute probabilities using MNL model
  P[["model"]] = apollo_mnl(mnl_settings, functionality)
  
  ### Take product across observation for same individual(heterogeneity is across per person; how likely is the sequences of choices per person in each group)
  P = apollo_panelProd(P, apollo_inputs, functionality)
  
  ### Average across inter-individual draws
  P = apollo_avgInterDraws(P, apollo_inputs, functionality)
  
  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)    
  return(P)
}

# ################################################################# #
#### MODEL ESTIMATION                                            ####
# ################################################################# #
model = apollo_estimate(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)

# ################################################################# #
#### MODEL OUTPUTS                                               ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN)                               ----
# ----------------------------------------------------------------- #
#apollo_modelOutput(model,modelOutput_settings = list(printPVal = 2))
apollo_modelOutput(model)
I hope I can get some clarity about this.

Regards,
Asmita
Last edited by apk0022 on 11 Apr 2025, 12:01, edited 1 time in total.
dpalma
Posts: 215
Joined: 24 Apr 2020, 17:54

Re: Dummy and effect coding result for heterogeneity (sigma)

Post by dpalma »

Hi Asmita,

First, I would like to say that I do not have much experience with effects coding. Personally, I always use dummy coding, so I will try to answer the best I can. Secondly, I want to highlight that identification of mixed mnl models with categorical variables is not simple, as the choice of base (at least in the case of dummy coding) not being arbitrary: you should use the level with the smallest variance (Walker 2002) or use a fully correlated coefficients (Burton 2019).

About your first question, i.e. why the significance of s.d. of effects and dummy coding are different in a mixed mnl, I don't see why they should be. Both approaches have a different base. In the dummy approach you are comparing the effect of a level against the base level, including its s.d. So if you change the base, the s.d. of the base will change as well, and so the s.d. of the other levels will change. In the case of effects coding, the base is the mean, so that will have again another s.d., so the s.d. estimates of the other effects will again change.

About your second question, regarding why the (robust) s.e. of dummy and effect coding in MNL models differ, I can only offer an educated guess. I would imagine that classical s.e. would be similar across mnl models with effects and dummy coding, but the calculation of the robust s.e. depends on the variance of the scores matrix, which itself depends on the gradient for each individual. The gradient at the optimum will change depending on the specification, and therefore, it would be reasonable to think that the robust s.e. would change as well.

Sorry for not being more helpful at this point. I will come back to this if I find out something new. But please note that these are mainly theoretical topics, more than technical issues with the software.

Best wishes,
David
stephanehess
Site Admin
Posts: 1264
Joined: 24 Apr 2020, 16:29

Re: Dummy and effect coding result for heterogeneity (sigma)

Post by stephanehess »

Hi

just to add to this.

In relation to the MNL results, if you compute the standard errors for differences across levels, you will get the same estimates and same std errors for dummy and effects coding. You simply cannot compare the estimates that you obtain as they relate to different normalisations, only differences in utility matter, and they will be the same with the two approaches.

The same applies to the MMNL results. In dummy coding, you get the std dev (i.e. the heterogeneity) for a specific level compares to the base level. In the effects coded results, that's not the case, you first need to calculate the differences between the level and the base level and then use the delta method for the std err. And the results should again be the same. If you simply compare the estimates, then it is not surprising that they are different, also in terms of heterogeneity.

Overall, there is no benefit to you using effects coding here, it just complicates your calculations

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply