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 specification in unlabelled form

Ask questions about model specifications. Ideally include a mathematical explanation of your proposed model.
Post Reply
Peter_C
Posts: 17
Joined: 03 May 2020, 13:52

ICLV model specification in unlabelled form

Post by Peter_C »

Dear Stephane and David,

I would like to ask that, is the following ICLV model (with random parameters) specification correct?

(I have three alternatives (in unlabelled form), 4 attributes (price (4 level), investment cost (4 level), level of environmental friendliness (4 level), comfort (2 level), and I have results from 12 statements (1-5 agreement scale) relate to environmental friendliness)

rm(list = ls())
library(apollo)
apollo_initialise()
apollo_control = list(
modelName = "ICLV",
modelDescr = "ICLV model with random parameters",
indivID = "ID",
mixing = TRUE,
nCores = 1
)

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

apollo_beta = c(asc_alt1 = 0,
asc_alt2 = 0,
asc_alt3 = 0,
mu_log_b_price = 0,
sigma_log_b_price = 0,
mu_log_b_cost = 0,
sigma_log_b_cost = 0,
b_envflow = 0,
mu_b_envfmedium = 0,
sigma_b_envfmedium = 0,
mu_b_envfhigh = 0,
sigma_b_envfhigh = 0,
mu_b_envfveryhigh = 0,
sigma_b_envfveryhigh = 0,
b_automatic = 0,
mu_b_manual = 0,
sigma_b_manual = 0,
b_envfmedium_shift_lambda = 0,
b_envfhigh_shift_lambda = 0,
b_envfveryhigh_shift_lambda = 0,
gamma_edu2 = 0,
gamma_edu3 = 0,
gamma_edu4 = 0,
gamma_residence2 = 0,
gamma_residence3 = 0,
gamma_residence4 = 0,
gamma_income2 = 0,
gamma_income3 = 0,
zeta_q1 = 1,
zeta_q2 = 1,
zeta_q3 = 1,
zeta_q4 = 1,
zeta_q5 = 1,
zeta_q6 = 1,
zeta_q7 = 1,
zeta_q8 = 1,
zeta_q9 = 1,
zeta_q10 = 1,
zeta_q11 = 1,
zeta_q12 = 1,
tau_q1_1 =-2,
tau_q1_2 =-1,
tau_q1_3 = 1,
tau_q1_4 = 2,
tau_q2_1 =-2,
tau_q2_2 =-1,
tau_q2_3 = 1,
tau_q2_4 = 2,
tau_q3_1 =-2,
tau_q3_2 =-1,
tau_q3_3 = 1,
tau_q3_4 = 2,
tau_q4_1 =-2,
tau_q4_2 =-1,
tau_q4_3 = 1,
tau_q4_4 = 2,
tau_q5_1 =-2,
tau_q5_2 =-1,
tau_q5_3 = 1,
tau_q5_4 = 2,
tau_q6_1 =-2,
tau_q6_2 =-1,
tau_q6_3 = 1,
tau_q6_4 = 2,
tau_q7_1 =-2,
tau_q7_2 =-1,
tau_q7_3 = 1,
tau_q7_4 = 2,
tau_q8_1 =-2,
tau_q8_2 =-1,
tau_q8_3 = 1,
tau_q8_4 = 2,
tau_q9_1 =-2,
tau_q9_2 =-1,
tau_q9_3 = 1,
tau_q9_4 = 2,
tau_q10_1 =-2,
tau_q10_2 =-1,
tau_q10_3 = 1,
tau_q10_4 = 2,
tau_q11_1 =-2,
tau_q11_2 =-1,
tau_q11_3 = 1,
tau_q11_4 = 2,
tau_q12_1 =-2,
tau_q12_2 =-1,
tau_q12_3 = 1,
tau_q12_4 = 2)

apollo_fixed = c("asc_alt1", "b_envflow", "b_automatic")

apollo_beta = apollo_readBeta(apollo_beta, apollo_fixed, "MLM model", overwriteFixed=FALSE)

apollo_draws = list(
interDrawsType="halton",
interNDraws=500,
interUnifDraws=c(),
interNormDraws=c("eta","draws_price","draws_cost","draws_envfmedium","draws_envfhigh","draws_envfveryhigh","draws_manual"),

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

apollo_randCoeff=function(apollo_beta, apollo_inputs){
randcoeff = list()

randcoeff[["LV"]] = gamma_edu2*edu2 + gamma_edu3*edu3 + gamma_edu4*edu4 + gamma_residence2*residence2 + gamma_residence3*residence3 + gamma_residence4*residence4 + gamma_income2*income2 + gamma_income3*income3 + eta
randcoeff[["b_price"]] = -exp( mu_log_b_price + sigma_log_b_price * draws_price )
randcoeff[["b_cost"]] = -exp( mu_log_b_cost + sigma_log_b_cost * draws_cost )
randcoeff[["b_envfmedium"]] = ( mu_b_envfmedium + sigma_b_envfmedium * draws_envfmedium )
randcoeff[["b_envfhigh"]] = ( mu_b_envfhigh + sigma_b_envfhigh * draws_envfhigh )
randcoeff[["b_envfveryhigh"]] = ( mu_b_envfveryhigh + sigma_b_envfveryhigh * draws_envfveryhigh )
randcoeff[["b_manual"]] = ( mu_b_manual + sigma_b_manual * draws_manual )

return(randcoeff)
}


apollo_inputs = apollo_validateInputs()

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


### Create coefficients using interactions with socio-demographics
b_envfmedium_value = b_envfmedium + b_envfmedium_shift_lambda * LV
b_envfhigh_value = b_envfhigh + b_envfhigh_shift_lambda * LV
b_envfveryhigh_value = b_envfveryhigh + b_envfveryhigh_shift_lambda * LV

### Likelihood of indicators
ol_settings1 = list(outcomeOrdered = q1,
V = zeta_q1*LV,
tau = c(tau_q1_1, tau_q1_2, tau_q1_3, tau_q1_4),
rows = (helyzet==1),
componentName = "indic_q1")
ol_settings2 = list(outcomeOrdered = q2,
V = zeta_q2*LV,
tau = c(tau_q2_1, tau_q2_2, tau_q2_3, tau_q2_4),
rows = (helyzet==1),
componentName = "indic_q2")
ol_settings3 = list(outcomeOrdered = q3,
V = zeta_q3*LV,
tau = c(tau_q3_1, tau_q3_2, tau_q3_3, tau_q3_4),
rows = (helyzet==1),
componentName = "indic_q3")
ol_settings4 = list(outcomeOrdered = q4,
V = zeta_q4*LV,
tau = c(tau_q4_1, tau_q4_2, tau_q4_3, tau_q4_4),
rows = (helyzet==1),
componentName = "indic_q4")
ol_settings5 = list(outcomeOrdered = q5,
V = zeta_q5*LV,
tau = c(tau_q5_1, tau_q5_2, tau_q5_3, tau_q5_4),
rows = (helyzet==1),
componentName = "indic_q5")
ol_settings6 = list(outcomeOrdered = q6,
V = zeta_q6*LV,
tau = c(tau_q6_1, tau_q6_2, tau_q6_3, tau_q6_4),
rows = (helyzet==1),
componentName = "indic_q6")
ol_settings7 = list(outcomeOrdered = q7,
V = zeta_q7*LV,
tau = c(tau_q7_1, tau_q7_2, tau_q7_3, tau_q7_4),
rows = (helyzet==1),
componentName = "indic_q7")
ol_settings8 = list(outcomeOrdered = q8,
V = zeta_q8*LV,
tau = c(tau_q8_1, tau_q8_2, tau_q8_3, tau_q8_4),
rows = (helyzet==1),
componentName = "indic_q8")
ol_settings9 = list(outcomeOrdered = q9,
V = zeta_q9*LV,
tau = c(tau_q9_1, tau_q9_2, tau_q9_3, tau_q9_4),
rows = (helyzet==1),
componentName = "indic_q9")
ol_settings10 = list(outcomeOrdered = q10,
V = zeta_q10*LV,
tau = c(tau_q10_1, tau_q10_2, tau_q10_3, tau_q10_4),
rows = (helyzet==1),
componentName = "indic_q10")
ol_settings11 = list(outcomeOrdered = q11,
V = zeta_q11*LV,
tau = c(tau_q11_1, tau_q11_2, tau_q11_3, tau_q11_4),
rows = (helyzet==1),
componentName = "indic_q11")
ol_settings12 = list(outcomeOrdered = q12,
V = zeta_q12*LV,
tau = c(tau_q12_1, tau_q12_2, tau_q12_3, tau_q12_4),
rows = (helyzet==1),
componentName = "indic_q12")
P[["indic_q1"]] = apollo_ol(ol_settings1, functionality)
P[["indic_q2"]] = apollo_ol(ol_settings2, functionality)
P[["indic_q3"]] = apollo_ol(ol_settings3, functionality)
P[["indic_q4"]] = apollo_ol(ol_settings4, functionality)
P[["indic_q5"]] = apollo_ol(ol_settings5, functionality)
P[["indic_q6"]] = apollo_ol(ol_settings6, functionality)
P[["indic_q7"]] = apollo_ol(ol_settings7, functionality)
P[["indic_q8"]] = apollo_ol(ol_settings8, functionality)
P[["indic_q9"]] = apollo_ol(ol_settings9, functionality)
P[["indic_q10"]] = apollo_ol(ol_settings10, functionality)
P[["indic_q11"]] = apollo_ol(ol_settings11, functionality)
P[["indic_q12"]] = apollo_ol(ol_settings12, functionality)


V = list()
V[['alt1']] = asc_alt1 + b_envflow * ( env_alt1 == 1 ) + b_envfmedium_value * ( env_alt1 == 2 ) + b_envfhigh_value * ( env_alt1 == 3 ) + b_envfveryhigh_value * ( env_alt1 == 4 ) + b_manual * ( comf_alt1 == 0 ) + b_automatic * ( comf_alt1 == 1 ) + b_cost * cost_alt1 + b_price * price_alt1
V[['alt2']] = asc_alt2 + b_envflow * ( env_alt2 == 1 ) + b_envfmedium_value * ( env_alt2 == 2 ) + b_envfhigh_value * ( env_alt2 == 3 ) + b_envfveryhigh_value * ( env_alt2 == 4 ) + b_manual * ( comf_alt2 == 0 ) + b_automatic * ( comf_alt2 == 1 ) + b_cost * cost_alt2 + b_price * price_alt2
V[['alt3']] = asc_alt3 + b_envflow * ( env_alt3 == 1 ) + b_envfmedium_value * ( env_alt3 == 2 ) + b_envfhigh_value * ( env_alt3 == 3 ) + b_envfveryhigh_value * ( env_alt3 == 4 ) + b_manual * ( comf_alt3 == 0 ) + b_automatic * ( comf_alt3 == 1 ) + b_cost * cost_alt3 + b_price * price_alt3

mnl_settings = list(
alternatives = c(alt1=1, alt2=2, alt3=3),
avail = list(alt1=av_alt1, alt2=av_alt2, alt3=av_alt3),
choiceVar = choice,
V = V,
componentName= "choice"
)

P[["choice"]] = apollo_mnl(mnl_settings, functionality)

P = apollo_combineModels(P, apollo_inputs, functionality)

P = apollo_panelProd(P, apollo_inputs, functionality)

P = apollo_avgInterDraws(P, apollo_inputs, functionality)

P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}


model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)

