Page 1 of 1

MNL model- elasticity calculations

Posted: 08 Jul 2021, 08:03
by Blake Huang
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()

Re: MNL model- elasticity calculations

Posted: 08 Jul 2021, 10:36
by stephanehess
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

Re: MNL model- elasticity calculations

Posted: 08 Jul 2021, 10:56
by Blake Huang
Dear Prof Hess,

Thank you very much for your quick reply!

Yue