Page 2 of 2

Re: post-estimation scenario analysis

Posted: 23 May 2024, 07:14
by stephanehess
Alvin

the answers to the first two are in the manual. You can use apollo_unconditionals for both of these.

Regarding the third, purchase price is an attribute of the alternatives and thus included in the within class choice model, not the class allocation model, so it will not lead to such a shift

Stephane

Re: post-estimation scenario analysis

Posted: 23 May 2024, 08:46
by alvinej
Hi stephane ,
thank you for such a fast reply,

I'll go through manual for the first two questions,

regarding third question,

at estimates,

Code: Select all

Aggregated prediction for model component: Class_1
     at MLE Sampled mean Sampled std.dev. Quantile 0.025 Quantile 0.975
alt1   3218         3192            118.9           2967           3390
alt2   1989         2015            118.9           1817           2240

Average prediction for model component: Class_1
     at MLE Sampled mean Sampled std.dev. Quantile 0.025 Quantile 0.975
alt1  0.618       0.6129          0.02283         0.5698         0.6511
alt2  0.382       0.3871          0.02283         0.3489         0.4302

Aggregated prediction for model component: Class_2
     at MLE Sampled mean Sampled std.dev. Quantile 0.025 Quantile 0.975
alt1   1231         1236            170.6          974.4           1563
alt2   3976         3971            170.6         3643.8           4233

Average prediction for model component: Class_2
     at MLE Sampled mean Sampled std.dev. Quantile 0.025 Quantile 0.975
alt1 0.2364       0.2373          0.03277         0.1871         0.3002
alt2 0.7636       0.7627          0.03277         0.6998         0.8129

Aggregated prediction for model component: model
     at MLE Sampled mean Sampled std.dev. Quantile 0.025 Quantile 0.975
alt1   2286         2299            57.12           2204           2428
alt2   2921         2908            57.12           2779           3003

Average prediction for model component: model
     at MLE Sampled mean Sampled std.dev. Quantile 0.025 Quantile 0.975
alt1  0.439       0.4415          0.01097         0.4233         0.4663
alt2  0.561       0.5585          0.01097         0.537         0.5767
after i increase purchase price by 10%, this is the new result,

Code: Select all

Prediction at user provided parameters for model component: Class_1
             alt1    alt2
Aggregate 3264.89 1942.11
Average      0.63    0.37

Prediction at user provided parameters for model component: Class_2
             alt1    alt2
Aggregate 1290.52 3916.48
Average      0.25    0.75

Prediction at user provided parameters for model component: model
             alt1    alt2
Aggregate 2338.55 2868.45
Average      0.45    0.55
so, here as per my understanding can i say class 1 and class 2 are seperate MMNL model ? and in class 1 before applying purchase price by 10%( scenario) the split between alt1 and alt 2 were 0.62 and 0.38 respectively but after applying sceanrio split changed to alt1 = 0.63, alt2 = 0.37 which means this scenario will help in increase alt1 choosing probability by 1% . same like this for class 2, is it ? how can i interpret the result ?

sorry if im repeating my question, im finding it hard to understand what is actually happening here ? where actually the segmentation happening ?

Thank you
Alvin

Re: post-estimation scenario analysis

Posted: 23 May 2024, 18:12
by stephanehess
Hi

there is a choice model in each class. When you change an attribute, the prediction changes in each class, and of course then also in the overall model, which is a weighted average across the classes.

Stephane

Re: post-estimation scenario analysis

Posted: 29 May 2024, 17:17
by alvinej
alvinej wrote: 20 May 2024, 11:32
please see the code below

Code: Select all

parallel::detectCores()

#### LOAD LIBRARY AND DEFINE CORE SETTINGS                       ####
# ################################################################# #

### Clear memory
rm(list = ls())

### Load Apollo library
library(apollo)
library(foreach)
library(iterators)
library(doParallel)

### Initialise code
apollo_initialise()
# LN-OC,CT,R_b,CF_b&T-EM BFGS_1500
### Set core controls
apollo_control = list(
  modelName       = "MODEL_X_2000",
  modelDescr      = "MODEL_X_2000",
  indivID         = "Person_ID",
  nCores          =  10, 
  outputDirectory = "output"
)

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

### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv), 
### the code would be: database = read.csv("data.csv",header=TRUE)
database = read.csv("D:\\IST\\Project\\Data\\fourWheelerFaceToFace_V3(cleaning).csv", header = TRUE)
database1= read.csv("D:\\IST\\Project\\Data\\fourWheelerFaceToFace_V3(cleaning).csv", header = TRUE)
### for data dictionary, use ?apollo_swissRouteChoiceData

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_1_a           = 0.329,
                asc_1_b           = -0.682,
                
                asc_2_a           = 0,
                asc_2_b           = 0,
                
                beta_pp_a       =  -0.005 ,
                beta_pp_b       =  -0.015 ,
                
                # beta_oc_a       = 0,
                # beta_oc_b       = 0,
                #
                 betaa_oc_a       = -0.054,
                # log_oc_a_mu     = 0,
                #
                 # log_oc_a_sig    =  0,
                log_oc_b_mu     =  -5.566,
                 log_oc_b_sig    =  3.756,
                #betaa_oc_b       = 0,
                
                # Jn_oc_a_mu     = 0,
                #  Jn_oc_b_mu     =  0,
                # Jn_oc_a_sig    =  0,
                #  Jn_oc_b_sig    =  0,
                
                
                # tr_oc_p_a = 0,
                #tr_oc_p_b = 0,
                # tr_oc_q_a = 0,
                # tr_oc_q_b = 0,
                
                # beta_ct_a       = 0,
                # beta_ct_b       = 0,
                betaa_ct_a       =  -0.020,
                # log_ct_a_mu   =  0,
                #
                # log_ct_a_sig    =   0,
                log_ct_b_mu    = 5.488,
                log_ct_b_sig    =-16.790,
                 # betaa_ct_b       =  0,
                
                # tr_ct_p_a = 0,
                #tr_ct_p_b = 0,
                # tr_ct_q_a = 0,
                # tr_ct_q_b = 0,
                
                 # betaa_r_a       = 0,
                # beta_r_b       =   0,
                # log_r_a_mu    = 0,
                # log_r_a_sig    =  0,
                # log_r_b_mu    = 0,
                # 
                # log_r_b_sig    = 0,
                
               #  tr_r_p_a = 0,
               # tr_r_p_b = 0,
                tr_r_q_a = 0.129,
                tr_r_q_b = 0.134,
               # betaa_r_b = 0,

                 beta_cf_a       =  0.112,
                 beta_cf_b       =  0.337,
               #  log_cf_a_mu   =  0,
               # log_cf_a_sig    =  0,
                # log_cf_b_mu    =  0,
                # 
                # log_cf_b_sig    =   0,
                
                # tr_cf_p_a = 0,
                # tr_cf_p_b = 0,
               # betaa_cf_a = 0,
                # tr_cf_q_a = 0,
                # tr_cf_q_b = 0,
                # betaa_cf_b = 0,
               
                # beta_em_a       =  0,
                #  beta_em_b       =  0,
               #  log_em_a_mu    =  -1.9987,
               # log_em_a_sig    =  -0.08649,
               #  log_em_b_mu    =  -10.6245,
               # 
               #  log_em_b_sig    =   -0.1149,

                # tr_em_p_a = 0,
                #tr_em_p_b = 0,
                  # tr_em_q_a = 0,
               # betaa_em_a       =  0,
                # tr_em_q_b = 0,
               #   
                beta_priorityLanes_a = 0.215,
                beta_priorityLanes_b =  -0.099,

                beta_freeParking_a =    0.077,
                beta_freeParking_b =  0.240,

                beta_tollExemption_a = 0,
                beta_tollExemption_b = 0,
                # 
                
                
                
                delta_a        = -0.841,
                # gamma_knowledgeLow_a = 0,
                # gamma_knowledgeMid_a = 0,
                #  gamma_KnowledgeHigh_a = 0,
                gamma_voCoded_a =  0.347,
                gamma_dailyDistanceLow_a =0 ,
                gamma_dailyDistanceMid_a = -0.669,
                gamma_dailyDistanceHigh_a = -1.397,
                gamma_longDistanceLow_a = 0,
                gamma_longDistanceMid_a =  1.013,
                gamma_longDistanceHigh_a = 1.732,
                # gamma_techEnth_a =  0,
                # gamma_enviEnth_a =  0,
              # gamma_socIma_a = 0,
                # gamma_monBen_a = 0,
                 gamma_perFee_a = -0.339,
                gamma_perRisk_a =  -0.608,
                # gamma_insUti_a = 0,
                
                delta_b         = 0,
                # gamma_knowledgeLow_b = 0,
                # gamma_knowledgeMid_b = 0,
                #  gamma_KnowledgeHigh_b = 0,
                gamma_voCoded_b = 0,
                gamma_dailyDistanceLow_b = 0,
                gamma_dailyDistanceMid_b = 0,
                gamma_dailyDistanceHigh_b = 0,
                gamma_longDistanceLow_b = 0,
                gamma_longDistanceMid_b = 0,
                gamma_longDistanceHigh_b = 0,
                # gamma_techEnth_b = 0,
                # gamma_enviEnth_b = 0,
               # gamma_socIma_b = 0,
                # gamma_monBen_b = 0,
               gamma_perFee_b = 0,
               gamma_perRisk_b = 0
                # gamma_insUti_b = 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_2_a","asc_2_b", "delta_b",
                 "gamma_dailyDistanceLow_a",
                 "gamma_longDistanceLow_a",
                 # "gamma_knowledgeLow_a",
                 # "gamma_knowledgeLow_b",
                 # "gamma_knowledgeMid_b",
                 #  "gamma_KnowledgeHigh_b",
                 "gamma_voCoded_b" ,
                 "gamma_dailyDistanceLow_b" ,
                 "gamma_dailyDistanceMid_b",
                 "gamma_dailyDistanceHigh_b",
                 "gamma_longDistanceLow_b",
                 "gamma_longDistanceMid_b" ,
                 "gamma_longDistanceHigh_b",
                 "beta_tollExemption_a",
                 "beta_tollExemption_b",
                 # # "gamma_techEnth_b",
                 # # "gamma_enviEnth_b",
                 # # "gamma_socIma_b",
                 # # "gamma_monBen_b",
                  "gamma_perFee_b",
                  "gamma_perRisk_b"
                 # "gamma_insUti_b"
                 )

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

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType="mlhs",           
  interNDraws= 2000,               
  interUnifDraws=c("draws_ct_a","draws_r_a", "draws_r_b"),                
  interNormDraws=c("draws_oc_a"),    
# "draws_oc_a", "draws_oc_b", "draws_ct_a", "draws_ct_b", "draws_r_a", "draws_r_b", "draws_cf_a", "draws_cf_b" , "draws_em_a", "draws_em_b"
# "draws_oc_a", "draws_ct_a", "draws_r_a", "draws_cf_a", "draws_em_a"
  
  intraDrawsType="mlhs",
  intraNDraws=0,
  intraUnifDraws=c(),
  intraNormDraws=c()
)

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

  randcoeff[["beta_oc_a"]] = betaa_oc_a + 0*(draws_oc_a)
  randcoeff[["beta_oc_b"]] = -exp(log_oc_b_mu + log_oc_b_sig*draws_oc_a)
  randcoeff[["beta_ct_a"]] = betaa_ct_a + 0*(draws_ct_a)
  randcoeff[["beta_ct_b"]] = -exp(log_ct_b_mu + log_ct_b_sig*draws_ct_a)
  # randcoeff[["beta_r_a"]] = (betaa_r_a + 0*draws_r_a)
  # randcoeff[["beta_r_b"]] = exp(log_r_b_mu + log_r_b_sig*draws_r_a)
  # randcoeff[["beta_cf_a"]] = (betaa_cf_a + 0*draws_cf_a)
  # randcoeff[["beta_cf_b"]] = exp(log_cf_b_mu + log_cf_b_sig*draws_cf_a)
  # randcoeff[["beta_em_a"]] = (log_em_a_mu + 0*draws_em_a)
  # randcoeff[["beta_em_b"]] = -exp(log_em_b_mu + log_em_b_sig*draws_em_a)
  
  # lognormal distribution
  
  #  randcoeff[["beta_oc_a"]] = -exp(log_oc_a_mu + log_oc_a_sig*draws_oc_a)
  #  randcoeff[["beta_oc_b"]] = -exp(log_oc_b_mu + log_oc_b_sig*draws_oc_a)
  # randcoeff[["beta_ct_a"]] = -exp(log_ct_a_mu + log_ct_a_sig*draws_ct_a)
  # randcoeff[["beta_ct_b"]] = -exp(log_ct_b_mu + log_ct_b_sig*draws_ct_a)
  # randcoeff[["beta_r_a"]] = exp(log_r_a_mu + log_r_a_sig*draws_r_a)
  # randcoeff[["beta_r_b"]] = exp(log_r_b_mu + log_r_b_sig*draws_r_a)
  # randcoeff[["beta_cf_a"]] = exp(log_cf_a_mu + log_cf_a_sig*draws_cf_a)
  # randcoeff[["beta_cf_b"]] = exp(log_cf_b_mu + log_cf_b_sig*draws_cf_a)
  # randcoeff[["beta_em_a"]] = -exp(log_em_a_mu + log_em_a_sig*draws_em_a)
  # randcoeff[["beta_em_b"]] = -exp(log_em_b_mu + log_em_b_sig*draws_em_a)

  #triangular distribution
  
  # randcoeff[["beta_oc_a"]] = tr_oc_q_a * (draws_oc_a + draws_oc_b)/2
  # randcoeff[["beta_oc_b"]] = tr_oc_q_b *(draws_oc_a + draws_oc_b)/2
  # randcoeff[["beta_ct_a"]] = tr_ct_q_a * (draws_ct_a + draws_ct_b)/2
  # randcoeff[["beta_ct_b"]] = tr_ct_q_b *(draws_ct_a + draws_ct_b)/2
  randcoeff[["beta_r_a"]] =  tr_r_q_a * (draws_r_a + draws_r_b)/2
  randcoeff[["beta_r_b"]] =  tr_r_q_b*(draws_r_a + draws_r_b)/2
  # randcoeff[["beta_cf_a"]] = tr_cf_q_a *(draws_cf_a + draws_cf_b)/2
  # randcoeff[["beta_cf_b"]] = tr_cf_q_b * (draws_cf_a + draws_cf_b)/2
  # randcoeff[["beta_em_a"]] = tr_em_q_a * (draws_em_a + draws_em_b)/2
  # randcoeff[["beta_em_a"]] = tr_em_q_a* (draws_em_a + draws_em_b)/2
  # randcoeff[["beta_em_b"]] = tr_em_q_b * (draws_em_a + draws_em_b)/2
  
  #Johnson's distribution
  # randcoeff[['beta_oc_a']] = exp(Jn_oc_a_mu + Jn_oc_a_sig*draws_oc_a)/(1 + exp(Jn_oc_a_mu + Jn_oc_a_sig*draws_oc_a))
  # randcoeff[['beta_oc_b']] = exp(Jn_oc_b_mu + Jn_oc_b_sig*draws_oc_a)/(1 + exp(Jn_oc_b_mu + Jn_oc_b_sig*draws_oc_a))
  
  return(randcoeff)
}

# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS                              ####
# ################################################################# #

apollo_lcPars = function(apollo_beta, apollo_inputs){
  lcpars = list()
  lcpars[["asc_1"]] = list(asc_1_a, asc_1_b)
  lcpars[["asc_2"]] = list(asc_2_a, asc_2_b)
  lcpars[["beta_pp"]] = list(beta_pp_a, beta_pp_b)
  lcpars[["beta_oc"]] = list(beta_oc_a, beta_oc_b)
  lcpars[["beta_ct"]] = list(beta_ct_a, beta_ct_b)
  lcpars[["beta_r"]]  = list(beta_r_a, beta_r_b)
  lcpars[["beta_cf"]] = list(beta_cf_a, beta_cf_b)
  # lcpars[["beta_em"]] = list(beta_em_a, beta_em_b)
  lcpars[["beta_priorityLanes"]] = list(beta_priorityLanes_a, beta_priorityLanes_b)
  lcpars[["beta_freeParking"]]   = list(beta_freeParking_a, beta_freeParking_b)
  lcpars[["beta_tollExemption"]] = list(beta_tollExemption_a, beta_tollExemption_b)
  
  V=list()
  V[["class_a"]] = delta_a + gamma_voCoded_a*evoCoded + gamma_dailyDistanceLow_a*dailyDistanceLow + gamma_dailyDistanceMid_a*dailyDistanceMiddle+ gamma_dailyDistanceHigh_a*dailyDistanceHigh  + gamma_longDistanceLow_a*longDistanceLow + gamma_longDistanceMid_a*longDistanceMiddle+ gamma_longDistanceHigh_a*longDistanceHigh + gamma_perFee_a*perFeeML + gamma_perRisk_a*perRiskML
  #+ gamma_perFee_a*perFeeML + gamma_perRisk_a*perRiskML
  #+ +gamma_voCoded_a*evoCoded
  #+ gamma_knowledgeLow_a*knowledgeLow + gamma_knowledgeMid_a*knowledgeMiddle + gamma_KnowledgeHigh_a*knowledgeHigh 
  #+ gamma_monBen_a*monBenML + gamma_perFee_a*perFeeML + gamma_perRisk_a*perRiskML + gamma_insUti_a*insUtiML  
  V[["class_b"]] = delta_b +gamma_voCoded_b*evoCoded  + gamma_dailyDistanceLow_b*dailyDistanceLow + gamma_dailyDistanceMid_b*dailyDistanceMiddle+ gamma_dailyDistanceHigh_b*dailyDistanceHigh + gamma_longDistanceLow_b*longDistanceLow + gamma_longDistanceMid_b*longDistanceMiddle+ gamma_longDistanceHigh_b*longDistanceHigh +  gamma_perFee_b*perFeeML + gamma_perRisk_b*perRiskML 
  #+ gamma_perFee_b*perFeeML + gamma_perRisk_b*perRiskML 
  #+ + gamma_voCoded_b*evoCoded 
  #+ gamma_monBen_b*monBenML + gamma_perFee_b*perFeeML + gamma_perRisk_b*perRiskML + gamma_insUti_b*insUtiML
  #+ gamma_knowledgeLow_b*knowledgeLow + gamma_knowledgeMid_b*knowledgeMiddle + gamma_KnowledgeHigh_b*knowledgeHigh  + gamma_voCoded_b*evoCoded
  
  classAlloc_settings = list(
    classes      = c(class_a=1, class_b=2), 
    utilities    = V
  )
  
  lcpars[["pi_values"]] = apollo_classAlloc(classAlloc_settings)
  
  return(lcpars)
}

