Page 1 of 1

MDCEV model with perhaps budget issue

Posted: 15 Nov 2022, 20:17
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)

Re: MDCEV model with perhaps budget issue

Posted: 25 Nov 2022, 16:41
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

Re: MDCEV model with perhaps budget issue

Posted: 25 Nov 2022, 18:56
by Miller Cooker
Hi David,

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

Thanks,

Connie