Page 1 of 1

Error in estimates in MDCEV

Posted: 24 Apr 2025, 15:22
by Julián
Dear Profesors,

Thanks for developing such a powerful tool.

I am trying to estimate a MDCEV model. However, I got the following error message:

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
Preparing user-defined functions.
Error en if (!test) {: valor ausente donde TRUE/FALSE es necesario

I don't know what's happening. I have used this code before and It has worked perfectly well.

Any help would be very appreciated!

Thanks in advance!

I share the code:

# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS: SL PORT CHOICE PAPER ####
# ################################################################# #

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

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName = "MDCEV_SL",
modelDescr = "MDCEV model, alpha-gamma profile, no outside good and constants only in utilities",
indivID = "ID",
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)
database = read.csv("vb.csv",header=TRUE, sep=";")


### Use only SP data
###database = subset(database,database$SP==1)

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(alpha_base = 0,
gamma_barcelona = 1,
gamma_valencia = 1,
delta_valencia = 0,
b_cost = 0,
b_productivity = 0,
b_hinterland = 0,
b_infras = 0,
sig = 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("sig", "alpha_base")

# ################################################################# #
#### 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("barcelona",
"valencia")

### Define availabilities
avail = list(barcelona = 1,
valencia = 1)

### Define continuous consumption for individual alternatives
continuousChoice = list(barcelona = barcelona,
valencia = valencia)

### Define utilities for individual alternatives
V = list()
V[["barcelona" ]] = b_cost * costbcn + b_infras * blbcn + b_productivity * prodbcn + b_hinterland * hintbcn
V[["valencia" ]] = delta_valencia + b_cost * costvlc + b_infras * blvlc + b_productivity * prodvlc + b_hinterland * hintvlc


### Define alpha parameters
alpha = list(barcelona = 1 /(1 + exp(-alpha_base)),
valencia = 1 /(1 + exp(-alpha_base)))

### Define gamma parameters
gamma = list(barcelona = gamma_barcelona,
valencia = gamma_valencia)

### Define costs for individual alternatives
cost = list(barcelona = 1,
valencia = 1)

### Define settings for MDCEV model
mdcev_settings <- list(alternatives = alternatives,
avail = avail,
continuousChoice = continuousChoice,
utilities = V,
alpha = alpha,
gamma = gamma,
sigma = sig,
cost = cost,
budget = 1)

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


# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #

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

apollo_modelOutput(model)

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

apollo_saveOutput(model)

Re: Error in estimates in MDCEV

Posted: 19 May 2025, 18:30
by stephanehess
Hi

apologies for the slow reply. Can you confirm you are using the latest version of Apollo? If the error persists, please send me the code and data and I'll have a look for you

Best wishes

Stephane