# ################################################################# #
#### 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 settings for MNL model component that are generic across classes
  mnl_settings = list(
    alternatives = c(alt1=1, alt2=2),
    avail        = list(alt1=1, alt2=1),
    choiceVar    = choiceVehicle
  )
  
  ### Loop over classes
  for(s in 1:2){
    ### Compute class-specific utilities
    V=list()
    V[["alt1"]]  = asc_1[[s]] + beta_pp[[s]]*ppEVI +  beta_oc[[s]]*ocEV+ beta_ct[[s]]*ctEV/60 + beta_r[[s]]*rEV/100 + beta_cf[[s]]*cfEV + beta_priorityLanes[[s]]*priorityLanes + beta_freeParking[[s]]*freeParking + beta_tollExemption[[s]]*tollExemption
  
    V[["alt2"]]  = asc_2[[s]]  + beta_pp[[s]]*ppCVI +  beta_oc[[s]]*ocCV                       + beta_r[[s]]*rCV/100        
    
    mnl_settings$utilities = V
    mnl_settings$componentName = paste0("Class_",s)
    
    ### Compute within-class choice probabilities using MNL model
    P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings, functionality)
    
    ### Take product across observation for same individual
    P[[paste0("Class_",s)]] = apollo_panelProd(P[[paste0("Class_",s)]], apollo_inputs ,functionality)
    
    ### Average across inter-individual draws within classes
    P[[paste0("Class_",s)]] = apollo_avgInterDraws(P[[paste0("Class_",s)]], apollo_inputs, functionality)
  }
  
  ### Compute latent class model probabilities
  lc_settings  = list(inClassProb = P, classProb=pi_values)
  P[["model"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
  
  ### Average across inter-individual draws in class allocation probabilities
  P[["model"]] = apollo_avgInterDraws(P[["model"]], apollo_inputs, functionality)
  
  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}


# ################################################################# #
#### MODEL ESTIMATION AND OUTPUT                                 ####
# ################################################################# #

### Estimate model
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs, estimate_settings = list(estimationRoutine="BFGS",maxIterations= 5000))

### Show output in screen
apollo_modelOutput(model)

### Save output to file(s)
apollo_saveOutput(model)

hi stephane,

if i want to calculate this in willingness to pay space what should i change ?

Re: post-estimation scenario analysis

Posted: 04 Jun 2024, 13:44
by stephanehess
Hi

You don’t need WTP space to calculate WTP. But in order to help you, I need to know what you want to calculate WTP for, i.e. what ratio of parameters?

Stephane

Re: post-estimation scenario analysis

Posted: 06 Jun 2024, 12:22
by alvinej
hi stephane,

i want to calculate wtp with respect to purchase price.

thank you
Alvin

Re: post-estimation scenario analysis

Posted: 11 Jun 2024, 13:43
by stephanehess
is that beta_pp? and what about the numerator?