modelOutput_settings = list()
modelOutput_settings$printPVal=TRUE
apollo_modelOutput(model,modelOutput_settings)

saveOutput_settings = list()
saveOutput_settings$printPVal=TRUE
apollo_saveOutput(model,modelOutput_settings)



Thank you very much for your answer!

Best regards,
Peter
stephanehess
Site Admin
Posts: 1035
Joined: 24 Apr 2020, 16:29

Re: ICLV model specification in unlabelled form

Post by stephanehess »

Peter

from a programming perspective, this is fine, but of course there are many other specifications you could try, e.g. which attributes to interact the LV with, etc.

But one thing that I would really suggest you to address is not to use Halton draws with your specification - you have seven random terms, and Halton draws will become highly collinear

Best wishes

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Peter_C
Posts: 17
Joined: 03 May 2020, 13:52

Re: ICLV model specification in unlabelled form

Post by Peter_C »

Dear Stephane,

Thank you very much for your answer!

Which type of draws you could suggest in this case? Which one is the least sensitive for collinearity?

Peter
stephanehess
Site Admin
Posts: 1035
Joined: 24 Apr 2020, 16:29

Re: ICLV model specification in unlabelled form

Post by stephanehess »

Peter

you can use MLHS or Sobol in Apollo, or load other types of draws from elsewhere

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Peter_C
Posts: 17
Joined: 03 May 2020, 13:52

Re: ICLV model specification in unlabelled form

Post by Peter_C »

Thank you Stephane!
lukasz
Posts: 3
Joined: 22 Feb 2024, 18:09

Re: ICLV model specification in unlabelled form

Post by lukasz »

Dear Stephane,

I have similar case as Peter had but in my experiment I have 2 alternatives that differ between task and repondents and the third alternative which is always Status quo. I have six attributes - subsidies attribute which is continuous and treated as non-random and five other attributes (plan, organic, rotation, contract, support) with 3 levels in each attribute. SQ alternative consists of the first level of each of the attribute. The first levels of attributes can also appear in different alternatives with the exception of "contract" attribute. Here, the first level means "no contract" and can only be used in SQ. In other alternatives 3 or 5 years contract must be chosen. I also have a latent variable (attitude) which is expressed by 4 indicators (1-7 Likert scale) and some further socio-demographic charasteristic that should influence the latent variable. My idea was that all attribute levels should be treated as random (except for subsidies) and also all these levels should be interacted with the LV. I also included ASC to know whether there is general tendency to move away from status quo but I am not sure if I coded properly (in variable asc_alt1 and asc_alt2 I put only "0" and in asc_alt3 I put "1" in the whole column).

