Page 1 of 1

Errors in Mixed Logit Model

Posted: 14 Apr 2022, 00:34
by eunkum
I conducted a mixed logit analysis using Apollo to examine the following research question: What participant characteristics (e.g. sociodemographic, health status, motivation) play a role in people’s preferences for certain characteristics of a proposed physical activity reward program?

Dependent variable= choice
Independent variable: income_recode, age, sex, pain_recode, IPAQ_cat, BMI_recode, pain_recode, IPAQ_cat, IPAQ_catGoal1, IPAQ_catGoal2, BMI_recode, intrinsic, loss_score

I used the following code. Please see the error at the end.

### Clear memory
### Load Apollo library
### Initialise code


rm(list = ls())
library(apollo)
apollo_initialise()

### Set core controls
apollo_control = list(
modelName = "MMNL_preference_space",
modelDescr = "Mixed logit model on choice data",
indivID = "ID",
mixing = TRUE,
nCores = 4,
outputDirectory = "output"
)

### Loading data
database <-read.table("C:/haysem/dataRQ3.txt", header=TRUE)
summary(database)


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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(mu_log_b_goal = -3,
sigma_log_b_goal = -0.01,
mu_log_b_form = -3,
sigma_log_b_form = -0.01,
mu_log_b_mag = -3,
sigma_log_b_mag = -0.01,
mu_log_b_dir = -3,
sigma_log_b_dir = -0.01
delta_a = -3,
delta_b = -0.01,
gamma_income_recode_a = -3,
gamma_age_a = -3,
gamma_sex_a =-3,
gamma_pain_recode_a = -3,
gamma_IPAQ_cat_a = -3,
gamma_IPAQ_catGoal1_a = -3,
gamma_IPAQ_catGoal2_a = -3,
gamma_BMI_recode_a = -3,
gamma_intrinsic_a = -3,
gamma_loss_score_a = -3,
gamma_income_recode_b = 0,
gamma_age_b = 0,
gamma_sex_b = 0,
gamma_pain_recode_b = 0,
gamma_IPAQ_cat_b = 0,
gamma_IPAQ_catGoal1_b = 0,
gamma_IPAQ_catGoal2_b = 0,
gamma_BMI_recode_b = 0,
gamma_intrinsic_b = 0,
gamma_loss_score_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()


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

### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "halton",
interNDraws = 500,
interUnifDraws = c(),
interNormDraws = c("draws_goal","draws_form","draws_mag","draws_dir"),
intraDrawsType = "halton",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)

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

randcoeff[["b_goal"]] = -exp( mu_log_b_goal + sigma_log_b_goal * draws_goal )
randcoeff[["b_form"]] = -exp( mu_log_b_form + sigma_log_b_form * draws_form )
randcoeff[["b_mag"]] = -exp( mu_log_b_mag + sigma_log_b_mag * draws_mag )
randcoeff[["b_dir"]] = -exp( mu_log_b_dir + sigma_log_b_dir * draws_dir )

return(randcoeff)
}

V=list()
V[["class_a"]] = delta_a + gamma_income_recode_a*income_recode + gamma_age_a*age + gamma_sex_a*sex + gamma_pain_recode_a*pain_recode + gamma_IPAQ_cat_a*IPAQ_cat + gamma_IPAQ_catGoal1_a*IPAQ_catGoal1 + gamma_IPAQ_catGoal2_a*IPAQ_catGoal2 + gamma_BMI_recode_a*BMI_recode + gamma_intrinsic_a*intrinsic + gamma_loss_score_a*loss_score
V[["class_b"]] = delta_b + gamma_income_recode_b*income_recode + gamma_age_b*age + gamma_sex_b*sex + gamma_pain_recode_b*pain_recode + gamma_IPAQ_cat_b*IPAQ_cat + gamma_IPAQ_catGoal1_b*IPAQ_catGoal1 + gamma_IPAQ_catGoal2_b*IPAQ_catGoal2 + gamma_BMI_recode_b*BMI_recode + gamma_intrinsic_b*intrinsic + gamma_loss_score_b*loss_score

