Thank you!
I'm not sure if I need to create a new post or if I could ask here. sorry.
I also want to run a mixed logit model for the dutch sample.
The aim of my analysis is to investigate wtp for different functionalities on bicycles.
I found some deterministic heterogeneity in the data MNL model and used the same interactions, which are significant for the mixed logit model.
Here is the mixed logit model.
I use the estimates from the MNL model as starting values for the mixed logit model. However, since the values for cost do not work I use as starting values for the mu_b_cost=log(abs(b_cost_mnl)).### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "Model_MNL_IATBR",
modelDescr = "MNL model with socios",
indivID = "ID",
mixing = TRUE,
nCores = 4
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
### Loading data from package
database = read.csv("data.csv",header=TRUE)
### Use only RP data
database = subset(database,database$country_code=='NL')
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_A = 0,
asc_B = 0,
asc_C = 0,
mu_b_Assistance_emergency_unit = 0,
mu_b_Assistance_emergency_call = 0.065319,
mu_b_Assistance_smart_routes = 0.305141,
mu_b_Automatic_speed_safe_distance = 0,
mu_b_Automatic_speed_speed_limit = 0.311208,
mu_b_Automatic_speed_risky_areas = 0.175928,
mu_b_Collision_avoidance_front_side = 0,
mu_b_Collision_avoidance_rear_side = 0.144246,
mu_b_Collision_avoidance_blind_spot = 0.164900,
mu_b_cost = -2.70885 ,#-0.001955,
sigma_b_Assistance_emergency_unit = 0,
sigma_b_Assistance_emergency_call = 0,
sigma_b_Assistance_smart_routes = 0,
sigma_b_Automatic_speed_safe_distance = 0,
sigma_b_Automatic_speed_speed_limit = 0,
sigma_b_Automatic_speed_risky_areas = 0,
sigma_b_Collision_avoidance_front_side = 0,
sigma_b_Collision_avoidance_rear_side = 0,
sigma_b_Collision_avoidance_blind_spot = 0,
sigma_b_cost = 0 ,
age18_39_Automatic_speed_speed_limit = 0,
age18_39_Automatic_speed_risky_areas = 0,
age18_39_Automatic_speed_safe_distance = 0,
male_Automatic_speed_speed_limit = 0,
male_Automatic_speed_risky_areas = 0,
#male_Automatic_speed_safe_distance = 0,
male_cost = 0,
young_cost =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_A",
"mu_log_b_Assistance_emergency_unit",
"mu_log_b_Automatic_speed_safe_distance",
"mu_log_b_Collision_avoidance_front_side")
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "mlhs", #halton
interNDraws = 500,
interUnifDraws = c(),
interNormDraws = c("draws_Ass_unit", "draws_Ass_call", "draws_Ass_routes", "draws_Aut_dist", "draws_Aut_limit", "draws_Aut_areas",
"draws_coll_front", "draws_coll_rear", "draws_coll_blind", "draws_cost"),
# intraDrawsType = "halton",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)
### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["Assistance_emergency_unit"]] = mu_b_Assistance_emergency_unit + sigma_b_Assistance_emergency_unit * draws_Ass_unit
randcoeff[["Assistance_emergency_call"]] = mu_b_Assistance_emergency_call + sigma_b_Assistance_emergency_call * draws_Ass_call
randcoeff[["Assistance_smart_routes"]] = mu_b_Assistance_smart_routes + sigma_b_Assistance_smart_routes * draws_Ass_routes
randcoeff[["Automatic_speed_safe_distance"]] = mu_b_Automatic_speed_safe_distance + sigma_b_Automatic_speed_safe_distance * draws_Aut_dist
randcoeff[["Automatic_speed_speed_limit"]] = mu_b_Automatic_speed_speed_limit + sigma_b_Automatic_speed_speed_limit * draws_Aut_limit
randcoeff[["Automatic_speed_risky_areas"]] = mu_b_Automatic_speed_risky_areas + sigma_b_Automatic_speed_risky_areas * draws_Aut_areas
randcoeff[["Collision_avoidance_front_side"]] = mu_b_Collision_avoidance_front_side + sigma_b_Collision_avoidance_front_side * draws_coll_front
randcoeff[["Collision_avoidance_rear_side"]] = mu_b_Collision_avoidance_rear_side + sigma_b_Collision_avoidance_rear_side * draws_coll_rear
randcoeff[["Collision_avoidance_blind_spot"]] = mu_b_Collision_avoidance_blind_spot + sigma_b_Collision_avoidance_blind_spot * draws_coll_blind
randcoeff[["b_cost"]] = -exp( mu_b_cost + sigma_b_cost * draws_cost )
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()
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[["A"]] = (asc_A
+ Assistance_emergency_unit * (Att1== 1)
+ Assistance_emergency_call * (Att1== 2)
+ Assistance_smart_routes * (Att1== 3)
+ (Automatic_speed_speed_limit
+ age18_39_Automatic_speed_speed_limit * (Age25_44 == 1)
+ male_Automatic_speed_speed_limit * (gender == 1) ) * (Att2== 1)
+ (Automatic_speed_risky_areas
+ age18_39_Automatic_speed_risky_areas * (Age25_44 == 1)
+ male_Automatic_speed_risky_areas * (gender == 1) ) * (Att2== 2)
+ (Automatic_speed_safe_distance
+ age18_39_Automatic_speed_safe_distance * (Age25_44 == 1)) * (Att2== 3)
+ Collision_avoidance_front_side * (Att3== 1)
+ Collision_avoidance_rear_side * (Att3== 2)
+ Collision_avoidance_blind_spot * (Att3== 3)
+ (b_cost
+ male_cost * (gender == 1)
+ young_cost * (Age25_44 == 1)) * ((Att4== 1) * 400
+(Att4== 2) * 600
+(Att4== 3) * 800
+(Att4== 4) * 1000))
V[["B"]] = (asc_B
+ Assistance_emergency_unit * (Att1.1== 1)
+ Assistance_emergency_call * (Att1.1== 2)
+ Assistance_smart_routes * (Att1.1== 3)
+ (Automatic_speed_speed_limit
+ age18_39_Automatic_speed_speed_limit * (Age25_44 == 1)
+ male_Automatic_speed_speed_limit * (gender == 1) ) * (Att2.1== 1)
+ (Automatic_speed_risky_areas
+ age18_39_Automatic_speed_risky_areas * (Age25_44 == 1)
+ male_Automatic_speed_risky_areas * (gender == 1) ) * (Att2.1== 2)
+ (Automatic_speed_safe_distance
+ age18_39_Automatic_speed_safe_distance * (Age25_44 == 1)) * (Att2.1== 3)
+ Collision_avoidance_front_side * (Att3.1== 1)
+ Collision_avoidance_rear_side * (Att3.1== 2)
+ Collision_avoidance_blind_spot * (Att3.1== 3)
+ (b_cost
+ male_cost * (gender == 1)
+ young_cost * (Age25_44 == 1)) * ((Att4.1== 1) * 400
+(Att4.1== 2) * 600
+(Att4.1== 3) * 800
+(Att4.1== 4) * 1000))
V[["C"]] = (asc_C)
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(A=1, B=2, C=3),
##avail = list(alt1=Concept, alt2=Concept.1),
choiceVar = choice,
utilities = V
)
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(A=1, B=2, C=3),
##avail = list(alt1=Concept, alt2=Concept.1),
choiceVar = choice,
utilities = 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 ####
# ################################################################# #
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)
Also, I use a negative lognormal distribution for the cost attribute, while for all the rest, I use a normal distribution since I assume they can take any value from positive to negative.
To calculate the wtp I take the ratio of the utilities of each attribute with the cost attribute. Is this the right way to do so? I am asking since the estimate for the cost becomes really high, affecting the willingness to pay (becomes really small), which seems meaningless.
I have also run a latent class model and there is a huge difference in wtp between these two models.
Thank you in advance for your help.
Best regards,
Georgios