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. This may take a day or two at busy times. There is no need to submit the post multiple times.

Issues with MDECV (prediction, outside good and covariates)

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
Miller Cooker
Posts: 4
Joined: 15 Nov 2022, 19:49

Issues with MDECV (prediction, outside good and covariates)

Post by Miller Cooker »

Hi Expert,

I have three issues with MDCEV estimation. I tried the following three models; each has an issue:

i) with outside good + without covariates: the model runs, but prediction is very huge outside the normal range

ii) without outside good + without covariates: there is error message: Error in if (choicematrix[2, a] == choicematrix[1, a] && inputs$altnames[a] != : missing value where TRUE/FALSE needed

iii) with outside good + with covariates: model runs, but gamma can not be estimated; it show 'NA'.

I am not sure whether it is because my 'budget' is not vector. I attached my codes there. Really appreciate if you would like to help me with those issues.

Many thanks,

Connie

i) with outside good + without covariates:

Code: Select all

### Set core controls
apollo_control = list(
  modelName  ="MDCEV food test2",
  modelDescr ="MDCEV model on MUP data, alpha-gamma profile with outside good and constants only in utilities",
  indivID    ="hid",
  panelData=TRUE,
  workInLogs=TRUE,
  outputDirectory = "output")

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

### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv), 
### the code would be: database = read.csv("data.csv",header=TRUE)
test.Scotland <- read.csv("D:/MDCEV/Data/test Scotland.csv")
database = test.Scotland 

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(alpha_base    = -1,
                gamma_alco    = 1,
                gamma_food1   = 1,
                gamma_food2   = 1,
                gamma_food3   = 1,
                gamma_food4   = 1,
                gamma_food5   = 1,
                gamma_food6   = 1,
                gamma_food7   = 1,
                gamma_food8   = 1,
                
                delta_alco    = 0,
                delta_food1   = 0,
                delta_food2   = 0,
                delta_food3   = 0,
                delta_food4   = 0,
                delta_food5   = 0,
                delta_food6   = 0,
                delta_food7   = 0,
                delta_food8   = 0,
                sigma         = 1)

### 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("sigma") 
#sigma is not fixed as price of products are different?? 
apollo_fixed = c()

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

apollo_inputs = apollo_validateInputs()

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

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
  
  ### Attach inputs and detach after function exit
  apollo_attach(apollo_beta, apollo_inputs)
  on.exit(apollo_detach(apollo_beta, apollo_inputs))
  
  ### Create list of probabilities P
  P = list()
  
  ### Define individual alternatives
  alternatives = c("outside", 
                   "alco", 
                   "food1", 
                   "food2", 
                   "food3", 
                   "food4", 
                   "food5", 
                   "food6",
                   "food7",
                   "food8")
  
  ### Define availabilities
  avail = list(outside  = 1, 
               alco     = 1,
               food1    = 1,
               food2    = 1,
               food3    = 1,
               food4    = 1,
               food5    = 1,
               food6    = 1,
               food7    = 1,
               food8    = 1)
  
  ### Define continuous consumption for individual alternatives
  continuousChoice = list(outside = outside,
                          alco    = alco,
                          food1   = food1,
                          food2   = food2,
                          food3   = food3,
                          food4   = food4,
                          food5   = food5,
                          food6   = food6,
                          food7   = food7,
                          food8   = food8)
  
  ### Define utilities for individual alternatives
  V = list()
  V[["outside" ]] = 0
  V[["alco"]]     = delta_alco    
  V[["food1"]]    = delta_food1  
  V[["food2"]]    = delta_food2
  V[["food3"]]    = delta_food3
  V[["food4" ]]   = delta_food4 
  V[["food5"]]    = delta_food5
  V[["food6"]]    = delta_food6
  V[["food7"]]    = delta_food7
  V[["food8"]]    = delta_food8
  
  ### Define alpha parameters
  alpha=setNames(as.list(rep(1/(1+exp(-alpha_base)),10)), alternatives)

  
  ### Define gamma parameters
  gamma = list(alco  = gamma_alco,
               food1 = gamma_food1,
               food2 = gamma_food2,
               food3 = gamma_food3,
               food4 = gamma_food4,
               food5 = gamma_food5,
               food6 = gamma_food6,
               food7 = gamma_food7,
               food8 = gamma_food8)
  
  ### Define costs for individual alternatives
  cost = list(outside = p_outside,
              alco  = p_alco,
              food1 = p1_IM,
              food2 = p2_IM,
              food3 = p3_IM,
              food4 = p4_IM,
              food5 = p5_IM,
              food6 = p6_IM,
              food7 = p7_IM,
              food8 = p8_IM)
  
  ### Define settings for MDCEV model
  mdcev_settings <- list(alternatives      = alternatives,
                         avail             = avail,
                         continuousChoice  = continuousChoice,
                         V                 = V,
                         alpha             = alpha,
                         gamma             = gamma, 
                         sigma             = sigma, 
                         cost              = cost,
                         budget            = EP) 
  
  ### Compute probabilities using MDCEV model
  P[["model"]] = apollo_mdcev(mdcev_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)
