Important: Read this before posting to this forum

  1. This forum is for questions related to the use of Apollo. We will answer some general choice modelling questions too, where appropriate, and time permitting. We cannot answer questions about how to estimate choice models with other software packages.
  2. There is a very detailed manual for Apollo available at http://www.ApolloChoiceModelling.com/manual.html. This contains detailed descriptions of the various Apollo functions, and numerous examples are available at http://www.ApolloChoiceModelling.com/examples.html. In addition, help files are available for all functions, using e.g. ?apollo_mnl
  3. Before asking a question on the forum, users are kindly requested to follow these steps:
    1. Check that the same issue has not already been addressed in the forum - there is a search tool.
    2. Ensure that the correct syntax has been used. For any function, detailed instructions are available directly in Apollo, e.g. by using ?apollo_mnl for apollo_mnl
    3. Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
    4. Make sure that R is using the latest official release of Apollo.
  4. If the above steps do not resolve the issue, then users should follow these steps when posting a question:
    1. provide full details on the issue, including the entire code and output, including any error messages
    2. posts will not immediately appear on the forum, but will be checked by a moderator first. This may take a day or two at busy times. There is no need to submit the post multiple times.

ICLV model using ordered measurement model for indicators

Ask questions about how to estimate models and how to change your settings for estimation.
Post Reply
sethyash52
Posts: 11
Joined: 09 Sep 2022, 06:38

ICLV model using ordered measurement model for indicators

Post by sethyash52 »

Hi Stephane,

I am working on an ICLV model with two latent variables using an ordered measurement model for indicators. Please check if my code is correct.
I am getting NaN values; I have tried changing the initial values, but the problem still needs to be solved. Thanks
### Clear memory
rm(list = ls())

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName = "Ordered HCM 4(3)",
modelDescr = "Hybrid choice model on drug choice data, using ordered measurement model for indicators",
indivID = "Person_ID",
mixing = TRUE,
nCores = 24,
outputDirectory = "New"
)
setwd("")

getwd()

database = read.csv("",header=TRUE)


apollo_beta = c(# alternate specific attributes
asc_CV = 0,
asc_EV = 0,
b_pp = 0,
b_oc = 0,
b_ct = 0,

# socio-demographic characteristic
gamma_Male_socIma = 0,
gamma_female_socIma = 0,
gamma_dailyDistanceAvg_socIma = 0,
gamma_Male_envEnth = 0,
gamma_female_envEnth = 0,
gamma_dailyDistanceAvg_envEnth = 0,

# Latent variable_1 (it has 4 indicatos)
lambda_socIma = 1,
sigma_eta_socIma =-3,
zeta_socIma_1 = 1,
zeta_socIma_2 = 1,
zeta_socIma_3 = 1,
zeta_socIma_4 = 1,
constant_socIma_1 = 0,
constant_socIma_2 = 0,
constant_socIma_3 = 0,
constant_socIma_4 = 0,
tau_socIma_1_1 = 0,
tau_socIma_1_2 = 5,
tau_socIma_1_3 = 7,
tau_socIma_1_4 = 9,
tau_socIma_2_1 = 0,
tau_socIma_2_2 = 5,
tau_socIma_2_3 = 7,
tau_socIma_2_4 = 9,
tau_socIma_3_1 = 0,
tau_socIma_3_2 = 5,
tau_socIma_3_3 = 7,
tau_socIma_3_4 = 9,
tau_socIma_4_1 = 0,
tau_socIma_4_2 = 5,
tau_socIma_4_3 = 7,
tau_socIma_4_4 = 9,

#Latent variable_2 (it has 3 indicators)
lambda_envEnth = 1,
sigma_eta_envEnth =-3,
zeta_envEnth_1 = 1,
zeta_envEnth_2 = 1,
zeta_envEnth_3 = 1,
constant_envEnth_1 = 0,
constant_envEnth_2 = 0,
constant_envEnth_3 = 0,
tau_envEnth_1_1 = 0,
tau_envEnth_1_2 = 5,
tau_envEnth_1_3 = 7,
tau_envEnth_1_4 = 9,
tau_envEnth_2_1 = 0,
tau_envEnth_2_2 = 5,
tau_envEnth_2_3 = 7,
tau_envEnth_2_4 = 9,
tau_envEnth_3_1 = 0,
tau_envEnth_3_2 = 5,
tau_envEnth_3_3 = 7,
tau_envEnth_3_4 = 9)

apollo_fixed = c("asc_CV","gamma_female_socIma","gamma_female_envEnth","tau_socIma_1_1","tau_socIma_2_1","tau_socIma_3_1","tau_socIma_4_1","zeta_socIma_1","tau_envEnth_1_1","tau_envEnth_2_1","tau_envEnth_3_1","zeta_envEnth_1")

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

### Set parameters for generating draws
apollo_draws = list(
interDrawsType="halton",
interNDraws=100,
interUnifDraws=c(),
interNormDraws=c("eta_socIma","eta_envEnth"),

intraDrawsType="",
intraNDraws=0,
intraUnifDraws=c(),
intraNormDraws=c()
)


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


randcoeff[["LV_socIma"]] = gamma_Male_socIma*genderCoded + gamma_dailyDistanceAvg_socIma*dailyDistanceAvg + sigma_eta_socIma*eta_socIma
randcoeff[["LV_envEnth"]] = gamma_Male_envEnth*genderCoded + gamma_dailyDistanceAvg_envEnth*dailyDistanceAvg + sigma_eta_envEnth*eta_envEnth

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()

### Likelihood of indicators
ol_settings1 = list(outcomeOrdered = socIma_1,
V = constant_socIma_1 + zeta_socIma_1*LV_socIma,
tau = list(tau_socIma_1_1, tau_socIma_1_2, tau_socIma_1_3, tau_socIma_1_4),
#rows = (task==1),
componentName = "indic_socIma_1")
ol_settings2 = list(outcomeOrdered = socIma_2,
V = constant_socIma_2 + zeta_socIma_2*LV_socIma,
tau = list(tau_socIma_2_1, tau_socIma_2_2, tau_socIma_2_3, tau_socIma_2_4),
#rows = (task==1),
componentName = "indic_socIma_2")
ol_settings3 = list(outcomeOrdered = socIma_3,
V = constant_socIma_3 + zeta_socIma_3*LV_socIma,
tau = list(tau_socIma_3_1, tau_socIma_3_2, tau_socIma_3_3, tau_socIma_3_4),
#rows = (task==1),
componentName = "indic_socIma_3")
ol_settings4 = list(outcomeOrdered = socIma_4,
V = constant_socIma_4 + zeta_socIma_4*LV_socIma,
tau = list(tau_socIma_4_1, tau_socIma_4_2, tau_socIma_4_3, tau_socIma_4_4),
#rows = (task==1),
componentName = "indic_socIma_4")
ol_settings5 = list(outcomeOrdered = envEnth_1,
V = constant_envEnth_1 + zeta_envEnth_1*LV_envEnth,
tau = list(tau_envEnth_1_1, tau_envEnth_1_2, tau_envEnth_1_3, tau_envEnth_1_4),
#rows = (task==1),
componentName = "indic_envEnth_1")
ol_settings6 = list(outcomeOrdered = envEnth_2,
V = constant_envEnth_2 + zeta_envEnth_2*LV_envEnth,
tau = list(tau_envEnth_2_1, tau_envEnth_2_2, tau_envEnth_2_3, tau_envEnth_2_4),
#rows = (task==1),
componentName = "indic_envEnth_2")
ol_settings7 = list(outcomeOrdered = envEnth_3,
V = constant_envEnth_3 + zeta_envEnth_3*LV_envEnth,
tau = list(tau_envEnth_3_1, tau_envEnth_3_2, tau_envEnth_3_3, tau_envEnth_3_4),
#rows = (task==1),
componentName = "indic_envEnth_3")


P[["indic_socIma_1"]] = apollo_ol(ol_settings1, functionality)
P[["indic_socIma_2"]] = apollo_ol(ol_settings2, functionality)
P[["indic_socIma_3"]] = apollo_ol(ol_settings3, functionality)
P[["indic_socIma_4"]] = apollo_ol(ol_settings4, functionality)
P[["indic_envEnth_1"]] = apollo_ol(ol_settings5, functionality)
P[["indic_envEnth_2"]] = apollo_ol(ol_settings6, functionality)
P[["indic_envEnth_3"]] = apollo_ol(ol_settings7, functionality)

### Likelihood of choices
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()

V[["alt1"]] = asc_EV + (b_pp*(ppEV*(50+20*log(incomeAvg/10000))) + b_oc*ocEV + b_ct*ctEV/60 )
V[["alt2"]] = asc_CV + (b_pp*(ppCV*(50+20*log(incomeAvg/10000))) + b_oc*ocCV + lambda_socIma*LV_socIma + lambda_envEnth*LV_envEnth )


### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2),
avail = list(alt1=1, alt2=1),
choiceVar = choiceVehicle,
utilities = V,
componentName = "choice"
)

### Compute probabilities for MNL model component
P[["choice"]] = apollo_mnl(mnl_settings, functionality)

### Likelihood of the whole model
P = apollo_combineModels(P, apollo_inputs, 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 = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs, estimate_settings = list(scaleAfterConvergence=FALSE))


apollo_modelOutput(model)
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: ICLV model using ordered measurement model for indicators

Post by stephanehess »

We can only help you if you show us the outputs, it's impossible to diagnose the problem otherwise
--------------------------------
Stephane Hess
www.stephanehess.me.uk
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: ICLV model using ordered measurement model for indicators

Post by stephanehess »

We can only help you if you show us the outputs, it's impossible to diagnose the problem otherwise
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply