Page 1 of 1

MDCEV model with time/individual specific covariates in gamma?

Posted: 28 Aug 2021, 02:20
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.

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

Posted: 31 Aug 2021, 15:54
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

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

Posted: 07 Feb 2022, 20:31
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)

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

Posted: 15 Feb 2022, 12:22
by stephanehess
Hi

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

Stephane

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

Posted: 24 Feb 2022, 12:41
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

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

Posted: 11 Mar 2022, 13:14
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