I run a code below - it works but I received warning: Estimation failed. No covariance matrix to compute. I received parameters but no standard errors. Could you give my some hints where could be the problem?

# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #

### Clear memory
rm(list = ls())

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName = "Hybrid_with_OL_and_MMNL",
modelDescr = "Hybrid choice model on carbon agriculture in Serbia",
indivID = "ID",
nCores = 3,
outputDirectory = "output"
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #


database = read_excel("C:/Users/KMiGŻ-FSKNMH2/Desktop/SECOFARM/Eksperymenty/Eksperyment i ankieta - Serbia/SerbiaR.xlsx")



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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(mu_b_ASC=0, sig_b_ASC=0,
b_No_plan_No_analysis = 0, mu_b_Analysis_no_plan = 0,
sig_b_Analysis_no_plan = 0, mu_b_Analysis_and_plan = 0,
sig_b_Analysis_and_plan = 0,
b_No_organic = 0, mu_b_50plus_organic = 0,
sig_b_50plus_organic = 0, mu_b_full_organic = 0,
sig_b_full_organic =0, b_no_rotation = 0,
mu_b_usual_rotation = 0, sig_b_usual_rotation = 0,
mu_b_advanced_rotation = 0, sig_b_advanced_rotation = 0,
b_no_contract = 0,
mu_b_three_years = 0, sig_b_three_years = 0,
mu_b_five_years = 0, sig_b_five_years = 0,
b_no_support = 0, mu_b_administrative_support = 0,
sig_b_administrative_support = 0, mu_b_Full_expertise = 0,
sig_b_Full_expertise = 0, b_subsidies = 0,
gamma_LV__over_50 = 0, gamma_LV_Gender = 0,
gamma_LV_member_organization = 0, gamma_LV_agri_education = 0,
gamma_LV_farm_area = 0, gamma_LV_workload = 0,
gamma_LV_production_sold =0, b_ASC_shift_lambda=0,
b_Analysis_no_plan_shift_lambda=0,
b_Analysis_and_plan_shift_lambda=0, b_50plus_organic_shift_lambda=0,
b_full_organic_shift_lambda=0, b_usual_rotation_shift_lambda=0,
b_advanced_rotation_shift_lambda= 0, b_three_years_shift_lambda= 0,
b_five_years_shift_lambda= 0, b_administrative_support_shift_lambda= 0,
b_Full_expertise_shift_lambda= 0,
zeta_attitude_1 = 1, zeta_attitude_2 = 1,
zeta_attitude_3 = 1, zeta_attitude_4 = 1,
tau_attitude_1_1 =-3, tau_attitude_1_2 =-2,
tau_attitude_1_3 = -1, tau_attitude_1_4 = 1,
tau_attitude_1_5 =2, tau_attitude_1_6 = 3,
tau_attitude_2_1 =-3, tau_attitude_2_2 =-2,
tau_attitude_2_3 = -1, tau_attitude_2_4 = 1,
tau_attitude_2_5 = 2, tau_attitude_2_6 =3,
tau_attitude_3_1 =-3, tau_attitude_3_2 =-2,
tau_attitude_3_3 = -1, tau_attitude_3_4 = 1,
tau_attitude_3_5 = 2, tau_attitude_3_6 = 3,
tau_attitude_4_1 =-3, tau_attitude_4_2 =-2,
tau_attitude_4_3 = -1, tau_attitude_4_4 = 1,
tau_attitude_4_5 = 2, tau_attitude_4_6 = 3)

### 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("b_No_plan_No_analysis", "b_No_organic", "b_no_rotation", "b_no_contract", "b_no_support")


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

### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "mlhs",
interNDraws = 500,
interNormDraws = c("eta","draws_ASC", "draws_Analysis_no_plan", "draws_Analysis_and_plan", "draws_50plus_organic", "draws_full_organic", "draws_usual_rotation", "draws_advanced_rotation", "draws_three_years", "draws_five_years", "draws_administrative_support", "draws_Full_expertise")
)

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

randcoeff[["LV"]] = gamma_LV__over_50*over_50 + gamma_LV_Gender*Gender + gamma_LV_member_organization*member_organization + gamma_LV_agri_education*agri_education + gamma_LV_farm_area*farm_area + gamma_LV_workload*workload + gamma_LV_production_sold*production_sold + eta
randcoeff[["b_ASC"]] = mu_b_ASC + sig_b_ASC * draws_ASC
randcoeff[["b_Analysis_no_plan"]] = mu_b_Analysis_no_plan + sig_b_Analysis_no_plan * draws_Analysis_no_plan
randcoeff[["b_Analysis_and_plan"]] = mu_b_Analysis_and_plan + sig_b_Analysis_and_plan * draws_Analysis_and_plan
randcoeff[["b_50plus_organic"]] = mu_b_50plus_organic + sig_b_50plus_organic * draws_50plus_organic
randcoeff[["b_full_organic"]] = mu_b_full_organic + sig_b_full_organic * draws_full_organic
randcoeff[["b_usual_rotation"]] = mu_b_usual_rotation + sig_b_usual_rotation * draws_usual_rotation
randcoeff[["b_advanced_rotation"]] = mu_b_advanced_rotation + sig_b_advanced_rotation * draws_advanced_rotation
randcoeff[["b_three_years"]] = mu_b_three_years + sig_b_three_years * draws_three_years
randcoeff[["b_five_years"]] = mu_b_five_years + sig_b_five_years * draws_five_years
randcoeff[["b_administrative_support"]] = mu_b_administrative_support + sig_b_administrative_support * draws_administrative_support
randcoeff[["b_Full_expertise"]] = mu_b_Full_expertise + sig_b_Full_expertise * draws_Full_expertise
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()
### Create coefficients using interactions with socio-demographics
b_ASC_value = b_ASC + b_ASC_shift_lambda * LV
b_Analysis_no_plan_value = b_Analysis_no_plan + b_Analysis_no_plan_shift_lambda * LV
b_Analysis_and_plan_value = b_Analysis_and_plan + b_Analysis_and_plan_shift_lambda * LV
b_50plus_organic_value = b_50plus_organic + b_50plus_organic_shift_lambda * LV
b_full_organic_value = b_full_organic + b_full_organic_shift_lambda * LV
b_usual_rotation_value = b_usual_rotation + b_usual_rotation_shift_lambda * LV
b_advanced_rotation_value = b_advanced_rotation + b_advanced_rotation_shift_lambda * LV
b_three_years_value = b_three_years + b_three_years_shift_lambda * LV
b_five_years_value = b_five_years + b_five_years_shift_lambda * LV
b_administrative_support_value = b_administrative_support + b_administrative_support_shift_lambda * LV
b_Full_expertise_value = b_Full_expertise + b_Full_expertise_shift_lambda * LV


### Likelihood of indicators
ol_settings1 = list(outcomeOrdered = attitude_1,
V = zeta_attitude_1*LV,
tau = list(tau_attitude_1_1, tau_attitude_1_2, tau_attitude_1_3, tau_attitude_1_4, tau_attitude_1_5, tau_attitude_1_6),
rows = (task==1), componentName = "indic_attitude_1")
ol_settings2 = list(outcomeOrdered = attitude_2,
V = zeta_attitude_2*LV,
tau = list(tau_attitude_2_1, tau_attitude_2_2, tau_attitude_2_3, tau_attitude_2_4, tau_attitude_2_5, tau_attitude_2_6),
rows = (task==1), componentName = "indic_attitude_2")
ol_settings3 = list(outcomeOrdered = attitude_3,
V = zeta_attitude_3*LV,
tau = list(tau_attitude_3_1, tau_attitude_3_2, tau_attitude_3_3, tau_attitude_3_4, tau_attitude_3_5, tau_attitude_3_6),
rows = (task==1), componentName = "indic_attitude_3")
ol_settings4 = list(outcomeOrdered = attitude_4,
V = zeta_attitude_4*LV,
tau = list(tau_attitude_4_1, tau_attitude_4_2, tau_attitude_4_3, tau_attitude_4_4, tau_attitude_4_5, tau_attitude_4_6),
rows = (task==1), componentName = "indic_attitude_4")
P[["indic_attitude_1"]] = apollo_ol(ol_settings1, functionality)
P[["indic_attitude_2"]] = apollo_ol(ol_settings2, functionality)
P[["indic_attitude_3"]] = apollo_ol(ol_settings3, functionality)
P[["indic_attitude_4"]] = apollo_ol(ol_settings4, functionality)

### Likelihood of choices
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['alt1']] = ( b_ASC_value*(asc_alt1==0) + b_ASC_value*(asc_alt1==1) + b_No_plan_No_analysis*(plan_alt1==1) + b_Analysis_no_plan_value*(plan_alt1==2) + b_Analysis_and_plan_value*(plan_alt1==3)
+ b_No_organic*(organic_alt1==1) + b_50plus_organic_value*(organic_alt1==2) + b_full_organic_value*(organic_alt1==3) + b_no_rotation*(rotation_alt1==1) + b_usual_rotation_value*(rotation_alt1==2) + b_advanced_rotation_value*(rotation_alt1==3)
+ b_no_contract*(contract_alt1==1) + b_three_years_value*(contract_alt1==2) + b_five_years_value*(contract_alt1==3)
+ b_no_support*(support_alt1==1) + b_administrative_support_value*(support_alt1==2) + b_Full_expertise_value*(support_alt1==3)
+ b_subsidies*subsidies_alt1)
V[['alt2']] = ( b_ASC_value*(asc_alt2==0) + b_ASC_value*(asc_alt2==1) + b_No_plan_No_analysis*(plan_alt2==1) + b_Analysis_no_plan_value*(plan_alt2==2) + b_Analysis_and_plan_value*(plan_alt2==3)
+ b_No_organic*(organic_alt2==1) + b_50plus_organic_value*(organic_alt2==2) + b_full_organic_value*(organic_alt2==3) + b_no_rotation*(rotation_alt2==1) + b_usual_rotation_value*(rotation_alt2==2) + b_advanced_rotation_value*(rotation_alt2==3)
+ b_no_contract*(contract_alt2==1) + b_three_years_value*(contract_alt2==2) + b_five_years_value*(contract_alt2==3)
+ b_no_support*(support_alt2==2) + b_administrative_support_value*(support_alt2==2) + b_Full_expertise_value*(support_alt2==3)
+ b_subsidies*subsidies_alt2)
V[['sq']] = ( b_ASC_value*(asc_sq==0) + b_ASC_value*(asc_sq==1) + b_No_plan_No_analysis*(plan_sq==1) + b_Analysis_no_plan_value*(plan_sq==2) + b_Analysis_and_plan_value*(plan_sq==3)
+ b_No_organic*(organic_sq==1) + b_50plus_organic_value*(organic_sq==2) + b_full_organic_value*(organic_sq==3) + b_no_rotation*(rotation_sq==1) + b_usual_rotation_value*(rotation_sq==2) + b_advanced_rotation_value*(rotation_sq==3)
+ b_no_contract*(contract_sq==1) + b_three_years_value*(contract_sq==2) + b_five_years_value*(contract_sq==3)
+ b_no_support*(support_sq==1) + b_administrative_support_value*(support_sq==2) + b_Full_expertise_value*(support_sq==3)
+ b_subsidies*subsidies_sq)


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

# ################################################################# #
#### CHECK FOR COMPUTATIONAL REQUIREMENTS ####
# ################################################################# #

speedTest_settings=list(
nDrawsTry = c(250, 500, 1000),
nCoresTry = 1:3,
nRep = 10
)

apollo_speedTest(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs, speedTest_settings)

# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #

### Estimate model
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)
stephanehess
Site Admin
Posts: 1035
Joined: 24 Apr 2020, 16:29

Re: ICLV model specification in unlabelled form

Post by stephanehess »

Hi

In order for us to help you, you need to show us the output, i.e. the parameter estimates.

Can you please also confirm that a MNL and mixed logit model worked on the data before moving to hybrid choice?

Thanks
--------------------------------
Stephane Hess
www.stephanehess.me.uk
lukasz
Posts: 3
Joined: 22 Feb 2024, 18:09

Re: ICLV model specification in unlabelled form

Post by lukasz »

Dear Stephane,

Below I paste the output. When it comes to covariance matrix, I paste only first three lines because there is always the same - NA. I will try MNL model and mixed logit model and will let you know in the next post. I started my estimation in R from hybrid model. Before, I run mixed logit model without any latent variables in STATA and it worked.

Thanks
Łukasz
Model run by User using Apollo 0.3.1 on R 4.3.1 for Windows.
Please acknowledge the use of Apollo by citing Hess & Palma (2019)
DOI 10.1016/j.jocm.2019.100170
www.ApolloChoiceModelling.com

Model name : Hybrid_with_OL_and_MMNL
Model description : Hybrid choice model on carbon agriculture in Serbia
Model run at : 2024-02-23 10:40:02.774681
Estimation method : bgw
Model diagnosis : Singular convergence
Number of individuals : 119
Number of rows in database : 952
Number of modelled outcomes : 1428
indic_attitude_1 : 119
indic_attitude_2 : 119
indic_attitude_3 : 119
indic_attitude_4 : 119
choice : 952

Number of cores used : 3
Number of inter-individual draws : 500 (mlhs)

LL(start) : -1611.94
LL (whole model) at equal shares, LL(0) : -1972.13
LL (whole model) at observed shares, LL(C) : -1890.28
LL(final, whole model) : -1583.09
Rho-squared vs equal shares : 0.1973
Adj.Rho-squared vs equal shares : 0.1623
Rho-squared vs observed shares : 0.1625
Adj.Rho-squared vs observed shares : 0.1398
AIC : 3304.19
BIC : 3495.95

LL(0,indic_attitude_1) : -231.56
LL(final,indic_attitude_1) : -222.43
LL(0,indic_attitude_2) : -231.56
LL(final,indic_attitude_2) : -214.65
LL(0,indic_attitude_3) : -231.56
LL(final,indic_attitude_3) : -226.15
LL(0,indic_attitude_4) : -231.56
LL(final,indic_attitude_4) : -181.71
LL(0,choice) : -1045.88
LL(final,choice) : -849.82