The following is the prediction from model (i). The values are so large.

Code: Select all

 predictions_base = apollo_prediction(model, apollo_probabilities, apollo_inputs)
Running predictions from model using parameter estimates...
0%....50%....100%
Aggregated predictions (continuous consumption, discrete choices, and expenditure) at model
  estimates
        cont_mean    cont_sd disc_mean disc_sd expe_mean expe_sd
outside   5913029   18878.05  54255.00    0.00 297226.34 4230.38
alco      1496090   34701.65  40115.32  102.25 102063.11 2281.19
food1    25799477  679103.48   8822.48   85.95  99703.92 2408.79
food2    59745120 1759992.33  10431.34   91.79  76651.62 2042.78
food3     1252930   92100.98    871.68   29.29  12782.19  891.28
food4    13203960  404122.83   3801.05   59.45  78401.26 2300.09
food5    30224559 1253845.88   7574.02   80.73  51724.56 1661.07
food6     2408805  139905.14   2216.24   46.10  17160.70  963.13
food7    56852787 2549171.29   6139.85   73.79  48255.05 1640.29
food8    27646613  777760.60  10537.38   92.14  78691.64 2067.66


ii) without outside good + without covariates

Code: Select all

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName  ="MDCEV food test3",
  modelDescr ="MDCEV model on MUP data, alpha-gamma profile without outside good and constants only in utilities",
  indivID    ="hid",
  panelData=TRUE,
  workInLogs=TRUE,
  outputDirectory = "output")

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

### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv), 
### the code would be: database = read.csv("data.csv",header=TRUE)
test.Scotland <- read.csv("D:/MDCEV/Data/test Scotland.csv")
database = test.Scotland 

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(alpha_base    = -1,
                gamma_alco    = 1,
                gamma_food1   = 1,
                gamma_food2   = 1,
                gamma_food3   = 1,
                gamma_food4   = 1,
                gamma_food5   = 1,
                gamma_food6   = 1,
                gamma_food7   = 1,
                gamma_food8   = 1,
                
                delta_alco    = 0,
                delta_food1   = 0,
                delta_food2   = 0,
                delta_food3   = 0,
                delta_food4   = 0,
                delta_food5   = 0,
                delta_food6   = 0,
                delta_food7   = 0,
                delta_food8   = 0,
                sigma         = 1)

### 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("sigma") 
#sigma is not fixed as price of products are different?? 
apollo_fixed = c()

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

