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. We check the forum at least twice a week. It may thus take a couple of days for your post to appear and before we reply. There is no need to submit the post multiple times.

eMDC with only two goods

Ask questions about how to estimate models and how to change your settings for estimation.
Post Reply
katya2727
Posts: 4
Joined: 17 Jul 2024, 19:49

eMDC with only two goods

Post 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)
dpalma
Posts: 227
Joined: 24 Apr 2020, 17:54

Re: eMDC with only two goods

Post 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
Post Reply