Estimated parameters : 69
Time taken (hh:mm:ss) : 00:37:48.78
pre-estimation : 00:21:16.8
estimation : 00:15:57.22
post-estimation : 00:00:34.76
Iterations : 36 (Singular convergence)

Unconstrained optimisation.

Estimates:
Estimate s.e. t.rat.(0)
mu_b_ASC 0.011334 NA NA
sig_b_ASC 2.605034 NA NA
b_No_plan_No_analysis 0.000000 NA NA
mu_b_Analysis_no_plan -1.551881 NA NA
sig_b_Analysis_no_plan -1.299444 NA NA
mu_b_Analysis_and_plan -0.420236 NA NA
sig_b_Analysis_and_plan 0.498176 NA NA
b_No_organic 0.000000 NA NA
mu_b_50plus_organic -2.970258 NA NA
sig_b_50plus_organic -1.992295 NA NA
mu_b_full_organic -3.338358 NA NA
sig_b_full_organic 1.868584 NA NA
b_no_rotation 0.000000 NA NA
mu_b_usual_rotation 0.931906 NA NA
sig_b_usual_rotation -0.619987 NA NA
mu_b_advanced_rotation 0.539023 NA NA
sig_b_advanced_rotation -1.090124 NA NA
b_no_contract 0.000000 NA NA
mu_b_three_years 1.222347 NA NA
sig_b_three_years -1.053549 NA NA
mu_b_five_years 0.628325 NA NA
sig_b_five_years -0.378051 NA NA
b_no_support 0.000000 NA NA
mu_b_administrative_support 2.022469 NA NA
sig_b_administrative_support -1.348492 NA NA
mu_b_Full_expertise -0.258105 NA NA
sig_b_Full_expertise -0.366002 NA NA
b_subsidies 0.006186 NA NA
gamma_LV__over_50 -0.269403 NA NA
gamma_LV_Gender -0.505667 NA NA
gamma_LV_member_organization 0.176540 NA NA
gamma_LV_agri_education 0.574911 NA NA
gamma_LV_farm_area -0.035290 NA NA
gamma_LV_workload -0.002072 NA NA
gamma_LV_production_sold -0.007546 NA NA
b_ASC_shift_lambda -7.803381 NA NA
b_Analysis_no_plan_shift_lambda -0.490940 NA NA
b_Analysis_and_plan_shift_lambda -0.323175 NA NA
b_50plus_organic_shift_lambda -0.547763 NA NA
b_full_organic_shift_lambda -0.742737 NA NA
b_usual_rotation_shift_lambda 0.107367 NA NA
b_advanced_rotation_shift_lambda -0.048087 NA NA
b_three_years_shift_lambda 0.504203 NA NA
b_five_years_shift_lambda 0.275113 NA NA
b_administrative_support_shift_lambda 0.514152 NA NA
b_Full_expertise_shift_lambda 0.016273 NA NA
zeta_attitude_1 2.883545 NA NA
zeta_attitude_2 6.070835 NA NA
zeta_attitude_3 1.802019 NA NA
zeta_attitude_4 0.528936 NA NA
tau_attitude_1_1 -11.203920 NA NA
tau_attitude_1_2 -8.458016 NA NA
tau_attitude_1_3 -6.640239 NA NA
tau_attitude_1_4 -5.220572 NA NA
tau_attitude_1_5 -3.352416 NA NA
tau_attitude_1_6 -1.187126 NA NA
tau_attitude_2_1 -21.890068 NA NA
tau_attitude_2_2 -16.328770 NA NA
tau_attitude_2_3 -11.856203 NA NA
tau_attitude_2_4 -9.666347 NA NA
tau_attitude_2_5 -7.106689 NA NA
tau_attitude_2_6 -4.773832 NA NA
tau_attitude_3_1 -6.906125 NA NA
tau_attitude_3_2 -4.953765 NA NA
tau_attitude_3_3 -3.757724 NA NA
tau_attitude_3_4 -2.671486 NA NA
tau_attitude_3_5 -1.657327 NA NA
tau_attitude_3_6 -0.442824 NA NA
tau_attitude_4_1 -3.941843 NA NA
tau_attitude_4_2 -3.771706 NA NA
tau_attitude_4_3 -2.744935 NA NA
tau_attitude_4_4 -2.157125 NA NA
tau_attitude_4_5 -1.428247 NA NA
tau_attitude_4_6 -0.759398 NA NA
Rob.s.e. Rob.t.rat.(0)
mu_b_ASC NA NA
sig_b_ASC NA NA
b_No_plan_No_analysis NA NA
mu_b_Analysis_no_plan NA NA
sig_b_Analysis_no_plan NA NA
mu_b_Analysis_and_plan NA NA
sig_b_Analysis_and_plan NA NA
b_No_organic NA NA
mu_b_50plus_organic NA NA
sig_b_50plus_organic NA NA
mu_b_full_organic NA NA
sig_b_full_organic NA NA
b_no_rotation NA NA
mu_b_usual_rotation NA NA
sig_b_usual_rotation NA NA
mu_b_advanced_rotation NA NA
sig_b_advanced_rotation NA NA
b_no_contract NA NA
mu_b_three_years NA NA
sig_b_three_years NA NA
mu_b_five_years NA NA
sig_b_five_years NA NA
b_no_support NA NA
mu_b_administrative_support NA NA
sig_b_administrative_support NA NA
mu_b_Full_expertise NA NA
sig_b_Full_expertise NA NA
b_subsidies NA NA
gamma_LV__over_50 NA NA
gamma_LV_Gender NA NA
gamma_LV_member_organization NA NA
gamma_LV_agri_education NA NA
gamma_LV_farm_area NA NA
gamma_LV_workload NA NA
gamma_LV_production_sold NA NA
b_ASC_shift_lambda NA NA
b_Analysis_no_plan_shift_lambda NA NA
b_Analysis_and_plan_shift_lambda NA NA
b_50plus_organic_shift_lambda NA NA
b_full_organic_shift_lambda NA NA
b_usual_rotation_shift_lambda NA NA
b_advanced_rotation_shift_lambda NA NA
b_three_years_shift_lambda NA NA
b_five_years_shift_lambda NA NA
b_administrative_support_shift_lambda NA NA
b_Full_expertise_shift_lambda NA NA
zeta_attitude_1 NA NA
zeta_attitude_2 NA NA
zeta_attitude_3 NA NA
zeta_attitude_4 NA NA
tau_attitude_1_1 NA NA
tau_attitude_1_2 NA NA
tau_attitude_1_3 NA NA
tau_attitude_1_4 NA NA
tau_attitude_1_5 NA NA
tau_attitude_1_6 NA NA
tau_attitude_2_1 NA NA
tau_attitude_2_2 NA NA
tau_attitude_2_3 NA NA
tau_attitude_2_4 NA NA
tau_attitude_2_5 NA NA
tau_attitude_2_6 NA NA
tau_attitude_3_1 NA NA
tau_attitude_3_2 NA NA
tau_attitude_3_3 NA NA
tau_attitude_3_4 NA NA
tau_attitude_3_5 NA NA
tau_attitude_3_6 NA NA
tau_attitude_4_1 NA NA
tau_attitude_4_2 NA NA
tau_attitude_4_3 NA NA
tau_attitude_4_4 NA NA
tau_attitude_4_5 NA NA
tau_attitude_4_6 NA NA


