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.

Strong differences in WTP and utility space estimations using mixed logit model

Ask questions about the results reported after estimation. If the output includes errors, please include your model code if possible.
Post Reply
mfritschle
Posts: 3
Joined: 18 May 2023, 13:14

Strong differences in WTP and utility space estimations using mixed logit model

Post by mfritschle »

Hi all,
I estimate a mixed logit model in WTP space with uncorrelated random parameters. Survey respondents could choose between two program alternatives and a status quo. The program options have three attributes, including a payment attribute (which how much respondent would be paid in compensation for participating in the programme described by the other attributes).
Some information about the model in WTP space:
  • I specify normal distributions for the non-cost attributes and a positive log-normal for the payment attribute.
  • The payment attribute is continuous; the other three contract attributes are dummy-coded. In addition, an ASC is included (normally distributed).
    • Organizer: Government (ref. level, omitted), Retailer (level 1), NGO (level 2).
    • Compensation: Transfer (ref. level, omitted), Label (level 1)
    • FC: No FC (ref. level, omitted), FC optional (level 1), FC required (level 2).
  • We use Sobol draws, as we are using more than five random coefficients (cf. Bhat, 2003), with 3000 inter draws (recommended by Czajkowski & Budzinski)
The transformation of the CL model in the WTP space works as planned. The LL and AIC are the same in utility and WTP space. The signs of the WTP estimations are in line with utility space estimations.
However, transforming the MXL in preference space into WTP space leads to issues.

Code: Select all

V = list()
V[['A']]  = b_cost * (b_asc + b_retailer * retailer_a + b_ngo * ngo_a + b_label * label_a + b_optional * optional_a + b_required * required_a + cost_a)
V[['B']]  = b_cost * (b_asc + b_retailer * retailer_b + b_ngo * ngo_b + b_label * label_b + b_optional * optional_b + b_required * required_b + cost_b)
V[['C']]  = 0
The results for the MXL in WTP-space strongly differ strongly from the initial utility-space MXL estimations, in sizes, the sign, and significance level (p-value & t-statistic) We conducted different approaches, which have not led to a result close to the utility space estimations:
  • tested different prior values (from the CL in utility-space, CL in WTP-space, MXL in utility-space)
  • we varied the position of the ASC in the utility function (V). In other words, we tried to conduct the WTP transformation with the ASC inside the brackets and outside the brackets.
    • ASC inside the brackets:

      Code: Select all

      V[['A']]  = b_cost * (b_asc + b_retailer * retailer_a + ...
    • ASC outside the brackets:

      Code: Select all

      V[['A']]  = b_asc + b_cost * (b_retailer * retailer_a + ...
    though we are interested in estimating the WTP associated with the ASC which represents the constant/basic payment respondents would need to receive to move from the status quo to a baseline programme.
  • testing different distributions for the ASC parameter (uniform or normal) and payment parameters (non-random, uniform, normal)
  • using the EM algorithm
  • considering random or fixed cost parameters.
The results are still very different from those of the utility space. Do you have an idea of why we could be obtaining results that are so different in WTP-space compared to utility space?

Below, I attached the code we used in R.

Thank you very much, and best regards,
Moritz

Code: Select all

##############################################################################################
####  MXL 1 w.o. interactions (WTP Space) (random cost parameter) WTP CL priors - EM algo ####
##############################################################################################
{
  ### Clear memory 
  rm(list = ls())
  #to check directory (folder where we are working)
  getwd()
  # to change working directory
  setwd("xxxxxxxxx")
  # Load Apollo library
  library(apollo)
  # Load tidyr library
  library(tidyr)
  # Load readxl library
  library(readxl)
  
  ### Initialise code
  apollo_initialise()
  
  ### Set core controls
  apollo_control = list(
    modelName  ="MXL1model_Paper4_WTP_Space_randomcost_CLL_WPT_priors_EM",
    modelDescr ="MXL model in WTP Space for Farmersurvey data (not correlated random parameters) + random cost parameter WTP CL priors + EM algorithm",
    indivID    ="id",
    mixing     = TRUE,
    nCores     = 6
  )
  # We apply the EM algorithm as suggested by Stephane Hess (https://www.apollochoicemodelling.com/forum/viewtopic.php?t=318)
  
  # ################################################################# #
  #### LOAD DATA AND APPLY ANY TRANSFORMATIONS                     ####
  # ################################################################# #
  
  library(haven)
  database <- read.csv("Data_UKNLFR_mf_.csv", header = TRUE)
  
  # ################################################################# #
  #### DEFINE MODEL PARAMETERS                                     ####
  # ################################################################# #
  
  ### Vector of parameters, including any that are kept fixed in estimation
  
  apollo_beta = c(mu_asc                        =  -599.410,
                  mu_b_retailer                 =  - 66.940, 
                  mu_b_ngo                      =  - 68.540, 
                  mu_b_label                    =  -119.230, 
                  mu_b_optional                 =    95.270, 
                  mu_b_required                 =  - 35.610, 
                  mu_log_b_cost                 =  -  5,
                  
                  sigma_asc                     =   0, 
                  sigma_b_retailer              =   0,
                  sigma_b_ngo                   =   0,
                  sigma_b_label                 =   0,
                  sigma_b_optional              =   0,
                  sigma_b_required              =   0,
                  sigma_log_b_cost              =   -2)
  
  
  ### 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() # maybe interesting for the latent class model. (independent availability model)
  
  # ################################################################# #
  #### DEFINE RANDOM COMPONENTS                                    ####
  # ################################################################# #
  ### Set parameters for generating draws
  apollo_draws = list(
    interDrawsType = "sobol", # Halton Draws are not recommended for more than five random coefficents (cf. Bhat, 2003). For computing Bayesian efficiency pseudo random draws or pseudo monte carlo are the simplest (ChoiceMetrics, 2018).
    interNDraws    = 3000,    # > 2000 Sobol Draws recommendet by Czajkowski & Budzinski 2019
    interUnifDraws = c("draws_asc"),
    interNormDraws = c("draws_retailer","draws_ngo","draws_label","draws_optional","draws_required","draws_cost")
  )
  
  ### Create random parameters
  apollo_randCoeff = function(apollo_beta, apollo_inputs){
    randcoeff = list()
    
    randcoeff[["b_asc"]]       =     ( mu_asc         + sigma_asc         * draws_asc )
    randcoeff[["b_retailer"]]  =     ( mu_b_retailer  + sigma_b_retailer  * draws_retailer )
    randcoeff[["b_ngo"]]       =     ( mu_b_ngo       + sigma_b_ngo       * draws_ngo )
    randcoeff[["b_label"]]     =     ( mu_b_label     + sigma_b_label     * draws_label )
    randcoeff[["b_optional"]]  =     ( mu_b_optional  + sigma_b_optional  * draws_optional )
    randcoeff[["b_required"]]  =     ( mu_b_required  + sigma_b_required  * draws_required)
    randcoeff[["b_cost"]]      =  exp( mu_log_b_cost  + sigma_log_b_cost  * draws_cost )
    
    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 initialization: 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()
    V[['A']]  = b_cost * (b_asc + b_retailer * retailer_a + b_ngo * ngo_a + b_label * label_a + b_optional * optional_a + b_required * required_a + cost_a)
    V[['B']]  = b_cost * (b_asc + b_retailer * retailer_b + b_ngo * ngo_b + b_label * label_b + b_optional * optional_b + b_required * required_b + cost_b)
    V[['C']]  = 0
    
    
    ### Define settings for MNL model component
    mnl_settings = list(
      alternatives  = c(A=1, B=2, C=3), 
      avail         = 1,
      choiceVar     = decision, # depend on the variable used in stata
      V             = V
    )
    
    ### Compute probabilities using MNL model
    P[['model']] = apollo_mnl(mnl_settings, functionality)
    
    ### Take product across observation for same individual
    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)
  }
  
  # ################################################################# #
  #### EM ESTIMATION                                               ####
  # ################################################################# #
  
  mixEM_settings = list(transforms=list(function(x) log(-x), 
                                        function(x) log(-x), 
                                        function(x) log(-x), 
                                        function(x) log(-x)))
  model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs, mixEM_settings)
  
  # ################################################################# #
  #### MODEL OUTPUTS                                               ####
  # ################################################################# #
  
  # ----------------------------------------------------------------- #
  #---- FORMATTED OUTPUT (TO SCREEN)                               ----
  # ----------------------------------------------------------------- #
  
  apollo_modelOutput(model, list(printPVal = 2))
  
  # ----------------------------------------------------------------- #
  #---- FORMATTED OUTPUT (TO FILE, using model name)               ----
  # ----------------------------------------------------------------- #
  apollo_saveOutput(model, list(printPVal = 2))
  
  #################################
  # Cost Parameter Transformation #
  #################################
  # transform the cost parameter. Look up into the Forum follow link to the Forum Post: use method suggested by D Palma : http://www.apollochoicemodelling.com/forum/viewtopic.php?f=16&t=262
  # sigma is the standard deviation,  mu gives average preference.
  apollo_deltaMethod(model, deltaMethod_settings = list(operation="lognormal",parName1="mu_log_b_cost",parName2="sigma_log_b_cost"))
  
}
dpalma
Posts: 217
Joined: 24 Apr 2020, 17:54

Re: Strong differences in WTP and utility space estimations using mixed logit model

Post by dpalma »

Hi Moritz,

There could be a number of issues, but my first guess has to do with identification. According to Walker (2002) and Burton (2019) using random coefficients for categorical variables is not straightforward. They recommend using one of two approaches.
  • Select the base level of the categorical variable arbitrarily, but use correlated random coefficients for ALL parameters in the model, not only the categorical variable levels. So you would have to estimate a full covariance matrix.
  • First estimate an over-specified model with random coefficients for all levels of the categorical variable, and all alternative specific constants. Then identify the level with the smallest estimated standard deviation. Then estimate a new model with random coefficients using the previously identified level as base (so no coefficient for that level). You do the same to chose what ASC you use as reference.
Please try these (one at a time, not both at the same time) and let us know if things improve.

Best wishes,
David
mfritschle
Posts: 3
Joined: 18 May 2023, 13:14

Re: Strong differences in WTP and utility space estimations using mixed logit model

Post by mfritschle »

Dear David,

Thank you very much for your response to my post. I followed your suggestions and tested both approaches. However, with both approaches we still find important differences between the results in utility and WTP space.

1. Model using correlated random coefficients for all parameters

First, the significance levels between the coefficients in Mixed logit in the utility space and the WTP space differ. Differences can be found in the attributes: RETAILER, NGO, FC Optional, FC Required.

Second, the ASC estimator is negative and is in line with our assumption. However, when we transform it into WTP space, the ASC turns positive and indicates the opposite than expected.

MXL Output in utility space - correlated random coefficients

Code: Select all

Model name                                  : MXL1model_Paper4_utility_palma
Model description                           : MXL model in Utility Space paper 4 - palma method - allCorrelations
Model run at                                : 2025-04-09 11:56:19.698261
Estimation method                           : bgw
Model diagnosis                             : Relative function convergence
Optimisation diagnosis                      : Maximum found
     hessian properties                     : Negative definite
     maximum eigenvalue                     : -0.967576
     reciprocal of condition number         : 0.0010795
Number of individuals                       : 761
Number of rows in database                  : 4552
Number of modelled outcomes                 : 4552

Number of cores used                        :  1 
Number of inter-individual draws            : 3000 (sobol)