apollo_inputs = apollo_validateInputs()

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

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
  
  ### Attach inputs and detach after function exit
  apollo_attach(apollo_beta, apollo_inputs)
  on.exit(apollo_detach(apollo_beta, apollo_inputs))
  
  ### Create list of probabilities P
  P = list()
  
  ### Define individual alternatives
  alternatives = c("alco", 
                   "food1", 
                   "food2", 
                   "food3", 
                   "food4", 
                   "food5", 
                   "food6",
                   "food7",
                   "food8")
  
  ### Define availabilities
  avail = list(alco     = 1,
               food1    = 1,
               food2    = 1,
               food3    = 1,
               food4    = 1,
               food5    = 1,
               food6    = 1,
               food7    = 1,
               food8    = 1)
  
  ### Define continuous consumption for individual alternatives
  continuousChoice = list(alco    = alco,
                          food1   = food1,
                          food2   = food2,
                          food3   = food3,
                          food4   = food4,
                          food5   = food5,
                          food6   = food6,
                          food7   = food7,
                          food8   = food8)
  
  ### Define utilities for individual alternatives
  V = list()
  V[["alco"]]     = delta_alco    
  V[["food1"]]    = delta_food1  
  V[["food2"]]    = delta_food2
  V[["food3"]]    = delta_food3
  V[["food4" ]]   = delta_food4 
  V[["food5"]]    = delta_food5
  V[["food6"]]    = delta_food6
  V[["food7"]]    = delta_food7
  V[["food8"]]    = delta_food8
  
  ### Define alpha parameters
  alpha=setNames(as.list(rep(1/(1+exp(-alpha_base)),9)), alternatives)
  
  
  ### Define gamma parameters
  gamma = list(alco  = gamma_alco,
               food1 = gamma_food1,
               food2 = gamma_food2,
               food3 = gamma_food3,
               food4 = gamma_food4,
               food5 = gamma_food5,
               food6 = gamma_food6,
               food7 = gamma_food7,
               food8 = gamma_food8)
  
  ### Define costs for individual alternatives
  cost = list(alco  = p_alco,
              food1 = p1_IM,
              food2 = p2_IM,
              food3 = p3_IM,
              food4 = p4_IM,
              food5 = p5_IM,
              food6 = p6_IM,
              food7 = p7_IM,
              food8 = p8_IM)
  
  ### Define settings for MDCEV model
  mdcev_settings <- list(alternatives      = alternatives,
                         avail             = avail,
                         continuousChoice  = continuousChoice,
                         V                 = V,
                         alpha             = alpha,
                         gamma             = gamma, 
                         sigma             = sigma, 
                         cost              = cost,
                         budget            = EP1) 
  
  ### Compute probabilities using MDCEV model
  P[["model"]] = apollo_mdcev(mdcev_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)
iii) with outside good + with covariates

Code: Select all

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName  ="MDCEV food test3",
  modelDescr ="MDCEV model on MUP data, alpha-gamma profile with outside good and covariates",
  indivID    ="hid",
  panelData=TRUE,
  workInLogs=TRUE,
  outputDirectory = "output")

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

### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv), 
### the code would be: database = read.csv("data.csv",header=TRUE)
test.Scotland <- read.csv("D:/MDCEV/Data/test Scotland.csv")
database = test.Scotland 

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(alpha_base    = -1,
                gamma_alco    = 1,
                gamma_food1   = 1,
                gamma_food2   = 1,
                gamma_food3   = 1,
                gamma_food4   = 1,
                gamma_food5   = 1,
                gamma_food6   = 1,
                gamma_food7   = 1,
                gamma_food8   = 1,
                
                delta_alco    = 0,
                delta_food1   = 0,
                delta_food2   = 0,
                delta_food3   = 0,
                delta_food4   = 0,
                delta_food5   = 0,
                delta_food6   = 0,
                delta_food7   = 0,
                delta_food8   = 0,
                delta_demo_age   =0,
                delta_demo_hsize =0, 
                delta_gender1    =0,
                sigma         = 1)

### 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("sigma") 
#sigma is not fixed as price of products are different?? 
apollo_fixed = c()

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