Overview of choices for model component indic_attitude_1:
1 2 3 4 5 6 7
Times chosen 5.0 12.00 17.00 16.00 23.00 26.00 20.00
Percentage chosen overall 4.2 10.08 14.29 13.45 19.33 21.85 16.81


Overview of choices for model component indic_attitude_2:
1 2 3 4 5 6 7
Times chosen 5.0 15.00 23.00 13.00 17.00 17.00 29.00
Percentage chosen overall 4.2 12.61 19.33 10.92 14.29 14.29 24.37


Overview of choices for model component indic_attitude_3:
1 2 3 4 5 6 7
Times chosen 8.00 17.00 19.00 20.00 18.00 17.00 20.00
Percentage chosen overall 6.72 14.29 15.97 16.81 15.13 14.29 16.81


Overview of choices for model component indic_attitude_4:
1 2 3 4 5 6 7
Times chosen 6.00 1.00 10.0 10.0 17.00 18.00 57.0
Percentage chosen overall 5.04 0.84 8.4 8.4 14.29 15.13 47.9


Overview of choices for MNL model component choice:
alt1 alt2 sq
Times available 952.00 952.00 952.00
Times chosen 349.00 339.00 264.00
Percentage chosen overall 36.66 35.61 27.73
Percentage chosen when available 36.66 35.61 27.73



Classical covariance matrix:
mu_b_ASC
mu_b_ASC NA
sig_b_ASC NA
mu_b_Analysis_no_plan NA
lukasz
Posts: 3
Joined: 22 Feb 2024, 18:09

Re: ICLV model specification in unlabelled form

Post by lukasz »

Dear Stephane,

I have also tried to estimate mixed logit model and have the same warning - Estimation failed. No covariance matrix to compute. Below there is a code:
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #

### Clear memory
rm(list = ls())

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName = "Mixed logit",
modelDescr = "Mixed Logit choice model on carbon agriculture in Serbia",
indivID = "ID",
nCores = 3,
outputDirectory = "output"
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #

database = read_excel("C:/Users/KMiGŻ-FSKNMH2/Desktop/SECOFARM/Eksperymenty/Eksperyment i ankieta - Serbia/Serbia_R.xlsx")


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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(mu_b_ASC=0, sig_b_ASC=0,
b_No_plan_No_analysis = 0, mu_b_Analysis_no_plan = 0,
sig_b_Analysis_no_plan = 0, mu_b_Analysis_and_plan = 0,
sig_b_Analysis_and_plan = 0,
b_No_organic = 0, mu_b_50plus_organic = 0,
sig_b_50plus_organic = 0, mu_b_full_organic = 0,
sig_b_full_organic =0, b_no_rotation = 0,
mu_b_usual_rotation = 0, sig_b_usual_rotation = 0,
mu_b_advanced_rotation = 0, sig_b_advanced_rotation = 0,
b_no_contract = 0,
mu_b_three_years = 0, sig_b_three_years = 0,
mu_b_five_years = 0, sig_b_five_years = 0,
b_no_support = 0, mu_b_administrative_support = 0,
sig_b_administrative_support = 0, mu_b_Full_expertise = 0,
sig_b_Full_expertise = 0, b_subsidies = 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("b_No_plan_No_analysis", "b_No_organic", "b_no_rotation", "b_no_contract", "b_no_support")


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

### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "mlhs",
interNDraws = 500,
interNormDraws = c("draws_ASC", "draws_Analysis_no_plan", "draws_Analysis_and_plan", "draws_50plus_organic", "draws_full_organic", "draws_usual_rotation", "draws_advanced_rotation", "draws_three_years", "draws_five_years", "draws_administrative_support", "draws_Full_expertise")
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["b_ASC"]] = mu_b_ASC + sig_b_ASC * draws_ASC
randcoeff[["b_Analysis_no_plan"]] = mu_b_Analysis_no_plan + sig_b_Analysis_no_plan * draws_Analysis_no_plan
randcoeff[["b_Analysis_and_plan"]] = mu_b_Analysis_and_plan + sig_b_Analysis_and_plan * draws_Analysis_and_plan
randcoeff[["b_50plus_organic"]] = mu_b_50plus_organic + sig_b_50plus_organic * draws_50plus_organic
randcoeff[["b_full_organic"]] = mu_b_full_organic + sig_b_full_organic * draws_full_organic
randcoeff[["b_usual_rotation"]] = mu_b_usual_rotation + sig_b_usual_rotation * draws_usual_rotation
randcoeff[["b_advanced_rotation"]] = mu_b_advanced_rotation + sig_b_advanced_rotation * draws_advanced_rotation
randcoeff[["b_three_years"]] = mu_b_three_years + sig_b_three_years * draws_three_years
randcoeff[["b_five_years"]] = mu_b_five_years + sig_b_five_years * draws_five_years
randcoeff[["b_administrative_support"]] = mu_b_administrative_support + sig_b_administrative_support * draws_administrative_support
randcoeff[["b_Full_expertise"]] = mu_b_Full_expertise + sig_b_Full_expertise * draws_Full_expertise
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[['alt1']] = ( b_ASC*(asc_alt1==0) + b_ASC*(asc_alt1==1) + b_No_plan_No_analysis*(plan_alt1==1) + b_Analysis_no_plan*(plan_alt1==2) + b_Analysis_and_plan*(plan_alt1==3)
+ b_No_organic*(organic_alt1==1) + b_50plus_organic*(organic_alt1==2) + b_full_organic*(organic_alt1==3) + b_no_rotation*(rotation_alt1==1) + b_usual_rotation*(rotation_alt1==2) + b_advanced_rotation*(rotation_alt1==3)
+ b_no_contract*(contract_alt1==1) + b_three_years*(contract_alt1==2) + b_five_years*(contract_alt1==3)
+ b_no_support*(support_alt1==1) + b_administrative_support*(support_alt1==2) + b_Full_expertise*(support_alt1==3)
+ b_subsidies*subsidies_alt1)
V[['alt2']] = ( b_ASC*(asc_alt2==0) + b_ASC*(asc_alt2==1) + b_No_plan_No_analysis*(plan_alt2==1) + b_Analysis_no_plan*(plan_alt2==2) + b_Analysis_and_plan*(plan_alt2==3)
+ b_No_organic*(organic_alt2==1) + b_50plus_organic*(organic_alt2==2) + b_full_organic*(organic_alt2==3) + b_no_rotation*(rotation_alt2==1) + b_usual_rotation*(rotation_alt2==2) + b_advanced_rotation*(rotation_alt2==3)
+ b_no_contract*(contract_alt2==1) + b_three_years*(contract_alt2==2) + b_five_years*(contract_alt2==3)
+ b_no_support*(support_alt2==2) + b_administrative_support*(support_alt2==2) + b_Full_expertise*(support_alt2==3)
+ b_subsidies*subsidies_alt2)
V[['sq']] = ( b_ASC*(asc_sq==0) + b_ASC*(asc_sq==1) + b_No_plan_No_analysis*(plan_sq==1) + b_Analysis_no_plan*(plan_sq==2) + b_Analysis_and_plan*(plan_sq==3)
+ b_No_organic*(organic_sq==1) + b_50plus_organic*(organic_sq==2) + b_full_organic*(organic_sq==3) + b_no_rotation*(rotation_sq==1) + b_usual_rotation*(rotation_sq==2) + b_advanced_rotation*(rotation_sq==3)
+ b_no_contract*(contract_sq==1) + b_three_years*(contract_sq==2) + b_five_years*(contract_sq==3)
+ b_no_support*(support_sq==1) + b_administrative_support*(support_sq==2) + b_Full_expertise*(support_sq==3)
+ b_subsidies*subsidies_sq)


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

### Compute probabilities for MNL model component
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)
}

# ################################################################# #
#### CHECK FOR COMPUTATIONAL REQUIREMENTS ####
# ################################################################# #

speedTest_settings=list(
nDrawsTry = c(250, 500, 1000),
nCoresTry = 1:3,
nRep = 10
)

apollo_speedTest(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs, speedTest_settings)

# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #

### Estimate model
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)
lukasz
Posts: 3
Joined: 22 Feb 2024, 18:09

Re: ICLV model specification in unlabelled form

Post by lukasz »

Dear Stephane,

I run a hybrid model once again but now without ASC and it works. However, in otput file I dont have "successful convergence" but rather "relative function convergence". It seems that there is a problem with ASC. Maybe I made a mistake in coding ASC? Below I provide a piece of output and then code below

Best regards,
Łukasz

Model description : Hybrid choice model on carbon agriculture in Serbia
Model run at : 2024-02-26 15:20:33.317189
Estimation method : bgw
Model diagnosis : Relative function convergence
Optimisation diagnosis : Maximum found
hessian properties : Negative definite
maximum eigenvalue : -0.005579
reciprocal of condition number : 2.35595e-09
Number of individuals : 119
Number of rows in database : 952
Number of modelled outcomes : 1428
indic_attitude_1 : 119
indic_attitude_2 : 119
indic_attitude_3 : 119
indic_attitude_4 : 119
choice : 952

Number of cores used : 3
Number of inter-individual draws : 500 (mlhs)

LL(start) : -1817.34
LL (whole model) at equal shares, LL(0) : -1972.13
LL (whole model) at observed shares, LL(C) : -1890.28
LL(final, whole model) : -1589.86
Rho-squared vs equal shares : 0.1938
Adj.Rho-squared vs equal shares : 0.1604
Rho-squared vs observed shares : 0.1589
Adj.Rho-squared vs observed shares : 0.1378
AIC : 3311.72
BIC : 3495.15

# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #

### Clear memory
rm(list = ls())

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName = "Hybrid_with_OL_and_MMNL",
modelDescr = "Hybrid choice model on carbon agriculture in Serbia",
indivID = "ID",
nCores = 3,
outputDirectory = "output"
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #


database = read_excel("C:/Users/KMiGŻ-FSKNMH2/Desktop/SECOFARM/Eksperymenty/Eksperyment i ankieta - Serbia/Serbia_R.xlsx")



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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(
b_No_plan_No_analysis = 0, mu_b_Analysis_no_plan = 0,
sig_b_Analysis_no_plan = 0, mu_b_Analysis_and_plan = 0,
sig_b_Analysis_and_plan = 0,
b_No_organic = 0, mu_b_50plus_organic = 0,
sig_b_50plus_organic = 0, mu_b_full_organic = 0,
sig_b_full_organic =0, b_no_rotation = 0,
mu_b_usual_rotation = 0, sig_b_usual_rotation = 0,
mu_b_advanced_rotation = 0, sig_b_advanced_rotation = 0,
b_no_contract = 0,
mu_b_three_years = 0, sig_b_three_years = 0,
mu_b_five_years = 0, sig_b_five_years = 0,
b_no_support = 0, mu_b_administrative_support = 0,
sig_b_administrative_support = 0, mu_b_Full_expertise = 0,
sig_b_Full_expertise = 0, b_subsidies = 0,
gamma_LV__over_50 = 0, gamma_LV_Gender = 0,
gamma_LV_member_organization = 0, gamma_LV_agri_education = 0,
gamma_LV_farm_area = 0, gamma_LV_workload = 0,
gamma_LV_production_sold =0,
b_Analysis_no_plan_shift_lambda=0,
b_Analysis_and_plan_shift_lambda=0, b_50plus_organic_shift_lambda=0,
b_full_organic_shift_lambda=0, b_usual_rotation_shift_lambda=0,
b_advanced_rotation_shift_lambda= 0, b_three_years_shift_lambda= 0,
b_five_years_shift_lambda= 0, b_administrative_support_shift_lambda= 0,
b_Full_expertise_shift_lambda= 0,
zeta_attitude_1 = 1, zeta_attitude_2 = 1,
zeta_attitude_3 = 1, zeta_attitude_4 = 1,
tau_attitude_1_1 =-3, tau_attitude_1_2 =-2,
tau_attitude_1_3 = -1, tau_attitude_1_4 = 1,
tau_attitude_1_5 =2, tau_attitude_1_6 = 3,
tau_attitude_2_1 =-3, tau_attitude_2_2 =-2,
tau_attitude_2_3 = -1, tau_attitude_2_4 = 1,
tau_attitude_2_5 = 2, tau_attitude_2_6 =3,
tau_attitude_3_1 =-3, tau_attitude_3_2 =-2,
tau_attitude_3_3 = -1, tau_attitude_3_4 = 1,
tau_attitude_3_5 = 2, tau_attitude_3_6 = 3,
tau_attitude_4_1 =-3, tau_attitude_4_2 =-2,
tau_attitude_4_3 = -1, tau_attitude_4_4 = 1,
tau_attitude_4_5 = 2, tau_attitude_4_6 = 3)

### 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("b_No_plan_No_analysis", "b_No_organic", "b_no_rotation", "b_no_contract", "b_no_support")
apollo_beta = apollo_readBeta(apollo_beta, apollo_fixed, "Hybrid_with_OL_and_MMNL")

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

### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "mlhs",
interNDraws = 500,
interNormDraws = c("eta", "draws_Analysis_no_plan", "draws_Analysis_and_plan", "draws_50plus_organic", "draws_full_organic", "draws_usual_rotation", "draws_advanced_rotation", "draws_three_years", "draws_five_years", "draws_administrative_support", "draws_Full_expertise")
)

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

