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.

Fitting variant of MDCEV by coding probabilities within apollo_probabilities

Ask questions about how to estimate models and how to change your settings for estimation.
Post Reply
PaulW
Posts: 5
Joined: 15 Mar 2021, 14:38

Fitting variant of MDCEV by coding probabilities within apollo_probabilities

Post by PaulW »

Hi,

I'm currently trying to fit the Ly-profile for the MDCEV model. I coded the loglikelihoods and the model converged using the maxLik package. However, I would like to be able to estimate the model within the Apollo framework but currently I obtain the following error:

"Error in apollo_insertComponentName(e[]) : Argument "e" must be a call" when running the apollo_estimate function."

I'm not very experienced with coding a model like this and I was wondering if you could help/point me in the right direction to make apollo_estimate run the model as I need to model other variants as well. Am I missing certain things that need to be coded in order for the model to be able to be estimated ?

Kind regards,
Paul

Code:

##################################

# ################################################################# #
#### 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 ="Test",
modelDescr ="Ly profile 2018",
indivID ="indivID"
)

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

database = read.csv("C..//apollo_timeUseData.csv",header=TRUE)

### Create consumption variables for combined activities
database$t_outside = rowSums(database[,c("t_a01", "t_a06", "t_a10", "t_a11", "t_a12")]) # outside good: time spent at home and travelling
database$t_leisure = rowSums(database[,c("t_a07", "t_a08", "t_a09")])

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(gamma_work = 1,
gamma_school = 1,
gamma_shopping = 1,
gamma_private = 1,
gamma_leisure = 1,
delta_work = 0,
delta_school = 0,
delta_shopping = 0,
delta_private = 0,
delta_leisure = 0,
sigma = 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("sigma")

# ################################################################# #
#### 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",
"work",
"school",
"shopping",
"private",
"leisure")

### Define utilities for individual alternatives
V = list()
V[["outside"]] = 0
V[["work"]] = delta_work
V[["school"]] = delta_school
V[["shopping"]] = delta_shopping
V[["private"]] = delta_private
V[["leisure"]] = delta_leisure

### Define gamma parameters
gamma = list(work = gamma_work,
school = gamma_school,
shopping = gamma_shopping,
private = gamma_private,
leisure = gamma_leisure)

### Define costs for individual alternatives
cost = list(outside = 1,
work = 1,
school = 1,
shopping = 1,
private = 1,
leisure = 1)

##############
nAlt = 5 #zonder outside good

multiplechoice=as.matrix(cbind(
t_a02,
t_a03,
t_a04,
t_a05,
t_leisure))
colnames(multiplechoice)<- c("work","school","shopping","private","leisure")

test=c()

#nrow(multiplechoice)
for (i in 1:nrow(multiplechoice)){

V2 = list()
Chosen = list()
ChosenE = list()
NotChosen = list()
NotChosenE = list()
logfi = list()

V2[["work"]] = V[["outside"]] - V[["work"]] + log(multiplechoice[[i,1]]/gamma[["work"]] + 1 ) + log(cost[["work"]])
V2[["school"]] = V[["outside"]] - V[["school"]] + log(multiplechoice[[i,2]]/gamma[["school"]] + 1 ) + log(cost[["school"]])
V2[["shopping"]] = V[["outside"]] - V[["shopping"]] + log(multiplechoice[[i,3]]/gamma[["shopping"]] + 1 ) + log(cost[["shopping"]])
V2[["private"]] = V[["outside"]] - V[["private"]] + log(multiplechoice[[i,4]]/gamma[["private"]] + 1 ) + log(cost[["private"]])
V2[["leisure"]] = V[["outside"]] - V[["leisure"]] + log(multiplechoice[[i,5]]/gamma[["leisure"]] + 1 ) + log(cost[["leisure"]])

for (j in 1:ncol(multiplechoice)){

if (multiplechoice[[i,j]] > 0){
Chosen[[colnames(multiplechoice)[j]]] = V2[[j]]
logfi[[colnames(multiplechoice)[j]]] = log(1/(multiplechoice[i,j] + gamma[[j]]))
}else {
NotChosen[[colnames(multiplechoice)[j]]] = V2[[j]]
}
}

M = length(Chosen)

#Term A
A = Reduce("+",logfi)

#Term B
B = lfactorial(M)

#Term C
C = M * log(sigma)

#Term D
D = -1/sigma * Reduce("+",Chosen)

#Term E
ChosenE = lapply(Chosen,function(x) exp(-x/sigma))
NotChosenE = lapply(NotChosen,function(x) exp(-x/sigma))

E = 1 + Reduce("+",ChosenE) + Reduce("+",NotChosenE)

#Log-likelihood single individual
LL = A + B - C + D - (M+1) * log(E)

test=c(test,LL)
}
P[["model"]] = test

### 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 = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)

apollo_modelOutput(model)
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: Fitting variant of MDCEV by coding probabilities within apollo_probabilities

Post by dpalma »

Hi Paul,

Thank you for being so patient with us.

We recently release a new version of Apollo (v0.2.5), which should be more flexible when it comes to running custom code.

I tried to run your code and found a couple of issues.
  • apollo_probabilities should return the likelihood, not the log-likelihood. So you should do P[["model"]] = exp(test)
  • For some reason, the variable "test" is only 2223 elements long, while it should be 2826 (one value per row in the data). My guess is that "LL" becomes NULL for some rows in the data, leading to a shorter "test" in the end.
Thanks for trying new model formulations in Apollo! Happy to help with your developments.

Cheers
David
Post Reply