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)

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)