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.
  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.

MNL model- elasticity calculations

Ask questions about post-estimation functions (e.g. prediction, conditionals, etc) or other processing of results.
Post Reply
Blake Huang
Posts: 9
Joined: 08 Dec 2020, 11:30

MNL model- elasticity calculations

Post by Blake Huang » 08 Jul 2021, 08:03

Hi, prof Hess,

I met a mistake when attempting to calculate the elasticity in the MNL model. Here is the warning:

WARNING: Element database in the global environment differs from that inside apollo_inputs. The latter will be used. If you wish to use the former, stop this function by pressing the "Escape" key, and rerun apollo_validateInputs before calling this function.

Could you please give some suggestions on what I can to handle this? Thank you.

Best regards,
Yue


The following is my code:

rm(list = ls())
library(apollo)
apollo_initialise()
apollo_control = list(
modelName ="multinomial logit model",
modelDescr ="MNL with P&R Mode Choice",
indivID ="ID"
)
database = read.csv("apollo_multinomial logit model.csv",header=TRUE)
apollo_beta = c( asc_1 = 0,
asc_2 = 0,
asc_3 = 0,
b_sex_3 = 0,
b_age2_3 = 0,
b_age3_3 = 0,
b_job1_3 = 0,
b_job2_3 = 0,
b_dyear_3 = 0,
b_pr_3 = 0,
b_fee = 0,
b_con = 0,
b_rec = 0,
b_med = 0,
b_tt = 0)
apollo_fixed = c("asc_1")

apollo_inputs = apollo_validateInputs()

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate") {
apollo_attach(apollo_beta, apollo_inputs)
on.exit(apollo_detach(apollo_beta, apollo_inputs))

P = list()
V = list()
V[['alt1']] = asc_1 + b_tt * tt1
V[['alt2']] = asc_2 + b_tt * tt2
V[['alt3']] = asc_3 + b_tt * tt3 + b_fee * fee + b_con * con + b_rec * rec + b_med * med + b_sex_3 * sex + b_age2_3 * age2 + b_age3_3 * age3 + b_job1_3 * job1 + b_job2_3 * job2 + b_dyear_3 * dyear + b_pr_3 * pr
mnl_settings = list(
alternatives = c(alt1=1, alt2=2, alt3=3),
avail = list(alt1=1, alt2=1, alt3=1),
choiceVar = choice,
V = V
)
P[["model"]] = apollo_mnl(mnl_settings, functionality)
P = apollo_panelProd(P, apollo_inputs, functionality)
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
apollo_modelOutput(model)
apollo_saveOutput(model)

sink(paste(model$apollo_control$modelName,"_additional_output.txt",sep=""),split=TRUE)

predictions_base = apollo_prediction(model, apollo_probabilities, apollo_inputs, prediction_settings=list(runs=30))
database$tt1 = 1.01*database$tt1
predictions_new = apollo_prediction(model, apollo_probabilities, apollo_inputs)
database$tt1 = 1/1.01*database$tt1
predictions_base=predictions_base[["at_estimates"]]
log(sum(predictions_new[,"alt1"])/sum(predictions_base[,"alt1"]))/log(1.01)
log(sum(predictions_new[,"alt2"])/sum(predictions_base[,"alt2"]))/log(1.01)
log(sum(predictions_new[,"alt3"])/sum(predictions_base[,"alt3"]))/log(1.01)

if(sink.number()>0) sink()

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

Re: MNL model- elasticity calculations

Post by stephanehess » 08 Jul 2021, 10:36

Hi

as the message tells you, you should run apollo_validateInputs again before making the prediction on the changed data

So you need to have

Code: Select all

predictions_base = apollo_prediction(model, apollo_probabilities, apollo_inputs, prediction_settings=list(runs=30))
database$tt1 = 1.01*database$tt1
apollo_inputs = apollo_validateInputs()
predictions_new = apollo_prediction(model, apollo_probabilities, apollo_inputs)
Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk

Blake Huang
Posts: 9
Joined: 08 Dec 2020, 11:30

Re: MNL model- elasticity calculations

Post by Blake Huang » 08 Jul 2021, 10:56

Dear Prof Hess,

Thank you very much for your quick reply!

Yue

Post Reply