LL(start)                                   : -5197.32
LL at equal shares, LL(0)                   : -5000.88
LL at observed shares, LL(C)                : -4952.04
LL(final)                                   : -3338.48
Rho-squared vs equal shares                  :  0.3324 
Adj.Rho-squared vs equal shares              :  0.3254 
Rho-squared vs observed shares               :  0.3258 
Adj.Rho-squared vs observed shares           :  0.3192 
AIC                                         :  6746.96 
BIC                                         :  6971.78 

Estimated parameters                        : 35
Time taken (hh:mm:ss)                       :  03:34:7.4 
     pre-estimation                         :  00:01:40.36 
     estimation                             :  00:34:58.45 
          initial estimation                :  00:32:57.92 
          estimation after rescaling        :  00:02:0.53 
     post-estimation                        :  02:57:28.59 
Iterations                                  :  41  
     initial estimation                     :  40 
     estimation after rescaling             :  1 

Unconstrained optimisation.

Estimates:
                      Estimate        s.e.   t.rat.(0)  p(2-sided)    Rob.s.e. Rob.t.rat.(0)
mu_asc               -1.327853      0.2976    -4.46193   8.123e-06     0.31450      -4.22210
mu_b_retailer        -0.312645      0.1433    -2.18207    0.029104     0.15910      -1.96504
mu_b_ngo             -0.161782      0.1424    -1.13650    0.255749     0.15488      -1.04459
mu_b_label           -0.552768      0.1083    -5.10387   3.328e-07     0.11392      -4.85206
mu_b_optional         0.165075      0.1351     1.22228    0.221602     0.13575       1.21600
mu_b_required        -0.184409      0.1332    -1.38420    0.166299     0.13170      -1.40025
mu_log_b_cost        -7.380863      0.3379   -21.84131    0.000000     0.34427     -21.43931
sigma_asc            -5.644180      0.4058   -13.90855    0.000000     0.41772     -13.51197
sigma_b_retailer     -1.227868      0.1595    -7.69633   1.399e-14     0.19471      -6.30601
sigma_retail_asc     -0.076185      0.1740    -0.43786    0.661484     0.18824      -0.40472
sigma_b_ngo          -0.760999      0.1659    -4.58672   4.503e-06     0.17446      -4.36206
sigma_ngo_asc         0.091434      0.1679     0.54461    0.586021     0.16486       0.55460
sigma_ngo_retail     -0.745207      0.1828    -4.07731   4.556e-05     0.21916      -3.40027
sigma_b_label        -0.418650      0.2326    -1.80015    0.071837     0.34326      -1.21963
sigma_label_asc      -0.350258      0.1261    -2.77695    0.005487     0.13299      -2.63368
sigma_label_retail   -0.002217      0.1150    -0.01928    0.984621     0.11852      -0.01870
sigma_label_ngo       0.143367      0.2348     0.61067    0.541415     0.33653       0.42602
sigma_b_optional      0.435506      0.7237     0.60180    0.547309     1.13013       0.38536
sigma_opt_asc        -0.013406      0.1682    -0.07970    0.936472     0.17664      -0.07590
sigma_opt_retail     -0.315630      0.2011    -1.56947    0.116539     0.27020      -1.16813
sigma_opt_ngo        -0.288612      0.2155    -1.33937    0.180450     0.23529      -1.22664
sigma_opt_label      -0.559378      0.5761    -0.97093    0.331582     0.91980      -0.60815
sigma_b_required     -0.466760      0.2609    -1.78875    0.073656     0.34696      -1.34528
sigma_req_asc        -0.047013      0.1629    -0.28852    0.772946     0.16070      -0.29255
sigma_req_retail     -0.081656      0.1905    -0.42855    0.668249     0.20796      -0.39265
sigma_req_ngo        -0.046687      0.1909    -0.24459    0.806772     0.19914      -0.23444
sigma_req_label      -0.463415      0.3852    -1.20319    0.228902     0.52502      -0.88266
sigma_req_opt         0.458375      0.3841     1.19322    0.232782     0.31058       1.47588
sigma_log_b_cost      0.920938      0.1785     5.15967   2.474e-07     0.14505       6.34907
sigma_cost_asc        1.357108      0.1311    10.35028    0.000000     0.12419      10.92761
sigma_cost_retail     0.652897      0.1550     4.21172   2.534e-05     0.12230       5.33858
sigma_cost_ngo       -1.476958      0.2232    -6.61676   3.672e-11     0.21439      -6.88922
sigma_cost_label      0.655773      0.1029     6.37103   1.878e-10     0.07743       8.46915
sigma_cost_opt       -0.395614      0.1222    -3.23757    0.001206     0.08651      -4.57304
sigma_cost_req       -0.809113      0.1256    -6.44318   1.170e-10     0.10496      -7.70888
                    p(2-sided)
mu_asc               2.420e-05
mu_b_retailer         0.049409
mu_b_ngo              0.296210
mu_b_label           1.222e-06
mu_b_optional         0.223984
mu_b_required         0.161439
mu_log_b_cost         0.000000
sigma_asc             0.000000
sigma_b_retailer     2.863e-10
sigma_retail_asc      0.685681
sigma_b_ngo          1.288e-05
sigma_ngo_asc         0.579165
sigma_ngo_retail    6.7319e-04
sigma_b_label         0.222606
sigma_label_asc       0.008446
sigma_label_retail    0.985078
sigma_label_ngo       0.670094
sigma_b_optional      0.699971
sigma_opt_asc         0.939501
sigma_opt_retail      0.242756
sigma_opt_ngo         0.219959
sigma_opt_label       0.543087
sigma_b_required      0.178535
sigma_req_asc         0.769867
sigma_req_retail      0.694576
sigma_req_ngo         0.814644
sigma_req_label       0.377423
sigma_req_opt         0.139976
sigma_log_b_cost     2.166e-10
sigma_cost_asc        0.000000
sigma_cost_retail    9.368e-08
sigma_cost_ngo       5.610e-12
sigma_cost_label      0.000000
sigma_cost_opt       4.807e-06
sigma_cost_req       1.266e-14


Overview of choices for MNL model component :
                                       A       B       C
Times available                  4552.00 4552.00 4552.00
Times chosen                     1391.00 1327.00 1834.00
Percentage chosen overall          30.56   29.15   40.29
Percentage chosen when available   30.56   29.15   40.29


MXL Output in WTPspace - correlated random coefficients

Code: Select all

 Model name                                  : MXL1model_Paper4_utility_palma_WTP
Model description                           : MXL model in Utility Space paper 4 - palma method - allCorrelations - WTP
Model run at                                : 2025-04-09 17:00:57.523386
Estimation method                           : bgw
Model diagnosis                             : Relative function convergence
Optimisation diagnosis                      : Maximum found
     hessian properties                     : Negative definite
     maximum eigenvalue                     : -2.2e-05
     reciprocal of condition number         : 1.69611e-07
Number of individuals                       : 761
Number of rows in database                  : 4552
Number of modelled outcomes                 : 4552

Number of cores used                        :  1 
Number of inter-individual draws            : 3000 (sobol)

LL(start)                                   : -5197.32
LL at equal shares, LL(0)                   : -5000.88
LL at observed shares, LL(C)                : -4952.04
LL(final)                                   : -3398
Rho-squared vs equal shares                  :  0.3205 
Adj.Rho-squared vs equal shares              :  0.3135 
Rho-squared vs observed shares               :  0.3138 
Adj.Rho-squared vs observed shares           :  0.3072 
AIC                                         :  6865.99 
BIC                                         :  7090.81 

Estimated parameters                        : 35
Time taken (hh:mm:ss)                       :  40:48:43.31 
     pre-estimation                         :  00:02:57.87 
     estimation                             :  17:05:41.52 
          initial estimation                :  16:53:1.81 
          estimation after rescaling        :  00:12:39.71 
     post-estimation                        :  23:40:3.91 
Iterations                                  :  117  
     initial estimation                     :  112 
     estimation after rescaling             :  5 

Unconstrained optimisation.

Estimates:
                      Estimate        s.e.   t.rat.(0)  p(2-sided)    Rob.s.e. Rob.t.rat.(0)  p(2-sided)
mu_asc                239.0326     75.6847      3.1583    0.001587     51.5657        4.6355   3.561e-06
mu_b_retailer        -131.2137     22.6735     -5.7871   7.162e-09     17.3616       -7.5577   4.108e-14
mu_b_ngo             -114.6090     25.8198     -4.4388   9.046e-06     19.6957       -5.8190   5.921e-09
mu_b_label           -217.4523     30.6169     -7.1024   1.226e-12     25.3354       -8.5829     0.00000
mu_b_optional          53.5241     36.5953      1.4626    0.143578     32.4540        1.6492     0.09910
mu_b_required        -132.2371     38.0406     -3.4762  5.0856e-04     37.0278       -3.5713  3.5523e-04
mu_log_b_cost          -5.6964      0.1312    -43.4159    0.000000      0.1436      -39.6797     0.00000
sigma_asc           -3140.8714    200.9382    -15.6310    0.000000    128.4453      -24.4530     0.00000
sigma_b_retailer      300.2885     16.8538     17.8173    0.000000     12.9087       23.2625     0.00000
sigma_retail_asc      -77.3675     41.2461     -1.8758    0.060690     34.2233       -2.2607     0.02378
sigma_b_ngo           178.0999     14.5314     12.2562    0.000000     12.1549       14.6526     0.00000
sigma_ngo_asc         -13.8831     41.7657     -0.3324    0.739583     34.2239       -0.4057     0.68499
sigma_ngo_retail      230.4230     16.2869     14.1478    0.000000     10.3016       22.3678     0.00000
sigma_b_label        -166.3562     12.7651    -13.0321    0.000000     11.5814      -14.3641     0.00000
sigma_label_asc      -127.1181     29.0898     -4.3698   1.243e-05     29.6132       -4.2926   1.766e-05
sigma_label_retail    -61.9102     12.8591     -4.8145   1.476e-06      8.2149       -7.5363   4.841e-14
sigma_label_ngo       -38.8563     10.4772     -3.7087  2.0837e-04      5.6509       -6.8762   6.149e-12
sigma_b_optional       87.4553     19.9060      4.3934   1.116e-05     15.6202        5.5988   2.158e-08
sigma_opt_asc          18.9105     37.2914      0.5071    0.612085     31.1095        0.6079     0.54328
sigma_opt_retail       36.9653     15.2206      2.4286    0.015156      8.7364        4.2312   2.325e-05
sigma_opt_ngo          44.3464     12.7748      3.4714  5.1776e-04      6.5975        6.7217   1.796e-11
sigma_opt_label      -147.3921     17.5042     -8.4204    0.000000     12.1154      -12.1657     0.00000
sigma_b_required      -14.9197     10.8370     -1.3767    0.168595      6.5724       -2.2700     0.02321
sigma_req_asc         -31.2373     30.6717     -1.0184    0.308468     17.9016       -1.7449     0.08099
sigma_req_retail      -41.9341     17.7356     -2.3644    0.018059     16.7036       -2.5105     0.01206
sigma_req_ngo          40.7840     13.7928      2.9569    0.003107      8.6555        4.7119   2.454e-06
sigma_req_label      -139.6590     14.2489     -9.8014    0.000000     11.3015      -12.3576     0.00000
sigma_req_opt           4.3137     15.3589      0.2809    0.778816     10.0641        0.4286     0.66820
sigma_log_b_cost       -0.2119      0.3579     -0.5922    0.553742      0.2489       -0.8514     0.39455
sigma_cost_asc          0.6344      0.1945      3.2625    0.001104      0.1862        3.4065  6.5801e-04
sigma_cost_retail       0.4080      0.2217      1.8398    0.065800      0.2059        1.9812     0.04757
sigma_cost_ngo          0.4648      0.2619      1.7752    0.075864      0.2295        2.0250     0.04286
sigma_cost_label       -1.3360      0.2327     -5.7426   9.326e-09      0.1731       -7.7168   1.199e-14
sigma_cost_opt         -0.8583      0.3092     -2.7761    0.005501      0.2308       -3.7194  1.9970e-04
sigma_cost_req         -0.5310      0.3863     -1.3745    0.169284      0.2551       -2.0811     0.03742


Overview of choices for MNL model component :
                                       A       B       C
Times available                  4552.00 4552.00 4552.00
Times chosen                     1391.00 1327.00 1834.00
Percentage chosen overall          30.56   29.15   40.29
Percentage chosen when available   30.56   29.15   40.29


Below the code for the utility space of the first approach and for the WTP space.

Code: Select all

# Version 1 - All Correlatons - Utility Space
# ################################################################# #
#### Setting up the work environment                             ####
# ################################################################# #

### Clear memory
rm(list = ls())
#to check directory (folder where we are working)
getwd()
# to change working directory
setwd("U:/Meine Bibliothek/Paper_/Paper 4 Choice Experiment - CL MNL/5_Data/5_MainData_Collection/3_R/WTP Space/MXL in WTP - Palma Method")

# Load R packages from library
library(apollo)
library(tidyr)
library(readxl)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName  ="MXL1model_Paper4_utility_palma",
  modelDescr ="MXL model in Utility Space paper 4 - palma method - allCorrelations",
  indivID    ="id",
  nCores     = 6,
  mixing     = TRUE
)

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

library(haven)
database <- read.csv("Data_UKNLFR_mf_.csv", header = TRUE)

# ########################################################################## #
#### Creating categorical variables 'government', 'transfer', 'nocluster' ####
# ########################################################################## #

# 'government' dummy
database$government_a <- ifelse(database$retailer_a == 0 & database$ngo_a == 0, 1, 0)
database$government_b <- ifelse(database$retailer_b == 0 & database$ngo_b == 0, 1, 0)

# 'transfer' dummy
database$transfer_a <- ifelse(database$label_a == 0, 1, 0)
database$transfer_b <- ifelse(database$label_b == 0, 1, 0)

# 'nocluster' dummy
database$nocluster_a <- ifelse(database$optional_a == 0 & database$required_a == 0, 1, 0)
database$nocluster_b <- ifelse(database$optional_b == 0 & database$required_b == 0, 1, 0)

# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation

# use cl model results for the mu
# use normal distribution for all, and the cost log-normal

# starting simpler w.o. corr btw. diff. attributes
# no corrs. btw. levels of one attribute, but across attributes


apollo_beta = c(mu_asc                         =  0,
                
                mu_b_retailer                 =  0,                                
                mu_b_ngo                      =  0,
                
                mu_b_label                    =  0,
                
                mu_b_optional                 =  0,
                mu_b_required                 =  0,
                
                mu_log_b_cost                 =  -5,
                
                sigma_asc                     =  0,
                
                sigma_b_retailer              =  0,
                sigma_retail_asc              =  0,
                
                sigma_b_ngo                   =  0,
                sigma_ngo_asc                 =  0,
                sigma_ngo_retail              =  0,
                
                
                sigma_b_label                 =  0,
                sigma_label_asc               =  0,
                sigma_label_retail            =  0,
                sigma_label_ngo               =  0,
                
                sigma_b_optional              =  0,
                sigma_opt_asc                 =  0,
                sigma_opt_retail              =  0,
                sigma_opt_ngo                 =  0,
                sigma_opt_label               =  0,
                
                sigma_b_required              =  0,
                sigma_req_asc                 =  0,
                sigma_req_retail              =  0,
                sigma_req_ngo                 =  0,
                sigma_req_label               =  0,
                sigma_req_opt                 =  0,
              
                sigma_log_b_cost              =  -2,
                sigma_cost_asc                =  -0.01,
                sigma_cost_retail             =  -0.01,
                sigma_cost_ngo                =  -0.01,
                sigma_cost_label              =  -0.01,
                sigma_cost_opt                =  -0.01,
                sigma_cost_req                =  -0.01)

# Increasing starting value to -5 according to Stephane Hess (http://www.apollochoicemodelling.com/forum/viewtopic.php?t=104&hilit=searchStart)
# No warning message

### 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() # maybe interesting for the latent class model. (independent availability model)

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

### Sobol: Analysis of diff. Types of draws: Czajkowski & Budzinski 2017
## min of 1000 draws

### Set parameters for generating draws

# What did I have put for settings in?

apollo_draws = list(
  interDrawsType = "sobol", #Halton Draws are not recommended for more than five random coefficents (cf. Bhat, 2003) For computing Bayesian efficiency pseudo random draws or pseudo monte carlo are the simplest (ChoiceMetrics, 2018).
  interNDraws    = 3000, # More than 2000 Sobol Draws recommendet by Czajkowskiei & Budzinski 2019
  interUnifDraws = c(),
  interNormDraws = c("draws_sq","draws_asc", "draws_government", "draws_retailer","draws_ngo", "draws_transfer", "draws_label", "draws_nocluster", "draws_optional","draws_required","draws_cost")
)

### Create random parameters

# What did I have created here?
## Adapt this to vector of parameters

apollo_randCoeff = function(apollo_beta, apollo_inputs){
  
  randcoeff = list()
  
  
  
  randcoeff[["b_asc"]]         =     ( mu_asc           + sigma_asc          * draws_asc )
  
  randcoeff[["b_retailer"]]    =     ( mu_b_retailer    + sigma_b_retailer   * draws_retailer
                                       
                                       + sigma_retail_asc * draws_asc )
  
  randcoeff[["b_ngo"]]         =     ( mu_b_ngo         + sigma_b_ngo        * draws_ngo
                                       
                                       + sigma_ngo_asc * draws_asc
                                       
                                       + sigma_ngo_retail * draws_retailer)
  
  randcoeff[["b_label"]]       =     ( mu_b_label       + sigma_b_label      * draws_label
                                       
                                       + sigma_label_asc * draws_asc
                                       
                                       + sigma_label_retail * draws_retailer
                                       
                                       + sigma_label_ngo * draws_ngo)
  
  randcoeff[["b_optional"]]    =     ( mu_b_optional    + sigma_b_optional   * draws_optional
                                       
                                       + sigma_opt_asc * draws_asc
                                       
                                       + sigma_opt_retail * draws_retailer
                                       
                                       + sigma_opt_ngo * draws_ngo
                                       
                                       + sigma_opt_label * draws_label)
  
  randcoeff[["b_required"]]    =     ( mu_b_required    + sigma_b_required   * draws_required
                                       
                                       + sigma_req_asc * draws_asc
                                       
                                       + sigma_req_retail * draws_retailer
                                       
                                       + sigma_req_ngo * draws_ngo
                                       
                                       + sigma_req_label * draws_label
                                       
                                       + sigma_req_opt * draws_optional)
  
  randcoeff[["b_cost"]]        =  exp( mu_log_b_cost    + sigma_log_b_cost   * draws_cost
                                       
                                       + sigma_cost_asc * draws_asc
                                       
                                       + sigma_cost_retail * draws_retailer
                                       
                                       + sigma_cost_ngo * draws_ngo
                                       
                                       + sigma_cost_label * draws_label
                                       
                                       + sigma_cost_opt * draws_optional
                                       
                                       + sigma_cost_req * draws_required)
  
  
  
  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 initialization: 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()
  V[['A']]  = b_asc + b_retailer * retailer_a + b_ngo * ngo_a + b_label * label_a + b_optional * optional_a + b_required * required_a + b_cost * cost_a
  V[['B']]  = b_asc + b_retailer * retailer_b + b_ngo * ngo_b + b_label * label_b + b_optional * optional_b + b_required * required_b + b_cost * cost_b
  V[['C']]  = 0 
  
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives  = c(A=1, B=2, C=3), 
    avail         = 1,
    choiceVar     = decision, # depend on the variable used in stata
    V             = V
  )
  
  ### Compute probabilities using MNL model
  P[['model']] = apollo_mnl(mnl_settings, functionality)
  
  ### Take product across observation for same individual
  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, list(printPVal = 2))

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

apollo_saveOutput(
  model, 
  list(printPVal = 2)
)

#################################
# Cost Parameter Transformation #
#################################

# transform the cost parameter. Look up into the Forum
# follow link to the Forum Post: use method suggested by D Palma : http://www.apollochoicemodelling.com/forum/viewtopic.php?f=16&t=262
# sigma is the standard deviation,  mu gives average preference.

apollo_deltaMethod(model, deltaMethod_settings = list(operation="lognormal",parName1="mu_log_b_cost",parName2="sigma_log_b_cost"))

############################################################
# Estimating difference between NGO and retailer estimator #
############################################################
# compare https://www.apollochoicemodelling.com/forum/viewtopic.php?t=202.
apollo_deltaMethod(model, deltaMethod_settings = list(operation="diff",parName1="mu_b_retailer",parName2="mu_b_ngo"))

#############################################################
# Estimating difference between FC optional and FC required #
#############################################################
# compare https://www.apollochoicemodelling.com/forum/viewtopic.php?t=202.
apollo_deltaMethod(model, deltaMethod_settings = list(operation="diff",parName1="mu_b_optional",parName2="mu_b_required"))

#################################
# SD - Parameter Transformation #
#################################

# SD ASC - No need for transformation

