diff --git a/R_Scripts/mnl_CMI_id_cont_sce.R b/R_Scripts/mnl_CMI_id_cont_sce.R new file mode 100644 index 0000000000000000000000000000000000000000..001d3a514ee6fca45f42bc4cb608737e257025b5 --- /dev/null +++ b/R_Scripts/mnl_CMI_id_cont_sce.R @@ -0,0 +1,156 @@ + +database<- database[database$gScenarios!=4,] +### Initialise code +apollo_initialise() + +### Set core controls +apollo_control = list( + modelName = "MNL_SP", + modelDescr = "Simple MNL model on mode choice SP data", + indivID = "id", + outputDirectory = "Output", + panelData = TRUE +) + + + +# ################################################################# # +#### DEFINE MODEL PARAMETERS #### +# ################################################################# # + +### Vector of parameters, including any that are kept fixed in estimation +apollo_beta=c(asc_a = 0, + asc_b = 0, + b_profit =1, + b_hold = 0, + b_loc = 0, + b_admin1 = 0, + b_admin2 = 0, + b_hold_CMI = 0, + b_hold_Q35_1 = 0, + b_hold_Q35_1_CMI = 0, + b_hold_sce1 = 0, + b_hold_CMI_sce1 = 0, + b_hold_Q35_1_sce1 = 0, + b_hold_Q35_1_CMI_sce1 = 0, + b_hold_sce2 = 0, + b_hold_CMI_sce2 = 0, + b_hold_Q35_1_sce2 = 0, + b_hold_Q35_1_CMI_sce2 = 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() + +# ################################################################# # +#### 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[["alt1"]] = asc_a + + b_profit * profit1 - + b_hold * hold1 - + b_hold_CMI*hold1*CMI - + b_hold_Q35_1_CMI*hold1*CMI*Q35_1 - + b_hold_Q35_1*hold1*Q35_1+ + b_loc * loc1 + + b_admin1 *admin_11+ + b_admin2 *admin_21 - + b_hold_sce1 * hold1*gScenarios_1 - + b_hold_CMI_sce1*hold1*CMI*gScenarios_1 - + b_hold_Q35_1_CMI_sce1*hold1*CMI*Q35_1*gScenarios_1 - + b_hold_Q35_1_sce1*hold1*Q35_1*gScenarios_1 - + b_hold_sce2 * hold1*gScenarios_2 - + b_hold_CMI_sce2*hold1*CMI*gScenarios_2 - + b_hold_Q35_1_CMI_sce2*hold1*CMI*Q35_1*gScenarios_2 - + b_hold_Q35_1_sce2*hold1*Q35_1*gScenarios_2 + + + + + V[["alt2"]] =asc_b + + b_profit * profit2 - + b_hold * hold2 - + b_hold_CMI*hold2*CMI - + b_hold_Q35_1_CMI*hold2*CMI*Q35_1 - + b_hold_Q35_1*hold2*Q35_1+ + b_loc * loc2 + + b_admin1 *admin_12+ + b_admin2 *admin_22 - + b_hold_sce1 * hold2*gScenarios_1 - + b_hold_CMI_sce1*hold2*CMI*gScenarios_1 - + b_hold_Q35_1_CMI_sce1*hold2*CMI*Q35_1*gScenarios_1 - + b_hold_Q35_1_sce1*hold2*Q35_1*gScenarios_1 - + b_hold_sce2 * hold2*gScenarios_2 - + b_hold_CMI_sce2*hold2*CMI*gScenarios_2 - + b_hold_Q35_1_CMI_sce2*hold2*CMI*Q35_1*gScenarios_2 - + b_hold_Q35_1_sce2*hold2*Q35_1*gScenarios_2 + + + + + V[["alt3"]] = b_profit * profit3 + + ### Define settings for MNL model component + mnl_settings = list( + alternatives = c(alt1=1, alt2=2, alt3=3), + avail = 1, + choiceVar = choosen, + 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) + + ### Prepare and return outputs of function + P = apollo_prepareProb(P, apollo_inputs, functionality) + return(P) +} + +# ################################################################# # +#### MODEL ESTIMATION #### +# ################################################################# # + +model_CMI_id_sce = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs) + +# ################################################################# # +#### MODEL OUTPUTS #### +# ################################################################# # + +# ----------------------------------------------------------------- # +#---- FORMATTED OUTPUT (TO SCREEN) ---- +# ----------------------------------------------------------------- # + +apollo_modelOutput(model_CMI_id_sce) + +# ----------------------------------------------------------------- # +#---- FORMATTED OUTPUT (TO FILE, using model name) ---- +# ----------------------------------------------------------------- # +summary(model_CMI_id_sce) + + + +