apollo_inputs = apollo_validateInputs()

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

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
  
  ### Attach inputs and detach after function exit
  apollo_attach(apollo_beta, apollo_inputs)
  on.exit(apollo_detach(apollo_beta, apollo_inputs))
  
  ### Create list of probabilities P
  P = list()
  
  ### Define individual alternatives
  alternatives = c("outside", 
                   "alco", 
                   "food1", 
                   "food2", 
                   "food3", 
                   "food4", 
                   "food5", 
                   "food6",
                   "food7",
                   "food8")
  
  ### Define availabilities
  avail = list(outside  = 1, 
               alco     = 1,
               food1    = 1,
               food2    = 1,
               food3    = 1,
               food4    = 1,
               food5    = 1,
               food6    = 1,
               food7    = 1,
               food8    = 1)
  
  ### Define continuous consumption for individual alternatives
  continuousChoice = list(outside = outside,
                          alco    = alco,
                          food1   = food1,
                          food2   = food2,
                          food3   = food3,
                          food4   = food4,
                          food5   = food5,
                          food6   = food6,
                          food7   = food7,
                          food8   = food8)
  
  ### Define utilities for individual alternatives
  V = list()
  V[["outside" ]] = 0
  V[["alco"]]     = delta_alco   + delta_demo_age*(demo_age)+ delta_demo_hsize*(demo_hsize)+ delta_gender1*(gender1)
  V[["food1"]]    = delta_food1  + delta_demo_age*(demo_age)+ delta_demo_hsize*(demo_hsize)+ delta_gender1*(gender1)
  V[["food2"]]    = delta_food2  + delta_demo_age*(demo_age)+ delta_demo_hsize*(demo_hsize)+ delta_gender1*(gender1)
  V[["food3"]]    = delta_food3  + delta_demo_age*(demo_age)+ delta_demo_hsize*(demo_hsize)+ delta_gender1*(gender1)
  V[["food4" ]]   = delta_food4  + delta_demo_age*(demo_age)+ delta_demo_hsize*(demo_hsize)+ delta_gender1*(gender1)
  V[["food5"]]    = delta_food5  + delta_demo_age*(demo_age)+ delta_demo_hsize*(demo_hsize)+ delta_gender1*(gender1)
  V[["food6"]]    = delta_food6  + delta_demo_age*(demo_age)+ delta_demo_hsize*(demo_hsize)+ delta_gender1*(gender1)
  V[["food7"]]    = delta_food7  + delta_demo_age*(demo_age)+ delta_demo_hsize*(demo_hsize)+ delta_gender1*(gender1)
  V[["food8"]]    = delta_food8  + delta_demo_age*(demo_age)+ delta_demo_hsize*(demo_hsize)+ delta_gender1*(gender1)
  
  ### Define alpha parameters
  alpha=setNames(as.list(rep(1/(1+exp(-alpha_base)),10)), alternatives)
  
  
  ### Define gamma parameters
  gamma = list(alco  = gamma_alco,
               food1 = gamma_food1,
               food2 = gamma_food2,
               food3 = gamma_food3,
               food4 = gamma_food4,
               food5 = gamma_food5,
               food6 = gamma_food6,
               food7 = gamma_food7,
               food8 = gamma_food8)
  
  ### Define costs for individual alternatives
  cost = list(outside = p_outside,
              alco  = p_alco,
              food1 = p1_IM,
              food2 = p2_IM,
              food3 = p3_IM,
              food4 = p4_IM,
              food5 = p5_IM,
              food6 = p6_IM,
              food7 = p7_IM,
              food8 = p8_IM)
  
  ### Define settings for MDCEV model
  mdcev_settings <- list(alternatives      = alternatives,
                         avail             = avail,
                         continuousChoice  = continuousChoice,
                         V                 = V,
                         alpha             = alpha,
                         gamma             = gamma, 
                         sigma             = sigma, 
                         cost              = cost,
                         budget            = EP) 
  
  ### Compute probabilities using MDCEV model
  P[["model"]] = apollo_mdcev(mdcev_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)
The following is the results from model (iii)

Code: Select all

Model name                                  : MDCEV food test2
Model description                           : MDCEV model on MUP data, alpha-gamma profile with outside good and covariates
Model run at                                : 2022-11-16 15:50:42
Estimation method                           : bfgs
Model diagnosis                             : successful convergence 
Number of individuals                       : 2144
Number of rows in database                  : 54255
Number of modelled outcomes                 : 54255