# SD RETAILER
deltaMethod_settings=list(expression=c(SD_RETAILER = "sqrt(sigma_b_retailer^2
                                                           +sigma_retail_asc^2)"))
utility_SD_RETAILER = apollo_deltaMethod(model, deltaMethod_settings)

# SD NGO
deltaMethod_settings=list(expression=c(SD_NGO = "sqrt(sigma_b_ngo^2
                                                           +sigma_ngo_asc ^2
                                                           +sigma_ngo_retail^2)"))
utility_SD_NGO = apollo_deltaMethod(model, deltaMethod_settings)
utility_SD_table = matrix(c(utility_SD_RETAILER,utility_SD_NGO),4,2)


# SD LABEL
deltaMethod_settings=list(expression=c(SD_LABEL = "sqrt(sigma_b_label^2
                                                           +sigma_label_asc^2
                                                           +sigma_label_retail^2
                                                           +sigma_label_ngo^2)"))
utility_SD_LABEL = apollo_deltaMethod(model, deltaMethod_settings)
utility_SD_table = matrix(c(utility_SD_table,utility_SD_LABEL),4,3)

# SD OPTIONAL
deltaMethod_settings=list(expression=c(SD_OPTIONAL = "sqrt(sigma_b_optional^2
                                                           +sigma_opt_asc^2
                                                           +sigma_opt_retail^2
                                                           +sigma_opt_ngo^2
                                                           +sigma_opt_label^2)"))
utility_SD_OPT = apollo_deltaMethod(model, deltaMethod_settings)
utility_SD_table = matrix(c(utility_SD_table,utility_SD_OPT),4,4)

# SD REQUIRED
deltaMethod_settings=list(expression=c(SD_REQUIRED = "sqrt(sigma_b_required^2
                                                           +sigma_req_asc^2
                                                           +sigma_req_retail^2
                                                           +sigma_req_ngo^2
                                                           +sigma_req_label^2
                                                           +sigma_req_opt^2)"))
utility_SD_REQ = apollo_deltaMethod(model, deltaMethod_settings)
utility_SD_table = matrix(c(utility_SD_table,utility_SD_REQ),4,5)

# SD COST
deltaMethod_settings=list(expression=c(SD_COST = "sqrt(sigma_log_b_cost^2
                                                           +sigma_cost_asc^2
                                                           +sigma_cost_retail^2
                                                           +sigma_cost_ngo^2
                                                           +sigma_cost_label^2
                                                           +sigma_cost_opt^2
                                                           +sigma_cost_req^2)"))
utility_SD_COST = apollo_deltaMethod(model, deltaMethod_settings)
utility_SD_table = matrix(c(utility_SD_table,utility_SD_COST),4,6)

rownames(utility_SD_table) = c("Coef", "Value","Robust s.e.", "Robust t-ratio(0)" )

write.csv(utility_SD_table, file = "SD coefs paper4 utility.csv")

Code: Select all

# Version 1 - All Correlatons - WTP Space
# ################################################################# #
#### Setting up the work environment                             ####
# ################################################################# #

### Clear memory
rm(list = ls())
#to check directory (folder where we are working)
getwd()
# to change working directory
setwd("U:/Meine Bibliothek/Paper_/Paper 4 Choice Experiment - CL MNL/5_Data/5_MainData_Collection/3_R/WTP Space/MXL in WTP - Palma Method")

# Load R packages from library
library(apollo)
library(tidyr)
library(readxl)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName  ="MXL1model_Paper4_utility_palma_WTP",
  modelDescr ="MXL model in Utility Space paper 4 - palma method - allCorrelations - WTP",
  indivID    ="id",
  mixing     = TRUE
)

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

library(haven)
database <- read.csv("Data_UKNLFR_mf_.csv", header = TRUE)

# ########################################################################## #
#### Creating categorical variables 'government', 'transfer', 'nocluster' ####
# ########################################################################## #

# 'government' dummy
database$government_a <- ifelse(database$retailer_a == 0 & database$ngo_a == 0, 1, 0)
database$government_b <- ifelse(database$retailer_b == 0 & database$ngo_b == 0, 1, 0)

# 'transfer' dummy
database$transfer_a <- ifelse(database$label_a == 0, 1, 0)
database$transfer_b <- ifelse(database$label_b == 0, 1, 0)

# 'nocluster' dummy
database$nocluster_a <- ifelse(database$optional_a == 0 & database$required_a == 0, 1, 0)
database$nocluster_b <- ifelse(database$optional_b == 0 & database$required_b == 0, 1, 0)

# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation

# use cl model results for the mu
# use normal distribution for all, and the cost log-normal

# starting simpler w.o. corr btw. diff. attributes
# no corrs. btw. levels of one attribute, but across attributes


apollo_beta = c(mu_asc                         =  0,
                
                mu_b_retailer                 =  0,                                
                mu_b_ngo                      =  0,
                
                mu_b_label                    =  0,
                
                mu_b_optional                 =  0,
                mu_b_required                 =  0,
                
                mu_log_b_cost                 =  -5,
                
                sigma_asc                     =  0,
                
                sigma_b_retailer              =  0,
                sigma_retail_asc              =  0,
                
                sigma_b_ngo                   =  0,
                sigma_ngo_asc                 =  0,
                sigma_ngo_retail              =  0,
                
                
                sigma_b_label                 =  0,
                sigma_label_asc               =  0,
                sigma_label_retail            =  0,
                sigma_label_ngo               =  0,
                
                sigma_b_optional              =  0,
                sigma_opt_asc                 =  0,
                sigma_opt_retail              =  0,
                sigma_opt_ngo                 =  0,
                sigma_opt_label               =  0,
                
                sigma_b_required              =  0,
                sigma_req_asc                 =  0,
                sigma_req_retail              =  0,
                sigma_req_ngo                 =  0,
                sigma_req_label               =  0,
                sigma_req_opt                 =  0,
                
                sigma_log_b_cost              =  -2,
                sigma_cost_asc                =  -0.01,
                sigma_cost_retail             =  -0.01,
                sigma_cost_ngo                =  -0.01,
                sigma_cost_label              =  -0.01,
                sigma_cost_opt                =  -0.01,
                sigma_cost_req                =  -0.01)

# Increasing starting value to -5 according to Stephane Hess (http://www.apollochoicemodelling.com/forum/viewtopic.php?t=104&hilit=searchStart)
# No warning message

### 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() # maybe interesting for the latent class model. (independent availability model)

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

### Sobol: Analysis of diff. Types of draws: Czajkowski & Budzinski 2017
## min of 1000 draws

### Set parameters for generating draws

# What did I have put for settings in?

apollo_draws = list(
  interDrawsType = "sobol", #Halton Draws are not recommended for more than five random coefficents (cf. Bhat, 2003) For computing Bayesian efficiency pseudo random draws or pseudo monte carlo are the simplest (ChoiceMetrics, 2018).
  interNDraws    = 3000, # More than 2000 Sobol Draws recommendet by Czajkowskiei & Budzinski 2019
  interUnifDraws = c(),
  interNormDraws = c("draws_sq","draws_asc", "draws_government", "draws_retailer","draws_ngo", "draws_transfer", "draws_label", "draws_nocluster", "draws_optional","draws_required","draws_cost")
)

### Create random parameters

# What did I have created here?
## Adapt this to vector of parameters

apollo_randCoeff = function(apollo_beta, apollo_inputs){
  
  randcoeff = list()
  
  
  
  randcoeff[["b_asc"]]         =     ( mu_asc           + sigma_asc          * draws_asc )
  
  randcoeff[["b_retailer"]]    =     ( mu_b_retailer    + sigma_b_retailer   * draws_retailer
                                       
                                       + sigma_retail_asc * draws_asc )
  
  randcoeff[["b_ngo"]]         =     ( mu_b_ngo         + sigma_b_ngo        * draws_ngo
                                       
                                       + sigma_ngo_asc * draws_asc
                                       
                                       + sigma_ngo_retail * draws_retailer)
  
  randcoeff[["b_label"]]       =     ( mu_b_label       + sigma_b_label      * draws_label
                                       
                                       + sigma_label_asc * draws_asc
                                       
                                       + sigma_label_retail * draws_retailer
                                       
                                       + sigma_label_ngo * draws_ngo)
  
  randcoeff[["b_optional"]]    =     ( mu_b_optional    + sigma_b_optional   * draws_optional
                                       
                                       + sigma_opt_asc * draws_asc
                                       
                                       + sigma_opt_retail * draws_retailer
                                       
                                       + sigma_opt_ngo * draws_ngo
                                       
                                       + sigma_opt_label * draws_label)
  
  randcoeff[["b_required"]]    =     ( mu_b_required    + sigma_b_required   * draws_required
                                       
                                       + sigma_req_asc * draws_asc
                                       
                                       + sigma_req_retail * draws_retailer
                                       
                                       + sigma_req_ngo * draws_ngo
                                       
                                       + sigma_req_label * draws_label
                                       
                                       + sigma_req_opt * draws_optional)
  
  randcoeff[["b_cost"]]        =  exp( mu_log_b_cost    + sigma_log_b_cost   * draws_cost
                                       
                                       + sigma_cost_asc * draws_asc
                                       
                                       + sigma_cost_retail * draws_retailer
                                       
                                       + sigma_cost_ngo * draws_ngo
                                       
                                       + sigma_cost_label * draws_label
                                       
                                       + sigma_cost_opt * draws_optional
                                       
                                       + sigma_cost_req * draws_required)
  
  
  
  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 initialization: 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()
  V[['A']]  = b_cost * (b_asc + b_retailer * retailer_a + b_ngo * ngo_a + b_label * label_a + b_optional * optional_a + b_required * required_a + cost_a)
  V[['B']]  = b_cost * (b_asc + b_retailer * retailer_b + b_ngo * ngo_b + b_label * label_b + b_optional * optional_b + b_required * required_b + cost_b)
  V[['C']]  = 0
  
  
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives  = c(A=1, B=2, C=3), 
    avail         = 1,
    choiceVar     = decision, # depend on the variable used in stata
    V             = V
  )
  
  ### Compute probabilities using MNL model
  P[['model']] = apollo_mnl(mnl_settings, functionality)
  
  ### Take product across observation for same individual
  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, list(printPVal = 2))

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

apollo_saveOutput(
  model, 
  list(printPVal = 2)
)

#################################
# Cost Parameter Transformation #
#################################

# transform the cost parameter. Look up into the Forum
# follow link to the Forum Post: use method suggested by D Palma : http://www.apollochoicemodelling.com/forum/viewtopic.php?f=16&t=262
# sigma is the standard deviation,  mu gives average preference.

apollo_deltaMethod(model, deltaMethod_settings = list(operation="lognormal",parName1="mu_log_b_cost",parName2="sigma_log_b_cost"))

############################################################
# Estimating difference between NGO and retailer estimator #
############################################################
# compare https://www.apollochoicemodelling.com/forum/viewtopic.php?t=202.
apollo_deltaMethod(model, deltaMethod_settings = list(operation="diff",parName1="mu_b_retailer",parName2="mu_b_ngo"))

#############################################################
# Estimating difference between FC optional and FC required #
#############################################################
# compare https://www.apollochoicemodelling.com/forum/viewtopic.php?t=202.
apollo_deltaMethod(model, deltaMethod_settings = list(operation="diff",parName1="mu_b_optional",parName2="mu_b_required"))

#################################
# SD - Parameter Transformation #
#################################

# SD ASC - No need for transformation

# SD RETAILER
deltaMethod_settings=list(expression=c(SD_RETAILER = "sqrt(sigma_b_retailer^2
                                                           +sigma_retail_asc^2)"))
wtp_SD_RETAILER = apollo_deltaMethod(model, deltaMethod_settings)

# SD NGO
deltaMethod_settings=list(expression=c(SD_NGO = "sqrt(sigma_b_ngo^2
                                                           +sigma_ngo_asc ^2
                                                           +sigma_ngo_retail^2)"))
wtp_SD_NGO = apollo_deltaMethod(model, deltaMethod_settings)
wtp_SD_table = matrix(c(wtp_SD_RETAILER,wtp_SD_NGO),4,2)


# SD LABEL
deltaMethod_settings=list(expression=c(SD_LABEL = "sqrt(sigma_b_label^2
                                                           +sigma_label_asc^2
                                                           +sigma_label_retail^2
                                                           +sigma_label_ngo^2)"))
wtp_SD_LABEL = apollo_deltaMethod(model, deltaMethod_settings)
wtp_SD_table = matrix(c(wtp_SD_table,wtp_SD_LABEL),4,3)

# SD OPTIONAL
deltaMethod_settings=list(expression=c(SD_OPTIONAL = "sqrt(sigma_b_optional^2
                                                           +sigma_opt_asc^2
                                                           +sigma_opt_retail^2
                                                           +sigma_opt_ngo^2
                                                           +sigma_opt_label^2)"))
wtp_SD_OPT = apollo_deltaMethod(model, deltaMethod_settings)
wtp_SD_table = matrix(c(wtp_SD_table,wtp_SD_OPT),4,4)

# SD REQUIRED
deltaMethod_settings=list(expression=c(SD_REQUIRED = "sqrt(sigma_b_required^2
                                                           +sigma_req_asc^2
                                                           +sigma_req_retail^2
                                                           +sigma_req_ngo^2
                                                           +sigma_req_label^2
                                                           +sigma_req_opt^2)"))
wtp_SD_REQ = apollo_deltaMethod(model, deltaMethod_settings)
wtp_SD_table = matrix(c(wtp_SD_table,wtp_SD_REQ),4,5)

# SD COST
deltaMethod_settings=list(expression=c(SD_COST = "sqrt(sigma_log_b_cost^2
                                                           +sigma_cost_asc^2
                                                           +sigma_cost_retail^2
                                                           +sigma_cost_ngo^2
                                                           +sigma_cost_label^2
                                                           +sigma_cost_opt^2
                                                           +sigma_cost_req^2)"))
wtp_SD_COST = apollo_deltaMethod(model, deltaMethod_settings)
wtp_SD_table = matrix(c(wtp_SD_table,wtp_SD_COST),4,6)

rownames(wtp_SD_table) = c("Coef", "Value","Robust s.e.", "Robust t-ratio(0)" )

write.csv(wtp_SD_table, file = "SD coefs paper4 WTP.csv")
(End of Part 1)
mfritschle
Posts: 3
Joined: 18 May 2023, 13:14

Re: Strong differences in WTP and utility space estimations using mixed logit model

Post by mfritschle »

(Part 2)
2. Over specified model

To estimate the overspecified model, I first estimated a CL model (all priors = 0) just with all levels of all attributes, including ASC and SQ. Then I increased complexity and estimated a MXL model with all levels of all attributes and SD parameters (mu_log_b_cost = -5, sigma_log_b_cost = -2, remaining priors = 0).

As you explained, I then estimated two models, with adjusted reference categories. The reference categories were the attribute levels with the lowest S.D. estimator for each attribute. SQ had a lower SD value than ASC. Nevertheless, I estimated two models once with ASC and once with SQ because I was not sure if I had defined the utility function correctly.

In both cases, the significance levels differ between the models with SQ and ASC and between the utility and WTP space within the models. However, it is less intense than in the previous model.

Second, the ASC estimator is still negative in the utility space, which aligns with our assumption. But when transformed into WTP space, it turns positive. The same is true, but with changed signs when SQ is the base level.

ASC as base level, and level with lowest SD as reference level for each attribute:

Code: Select all

Model name                                  : MXLmodel_Paper4_utility_palma_overspecified_adapted
Model description                           : MXL model in Utility Space paper 4 - palma method - overspecified
Model run at                                : 2025-04-11 13:37:54.053723
Estimation method                           : bgw
Model diagnosis                             : Relative function convergence
Optimisation diagnosis                      : Maximum found
     hessian properties                     : Negative definite
     maximum eigenvalue                     : -4.838426
     reciprocal of condition number         : 0.0160329
Number of individuals                       : 761
Number of rows in database                  : 4552
Number of modelled outcomes                 : 4552

Number of cores used                        :  6 
Number of inter-individual draws            : 3000 (sobol)

LL(start)                                   : -5197.37
LL at equal shares, LL(0)                   : -5000.88
LL at observed shares, LL(C)                : -4952.04
LL(final)                                   : -3359.87
Rho-squared vs equal shares                  :  0.3281 
Adj.Rho-squared vs equal shares              :  0.3253 
Rho-squared vs observed shares               :  0.3215 
Adj.Rho-squared vs observed shares           :  0.3191 
AIC                                         :  6747.74 
BIC                                         :  6837.67 

Estimated parameters                        : 14
Time taken (hh:mm:ss)                       :  00:15:34.15 
     pre-estimation                         :  00:02:12.39 
     estimation                             :  00:02:52.67 
          initial estimation                :  00:02:26.03 
          estimation after rescaling        :  00:00:26.64 
     post-estimation                        :  00:10:29.09 
Iterations                                  :  16  
     initial estimation                     :  15 
     estimation after rescaling             :  1 

Unconstrained optimisation.

Estimates:
                      Estimate        s.e.   t.rat.(0)  p(2-sided)    Rob.s.e. Rob.t.rat.(0)  p(2-sided)
mu_asc                -0.93806     0.26414     -3.5514  3.8323e-04     0.27966       -3.3543  7.9564e-04
mu_b_government        0.16126     0.08508      1.8953     0.05805     0.08718        1.8498     0.06435
mu_b_retailer         -0.02812     0.08376     -0.3358     0.73705     0.08540       -0.3293     0.74193
mu_b_label            -0.29784     0.06427     -4.6346   3.577e-06     0.06520       -4.5682   4.920e-06
mu_b_nocluster         0.09426     0.08219      1.1468     0.25148     0.08372        1.1258     0.26024
mu_b_optional          0.36417     0.07856      4.6354   3.562e-06     0.08131        4.4788   7.505e-06
mu_log_b_cost         -7.63466     0.34751    -21.9696     0.00000     0.35498      -21.5072     0.00000
sigma_asc              5.25641     0.34685     15.1546     0.00000     0.34596       15.1939     0.00000
sigma_b_government    -0.88506     0.13136     -6.7377   1.609e-11     0.14478       -6.1133   9.761e-10
sigma_b_retailer       0.70208     0.15314      4.5846   4.549e-06     0.15828        4.4357   9.179e-06
sigma_b_label         -0.60023     0.11382     -5.2736   1.338e-07     0.12239       -4.9042   9.382e-07
sigma_b_nocluster     -0.65788     0.14964     -4.3964   1.101e-05     0.15543       -4.2326   2.310e-05
sigma_b_optional       0.41747     0.20435      2.0429     0.04106     0.21872        1.9087     0.05630
sigma_log_b_cost      -2.53313     0.29542     -8.5748     0.00000     0.28394       -8.9214     0.00000

Code: Select all

Model name                                  : MXLmodel_Paper4_WTP_palma_overspecified_adapted
Model description                           : MXL model in WTP paper 4 - palma method - overspecified
Model run at                                : 2025-04-11 12:37:31.296002
Estimation method                           : bgw
Model diagnosis                             : Relative function convergence
Optimisation diagnosis                      : Maximum found
     hessian properties                     : Negative definite
     maximum eigenvalue                     : -4e-06
     reciprocal of condition number         : 3.13322e-08
Number of individuals                       : 761
Number of rows in database                  : 4552
Number of modelled outcomes                 : 4552

Number of cores used                        :  6 
Number of inter-individual draws            : 3000 (sobol)

LL(start)                                   : -5197.37
LL at equal shares, LL(0)                   : -5000.88
LL at observed shares, LL(C)                : -4952.04
LL(final)                                   : -3423.68
Rho-squared vs equal shares                  :  0.3154 
Adj.Rho-squared vs equal shares              :  0.3126 
Rho-squared vs observed shares               :  0.3086 
Adj.Rho-squared vs observed shares           :  0.3062 
AIC                                         :  6875.35 
BIC                                         :  6965.28 

Estimated parameters                        : 14
Time taken (hh:mm:ss)                       :  00:29:25.8 
     pre-estimation                         :  00:02:7.58 
     estimation                             :  00:11:2.68 
          initial estimation                :  00:10:35.14 
          estimation after rescaling        :  00:00:27.54 
     post-estimation                        :  00:16:15.55 
Iterations                                  :  52  
     initial estimation                     :  51 
     estimation after rescaling             :  1 

Unconstrained optimisation.

Estimates:
                      Estimate        s.e.   t.rat.(0)  p(2-sided)    Rob.s.e. Rob.t.rat.(0)  p(2-sided)
mu_asc                 336.825     98.3909      3.4233  6.1859e-04     69.0360        4.8790   1.066e-06
mu_b_government         20.977     23.5754      0.8898    0.373575     21.9468        0.9558    0.339158
mu_b_retailer          -15.631     17.6182     -0.8872    0.374975     15.8012       -0.9892    0.322556
mu_b_label            -108.461     15.8292     -6.8519   7.286e-12     15.0945       -7.1855   6.699e-13
mu_b_nocluster          58.201     19.2674      3.0207    0.002522     16.1577        3.6021  3.1569e-04
mu_b_optional          135.343     19.9282      6.7915   1.109e-11     17.4455        7.7581   8.660e-15
mu_log_b_cost           -6.269      0.1081    -58.0150    0.000000      0.1132      -55.3557    0.000000
sigma_asc             4898.732    489.7589     10.0023    0.000000    368.8395       13.2815    0.000000
sigma_b_government     235.301     22.4876     10.4636    0.000000     18.5668       12.6733    0.000000
sigma_b_retailer        12.815     19.9125      0.6435    0.519872      7.0105        1.8279    0.067563
sigma_b_label           61.142     29.7402      2.0559    0.039794     25.8084        2.3691    0.017832
sigma_b_nocluster      -23.557     31.1745     -0.7557    0.449857     22.1092       -1.0655    0.286654
sigma_b_optional        33.711     21.0109      1.6045    0.108609     11.9539        2.8201    0.004801
sigma_log_b_cost        -1.697      0.1628    -10.4207    0.000000      0.1516      -11.1960    0.000000
SQ as base level and level with lowest SD as reference level for each attribute:

Code: Select all

Model name                                  : MXLmodel_Paper4_utility_palma_overspecified_adapted
Model description                           : MXL model in Utility Space paper 4 - palma method - overspecified
Model run at                                : 2025-04-14 08:37:03.002458
Estimation method                           : bgw
Model diagnosis                             : Relative function convergence
Optimisation diagnosis                      : Maximum found
     hessian properties                     : Negative definite
     maximum eigenvalue                     : -4.346368
     reciprocal of condition number         : 0.0146042
Number of individuals                       : 761
Number of rows in database                  : 4552
Number of modelled outcomes                 : 4552

Number of cores used                        :  6 
Number of inter-individual draws            : 3000 (sobol)

LL(start)                                   : -5197.37
LL at equal shares, LL(0)                   : -5000.88
LL at observed shares, LL(C)                : -4952.04
LL(final)                                   : -3360.71
Rho-squared vs equal shares                  :  0.328 
Adj.Rho-squared vs equal shares              :  0.3252 
Rho-squared vs observed shares               :  0.3213 
Adj.Rho-squared vs observed shares           :  0.3189 
AIC                                         :  6749.41 
BIC                                         :  6839.34 

Estimated parameters                        : 14
Time taken (hh:mm:ss)                       :  00:15:1.95 
     pre-estimation                         :  00:02:5.21 
     estimation                             :  00:02:48.04 
          initial estimation                :  00:02:20.91 
          estimation after rescaling        :  00:00:27.13 
     post-estimation                        :  00:10:8.7 
Iterations                                  :  15  
     initial estimation                     :  14 
     estimation after rescaling             :  1 

Unconstrained optimisation.

Estimates:
                      Estimate        s.e.   t.rat.(0)  p(2-sided)    Rob.s.e. Rob.t.rat.(0)  p(2-sided)
mu_sq                  1.18549     0.26006      4.5585   5.152e-06     0.27417        4.3239   1.533e-05
mu_b_government        0.16217     0.08504      1.9070     0.05653     0.08631        1.8789     0.06026
mu_b_ngo              -0.04066     0.08404     -0.4838     0.62854     0.08625       -0.4714     0.63735
mu_b_transfer          0.25886     0.06251      4.1408   3.461e-05     0.06421        4.0312   5.549e-05
mu_b_nocluster         0.11361     0.08454      1.3439     0.17897     0.08510        1.3350     0.18187
mu_b_optional          0.38154     0.08116      4.7013   2.585e-06     0.08319        4.5864   4.511e-06
mu_log_b_cost         -7.63206     0.35841    -21.2942     0.00000     0.36709      -20.7908     0.00000
sigma_sq              -5.29764     0.34768    -15.2372     0.00000     0.34200      -15.4902     0.00000
sigma_b_government    -0.88691     0.13609     -6.5169   7.177e-11     0.15010       -5.9090   3.443e-09
sigma_b_ngo            0.72889     0.14925      4.8838   1.041e-06     0.14957        4.8734   1.097e-06
sigma_b_transfer      -0.63695     0.10572     -6.0248   1.693e-09     0.10961       -5.8112   6.202e-09
sigma_b_nocluster     -0.67027     0.15023     -4.4617   8.132e-06     0.15355       -4.3652   1.270e-05
sigma_b_optional       0.45738     0.18245      2.5068     0.01218     0.18059        2.5327     0.01132
sigma_log_b_cost      -2.55618     0.32211     -7.9357   1.998e-15     0.31867       -8.0214   1.110e-15

Code: Select all

Model name                                  : MXLmodel_Paper4_WTP_palma_overspecified_adapted
Model description                           : MXL model in WTP paper 4 - palma method - overspecified
Model run at                                : 2025-04-14 10:13:23.458422
Estimation method                           : bgw
Model diagnosis                             : Relative function convergence
Optimisation diagnosis                      : Maximum found
     hessian properties                     : Negative definite
     maximum eigenvalue                     : -0.000891
     reciprocal of condition number         : 4.08699e-08
Number of individuals                       : 761
Number of rows in database                  : 4552
Number of modelled outcomes                 : 4552

Number of cores used                        :  6 
Number of inter-individual draws            : 3000 (sobol)

LL(start)                                   : -4661.22
LL at equal shares, LL(0)                   : -5000.88
LL at observed shares, LL(C)                : -4952.04
LL(final)                                   : -3368.94
Rho-squared vs equal shares                  :  0.3263 
Adj.Rho-squared vs equal shares              :  0.3235 
Rho-squared vs observed shares               :  0.3197 
Adj.Rho-squared vs observed shares           :  0.3173 
AIC                                         :  6765.88 
BIC                                         :  6855.81 

Estimated parameters                        : 14
Time taken (hh:mm:ss)                       :  00:29:35.55 
     pre-estimation                         :  00:02:24.51 
     estimation                             :  00:09:47.19 
          initial estimation                :  00:09:7.76 
          estimation after rescaling        :  00:00:39.44 
     post-estimation                        :  00:17:23.85 
Iterations                                  :  42  
     initial estimation                     :  41 
     estimation after rescaling             :  1 

Unconstrained optimisation.

Estimates:
                      Estimate        s.e.   t.rat.(0)  p(2-sided)    Rob.s.e. Rob.t.rat.(0)  p(2-sided)
mu_asc                 -31.787    10.39383     -3.0582    0.002226    11.84460       -2.6837    0.007282
mu_b_government          6.041     3.51739      1.7176    0.085870     3.98979        1.5142    0.129966
mu_b_ngo                -1.459     3.21553     -0.4536    0.650097     3.89386       -0.3746    0.707955
mu_b_transfer           11.734     2.71293      4.3251   1.524e-05     3.12502        3.7548  1.7350e-04
mu_b_nocluster           8.098     3.84014      2.1089    0.034957     5.57671        1.4522    0.146456
mu_b_optional           18.942     3.67421      5.1554   2.531e-07     4.69248        4.0366   5.422e-05
mu_log_b_cost           -3.275     0.08318    -39.3751    0.000000     0.08632      -37.9429    0.000000
sigma_asc              269.956    32.84292      8.2196   2.220e-16    35.57200        7.5890   3.220e-14
sigma_b_government      38.329     5.03098      7.6185   2.576e-14     6.06232        6.3225   2.574e-10
sigma_b_ngo             27.774     5.23502      5.3055   1.124e-07     6.33687        4.3830   1.171e-05
sigma_b_transfer       -23.021     2.95779     -7.7832   7.105e-15     2.75373       -8.3600    0.000000
sigma_b_nocluster      -25.781     6.42401     -4.0133   5.988e-05    10.34736       -2.4916    0.012718
sigma_b_optional       -12.870     6.35671     -2.0246    0.042913     7.94618       -1.6196    0.105321
sigma_log_b_cost        -1.518     0.08757    -17.3394    0.000000     0.09376      -16.1949    0.000000
Below the code for the utility space of the second approach and for the WTP space with the ASC/SQ as base level.

Code: Select all

# ########################################################################## #
#### Overspecified CL model in utility space                              ####
# ########################################################################## #

### Set core controls
apollo_control = list(
  modelName  ="CLmodel_Paper4_utility_palma",
  modelDescr ="CL model in Utility Space paper 4 - palma method - overspecified",
  indivID    ="id",
  nCores     = 6
)

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

library(haven)
database <- read.csv("Data_UKNLFR_mf_.csv", header = TRUE)

# ########################################################################## #
#### Creating categorical variables 'government', 'transfer', 'nocluster' ####
# ########################################################################## #

# 'government' dummy
database$government_a <- ifelse(database$retailer_a == 0 & database$ngo_a == 0, 1, 0)
database$government_b <- ifelse(database$retailer_b == 0 & database$ngo_b == 0, 1, 0)

# 'transfer' dummy
database$transfer_a <- ifelse(database$label_a == 0, 1, 0)
database$transfer_b <- ifelse(database$label_b == 0, 1, 0)

# 'nocluster' dummy
database$nocluster_a <- ifelse(database$optional_a == 0 & database$required_a == 0, 1, 0)
database$nocluster_b <- ifelse(database$optional_b == 0 & database$required_b == 0, 1, 0)

# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation

apollo_beta=c(b_sq         = 0,
              b_asc        = 0,
              b_government = 0,
              b_retailer   = 0,
              b_ngo        = 0,
              b_transfer   = 0,
              b_label      = 0,
              b_nocluster  = 0,
              b_optional   = 0,
              b_required   = 0,
              b_cost       = 0)

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

### 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()

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()
  V[['A']]  = b_asc + b_government * government_a + b_retailer * retailer_a + b_ngo * ngo_a + b_transfer * transfer_a + b_label * label_a + b_nocluster * nocluster_a + b_optional * optional_a + b_required * required_a + b_cost * cost_a
  V[['B']]  = b_asc + b_government * government_b + b_retailer * retailer_b + b_ngo * ngo_b + b_transfer * transfer_b + b_label * label_b + b_nocluster * nocluster_b + b_optional * optional_b + b_required * required_b + b_cost * cost_b
  V[['C']]  = b_sq
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives  = c(A=1, B=2, C=3), 
    avail         = 1, 
    choiceVar     = decision,
    V             = V
  )
  
  ### Compute probabilities using MNL model
  P[['model']] = apollo_mnl(mnl_settings, functionality)
  
  ### Take product across observation for same individual
  P = apollo_panelProd(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, list(printPVal = 2))

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

apollo_saveOutput(model, list(printPVal = 2))



# ########################################################################## #
#### Overspecified MXL model in utility space                              ####
# ########################################################################## #
# ################################################################# #
#### Setting up the work environment                             ####
# ################################################################# #

### Clear memory
rm(list = ls())
#to check directory (folder where we are working)
getwd()
# to change working directory
setwd("U:/Meine Bibliothek/Paper_/Paper 4 Choice Experiment - CL MNL/5_Data/5_MainData_Collection/3_R/WTP Space/MXL in WTP - Palma Method")

# Load R packages from library
library(apollo)
library(tidyr)
library(readxl)

### Initialise code
apollo_initialise()

# ########################################################################## #
#### Overspecified MXL model in utility space                              ####
# ########################################################################## #


### Set core controls
apollo_control = list(
  modelName  ="MXLmodel_Paper4_utility_palma_overspecified",
  modelDescr ="MXL model in Utility Space paper 4 - palma method - overspecified",
  indivID    ="id",
  nCores     = 6,
  mixing     = TRUE
)

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

library(haven)
database <- read.csv("Data_UKNLFR_mf_.csv", header = TRUE)

# ########################################################################## #
#### Creating categorical variables 'government', 'transfer', 'nocluster' ####
# ########################################################################## #

# 'government' dummy
database$government_a <- ifelse(database$retailer_a == 0 & database$ngo_a == 0, 1, 0)
database$government_b <- ifelse(database$retailer_b == 0 & database$ngo_b == 0, 1, 0)

# 'transfer' dummy
database$transfer_a <- ifelse(database$label_a == 0, 1, 0)
database$transfer_b <- ifelse(database$label_b == 0, 1, 0)

# 'nocluster' dummy
database$nocluster_a <- ifelse(database$optional_a == 0 & database$required_a == 0, 1, 0)
database$nocluster_b <- ifelse(database$optional_b == 0 & database$required_b == 0, 1, 0)

# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation

apollo_beta = c(mu_sq                         =  0,
                mu_asc                        =  0,
                mu_b_government               =  0,
                mu_b_retailer                 =  0, 
                mu_b_ngo                      =  0, 
                mu_b_transfer                 =  0,
                mu_b_label                    =  0, 
                mu_b_nocluster                =  0,
                mu_b_optional                 =  0, 
                mu_b_required                 =  0, 
                mu_log_b_cost                 = -5,
                sigma_sq                      =  0,
                sigma_asc                     =  0, 
                sigma_b_government            =  0,
                sigma_b_retailer              =  0,
                sigma_b_ngo                   =  0,
                sigma_b_transfer              =  0,
                sigma_b_label                 =  0,
                sigma_b_nocluster             =  0,
                sigma_b_optional              =  0,
                sigma_b_required              =  0,
                sigma_log_b_cost              = -2)

### 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() # maybe interesting for the latent class model. (independent availability model)

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

### Sobol: Analysis of diff. Types of draws: Czajkowski & Budzinski 2017
apollo_draws = list(
  interDrawsType = "sobol", #Halton Draws are not recommended for more than five random coefficents (cf. Bhat, 2003) For computing Bayesian efficiency pseudo random draws or pseudo monte carlo are the simplest (ChoiceMetrics, 2018).
  interNDraws    = 3000, # More than 2000 Sobol Draws recommendet by Czajkowskiei & Budzinski 2019
  interUnifDraws = c(),
  interNormDraws = c("draws_sq","draws_asc", "draws_government", "draws_retailer","draws_ngo", "draws_transfer", "draws_label", "draws_nocluster", "draws_optional","draws_required","draws_cost")
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  
  randcoeff[["b_sq"]]          =     ( mu_sq            + sigma_sq           * draws_sq )
  randcoeff[["b_asc"]]         =     ( mu_asc           + sigma_asc          * draws_asc )
  
  randcoeff[["b_government"]]  =     ( mu_b_government  + sigma_b_government * draws_government )
  randcoeff[["b_retailer"]]    =     ( mu_b_retailer    + sigma_b_retailer   * draws_retailer )
  randcoeff[["b_ngo"]]         =     ( mu_b_ngo         + sigma_b_ngo        * draws_ngo )
  
  randcoeff[["b_transfer"]]    =     ( mu_b_transfer    + sigma_b_transfer   * draws_transfer )
  randcoeff[["b_label"]]       =     ( mu_b_label       + sigma_b_label      * draws_label )
  
  randcoeff[["b_nocluster"]]   =     ( mu_b_nocluster   + sigma_b_nocluster  * draws_nocluster )
  randcoeff[["b_optional"]]    =     ( mu_b_optional    + sigma_b_optional   * draws_optional )
  randcoeff[["b_required"]]    =     ( mu_b_required    + sigma_b_required   * draws_required)
  
  randcoeff[["b_cost"]]        =  exp( mu_log_b_cost    + sigma_log_b_cost   * draws_cost )
  
  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 initialization: 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()
  V[['A']]  = b_asc + b_government * government_a + b_retailer * retailer_a + b_ngo * ngo_a + b_transfer * transfer_a + b_label * label_a + b_nocluster * nocluster_a + b_optional * optional_a + b_required * required_a + b_cost * cost_a
  V[['B']]  = b_asc + b_government * government_b + b_retailer * retailer_b + b_ngo * ngo_b + b_transfer * transfer_b + b_label * label_b + b_nocluster * nocluster_b + b_optional * optional_b + b_required * required_b + b_cost * cost_b
  V[['C']]  = b_sq
  
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives  = c(A=1, B=2, C=3), 
    avail         = 1,
    choiceVar     = decision, # depend on the variable used in stata
    V             = V
  )
  
  ### Compute probabilities using MNL model
  P[['model']] = apollo_mnl(mnl_settings, functionality)
  
  ### Take product across observation for same individual
  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, list(printPVal = 2))

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

apollo_saveOutput(
  model, 
  list(printPVal = 2)
)

#################################
# Cost Parameter Transformation #
#################################

# transform the cost parameter. Look up into the Forum
# follow link to the Forum Post: use method suggested by D Palma : http://www.apollochoicemodelling.com/forum/viewtopic.php?f=16&t=262
# sigma is the standard deviation,  mu gives average preference.

apollo_deltaMethod(model, deltaMethod_settings = list(operation="lognormal",parName1="mu_log_b_cost",parName2="sigma_log_b_cost"))

############################################################
# Estimating difference between NGO and retailer estimator #
############################################################
# compare https://www.apollochoicemodelling.com/forum/viewtopic.php?t=202.
apollo_deltaMethod(model, deltaMethod_settings = list(operation="diff",parName1="mu_b_retailer",parName2="mu_b_ngo"))

#############################################################
# Estimating difference between FC optional and FC required #
#############################################################
# compare https://www.apollochoicemodelling.com/forum/viewtopic.php?t=202.
apollo_deltaMethod(model, deltaMethod_settings = list(operation="diff",parName1="mu_b_optional",parName2="mu_b_required"))




# ########################################################################## #
#### Adapted Overspecified MXL model in utility space                     ####
# ########################################################################## #
# ################################################################# #
#### Setting up the work environment                             ####
# ################################################################# #

### Clear memory
rm(list = ls())
#to check directory (folder where we are working)
getwd()
# to change working directory
setwd("U:/Meine Bibliothek/Paper_/Paper 4 Choice Experiment - CL MNL/5_Data/5_MainData_Collection/3_R/WTP Space/MXL in WTP - Palma Method")

# Load R packages from library
library(apollo)
library(tidyr)
library(readxl)

### Initialise code
apollo_initialise()

# ########################################################################## #
#### Adapted Overspecified MXL model in utility space                     ####
# ########################################################################## #

### Set core controls
apollo_control = list(
  modelName  ="MXLmodel_Paper4_utility_palma_overspecified_adapted",
  modelDescr ="MXL model in Utility Space paper 4 - palma method - overspecified SQ",
  indivID    ="id",
  nCores     = 6,
  mixing     = TRUE
)

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

library(haven)
database <- read.csv("Data_UKNLFR_mf_.csv", header = TRUE)

# ########################################################################## #
#### Creating categorical variables 'government', 'transfer', 'nocluster' ####
# ########################################################################## #

# 'government' dummy
database$government_a <- ifelse(database$retailer_a == 0 & database$ngo_a == 0, 1, 0)
database$government_b <- ifelse(database$retailer_b == 0 & database$ngo_b == 0, 1, 0)

# 'transfer' dummy
database$transfer_a <- ifelse(database$label_a == 0, 1, 0)
database$transfer_b <- ifelse(database$label_b == 0, 1, 0)

# 'nocluster' dummy
database$nocluster_a <- ifelse(database$optional_a == 0 & database$required_a == 0, 1, 0)
database$nocluster_b <- ifelse(database$optional_b == 0 & database$required_b == 0, 1, 0)

# ################################################################# #
#### Choosing Reference levels                                   ####
# ################################################################# #

# Depending on the sigma parameters we choose the reference levels for the MXL model.
# The reference level is the attribute with the lowest SD.
# Reference levels: 
# ASC      = -0.30596
# retailer =  0.23446
# label    = -0.14067
# required =  0.01226

# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation

apollo_beta = c(mu_sq                         =  0,
                #mu_asc                        =  0, 
                mu_b_government               =  0,
                mu_b_ngo                      =  0, 
                mu_b_transfer                 =  0, 
                mu_b_nocluster                =  0,
                mu_b_optional                 =  0, 
                mu_log_b_cost                 = -5,
                sigma_sq                      =  0,
                #sigma_asc                     =  0,
                sigma_b_government            =  0,
                sigma_b_ngo                   =  0,
                sigma_b_transfer              =  0,
                sigma_b_nocluster             =  0,
                sigma_b_optional              =  0,
                sigma_log_b_cost              = -2)

### 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() # maybe interesting for the latent class model. (independent availability model)

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

### Sobol: Analysis of diff. Types of draws: Czajkowski & Budzinski 2017
apollo_draws = list(
  interDrawsType = "sobol", #Halton Draws are not recommended for more than five random coefficents (cf. Bhat, 2003) For computing Bayesian efficiency pseudo random draws or pseudo monte carlo are the simplest (ChoiceMetrics, 2018).
  interNDraws    = 3000, # More than 2000 Sobol Draws recommendet by Czajkowskiei & Budzinski 2019
  interUnifDraws = c(),
  interNormDraws = c("draws_sq","draws_asc", "draws_government", "draws_retailer","draws_ngo", "draws_transfer", "draws_label", "draws_nocluster", "draws_optional","draws_required","draws_cost")
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  
  randcoeff[["b_sq"]]              =     ( mu_sq            + sigma_sq           * draws_sq         )
  #randcoeff[["b_asc"]]            =     ( mu_asc           + sigma_asc          * draws_asc        )
  

  randcoeff[["b_government"]]      =     ( mu_b_government  + sigma_b_government * draws_government )
  randcoeff[["b_ngo"]]             =     ( mu_b_ngo         + sigma_b_ngo        * draws_ngo        )

  randcoeff[["b_transfer"]]        =     ( mu_b_transfer    + sigma_b_transfer   * draws_transfer   )
  
  randcoeff[["b_nocluster"]]       =     ( mu_b_nocluster   + sigma_b_nocluster  * draws_nocluster  )
  randcoeff[["b_optional"]]        =     ( mu_b_optional    + sigma_b_optional   * draws_optional   )

  randcoeff[["b_cost"]]            =  exp( mu_log_b_cost    + sigma_log_b_cost   * draws_cost       )
  
  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 initialization: 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()
  V[['A']]  = b_government * government_a + b_ngo * ngo_a + b_transfer * transfer_a + b_nocluster * nocluster_a + b_optional * optional_a + b_cost * cost_a
  V[['B']]  = b_government * government_b + b_ngo * ngo_b + b_transfer * transfer_b + b_nocluster * nocluster_b + b_optional * optional_b + b_cost * cost_b
  V[['C']]  = b_sq
  #V[['C']]  = 0
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives  = c(A=1, B=2, C=3), 
    avail         = 1,
    choiceVar     = decision, # depend on the variable used in stata
    V             = V
  )
  
  ### Compute probabilities using MNL model
  P[['model']] = apollo_mnl(mnl_settings, functionality)
  
  ### Take product across observation for same individual
  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, list(printPVal = 2))

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

apollo_saveOutput(
  model, 
  list(printPVal = 2)
)

#################################
# Cost Parameter Transformation #
#################################

# transform the cost parameter. Look up into the Forum
# follow link to the Forum Post: use method suggested by D Palma : http://www.apollochoicemodelling.com/forum/viewtopic.php?f=16&t=262
# sigma is the standard deviation,  mu gives average preference.

apollo_deltaMethod(model, deltaMethod_settings = list(operation="lognormal",parName1="mu_log_b_cost",parName2="sigma_log_b_cost"))

############################################################
# Estimating difference between NGO and retailer estimator #
############################################################
# compare https://www.apollochoicemodelling.com/forum/viewtopic.php?t=202.
apollo_deltaMethod(model, deltaMethod_settings = list(operation="diff",parName1="mu_b_government",parName2="mu_b_retailer"))

#############################################################
# Estimating difference between FC optional and FC required #
#############################################################
# compare https://www.apollochoicemodelling.com/forum/viewtopic.php?t=202.
apollo_deltaMethod(model, deltaMethod_settings = list(operation="diff",parName1="mu_b_nocluster",parName2="mu_b_optional"))


# ################################################################# #
#### Setting up the work environment                             ####
# ################################################################# #

### Clear memory
rm(list = ls())
#to check directory (folder where we are working)
getwd()
# to change working directory
setwd("U:/Meine Bibliothek/Paper_/Paper 4 Choice Experiment - CL MNL/5_Data/5_MainData_Collection/3_R/WTP Space/MXL in WTP - Palma Method")

# Load R packages from library
library(apollo)
library(tidyr)
library(readxl)

### Initialise code
apollo_initialise()

# ########################################################################## #
#### Adapted MXL model in utility space                                   ####
# ########################################################################## #

### Set core controls
apollo_control = list(
  modelName  ="MXLmodel_Paper4_WTP_palma_overspecified_adapted ASC",
  modelDescr ="MXL model in WTP paper 4 - palma method - overspecified ASC",
  indivID    ="id",
  nCores     = 6,
  mixing     = TRUE
)

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

library(haven)
database <- read.csv("Data_UKNLFR_mf_.csv", header = TRUE)

# ########################################################################## #
#### Creating categorical variables 'government', 'transfer', 'nocluster' ####
# ########################################################################## #

# 'government' dummy
database$government_a <- ifelse(database$retailer_a == 0 & database$ngo_a == 0, 1, 0)
database$government_b <- ifelse(database$retailer_b == 0 & database$ngo_b == 0, 1, 0)

# 'transfer' dummy
database$transfer_a <- ifelse(database$label_a == 0, 1, 0)
database$transfer_b <- ifelse(database$label_b == 0, 1, 0)

# 'nocluster' dummy
database$nocluster_a <- ifelse(database$optional_a == 0 & database$required_a == 0, 1, 0)
database$nocluster_b <- ifelse(database$optional_b == 0 & database$required_b == 0, 1, 0)

# ################################################################# #
#### Choosing Reference levels                                   ####
# ################################################################# #

# Depending on the sigma parameters we choose the reference levels for the MXL model.
# The reference level is the attribute with the lowest SD.
# Reference levels: NGO, transfer, required
# ASC is due to WTP transformation not the ref level anymore

# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation

apollo_beta = c(mu_sq                         =  0,
                #mu_asc                        =  0, 
                mu_b_government               =  0,
                mu_b_ngo                      =  0, 
                mu_b_transfer                 =  0, 
                mu_b_nocluster                =  0,
                mu_b_optional                 =  0, 
                mu_log_b_cost                 = -5,
                sigma_sq                      =  0,
              #  sigma_asc                     =  0,
                sigma_b_government            =  0,
                sigma_b_ngo                   =  0,
                sigma_b_transfer              =  0,
                sigma_b_nocluster             =  0,
                sigma_b_optional              =  0,
                sigma_log_b_cost              = -2)

### 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() # maybe interesting for the latent class model. (independent availability model)

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

### Sobol: Analysis of diff. Types of draws: Czajkowski & Budzinski 2017
apollo_draws = list(
  interDrawsType = "sobol", #Halton Draws are not recommended for more than five random coefficents (cf. Bhat, 2003) For computing Bayesian efficiency pseudo random draws or pseudo monte carlo are the simplest (ChoiceMetrics, 2018).
  interNDraws    = 3000, # More than 2000 Sobol Draws recommendet by Czajkowskiei & Budzinski 2019
  interUnifDraws = c(),
  interNormDraws = c("draws_sq","draws_asc", "draws_government", "draws_retailer","draws_ngo", "draws_transfer", "draws_label", "draws_nocluster", "draws_optional","draws_required","draws_cost")
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  
  randcoeff[["b_sq"]]              =     ( mu_sq            + sigma_sq           * draws_sq         )
  #randcoeff[["b_asc"]]            =     ( mu_asc           + sigma_asc          * draws_asc        )
  
  
  randcoeff[["b_government"]]      =     ( mu_b_government  + sigma_b_government * draws_government )
  randcoeff[["b_ngo"]]             =     ( mu_b_ngo         + sigma_b_ngo        * draws_ngo        )
  
  randcoeff[["b_transfer"]]        =     ( mu_b_transfer    + sigma_b_transfer   * draws_transfer   )
  
  randcoeff[["b_nocluster"]]       =     ( mu_b_nocluster   + sigma_b_nocluster  * draws_nocluster  )
  randcoeff[["b_optional"]]        =     ( mu_b_optional    + sigma_b_optional   * draws_optional   )
  
  randcoeff[["b_cost"]]            =  exp( mu_log_b_cost    + sigma_log_b_cost   * draws_cost       )
  
  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 initialization: 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()
  #V[['A']]  = b_cost * (b_asc + b_government * government_a + b_retailer * retailer_a + b_label * label_a + b_nocluster * nocluster_a + b_optional * optional_a +  cost_a)
  #V[['B']]  = b_cost * (b_asc + b_government * government_b + b_retailer * retailer_b + b_label * label_b + b_nocluster * nocluster_b + b_optional * optional_b +  cost_b)
  #V[['C']]  = 0
  
  #V[['A']]  = b_cost * (b_asc + b_government * government_a + b_ngo * ngo_a + b_transfer * transfer_a + b_nocluster * nocluster_a + b_optional * optional_a + b_cost * cost_a)
  #V[['B']]  = b_cost * (b_asc + b_government * government_b + b_ngo * ngo_b + b_transfer * transfer_b + b_nocluster * nocluster_b + b_optional * optional_b + b_cost * cost_b)
  #V[['C']]  = 0
  
 # Results of the V with sq times b_sq makes no sense.
 V[['A']]  = b_cost * (b_government * government_a + b_ngo * ngo_a + b_transfer * transfer_a + b_nocluster * nocluster_a + b_optional * optional_a + cost_a)
 V[['B']]  = b_cost * (b_government * government_b + b_ngo * ngo_b + b_transfer * transfer_b + b_nocluster * nocluster_b + b_optional * optional_b + cost_b)
 V[['C']]  = b_cost * ( b_sq)

    ### Define settings for MNL model component
    mnl_settings = list(
      alternatives  = c(A=1, B=2, C=3), 
      avail         = 1,
      choiceVar     = decision, # depend on the variable used in stata
      V             = V
    )
    
    ### Compute probabilities using MNL model
    P[['model']] = apollo_mnl(mnl_settings, functionality)
    
    ### Take product across observation for same individual
    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, list(printPVal = 2))
  
  ## Output of s.e., t.rat(0), p(2-sided), Rob.s.e., Rob.t.rat.(0), p(2-sided) => NA
  ## Maybe because of too little observations?
  ## But I currently do not know which values I need to put into the vectors of the parameters
  
  # ----------------------------------------------------------------- #
  #---- FORMATTED OUTPUT (TO FILE, using model name)               ----
  # ----------------------------------------------------------------- #
  apollo_saveOutput(
    model, 
    list(printPVal = 2)
  )
  
#################################
# Cost Parameter Transformation #
#################################

# transform the cost parameter. Look up into the Forum
# follow link to the Forum Post: use method suggested by D Palma : http://www.apollochoicemodelling.com/forum/viewtopic.php?f=16&t=262
# sigma is the standard deviation,  mu gives average preference.

apollo_deltaMethod(model, deltaMethod_settings = list(operation="lognormal",parName1="mu_log_b_cost",parName2="sigma_log_b_cost"))

############################################################
# Estimating difference between NGO and retailer estimator #
############################################################
# compare https://www.apollochoicemodelling.com/forum/viewtopic.php?t=202.
apollo_deltaMethod(model, deltaMethod_settings = list(operation="diff",parName1="mu_b_government",parName2="mu_b_retailer"))

#############################################################
# Estimating difference between FC optional and FC required #
#############################################################
# compare https://www.apollochoicemodelling.com/forum/viewtopic.php?t=202.
apollo_deltaMethod(model, deltaMethod_settings = list(operation="diff",parName1="mu_b_nocluster",parName2="mu_b_optional"))
All models in utility space differ quite strongly from the initial MXL estimated in utility space using the standard approach from the Apollo scripts.

Each estimation leads to results that differ between utility and WTP spaces. In all cases, strong heterogeneity remains. Furthermore, the ASC/SQ estimator in the WTP space does not align with the utility space. An LL ratio test shows us that the first approach (correlation model) has the best model fit.

Considering all the information, I am concerned about whether I have done the estimation right and, if yes, how I should decide on one of the models?

To make the above passage easier to comprehend, I have added my original, standard mixed logit model in the utility space below. The 'Standard Mixed logit Model' shows the model in the utility space, which, like the MMNL in the preference space, was coded on the basis of the Apollo example files (https://www.apollochoicemodelling.com/f ... ce_space.r).

Code: Select all

Model name                                  : FarmerDCE_MXL1
Model description                           : MXL model for Farmersurvey data Paper 2 (not correlated random parameters)
Model run at                                : 2025-03-28 11:14:17.868265
Estimation method                           : bgw
Model diagnosis                             : Relative function convergence
Optimisation diagnosis                      : Maximum found
     hessian properties                     : Negative definite
     maximum eigenvalue                     : -2.618964
     reciprocal of condition number         : 0.0074851
Number of individuals                       : 761
Number of rows in database                  : 4552
Number of modelled outcomes                 : 4552

Number of cores used                        :  1 
Number of inter-individual draws            : 3000 (sobol)

LL(start)                                   : -4488.61
LL at equal shares, LL(0)                   : -5000.88
LL at observed shares, LL(C)                : -4952.04
LL(final)                                   : -3363.6
Rho-squared vs equal shares                  :  0.3274 
Adj.Rho-squared vs equal shares              :  0.3246 
Rho-squared vs observed shares               :  0.3208 
Adj.Rho-squared vs observed shares           :  0.3183 
AIC                                         :  6755.19 
BIC                                         :  6845.12 

Estimated parameters                        : 14
Time taken (hh:mm:ss)                       :  00:27:5.33 
     pre-estimation                         :  00:00:47.4 
     estimation                             :  00:06:43.47 
          initial estimation                :  00:05:44.67 
          estimation after rescaling        :  00:00:58.8 
     post-estimation                        :  00:19:34.45 
Iterations                                  :  17  
     initial estimation                     :  16 
     estimation after rescaling             :  1 

Unconstrained optimisation.

Estimates:
                    Estimate        s.e.   t.rat.(0)  p(2-sided)    Rob.s.e. Rob.t.rat.(0)  p(2-sided)
mu_asc              -0.56515     0.24762     -2.2823    0.022471     0.25116       -2.2502    0.024439
mu_b_retailer       -0.22523     0.08186     -2.7514    0.005934     0.08632       -2.6093    0.009074
mu_b_ngo            -0.23255     0.08224     -2.8279    0.004686     0.08813       -2.6387    0.008322
mu_b_label          -0.28538     0.06166     -4.6283   3.686e-06     0.06197       -4.6052   4.120e-06
mu_b_optional        0.21149     0.07670      2.7576    0.005823     0.07852        2.6935    0.007070
mu_b_required       -0.12124     0.07845     -1.5454    0.122239     0.07974       -1.5205    0.128386
mu_log_b_cost       -7.71787     0.35608    -21.6747    0.000000     0.36727      -21.0142    0.000000
sigma_asc            5.21791     0.34170     15.2706    0.000000     0.34525       15.1134    0.000000
sigma_b_retailer    -0.77993     0.13290     -5.8685   4.398e-09     0.13393       -5.8232   5.772e-09
sigma_b_ngo         -0.79100     0.13067     -6.0535   1.417e-09     0.13348       -5.9261   3.103e-09
sigma_b_label       -0.61441     0.10529     -5.8355   5.362e-09     0.11125       -5.5228   3.337e-08
sigma_b_optional    -0.36416     0.22565     -1.6139    0.106557     0.24922       -1.4612    0.143960
sigma_b_required    -0.09507     0.61710     -0.1541    0.877560     0.50706       -0.1875    0.851270
sigma_log_b_cost    -2.53072     0.27538     -9.1898    0.000000     0.26214       -9.6540    0.000000

Thank you very much for your help already.
Looking forward to reading your thoughts.
Best,
Moritz

(End of Part 2)
stephanehess
Site Admin
Posts: 1295
Joined: 24 Apr 2020, 16:29

Re: Strong differences in WTP and utility space estimations using mixed logit model

Post by stephanehess »

Hi

you should not expext to get the same results for utility space and WTP space with mixed logit as the distributions for the impacts of the attributes will not be the same.

imagine e.g. you have:

Code: Select all

b1 * x1 + bcost * cost
vs

Code: Select all

b_cost * ( v1 * x1 + cost)
if you use a lognormal distribution for bcost and normal for b1, then the impact of x1 will be normally distributed in the first model, but will be the product of normal and lognormal in the second model. They cannot be the same

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
ramkumar
Posts: 6
Joined: 02 Jun 2023, 16:53

Re: Strong differences in WTP and utility space estimations using mixed logit model

Post by ramkumar »

Hi Stephane,

I have two broad questions here. First, why do we get the estimated values for 'cost or price parameter' from the preference space and WTP space model different, since they have the same notations in both specifications (Trains and Weeks 2005; Scarpa, Thiene and Train 2008).

Second, are the parameter values that we get from the preference space model scaled by scale parameter (k) or not? I did not see any scaling of utility coefficients in Apollo package. However, Trains and Weeks (2005) and Scarpa, Thiene and Train (2008) mentioned that these values are scaled. One of your papers (Hess and Train 2017) mentioned that it is unscaled values.
stephanehess
Site Admin
Posts: 1295
Joined: 24 Apr 2020, 16:29

Re: Strong differences in WTP and utility space estimations using mixed logit model

Post by stephanehess »

Hi

the answer to the first question relates to the point I made before. With your choice of distributions, the two models are not equivalent, and so there is no reason that the coefficient for cost would be the same in the two models.

In relation to the parameters being scaled, I'm not sure what exactly you refer to there. All parameters depend on the scale used, and the assumption in your model is that mu=1

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