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.

An error when estimating nonlinear utility specification

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
arohamirai
Posts: 18
Joined: 28 Jun 2020, 16:26

An error when estimating nonlinear utility specification

Post by arohamirai »

Dear Stephane,

Thank you for inventing the Apollo package and answering my past questions. I have encountered an error when estimating a nonlinear utility specification using MMNL.

Description of my model: I have a health attribute (h) and a probability attribute (p) associated with the health outcome (The two attributes can vary independently in experimental design). Under an expected utility theory assumption, it is expected that the utility of the health attribute is concave, so I want to estimate the concavity of this attribute by estimating an alpha parameter, where E(U)=p*(h^(1-alpha))/(1-alpha). Therefore, the utility function for alternative i under RUM assumption is written as:

U=ASC+beta1*(p*(h^(1-alpha))/(1-alpha))+beta2*other attributes+error term

I want to estimate the alpha in order to capture the degree of nonlinear of the health attribute.

The problem: When the estimation is run using MNL, it is all fine; but an error occurs when it is estimated using MMNL. Please find below for the codes.

Thank you very much.

Best wishes,
Tim

--------------------------
codes
# ################################################################# #
#### 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 ="T2_EUgain",
modelDescr ="T2_EUgain",
indivID ="ID",
nCores = 3,
noDiagnostics = TRUE
)

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

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


##renames##
names(database)[names(database)=="id"] <- "ID"

database= subset(database, database$same_answersG==0&same_answersQ==0&same_answersQ==0)

#rescale
database$p_1_inc=database$p_1_inc/100
database$p_2_inc=database$p_2_inc/100
database$p_1_dec=database$p_1_dec/100
database$p_2_dec=database$p_2_dec/100
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_1 = 0,



asc_3 = 0,



EU_inc_mu = 0,
EU_inc_sig = 0,


h_dec_mu = 0,
h_dec_sig = 0,

p_dec_mu = 0,
p_dec_sig = 0,

v = 0,


alpha =0,

c = 0



)

### 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( "asc_1")

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

### Set parameters for generating draws
apollo_draws = list(
interDrawsType="mlhs",
interNDraws=500,
interUnifDraws=c(),
interNormDraws=c("draws_v", "draws_h_dec","draws_p_dec","draws_asc_3",
"draws_EU_inc"),

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

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

randcoeff[["v"]] = v_mu + v_sig*draws_v
randcoeff[["hdec"]] = h_dec_mu + h_dec_sig*draws_h_dec
randcoeff[["pdec"]] = p_dec_mu + p_dec_sig*draws_p_dec
randcoeff[["asc_3"]] = asc_3_mu + asc_3_sig*draws_asc_3


randcoeff[["EU_inc"]] = EU_inc_mu + EU_inc_sig*draws_EU_inc


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





### Compute utilities
V=list()
V[['alt1']] = asc_1 + EU_inc*(p_1_inc*(h_1_inc^(1-alpha))/(1-alpha)) + hdec*h_1_dec + pdec*p_1_dec + v*v_1 + c*c_1

V[['alt2']] = asc_1 + EU_inc*(p_2_inc*(h_2_inc^(1-alpha))/(1-alpha)) + hdec*h_2_dec + pdec*p_2_dec + v*v_2 + c*c_2

V[['alt3']] = asc_3 + v*v_3 + c*c_3

### Define settings for MNL model component that are generic across classes
mnl_settings = list(
alternatives = c(alt1=1, alt2=2,alt3=3),
avail = list(alt1=1, alt2=1,alt3=1),
choiceVar = choice,
V = V

)

### Compute probabilities using MNL model
P[['model']] = apollo_mnl(mnl_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 function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}


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

### Optional starting values search
# apollo_beta=apollo_searchStart(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)


#model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs, estimate_settings=list(bootstrapSE=2))
#apollo_bootstrap(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)



# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #

apollo_modelOutput(model)

Errors
......
......
# ################################################################# #
> #### MODEL ESTIMATION ####
> # ################################################################# #
>
> ### Optional starting values search
> # apollo_beta=apollo_searchStart(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)
>
> model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
Validating inputs of likelihood function (apollo_probabilities)
Error in apollo_probabilities(apollo_beta, apollo_inputs, functionality = "validate") :
object 'EU_inc' not found
stephanehess
Site Admin
Posts: 1050
Joined: 24 Apr 2020, 16:29

Re: An error when estimating nonlinear utility specification

Post by stephanehess »

Tim

do you get any errors when running apollo_inputs = apollo_validateInputs()

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
arohamirai
Posts: 18
Joined: 28 Jun 2020, 16:26

Re: An error when estimating nonlinear utility specification

Post by arohamirai »

Dear Stephane,

Yes I had errors when running apollo_inputs = apollo_validateInputs(), but I have solved the problem; it was a small mistake - I forgot to turn on the mixing=TRUE at the start. I can confirm that the non-linear specification can be run successfully using MMNL. Thank you again for your remind and sorry for my carelessness.

Tim
stephanehess
Site Admin
Posts: 1050
Joined: 24 Apr 2020, 16:29

Re: An error when estimating nonlinear utility specification

Post by stephanehess »

Tim

good lesson to always track back to see where you get the first error

Best wishes

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