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.

Error in apollo_estimate when using MDCNEV

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
LydiaYang
Posts: 12
Joined: 18 Feb 2021, 15:35

Error in apollo_estimate when using MDCNEV

Post by LydiaYang »

Dear all,

When using MDCNEV model, I run into this message:

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
Preparing user-defined functions.
Error in mdcnev_settings$probs_MDCNEV(mdcnev_settings) :
object 'tmp' not found

Could you please help me with checking what the problem is? Please feel free to let me know if more information is needed.

Thank you in advance!

Best Regards,
Lydia

My original code is as follows:

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

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

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName = "Apollo_MDCNEV_base",
modelDescr = "MDCNEV model on time use data, alpha-gamma profile with outside good and socio-demographics",
indivID = "Q144",
nCores = 4,
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)
library(haven)
database <- read_dta("MDCEV_2112.dta")
### for data dictionary, use ?apollo_timeUseData

### Create consumption variables for combined activities
database$t_outside = rowSums(database[,c("other")]) # outside good: time spent at home and travelling
database$t_leisure = rowSums(database[,c("nrfund", "rfund")])

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(alpha_base = 0,
gamma_nrfund = 1,
gamma_rfund = 1,
delta_nrfund = 0,
delta_rfund = 0,
theta_mandatory = 1,
theta_optional = 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("theta_mandatory")

### Read in starting values for at least some parameters from existing model output file
apollo_beta=apollo_readBeta(apollo_beta,apollo_fixed,"Apollo_PensionInvestment_base",overwriteFixed=FALSE)

# ################################################################# #
#### 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("outside",
"nrfund",
"rfund")

### Define availabilities
avail = list(outside = 1,
nrfund = 1,
rfund = 1)

### Define continuous consumption for individual alternatives
continuousChoice = list(outside = other,
nrfund = nrfund,
rfund = rfund)

### Define utilities for individual alternatives
V = list()
V[["outside"]] = 0
V[["nrfund"]] = delta_nrfund

V[["rfund"]] = delta_rfund



### Define alpha parameters
alpha = list(outside = 1 /(1 + exp(-alpha_base)),
nrfund = 1 /(1 + exp(-alpha_base)),
rfund = 1 /(1 + exp(-alpha_base)))

### Define gamma parameters
gamma = list(nrfund = gamma_nrfund,
rfund = gamma_rfund)

### Define nesting structure
mdcnevNests = list(mandatory = theta_mandatory,
optional = theta_optional)

mdcnevStructure = matrix(0, nrow=length(mdcnevNests), ncol=length(V))
### outside work school shopping private leisure
mdcnevStructure[1,] = c( 0, 1, 1) # mandatory
mdcnevStructure[2,] = c( 1, 0, 0) # optional

### Define costs for individual alternatives
cost = list(outside = 1,
nrfund = 1,
rfund = 1)

### Define budget
budget = nrfund + rfund + other

### Define settings for MDCNEV model
mdcnev_settings <- list(alternatives = alternatives,
avail = avail,
continuousChoice = continuousChoice,
utilities = V,
alpha = alpha,
gamma = gamma,
cost = cost,
budget = budget,
mdcnevNests = mdcnevNests,
mdcnevStructure = mdcnevStructure)

### Compute probabilities using MDCNEV model
P[["model"]] = apollo_mdcnev(mdcnev_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)

# ################################################################# #
##### POST-PROCESSING ####
# ################################################################# #

### Print outputs of additional diagnostics to new output file (remember to close file writing when complete)
apollo_sink()

# ----------------------------------------------------------------- #
#---- MODEL PREDICTIONS ----
# ----------------------------------------------------------------- #

### Use the estimated model to make predictions
predictions_base = apollo_prediction(model, apollo_probabilities, apollo_inputs)

colMeans(predictions_base)

# ----------------------------------------------------------------- #
#---- switch off writing to file ----
# ----------------------------------------------------------------- #

apollo_sink()
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Error in apollo_estimate when using MDCNEV

Post by stephanehess »

Hi

what version of Apollo are you using?

Also, I believe you need to normalise theta_optional, not theta_mandatory

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
LydiaYang
Posts: 12
Joined: 18 Feb 2021, 15:35

Re: Error in apollo_estimate when using MDCNEV

Post by LydiaYang »

Dear Stephane,

Thank you for your reply.

I am using the version 0.2.7. After changing the normalization from "mandatory" to "optional", the same error still shows up as follows,
"Error in mdcnev_settings$probs_MDCNEV(mdcnev_settings) :
object 'tmp' not found"
Could you please help me with checking this issue further?

Besides, the estimation of MDCEV model works fine, while all the translation parameters have the opposite sign as the utility parameters, does this indicate that something goes wrong or this is normal?

Thank you again for your help & looking forward to your insights!

Best regards,
Lydia
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Error in apollo_estimate when using MDCNEV

Post by stephanehess »

Lydia

could you share your code and data with me outside the forum so I can investigate?

Thanks
--------------------------------
Stephane Hess
www.stephanehess.me.uk
LydiaYang
Posts: 12
Joined: 18 Feb 2021, 15:35

Re: Error in apollo_estimate when using MDCNEV

Post by LydiaYang »

Hi Stephane,

I have sent the code as well as the data to you via email. Please let me know if more information is needed.
Thank you for the help!

Best,
Lydia
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Error in apollo_estimate when using MDCNEV

Post by stephanehess »

Lydia

this was a bit of an odd one. The problem is caused by you importing data from Stata using haven - Apollo then can't handle some of the columns later. If you just save your data as a csv file, that issue goes away.

A couple of other points:

- you can't have the column called budget in the database and then redefine it in apollo_probabilities
- you can't use apollo_panelProd as you only have one row per individual

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
LydiaYang
Posts: 12
Joined: 18 Feb 2021, 15:35

Re: Error in apollo_estimate when using MDCNEV

Post by LydiaYang »

Dear Stephane,

Thank you for the investigation.
I annotated the "apollo_panelProd" and also changed the name of "budget" in my original dataset to a different name. Now it works! While in the output, there are many missing values for both utility parameters and translation parameters (please see the estimates table below). I suppose this issue is caused by my data?

Estimates:
Estimate s.e. t.rat.(0) Rob.s.e. Rob.t.rat.(0)
alpha_base -18.5413 220.3 -0.08415 0.09447 -196.2617
gamma_nrfund 0.1378 NaN NaN 0.17058 0.8077
gamma_rfund 2.5017 NaN NaN 1.78356 1.4027
delta_nrfund -526.3160 NaN NaN 0.52763 -997.5088
delta_rfund -253.9193 NaN NaN 0.39361 -645.1055
theta_mandatory 530.3427 NaN NaN 0.17835 2973.5275
theta_optional 1.0000 NA NA NA NA

Besides, when I try to run a prediction, the following error shows up:

Running predictions from model using parameter estimates...
Error in chol.default(varcov) :
the leading minor of order 3 is not positive definite
Called from: chol.default(varcov)

Is there any thing I could do to prevent this error?

Appreciated all the help!

Best regards,
Lydia
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Error in apollo_estimate when using MDCNEV

Post by stephanehess »

Lydia

theta_mandatory clearly shows an issue. How does the model compare to a non-nested MDCEV?

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
LydiaYang
Posts: 12
Joined: 18 Feb 2021, 15:35

Re: Error in apollo_estimate when using MDCNEV

Post by LydiaYang »

Hi Stephane,

"How does the model compare to a non-nested MDCEV?" The idea is that nrfund and rfund should be nested in one category, but they are not chosen by every participant.
The amount of outside good "other" here is calculated by other = monthly_income - nrfund - rfund + 1 (plus 1 is to make sure that "other" is always chosen as the outside good).

Hope I understand your question correctly. Otherwise please let me know if more information is needed!

Best,
Lydia
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Error in apollo_estimate when using MDCNEV

Post by stephanehess »

Lydia

sorry, what I meant is in terms of fit and parameters. Did you try a standard MDCEV first?

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply