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.

MDCEV model with time/individual specific covariates in gamma?

Ask questions about model specifications. Ideally include a mathematical explanation of your proposed model.
Post Reply
yaoyao7031
Posts: 9
Joined: 28 Aug 2021, 01:54

MDCEV model with time/individual specific covariates in gamma?

Post by yaoyao7031 »

Hi all,

I am trying to estimate an MDCEV (discrete-continuous model) similar to Apollo_example_12.r, but with an extension. In the example, the gamma (satiation) parameter is alternative j specific, gamma_j. Is there any way that I can add a time/individual and alternative variant covariates (Xijt) to gamma? I tried to write the code:

gamma = list(work = gamma_work + b*X_ijt,
school = gamma_school + b*X_ijt,
shopping = gamma_shopping + b*X_ijt,
private = gamma_private + b*X_ijt,
leisure = gamma_leisure + b*X_ijt)

but it shows the error message:

Error in if (all(testL == 0)) stop("All observations have zero probability at starting value for model component \"", :
missing value where TRUE/FALSE needed

And when I remove the covariates X_ijt, everything works fine. Any suggestions? Thank you.
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: MDCEV model with time/individual specific covariates in gamma?

Post by dpalma »

Hi,

It's not possible to determine the exact cause of the problem without looking at the whole code and data, but my guess is that some of the gammas become negative or zero under your specification. Remember that gammas must be positive in MDCEV, so I would recommend making sure that b_j + b*X_ijt > 0 for all alternatives j for all observations using your starting values.

Another way to ensure gamma > 0 is using an exponential transformation, as follows.

Code: Select all

gamma = list(work     = exp(gamma_work     + b*X_ijt),
             school   = exp(gamma_school   + b*X_ijt),
             shopping = exp(gamma_shopping + b*X_ijt),
             private  = exp(gamma_private  + b*X_ijt),
             leisure  = exp(gamma_leisure  + b*X_ijt))
Using the exponential will make convergence a bit more difficult, but it should avoid your current problem.

Best
David
punyabeet
Posts: 19
Joined: 08 May 2020, 11:43

Re: MDCEV model with time/individual specific covariates in gamma?

Post by punyabeet »

Dear Prof. Hess and Prof. Palma

I just wanted to know is it possible to incorporate the intra-person correlation while estimating MDCEV? I have three non-work activities - Personal/Family Care (PC), Other Maintenance (Main), Recreation (REC), and three weeks of activity diary data of the same person. The output should look like this: (Output Table.png). The data is in the following pattern (Data Snip.png). Below is the code for the constant-only model with random baseline preference constants and random gamma parameters.

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

### Load Apollo library
library(apollo)

### Initialize code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName = "Mixed MDCEV",
modelDescr = "MDCEV model, alpha-gamma profile, with outside good and constants only in utilities",
indivID = "REPEAT_ID",
mixing = TRUE,
nCores = 8
)

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

database = read.csv("MDCEV_Master_Data.csv", header = TRUE)

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(
## Alpha is Fixed
alpha_base = 0,

## Random Gamma Profile Estimated
## Mean
gamma_pc_mu = 0,
gamma_main_mu = 0,
gamma_rec_mu = 0,

## Sigma
gamma_pc_sigma = 0,
gamma_main_sigma = 0,
gamma_rec_sigma = 0,


## Random ASC
## Mean
delta_pc_mu = 0,
delta_main_mu = 0,
delta_rec_mu = 0,

## Sigma
delta_pc_sigma = 0,
delta_main_sigma = 0,
delta_rec_sigma = 0,

## Cost Parameter
sigma = 1)

### Vector of parameters to be kept fixed at their starting value
apollo_fixed = c("sigma")


# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #

### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "mlhs",
interNDraws = 250,
interUnifDraws = c(),
interNormDraws = paste0("draws_", c("pc", "main", "rec",
"pc_gamma", "main_gamma", "rec_gamma")),

intraDrawsType = "",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()

randcoeff[["delta_pc"]] = delta_pc_mu + delta_pc_sigma * draws_pc

randcoeff[["delta_main"]] = delta_main_mu + delta_main_sigma * draws_main

randcoeff[["delta_rec"]] = delta_rec_mu + delta_rec_sigma * draws_rec

randcoeff[["gamma_pc"]] = gamma_pc_mu + gamma_pc_sigma * draws_pc_gamma

randcoeff[["gamma_main"]] = gamma_main_mu + gamma_main_sigma * draws_main_gamma

randcoeff[["gamma_rec"]] = gamma_rec_mu + gamma_rec_sigma * draws_rec_gamma

return(randcoeff)
}

# ################################################################# #
#### 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",
"PC",
"MAIN",
"REC")


### Define availability
avail = list( outside = 1,
PC = 1,
MAIN = 1,
REC = 1)


### Define continuous consumption for individual alternatives
continuousChoice = list(outside = OTHER_TIME/60,
PC = PC_TIME/60,
MAIN = MAIN_TIME/60,
REC = REC_TIME/60)


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

V[["PC"]] = delta_pc

V[["MAIN"]] = delta_main

V[["REC"]] = delta_rec


### Define alpha parameters
alpha = list(outside = 1 / (1 + exp(-alpha_base)),
PC = 1 / (1 + exp(-alpha_base)),
MAIN = 1 / (1 + exp(-alpha_base)),
REC = 1 / (1 + exp(-alpha_base)))


### Define gamma parameters
gamma = list(outside = 1,
PC = exp(gamma_pc),
MAIN = exp(gamma_main),
REC = exp(gamma_rec))


### Define costs for individual alternatives
cost = list(outside = 1,
PC = 1,
MAIN = 1,
REC = 1)


### Define budget
budget = MAX_TIME/60


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


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

### Average across inter-individual draws
P = apollo_avgInterDraws(P, apollo_inputs, functionality)

### Prepare and return outputs of the function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}

# #################################################
#### MODEL ESTIMATION ####
# #################################################

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs,
estimate_settings = list(maxIterations = 1000))

# ################################################################# #
#### 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)
sink(paste(model$apollo_control$modelName,"_additional_output.txt",sep=""),split=TRUE)
Attachments
Data Snip.png
Data Snip.png (19.62 KiB) Viewed 6579 times
Output Table.png
Output Table.png (23.49 KiB) Viewed 6579 times
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: MDCEV model with time/individual specific covariates in gamma?

Post by stephanehess »

Hi

could you please be more specific with what you mean by intra-person correlation in this context?

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
punyabeet
Posts: 19
Joined: 08 May 2020, 11:43

Re: MDCEV model with time/individual specific covariates in gamma?

Post by punyabeet »

Hi Prof Hess

Sorry for the delayed response. The reply to your query is that -
I want the correlation values between the panels i.e. between week 1 -week 2, week 1- week 3, and week2-week3 in the MDCEV model.

The other issue I am facing is that-
I get the following error message while doing the prediction for the final mixed MDCEV model with all the significant coefficients (with 6 coefficients showing SE as NaN although I am getting the robust t-ratio and robust SE as significant for these coefficients):

Error in gamma[i, 2:s$nAlt] : invalid subscript type 'closure'
In addition: Warning message:
In eval(formal.args[[as.character(substitute(arg))]], envir = sys.frame(sysP)) :
restarting interrupted promise evaluation
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: MDCEV model with time/individual specific covariates in gamma?

Post by stephanehess »

Hi

would you be able to share the code, the rds file (model object), and the data file with us offline and we'll try to fix this

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