randcoeff[["LV"]] = gamma_LV__over_50*over_50 + gamma_LV_Gender*Gender + gamma_LV_member_organization*member_organization + gamma_LV_agri_education*agri_education + gamma_LV_farm_area*farm_area + gamma_LV_workload*workload + gamma_LV_production_sold*production_sold + eta
randcoeff[["b_Analysis_no_plan"]] = mu_b_Analysis_no_plan + sig_b_Analysis_no_plan * draws_Analysis_no_plan
randcoeff[["b_Analysis_and_plan"]] = mu_b_Analysis_and_plan + sig_b_Analysis_and_plan * draws_Analysis_and_plan
randcoeff[["b_50plus_organic"]] = mu_b_50plus_organic + sig_b_50plus_organic * draws_50plus_organic
randcoeff[["b_full_organic"]] = mu_b_full_organic + sig_b_full_organic * draws_full_organic
randcoeff[["b_usual_rotation"]] = mu_b_usual_rotation + sig_b_usual_rotation * draws_usual_rotation
randcoeff[["b_advanced_rotation"]] = mu_b_advanced_rotation + sig_b_advanced_rotation * draws_advanced_rotation
randcoeff[["b_three_years"]] = mu_b_three_years + sig_b_three_years * draws_three_years
randcoeff[["b_five_years"]] = mu_b_five_years + sig_b_five_years * draws_five_years
randcoeff[["b_administrative_support"]] = mu_b_administrative_support + sig_b_administrative_support * draws_administrative_support
randcoeff[["b_Full_expertise"]] = mu_b_Full_expertise + sig_b_Full_expertise * draws_Full_expertise
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()
### Create coefficients using interactions with socio-demographics
b_Analysis_no_plan_value = b_Analysis_no_plan + b_Analysis_no_plan_shift_lambda * LV
b_Analysis_and_plan_value = b_Analysis_and_plan + b_Analysis_and_plan_shift_lambda * LV
b_50plus_organic_value = b_50plus_organic + b_50plus_organic_shift_lambda * LV
b_full_organic_value = b_full_organic + b_full_organic_shift_lambda * LV
b_usual_rotation_value = b_usual_rotation + b_usual_rotation_shift_lambda * LV
b_advanced_rotation_value = b_advanced_rotation + b_advanced_rotation_shift_lambda * LV
b_three_years_value = b_three_years + b_three_years_shift_lambda * LV
b_five_years_value = b_five_years + b_five_years_shift_lambda * LV
b_administrative_support_value = b_administrative_support + b_administrative_support_shift_lambda * LV
b_Full_expertise_value = b_Full_expertise + b_Full_expertise_shift_lambda * LV


### Likelihood of indicators
ol_settings1 = list(outcomeOrdered = attitude_1,
V = zeta_attitude_1*LV,
tau = list(tau_attitude_1_1, tau_attitude_1_2, tau_attitude_1_3, tau_attitude_1_4, tau_attitude_1_5, tau_attitude_1_6),
rows = (task==1), componentName = "indic_attitude_1")
ol_settings2 = list(outcomeOrdered = attitude_2,
V = zeta_attitude_2*LV,
tau = list(tau_attitude_2_1, tau_attitude_2_2, tau_attitude_2_3, tau_attitude_2_4, tau_attitude_2_5, tau_attitude_2_6),
rows = (task==1), componentName = "indic_attitude_2")
ol_settings3 = list(outcomeOrdered = attitude_3,
V = zeta_attitude_3*LV,
tau = list(tau_attitude_3_1, tau_attitude_3_2, tau_attitude_3_3, tau_attitude_3_4, tau_attitude_3_5, tau_attitude_3_6),
rows = (task==1), componentName = "indic_attitude_3")
ol_settings4 = list(outcomeOrdered = attitude_4,
V = zeta_attitude_4*LV,
tau = list(tau_attitude_4_1, tau_attitude_4_2, tau_attitude_4_3, tau_attitude_4_4, tau_attitude_4_5, tau_attitude_4_6),
rows = (task==1), componentName = "indic_attitude_4")
P[["indic_attitude_1"]] = apollo_ol(ol_settings1, functionality)
P[["indic_attitude_2"]] = apollo_ol(ol_settings2, functionality)
P[["indic_attitude_3"]] = apollo_ol(ol_settings3, functionality)
P[["indic_attitude_4"]] = apollo_ol(ol_settings4, functionality)

### Likelihood of choices
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['alt1']] = ( b_No_plan_No_analysis*(plan_alt1==1) + b_Analysis_no_plan_value*(plan_alt1==2) + b_Analysis_and_plan_value*(plan_alt1==3)
+ b_No_organic*(organic_alt1==1) + b_50plus_organic_value*(organic_alt1==2) + b_full_organic_value*(organic_alt1==3) + b_no_rotation*(rotation_alt1==1) + b_usual_rotation_value*(rotation_alt1==2) + b_advanced_rotation_value*(rotation_alt1==3)
+ b_no_contract*(contract_alt1==1) + b_three_years_value*(contract_alt1==2) + b_five_years_value*(contract_alt1==3)
+ b_no_support*(support_alt1==1) + b_administrative_support_value*(support_alt1==2) + b_Full_expertise_value*(support_alt1==3)
+ b_subsidies*subsidies_alt1)
V[['alt2']] = ( b_No_plan_No_analysis*(plan_alt2==1) + b_Analysis_no_plan_value*(plan_alt2==2) + b_Analysis_and_plan_value*(plan_alt2==3)
+ b_No_organic*(organic_alt2==1) + b_50plus_organic_value*(organic_alt2==2) + b_full_organic_value*(organic_alt2==3) + b_no_rotation*(rotation_alt2==1) + b_usual_rotation_value*(rotation_alt2==2) + b_advanced_rotation_value*(rotation_alt2==3)
+ b_no_contract*(contract_alt2==1) + b_three_years_value*(contract_alt2==2) + b_five_years_value*(contract_alt2==3)
+ b_no_support*(support_alt2==2) + b_administrative_support_value*(support_alt2==2) + b_Full_expertise_value*(support_alt2==3)
+ b_subsidies*subsidies_alt2)
V[['sq']] = ( b_No_plan_No_analysis*(plan_sq==1) + b_Analysis_no_plan_value*(plan_sq==2) + b_Analysis_and_plan_value*(plan_sq==3)
+ b_No_organic*(organic_sq==1) + b_50plus_organic_value*(organic_sq==2) + b_full_organic_value*(organic_sq==3) + b_no_rotation*(rotation_sq==1) + b_usual_rotation_value*(rotation_sq==2) + b_advanced_rotation_value*(rotation_sq==3)
+ b_no_contract*(contract_sq==1) + b_three_years_value*(contract_sq==2) + b_five_years_value*(contract_sq==3)
+ b_no_support*(support_sq==1) + b_administrative_support_value*(support_sq==2) + b_Full_expertise_value*(support_sq==3)
+ b_subsidies*subsidies_sq)


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

# ################################################################# #
#### CHECK FOR COMPUTATIONAL REQUIREMENTS ####
# ################################################################# #

speedTest_settings=list(
nDrawsTry = c(250, 500, 1000),
nCoresTry = 1:3,
nRep = 10
)

apollo_speedTest(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs, speedTest_settings)

# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #

### Estimate model
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)
Last edited by lukasz on 26 Feb 2024, 16:21, edited 1 time in total.
Post Reply