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.

MDCEV model with perhaps budget issue

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

MDCEV model with perhaps budget issue

Post by Miller Cooker »

Hi Experts,

I am facing an issue about MDCEV model. There is an error message "Error in if (choicematrix[2, a] == choicematrix[1, a] && inputs$altnames[a] != :
missing value where TRUE/FALSE needed". My data has unit price for each alternative and a different budget for different observations. I guess the issue is my budget is not vector, but I do not know to adjust it in codes. However, if budget is not an issue, could you please help to check where the issue is?

Many thanks,

MC

Code: Select all

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName  ="MDCEV xx",
  modelDescr ="MDCEV model on xx data, alpha-gamma profile without outside good and without covariates 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 

EP<-database$EP_no

# ################################################################# #
#### 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 probabilities
  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  = p0,
              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_no) ## is this the issue? as this is a variable in my data but not a vector? 
  
  
  ### 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)
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: MDCEV model with perhaps budget issue

Post by dpalma »

Hi,

EP_no, the variable you are using as budget, is actually a column in your database (i.e. a vector), and not a scalar. So there should be no issue with that, assuming that the column actually exists in your data.

Not sure what the issue is. As a first step, I would recommend updating Apollo to the latest version by typing install.packages("apollo") in the console, and try running the model again. If that doesn't solve the issue, please share your full script and (at least sample of) your database. You can send it to D.Palma [at] leeds.ac.uk . It will be treated in a confidential way.

Cheers
David
Miller Cooker
Posts: 4
Joined: 15 Nov 2022, 19:49

Re: MDCEV model with perhaps budget issue

Post by Miller Cooker »

Hi David,

Many thanks for the help! I will contact you later.

Thanks,

Connie
Post Reply