classAlloc_settings = list(
classes = c(class_a=1, class_b=2),
utilities = V
)

randcoeff[["pi_values"]] = apollo_classAlloc(classAlloc_settings)

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()
V[["alt1"]] = b_goal * goal1 + b_form * form1 + b_mag * mag1 + b_dir * dir1
V[["alt2"]] = b_goal * goal2 + b_form * form2 + b_mag * mag2


### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2),
avail = list(alt1=1, alt2=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)

I got the following error:

> apollo_beta = c(mu_log_b_goal = -3,
+ sigma_log_b_goal = -0.01,
+ mu_log_b_form = -3,
+ sigma_log_b_form = -0.01,
+ mu_log_b_mag = -3,
+ sigma_log_b_mag = -0.01,
+ mu_log_b_dir = -3,
+ sigma_log_b_dir = -0.01
+ delta_a = -3,
Error: unexpected symbol in:
" sigma_log_b_dir = -0.01
delta_a"
> delta_b = -0.01,
Error: unexpected ',' in "delta_b = -0.01,"
> gamma_income_recode_a = -3,
Error: unexpected ',' in "gamma_income_recode_a = -3,"
> gamma_age_a = -3,
Error: unexpected ',' in "gamma_age_a = -3,"
> gamma_sex_a =-3,
Error: unexpected ',' in "gamma_sex_a =-3,"
> gamma_pain_recode_a = -3,
Error: unexpected ',' in "gamma_pain_recode_a = -3,"
> gamma_IPAQ_cat_a = -3,
Error: unexpected ',' in "gamma_IPAQ_cat_a = -3,"
> gamma_IPAQ_catGoal1_a = -3,
Error: unexpected ',' in "gamma_IPAQ_catGoal1_a = -3,"
> gamma_IPAQ_catGoal2_a = -3,
Error: unexpected ',' in "gamma_IPAQ_catGoal2_a = -3,"
> gamma_BMI_recode_a = -3,
Error: unexpected ',' in "gamma_BMI_recode_a = -3,"
> gamma_intrinsic_a = -3,
Error: unexpected ',' in "gamma_intrinsic_a = -3,"
> gamma_loss_score_a = -3,
Error: unexpected ',' in "gamma_loss_score_a = -3,"
> gamma_income_recode_b = 0,
Error: unexpected ',' in "gamma_income_recode_b = 0,"
> gamma_age_b = 0,
Error: unexpected ',' in "gamma_age_b = 0,"
> gamma_sex_b = 0,
Error: unexpected ',' in "gamma_sex_b = 0,"
> gamma_pain_recode_b = 0,
Error: unexpected ',' in "gamma_pain_recode_b = 0,"
> gamma_IPAQ_cat_b = 0,
Error: unexpected ',' in "gamma_IPAQ_cat_b = 0,"
> gamma_IPAQ_catGoal1_b = 0,
Error: unexpected ',' in "gamma_IPAQ_catGoal1_b = 0,"
> gamma_IPAQ_catGoal2_b = 0,
Error: unexpected ',' in "gamma_IPAQ_catGoal2_b = 0,"
> gamma_BMI_recode_b = 0,
Error: unexpected ',' in "gamma_BMI_recode_b = 0,"
> gamma_intrinsic_b = 0,
Error: unexpected ',' in "gamma_intrinsic_b = 0,"
> gamma_loss_score_b = 0)

Re: Errors in Mixed Logit Model

Posted: 15 Apr 2022, 11:03
by stephanehess
Hi, the error is pretty easy to understand

Code: Select all

+ mu_log_b_dir = -3,
+ sigma_log_b_dir = -0.01
+ delta_a = -3,
Error: unexpected symbol in:
" sigma_log_b_dir = -0.01
You forgot the comma at the end of the line with sigma_log_b_dir