Number of cores used                        :  1 
Model without mixing

LL(start)                                   : -8158414
LL at equal shares, LL(0)                   : Not applicable
LL at observed shares, LL(C)                : Not applicable
LL(final)                                   : -4897786
Rho-squared vs equal shares                  :  Not applicable 
Adj.Rho-squared vs equal shares              :  Not applicable 
Rho-squared vs observed shares               :  Not applicable 
Adj.Rho-squared vs observed shares           :  Not applicable 
AIC                                         :  9795612 
BIC                                         :  9795790 

Estimated parameters                        :  20
Time taken (hh:mm:ss)                       :  00:46:28.44 
     pre-estimation                         :  00:00:10.88 
     estimation                             :  00:33:55.94 
     post-estimation                        :  00:12:21.61 
Iterations                                  :  90  
Min abs eigenvalue of Hessian               :  4.524185e+14 
Some eigenvalues of Hessian are positive, indicating potential problems!

Unconstrained optimisation.

These outputs have had the scaling used in estimation applied to them.
Estimates:
               Estimate        s.e.   t.rat.(0)    Rob.s.e. Rob.t.rat.(0)
alpha_base  -2939.04906          NA          NA          NA            NA
gamma_alco    3.649e-07          NA          NA          NA            NA
gamma_food1     0.09901          NA          NA          NA            NA
gamma_food2   1.240e-06          NA          NA          NA            NA
gamma_food3     0.09402          NA          NA          NA            NA
gamma_food4     1.75688          NA          NA          NA            NA
gamma_food5     0.66024          NA          NA          NA            NA
gamma_food6     0.43798          NA          NA          NA            NA
gamma_food7     0.49028          NA          NA          NA            NA
gamma_food8     0.04843          NA          NA          NA            NA
delta_alco      7.68733          NA          NA          NA            NA
delta_food1    32.68932          NA          NA          NA            NA
delta_food2    36.92221          NA          NA          NA            NA
delta_food3   -36.89705          NA          NA          NA            NA
delta_food4   -52.97052          NA          NA          NA            NA
delta_food5    28.47300          NA          NA          NA            NA
delta_food6   -44.84746          NA          NA          NA            NA
delta_food7   -22.13459          NA          NA          NA            NA
delta_food8    33.56858          NA          NA          NA            NA
sigma          28.95119          NA          NA          NA            NA
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Issues with MDECV (prediction, outside good and covariates)

Post by stephanehess »

Connie

i) these are aggregate predictions, i.e. summing up demand across the whole data. Does that make more sense to you?

ii) which version of Apollo are you using?

iii) this could be an identification issue. How does the fit compare to model i)?

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Miller Cooker
Posts: 4
Joined: 15 Nov 2022, 19:49

Re: Issues with MDECV (prediction, outside good and covariates)

Post by Miller Cooker »

Stephane,

i) these are aggregate predictions, i.e. summing up demand across the whole data. Does that make more sense to you?

Yes. But the average predictions are three times smaller than the actual values. Alternatively, I tired to use expenditure as outcomes with price equals to 1; the prediction is very close to the actual ones. I guess goods with different unit prices may make estimation complicated? We have volume of good and price of that good. To calculate unit price, I just divide price/grams to get price per gram. This value however is very small (e.g. 0.0005). This may distort the estimation?

ii) which version of Apollo are you using?
I am using the latest one.

iii) this could be an identification issue. How does the fit compare to model i)?
I finally did not add any covariates. I tired to only add one covariate (e.g. age) for one alternative only, it shows “Log-likelihood calculation fails at starting values". I tried to debug this following Apollo handbook by using the starting values estimated from simple model, it does not work either.

Thanks,

Connie
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Issues with MDECV (prediction, outside good and covariates)

Post by stephanehess »

Hi

if your unit costs are different across products, then you can estimate sigma. Can you see what happens then?

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