Thank you for your response.
Code: Select all
# ################################################################# #
#### DUMMYYYY - MNL ####
# ################################################################# #
### Clear memory
rm(list = ls())
install.packages("apollo")
### Load Apollo library
library(apollo)
library(dplyr)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "DCE3",
modelDescr = "MNL model_optout",
indivID = "ID",
#nAlternatives = 3, # Number of alternatives
#alternatives = c(1, 2, 0), # Define alternative IDs
outputDirectory = "output"
)
# Load the data
data = read.csv("DCE3_after_removal_modified_withcovariates_1.csv",header=TRUE)
head(data)
# Dummy coding for all the attributes
# Fasting Time (Fast_A and Fast_B)
database <- data %>%
mutate(Fast1_2 = ifelse(Fast1 == 2, 1, 0),
Fast1_3 = ifelse(Fast1 == 3, 1, 0),
Fast1_4 = ifelse(Fast1 == 4, 1, 0),
Fast2_2 = ifelse(Fast2 == 2, 1, 0),
Fast2_3 = ifelse(Fast2 == 3, 1, 0),
Fast2_4 = ifelse(Fast2 == 4, 1, 0),
# Invasiveness (Inv_A and Inv_B)
Inv1_2 = ifelse(Inv1 == 2, 1, 0),
Inv2_2 = ifelse(Inv2 == 2, 1, 0),
#Sen_A (Sen_A and Sen_B)
Sen1_2 = ifelse(Sen1 == 2, 1, 0),
Sen1_3 = ifelse(Sen1 == 3, 1, 0),
Sen2_2 = ifelse(Sen2 == 2, 1, 0),
Sen2_3 = ifelse(Sen2 == 3, 1, 0),
# Specificity (Spe_A and Spe_B)
Spe1_2 = ifelse(Spe1 == 2, 1, 0),
Spe1_3 = ifelse(Spe1 == 3, 1, 0),
Spe2_2 = ifelse(Spe2 == 2, 1, 0),
Spe2_3 = ifelse(Spe2 == 3, 1, 0),
# Prediction (Pre_A and Pre_B)
Pre1_2 = ifelse(Pre1 == 2, 1, 0),
Pre2_2 = ifelse(Pre2 == 2, 1, 0),
# Cost (Cost_A and Cost_B)
Cost1_2 = ifelse(Cost1 == 2, 1, 0),
Cost1_3 = ifelse(Cost1 == 3, 1, 0),
Cost1_4 = ifelse(Cost1 == 4, 1, 0),
Cost2_2 = ifelse(Cost2 == 2, 1, 0),
Cost2_3 = ifelse(Cost2 == 3, 1, 0),
Cost2_4 = ifelse(Cost2 == 4, 1, 0)
)
# View the data to check the dummy-coded variables
head(database)
### Vector of parameters, including any that are kept fixed in estimation
# Initial parameter values for 2 alternatives and 6 attributes
# Example with corrected parameters
apollo_beta <- c(
asc_alt = 0, # Alternative 1 and 2 (ASC)
asc_alt3 = 0, # Alternative 3 (ASC_optout)
# Fasting Time for alternative 1 and alternative 2 (4 levels)
b_fasting_time_2 = 0,
b_fasting_time_3 = 0,
b_fasting_time_4 = 0,
# Invasiveness for alternative 1 and alternative 2
b_invasiveness_2 = 0,
# Sensitivity for alternative 1 and alternative 2 (3 levels)
b_sensitivity_2 = 0,
b_sensitivity_3 = 0,
# Specificity for alternative 1 and alternative 2 (3 levels)
b_specificity_2 = 0,
b_specificity_3 = 0,
# Prediction for alternative 1 and alternative 2
b_prediction_2 = 0,
# Cost for alternative 1 and alternative 2 (4 levels)
b_cost_2 = 0,
b_cost_3 = 0,
b_cost_4 = 0
)
apollo_fixed <- c("asc_alt")
# ################################################################# #
#### 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(
# Utility function for alternative 1
alt1 = asc_alt +
b_fasting_time_2 * (Fast1_2) +
b_fasting_time_3 * (Fast1_3) +
b_fasting_time_4 * (Fast1_4) +
b_invasiveness_2 * (Inv1_2) +
b_sensitivity_2 * (Sen1_2) +
b_sensitivity_3 * (Sen1_3) +
b_specificity_2 * (Spe1_2) +
b_specificity_3 * (Spe1_3) +
b_prediction_2 * (Pre1_2) +
b_cost_2 * (Cost1_2) +
b_cost_3 * (Cost1_3) +
b_cost_4 * (Cost1_4),
# Utility function for alternative 2
alt2 = asc_alt +
b_fasting_time_2 * (Fast2_2) +
b_fasting_time_3 * (Fast2_3) +
b_fasting_time_4 * (Fast2_4) +
b_invasiveness_2 * (Inv2_2) +
b_sensitivity_2 * (Sen2_2) +
b_sensitivity_3 * (Sen2_3) +
b_specificity_2 * (Spe2_2) +
b_specificity_3 * (Spe2_3) +
b_prediction_2 * (Pre2_2) +
b_cost_2 * (Cost2_2) +
b_cost_3 * (Cost2_3) +
b_cost_4 * (Cost2_4),
alt3 = asc_alt3
)
### Define settings for MNL model component
mnl_settings <- list(
alternatives = c(alt1 = 1, alt2 = 2, alt3=0),
avail = 1, # Assuming all alternatives are available
choiceVar = Choice, # Ensure 'Choice' column exists in your data
utilities = V
)
### Compute probabilities using MNL model
P[["model"]] <- apollo_mnl(mnl_settings, functionality)
### Take product across observations for the same individual if needed (for panel data)
P <- apollo_panelProd(P, apollo_inputs, functionality)
### Prepare and return outputs of the function (final preparation)
P <- apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# ################################################################# #
#### MODEL ESTIMATION ####
# #################################################################
# Estimating the model
model <- apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model,modelOutput_settings = list(printClassical=FALSE))
apollo_saveOutput(model)
####Effect Coded#####
[code]### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
library(dplyr)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "DCE3",
modelDescr = "MNL model",
indivID = "ID",
nAlternatives = 2, # Number of alternatives
alternatives = c(1, 2), # Define alternative IDs
outputDirectory = "output"
)
# Load the data
database <- read.csv("DCE3_after_removal_modified_withcovariates_1.csv")
database
######
# Effect coding (Consistent)
database <- database %>%
mutate(
Fast1_1 = ifelse(Fast1 == 1, 1, ifelse(Fast1 == 4, -1, 0)),
Fast1_2 = ifelse(Fast1 == 2, 1, ifelse(Fast1 == 4, -1, 0)),
Fast1_3 = ifelse(Fast1 == 3, 1, ifelse(Fast1 == 4, -1, 0)),
Fast2_1 = ifelse(Fast2 == 1, 1, ifelse(Fast2 == 4, -1, 0)),
Fast2_2 = ifelse(Fast2 == 2, 1, ifelse(Fast2 == 4, -1, 0)),
Fast2_3 = ifelse(Fast2 == 3, 1, ifelse(Fast2 == 4, -1, 0)),
Inv1_1 = ifelse(Inv1 == 1, 1, -1),
Inv2_1 = ifelse(Inv2 == 1, 1, -1),
Sen1_1 = ifelse(Sen1 == 1, 1, ifelse(Sen1 == 3, -1, 0)),
Sen1_2 = ifelse(Sen1 == 2, 1, ifelse(Sen1 == 3, -1, 0)),
Sen2_1 = ifelse(Sen2 == 1, 1, ifelse(Sen2 == 3, -1, 0)),
Sen2_2 = ifelse(Sen2 == 2, 1, ifelse(Sen2 == 3, -1, 0)),
Spe1_1 = ifelse(Spe1 == 1, 1, ifelse(Spe1 == 3, -1, 0)),
Spe1_2 = ifelse(Spe1 == 2, 1, ifelse(Spe1 == 3, -1, 0)),
Spe2_1 = ifelse(Spe2 == 1, 1, ifelse(Spe2 == 3, -1, 0)),
Spe2_2 = ifelse(Spe2 == 2, 1, ifelse(Spe2 == 3, -1, 0)),
Pre1_1 = ifelse(Pre1 == 1, 1, ifelse(Pre1 == 2, -1, 0)),
Pre2_1 = ifelse(Pre2 == 1, 1, ifelse(Pre2 == 2, -1, 0)),
Cost1_1 = ifelse(Cost1 == 1, 1, ifelse(Cost1 == 4, -1, 0)),
Cost1_2 = ifelse(Cost1 == 2, 1, ifelse(Cost1 == 4, -1, 0)),
Cost1_3 = ifelse(Cost1 == 3, 1, ifelse(Cost1 == 4, -1, 0)),
Cost2_1 = ifelse(Cost2 == 1, 1, ifelse(Cost2 == 4, -1, 0)),
Cost2_2 = ifelse(Cost2 == 2, 1, ifelse(Cost2 == 4, -1, 0)),
Cost2_3 = ifelse(Cost2 == 3, 1, ifelse(Cost2 == 4, -1, 0))
)
view(database)
write.csv(database, "DCE3_effect_coded.csv", row.names = FALSE)
# Apollo setup (as before)
apollo_beta <- c(
asc_alt = 0,
asc_alt3 = 0, # Opt-out ASC
b_Fast_1 = 0,
b_Fast_2 = 0,
b_Fast_3 = 0,
b_Inv_1 = 0,
b_Sen_1 = 0,
b_Sen_2 = 0,
b_Spe_1 = 0,
b_Spe_2 = 0,
b_Pre_1 = 0,
b_Cost_1 = 0,
b_Cost_2 = 0,
b_Cost_3 = 0
)
apollo_fixed <- c("asc_alt3") #Fixing opt-out ASC
# ################################################################# #
#### 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()
### Effects coding constraint
b_Fast_4 = - b_Fast_1 - b_Fast_2- b_Fast_3
b_Inv_2 = - b_Inv_1
b_Sen_3 = - b_Sen_1 - b_Sen_2
b_Spe_3 = - b_Spe_1 - b_Spe_2
b_Pre_2 = - b_Pre_1
b_Cost_4 = -b_Cost_1 -b_Cost_2 -b_Cost_3
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V <- list(
alt1 = asc_alt +
b_Fast_1 * Fast1_1 + b_Fast_2 * Fast1_2 + b_Fast_3 * Fast1_3 +
b_Inv_1 * Inv1_1 +
b_Sen_1 * Sen1_1 + b_Sen_2 * Sen1_2 +
b_Spe_1 * Spe1_1 + b_Spe_2 * Spe1_2 +
b_Pre_1 * Pre1_1 +
b_Cost_1 * Cost1_1 + b_Cost_2 * Cost1_2 + b_Cost_3 * Cost1_3 ,
alt2 = asc_alt +
b_Fast_1 * Fast2_1 + b_Fast_2 * Fast2_2 + b_Fast_3 * Fast2_3 +
b_Inv_1 * Inv2_1 +
b_Sen_1 * Sen2_1 + b_Sen_2 * Sen2_2 +
b_Spe_1 * Spe2_1 + b_Spe_2 * Spe2_2 +
b_Pre_1 * Pre2_1 +
b_Cost_1 * Cost2_1 + b_Cost_2 * Cost2_2 + b_Cost_3 * Cost2_3,
alt3 = asc_alt3 # Opt-out utility
)
### Define settings for MNL model component
mnl_settings <- list(
alternatives = c(alt1 = 1, alt2 = 2, alt3=0),
avail = 1, # Assuming all alternatives are available
choiceVar = Choice, # Ensure 'Choice' column exists in your data
utilities = V
)
### Compute probabilities using MNL model
P[["model"]] <- apollo_mnl(mnl_settings, functionality)
### Take product across observations for the same individual if needed (for panel data)
P <- apollo_panelProd(P, apollo_inputs, functionality)
### Prepare and return outputs of the function (final preparation)
P <- apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# ################################################################# #
#### MODEL ESTIMATION ####
# #################################################################
# Estimating the model
model <- apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model,modelOutput_settings = list(printClassical=TRUE, printPVal =2))
apollo_saveOutput(model)
# ################################################################# #
##### POST-PROCESSING ####
# ################################################################# #
### Print outputs of additional diagnostics to new output file (remember to close file writing when complete)
apollo_sink()
### calculate value and standard error for base of effects coded parameter
apollo_deltaMethod(model,deltaMethod_settings = list(expression=c(b_Fast_4 = "- b_Fast_1 - b_Fast_2- b_Fast_3",
b_Inv_2 = "- b_Inv_1",
b_Sen_3 = "- b_Sen_1 - b_Sen_2",
b_Spe_3 = "- b_Spe_1 - b_Spe_2",
b_Pre_2 = "- b_Pre_1",
b_Cost_4 = "-b_Cost_1 -b_Cost_2 -b_Cost_3"
)))
Code: Select all
###EFFECT CODE- mixed logit####
#########OPTOUT#################################################
rm(list = ls())
### Load Apollo library
library(apollo)
library(tidyverse)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "MMNL_DCE3_uncorrelated_effect",
modelDescr = "Mixed logit model on DCE3 data",
indivID = "ID",
nCores = 4,
seed = 2025,
outputDirectory = "output"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
### Loading data
database <- read.csv("DCE3_after_removal_modified_withoutcovariates_1.csv")
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta <- c(asc =0,
asc_3 = 0,
mu_b_Fast_2= 0,
mu_b_Fast_3= 0,
mu_b_Fast_4= 0,
mu_b_Inv_2 = 0,
mu_b_Sen_2 = 0,
mu_b_Sen_3 = 0,
mu_b_Spe_2 = 0,
mu_b_Spe_3 = 0,
mu_b_Pre_2 = 0,
mu_b_Cost_2 = 0,
mu_b_Cost_3 = 0,
mu_b_Cost_4 = 0,
# Standard deviation parameters (sigma) - small positive values
sigma_b_Fast_2 = 0,
sigma_b_Fast_3 = 0,
sigma_b_Fast_4 = 0,
sigma_b_Inv_2 = 0,
sigma_b_Sen_2 = 0,
sigma_b_Sen_3 = 0,
sigma_b_Spe_2 = 0,
sigma_b_Spe_3 = 0,
sigma_b_Pre_2 = 0,
sigma_b_Cost_2 = 0.01,
sigma_b_Cost_3 = 0.01,
sigma_b_Cost_4 = 0.01
)
apollo_fixed <- c("asc")
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "mlhs", #distribution is across people rather than across choice for the same person
interNDraws = 1000,
interUnifDraws = c(),
interNormDraws = c("draws_Fast_2","draws_Fast_3","draws_Fast_4",
"draws_Inv_2",
"draws_Sen_2","draws_Sen_3",
"draws_Spe_2","draws_Spe_3",
"draws_Pre_2",
"draws_Cost_2","draws_Cost_3","draws_Cost_4")
)
### Create random parameter
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
#(mean + std dev * std normal variable)
randcoeff[["b_Fast_2"]] = mu_b_Fast_2 + sigma_b_Fast_2 * draws_Fast_2
randcoeff[["b_Fast_3"]] = mu_b_Fast_3 + sigma_b_Fast_3 * draws_Fast_3
randcoeff[["b_Fast_4"]] = mu_b_Fast_4 + sigma_b_Fast_4 * draws_Fast_4
randcoeff[["b_Inv_2"]] = mu_b_Inv_2 + sigma_b_Inv_2 * draws_Inv_2
randcoeff[["b_Sen_2"]] = mu_b_Sen_2 + sigma_b_Sen_2 * draws_Sen_2
randcoeff[["b_Sen_3"]] = mu_b_Sen_3 + sigma_b_Sen_3 * draws_Sen_3
randcoeff[["b_Spe_2"]] = mu_b_Spe_2 + sigma_b_Spe_2 * draws_Spe_2
randcoeff[["b_Spe_3"]] = mu_b_Spe_3 + sigma_b_Spe_3 * draws_Spe_3
randcoeff[["b_Pre_2"]] = mu_b_Pre_2 + sigma_b_Pre_2 * draws_Pre_2
randcoeff[["b_Cost_2"]] = mu_b_Cost_2 + sigma_b_Cost_2 * draws_Cost_2
randcoeff[["b_Cost_3"]] = mu_b_Cost_3 + sigma_b_Cost_3 * draws_Cost_3
randcoeff[["b_Cost_4"]] = mu_b_Cost_4 + sigma_b_Cost_4 * draws_Cost_4
return(randcoeff)
}
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
### Function initialisation: do not change the following three commands
### 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()
### Effects coding constraint
b_Fast_1 = -b_Fast_2 -b_Fast_3 - b_Fast_4
b_Inv_1 = -b_Inv_2
b_Sen_1 = -b_Sen_2 - b_Sen_3
b_Spe_1 = -b_Spe_2- b_Spe_3
b_Pre_1 = -b_Pre_2
b_Cost_1 = -b_Cost_2 - b_Cost_3 - b_Cost_4
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V <- list(
alt1 = asc +
b_Fast_1 * (Fast1 ==1) + b_Fast_2 * (Fast1==2) + b_Fast_3 * (Fast1 == 3) + b_Fast_4 * (Fast1 == 4) +
b_Inv_1 * (Inv1==1) + b_Inv_2* (Inv1==2)+
b_Sen_1 * (Sen1 == 1) + b_Sen_2 * (Sen1==2) + b_Sen_3 * (Sen1==3)+
b_Spe_1 * (Spe1==1) + b_Spe_2 * (Spe1==2) + b_Spe_3 * (Spe1==3) +
b_Pre_1 * (Pre1==1) + b_Pre_2 * (Pre1==2)+
b_Cost_1 * (Cost1==1) + b_Cost_2 * (Cost1==2) + b_Cost_3 * (Cost1==3)+ b_Cost_4 * (Cost1==4),
alt2 = asc +
b_Fast_1 * (Fast2==1) + b_Fast_2 * (Fast2==2) + b_Fast_3 * (Fast2==3) + b_Fast_4 * (Fast2==4)+
b_Inv_1 * (Inv2==1) + b_Inv_2 * (Inv2==2) +
b_Sen_1 * (Sen2==1) + b_Sen_2 * (Sen2==2) + b_Sen_3 * (Sen2==3)+
b_Spe_1 * (Spe2==1) + b_Spe_2 * (Spe2==2) + b_Spe_3 * (Spe2==3)+
b_Pre_1 * (Pre2==1) + b_Pre_2 * (Pre2==2)+
b_Cost_1 * (Cost2==1) + b_Cost_2 * (Cost2==2) + b_Cost_3 * (Cost2==3) + b_Cost_4 * (Cost2==4),
alt3 = asc_3
)
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2,alt3=0),
avail = list(alt1=1, alt2=1,alt3=1),
choiceVar = Choice,
utilities = V
)
### Compute probabilities using MNL model
P[["model"]] = apollo_mnl(mnl_settings, functionality)
### Take product across observation for same individual(heterogeneity is across per person; how likely is the sequences of choices per person in each group)
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,list(writeIter=FALSE,
computeVCov = TRUE, printClassical=TRUE ))
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput (model, modelOutput_settings=list(printPVal = 2))
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)
###POST PROCESSING#####
### Print outputs of additional diagnostics to new output file (remember to close file writing when complete)
apollo_sink()
### calculate value and standard error for base of effects coded parameter
delta<-apollo_deltaMethod(model,deltaMethod_settings = list(expression=c(
mu_b_Fast_1 = "- mu_b_Fast_2 - mu_b_Fast_3 - mu_b_Fast_4",
mu_b_Inv_1 = "- mu_b_Inv_2",
mu_b_Sen_1 = "- mu_b_Sen_2 - mu_b_Sen_3",
mu_b_Spe_1 = "- mu_b_Spe_2 - mu_b_Spe_3",
mu_b_Pre_1 = "- mu_b_Pre_2",
mu_b_Cost_1 = "-mu_b_Cost_2 -mu_b_Cost_3 -mu_b_Cost_4",
sigma_b_Fast_1 = "- sigma_b_Fast_2 - sigma_b_Fast_3 - sigma_b_Fast_4",
sigma_b_Inv_1 = "- sigma_b_Inv_2",
sigma_b_Sen_1 = "- sigma_b_Sen_2 - sigma_b_Sen_3",
sigma_b_Spe_1 = "- sigma_b_Spe_2 -sigma_b_Spe_3",
sigma_b_Pre_1 = "- sigma_b_Pre_2",
sigma_b_Cost_1 = "-sigma_b_Cost_2 -sigma_b_Cost_3 -sigma_b_Cost_4"
)))
print(delta)
# ----------------------------------------------------------------- #
#---- switch off writing to file ----
# ----------------------------------------------------------------- #
apollo_sink()
######DUMMY -mixed logit#########
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
library(tidyverse)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "MMNL_DCE3_uncorrelated_dummy",
modelDescr = "Mixed logit model on DCE3 data, uncorrelated__Normal",
indivID = "ID",
nCores = 4,
seed = 2025,
outputDirectory = "output"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
### Loading data
database = read.csv("DCE3_after_removal_modified_withoutcovariates_1.csv",header=TRUE)
# View the data to check the dummy-coded variables
head(database)
###################################################################
#### DEFINE MODEL PARAMETERS ####
###################################################################
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc =0,
asc_3 = 0,
b_Fast_1 = 0,
mu_b_Fast_2 = 0,
mu_b_Fast_3 = 0,
mu_b_Fast_4 = 0,
b_Inv_1 = 0,
mu_b_Inv_2 = 0,
b_Sen_1 = 0,
mu_b_Sen_2 = 0,
mu_b_Sen_3 = 0,
b_Spe_1 = 0,
mu_b_Spe_2 = 0,
mu_b_Spe_3 = 0,
b_Pre_1 = 0,
mu_b_Pre_2 = 0,
b_Cost_1 = 0,
mu_log_b_Cost_2 = 0,
mu_log_b_Cost_3 = 0,
mu_log_b_Cost_4 = 0,
sigma_b_Fast_2 = 0,
sigma_b_Fast_3 = 0,
sigma_b_Fast_4 = 0,
sigma_b_Inv_2 = 0,
sigma_b_Sen_2 = 0,
sigma_b_Sen_3 = 0,
sigma_b_Spe_2 = 0,
sigma_b_Spe_3 = 0,
sigma_b_Pre_2 = 0,
sigma_log_b_Cost_2 = 0.01,
sigma_log_b_Cost_3 = 0.01,
sigma_log_b_Cost_4 = 0.01
)
### 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","b_Fast_1", "b_Inv_1", "b_Sen_1", "b_Spe_1", "b_Pre_1", "b_Cost_1")
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "mlhs", #distribution is across people rather than across choice for the same person
interNDraws = 1000,
interUnifDraws = c(),
interNormDraws = c("draws_Fast_2","draws_Fast_3","draws_Fast_4",
"draws_Inv_2",
"draws_Sen_2","draws_Sen_3",
"draws_Spe_2","draws_Spe_3",
"draws_Pre_2",
"draws_Cost_2","draws_Cost_3", "draws_Cost_4"
),
intraDrawsType = "mlhs",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)
### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
#(mean + std dev * std normal variable)
randcoeff[["b_Fast_2"]] = mu_b_Fast_2 + sigma_b_Fast_2 * draws_Fast_2
randcoeff[["b_Fast_3"]] = mu_b_Fast_3 + sigma_b_Fast_3 * draws_Fast_3
randcoeff[["b_Fast_4"]] = mu_b_Fast_4 + sigma_b_Fast_4 * draws_Fast_4
randcoeff[["b_Inv_2"]] = mu_b_Inv_2 + sigma_b_Inv_2 * draws_Inv_2
randcoeff[["b_Sen_2"]] = mu_b_Sen_2 + sigma_b_Sen_2 * draws_Sen_2
randcoeff[["b_Sen_3"]] = mu_b_Sen_3 + sigma_b_Sen_3 * draws_Sen_3
randcoeff[["b_Spe_2"]] = mu_b_Spe_2 + sigma_b_Spe_2 * draws_Spe_2
randcoeff[["b_Spe_3"]] = mu_b_Spe_3 + sigma_b_Spe_3 * draws_Spe_3
randcoeff[["b_Pre_2"]] = mu_b_Pre_2 + sigma_b_Pre_2 * draws_Pre_2
randcoeff[["b_Cost_2"]] = mu_log_b_Cost_2 + sigma_log_b_Cost_2*draws_Cost_2
randcoeff[["b_Cost_3"]] = mu_log_b_Cost_3 + sigma_log_b_Cost_3*draws_Cost_3
randcoeff[["b_Cost_4"]] = mu_log_b_Cost_4 + sigma_log_b_Cost_4*draws_Cost_4
return(randcoeff)
}
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
### Function initialisation: do not change the following three commands
### 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(
alt1 = asc +
b_Fast_1*(Fast1==1) + b_Fast_2*(Fast1==2) + b_Fast_3* (Fast1 == 3) + b_Fast_4 * (Fast1 == 4)+
b_Inv_1*(Inv1==1) + b_Inv_2 * (Inv1 ==2)+
b_Sen_1*(Sen1==1) + b_Sen_2 * (Sen1==2) + b_Sen_3 * (Sen1==3) +
b_Spe_1*(Spe1==1) + b_Spe_2 * (Spe1==2) + b_Spe_3 * (Spe1==3) +
b_Pre_1*(Pre1==1) + b_Pre_2 * (Pre1==2) +
b_Cost_1*(Cost1==1) + b_Cost_2* (Cost1==2)+ b_Cost_3* (Cost1==3) + b_Cost_4 * (Cost1==4) ,
alt2 = asc +
b_Fast_1*(Fast2==1) + b_Fast_2* (Fast2==2)+ b_Fast_3* (Fast2==3) + b_Fast_4 * (Fast2==4)+
b_Inv_1*(Inv2==1) + b_Inv_2 * (Inv2==2) +
b_Sen_1*(Sen2==1) + b_Sen_2 * (Sen2==2) + b_Sen_3 * (Sen2==3) +
b_Spe_1*(Spe2==1) + b_Spe_2 * (Spe2==2) + b_Spe_3 * (Spe2==3) +
b_Pre_1*(Pre2==1) + b_Pre_2 * (Pre2==2) +
b_Cost_1*(Cost2==1) + b_Cost_2* (Cost2==2)+ b_Cost_3* (Cost2==3) + b_Cost_4 * (Cost2==4),
alt3 = asc_3
)
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2, alt3=0),
avail = list(alt1=1, alt2=1, alt3=1),
choiceVar = Choice,
utilities = V
)
### Compute probabilities using MNL model
P[["model"]] = apollo_mnl(mnl_settings, functionality)
### Take product across observation for same individual(heterogeneity is across per person; how likely is the sequences of choices per person in each group)
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,modelOutput_settings = list(printPVal = 2))
apollo_modelOutput(model)
I hope I can get some clarity about this.