Page 1 of 1

eMDC with only two goods

Posted: 19 Aug 2025, 17:21
by katya2727
Hello Stephane & David,

Thank you so much for the great package and for your help on this forum.

My question concerns the simplest case of the eMDC model with only two goods. My main goal is to understand whether the two goods are complements or substitutes, first by examining the delta parameter and then by computing price elasticities. However, even in the simplest model specification I receive the following warnings: In log(Jdet): NaNs produced; In log(xi/gi + 1): NaNs produced. These prevent me from further calculating price elasticities.

Are there any mistakes in my code that stand out to you? Or do you perhaps have an idea why this could be happening?
I've checked many times that the quantities and the prices of the products are positive and that total expenses do not exceed the proposed budget.

I would be very grateful for any suggestions!

Best,
Katya


# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #

#install.packages("apollo")

### Clear memory and initialise
rm(list = ls())
library(apollo)
apollo_initialise()

### Set core controls
apollo_control = list(
modelName ="eMDC_with_budget",
modelDescr ="Extended MDC with complementarity and substitution",
indivID ="ResponseId",
outputDirectory="output"
)
apollo_control$panelData = FALSE

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

### Load data from within the Apollo package
database = read.csv("data.csv")

### Randomly split dataset into estimation (70%) and validation (30%)
set.seed(1)
database$validation <- runif(nrow(database))>0.7
dbVal <- database[ database$validation,] # validation sample
database <- database[!database$validation,] # estimation sample

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

### Parameters starting values c(name1=value1, name2=value2, ...)
apollo_beta = c( sigma = 0.5,
# Satiation
g_first = 0.1,
g_second = 0.1,

# Base utility
b_first =0.1, b_second =0.1,

# Compl/subst
dfirst_second= 0.1)

### Names of fixed parameters
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"){

### Initialise
apollo_attach(apollo_beta, apollo_inputs)
on.exit(apollo_detach(apollo_beta, apollo_inputs))
P = list()

### Prepare Inputs
alts = c("first", "second")
nAlt = length(alts)
ones = setNames(as.list(rep(1, nAlt)), alts)
continuousChoice = list(first = Q11,
second = Q12)
utilities = list(
first = b_first,
second = b_second
)
gamma = list(first = g_first,
second = g_second)
delta <- c(0, 0,
dfirst_second, 0
)
delta <- matrix(delta, nrow=nAlt, ncol=nAlt, byrow=TRUE)

### Define costs for individual alternatives
cost = list(first = P11,
second = P12)

emdc_settings <- list(continuousChoice = continuousChoice,
avail = ones,
utilityOutside = 0,
utilities = utilities,
#budget = 20,
sigma = sigma,
gamma = gamma,
delta = delta,
cost = cost)
P[["model"]] = apollo_emdc(emdc_settings, functionality)

### Comment out as necessary
#P = apollo_panelProd(P, apollo_inputs, functionality)
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}

`# ################################################################# #
#### MODEL ESTIMATION & OUTPUT ####
# ################################################################# #

model = apollo_estimate(apollo_beta, apollo_fixed,
apollo_probabilities, apollo_inputs)

apollo_modelOutput(model)

apollo_saveOutput(model)

Re: eMDC with only two goods

Posted: 20 Sep 2025, 00:43
by dpalma
Hi Katya,

If these are warnings you get after estimation, I would not worry about them. During the optimisation process the algorithm may try invalid parameter values (e.g. a negative g_first < 0). These will generate the warning, but the optimiser will be smart enough to try another value (i.e. a value that satisfies g_first > 0). You may want to use g_first = 1 and g_second = 1 as starting values to avoid these situations.

These warnings should not prevent you from calculating price elasticities.

Best wishes,
David