Hi-I am getting a similar error with the code below, however I do not see where the mistake is (even after consulting this thread).
Thanks a lot for your help!
Code: Select all
# ################################################################# #
#### 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 = "Ireland_HLCM",
modelDescr = "Firt HlCM test Ireland",
indivID = "ID",
mixing = TRUE,
nCores = 2
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
database = read.csv("ireland_VBN.csv",header=TRUE)
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(
bt1_a = 1.927,
bt1_b = 0.369,
bt1_c = -0.677,
bt2_a = 2.02,
bt2_b = 0.687,
bt2_c = 0.256,
bt3_a = 1.99,
bt3_b = 1.67,
bt3_c = 0.814,
bsave_a = 0.783,
bsave_b = 0.750,
bsave_c = 3.3,
bind_a =0.21,
bind_b =-0.01,
bind_c =0.264,
bpay_a =-0.02,
bpay_b =-0.140,
bpay_c =-0.206,
bcost_a =-0.201,
bcost_b =-0.363,
bcost_c =-0.123,
bemi_a =0.709,
bemi_b =-0.06,
bemi_c =2.33,
delta_a = 0,
delta_b = -1.29,
delta_c = -0.62,
pi_LV1_a=0,
pi_LV1_b=-0.3,
pi_LV1_c=-0.2,
zeta_Cons01=-4,
zeta_Cons02=-3,
zeta_Cons03=-2,
zeta_Cons04=-1,
zeta_Cons05=-1,
zeta_Cons06=-1,
zeta_Cons07=-1.8,
zeta_Cons08=-1,
zeta_Cons09=-1,
zeta_Cons10=-1,
zeta_Cons11=-2,
zeta_Cons12=-1,
zeta_Cons13=-1,
zeta_Cons14=-3,
zeta_Cons15=-1,
zeta_Cons16=-1,
tau_Cons01_1=-2.5,
tau_Cons01_2=-1.3,
tau_Cons01_3=0.25,
tau_Cons01_4=1.6,
tau_Cons02_1=-3.2,
tau_Cons02_2=-1.8,
tau_Cons02_3=0.11,
tau_Cons02_4=1.58,
tau_Cons03_1=-2.9,
tau_Cons03_2=-1.65,
tau_Cons03_3=0.11,
tau_Cons03_4=1.67,
tau_Cons04_1=-2.38,
tau_Cons04_2=-1.14,
tau_Cons04_3=0.42,
tau_Cons04_4=1.85,
tau_Cons05_1=-2.38,
tau_Cons05_2=-1.14,
tau_Cons05_3=0.42,
tau_Cons05_4=3,
tau_Cons06_1=-2.38,
tau_Cons06_2=-1.14,
tau_Cons06_3=0.42,
tau_Cons06_4=1.85,
tau_Cons07_1=-2.38,
tau_Cons07_2=-1.14,
tau_Cons07_3=0.42,
tau_Cons07_4=1.85,
tau_Cons08_1=-2.38,
tau_Cons08_2=-1.14,
tau_Cons08_3=0.42,
tau_Cons08_4=1.85,
tau_Cons09_1=-2.38,
tau_Cons09_2=-1.14,
tau_Cons09_3=0.42,
tau_Cons09_4=1.85,
tau_Cons10_1=-2.38,
tau_Cons10_2=-1.14,
tau_Cons10_3=0.42,
tau_Cons10_4=1.85,
tau_Cons11_1=-2.38,
tau_Cons11_2=-1.14,
tau_Cons11_3=0.42,
tau_Cons11_4=1.85,
tau_Cons12_1=-2.38,
tau_Cons12_2=-1.14,
tau_Cons12_3=0.42,
tau_Cons12_4=1.85,
tau_Cons13_1=-2.38,
tau_Cons13_2=-1.14,
tau_Cons13_3=0.42,
tau_Cons13_4=1.85,
tau_Cons14_1=-2.38,
tau_Cons14_2=-1.14,
tau_Cons14_3=0.42,
tau_Cons14_4=1.85,
tau_Cons15_1=-2.38,
tau_Cons15_2=-1.14,
tau_Cons15_3=0.42,
tau_Cons15_4=1.85,
tau_Cons16_1=-2.38,
tau_Cons16_2=-1.14,
tau_Cons16_3=0.42,
tau_Cons16_4=1.85,
gamma_pb=1,
gamma_envb=0.5,
gamma_feas=0.7,
gamma_trial=0.8,
gamma_health=0.9
)
### 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("delta_a", "pi_LV1_a")
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType="mlhs",
interNDraws=100,
interUnifDraws=c(),
interNormDraws=c("eta_LV1","eta_LV2","eta_LV3","eta_LV4", "eta_LV5", "eta_LV6")
)
### Create random parameters
apollo_randCoeff=function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["LV6"]] = eta_LV6
randcoeff[["LV5"]] = eta_LV5
randcoeff[["LV4"]] = eta_LV4
randcoeff[["LV3"]] = eta_LV3
randcoeff[["LV2"]] = eta_LV2
randcoeff[["LV1"]] = eta_LV1+gamma_pb*eta_LV2+gamma_envb*eta_LV3+gamma_feas*eta_LV4+gamma_trial*eta_LV5+gamma_health*eta_LV6
return(randcoeff)
}
# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS ####
# ################################################################# #
apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
### Define lists of parameters for each class
### classA classB ClassC
lcpars[["bt1"]] = list(bt1_a, bt1_b,bt1_c)
lcpars[["bt2"]] = list(bt2_a, bt2_b,bt2_c)
lcpars[["bt3"]] = list(bt3_a, bt3_b,bt3_c)
lcpars[["bsave"]] = list(bsave_a, bsave_b, bsave_c)
lcpars[["bpay"]] = list(bpay_a, bpay_b, bpay_c)
lcpars[["bind"]] = list(bind_a, bind_b, bind_c)
lcpars[["bemi"]] = list(bemi_a, bemi_b, bemi_c)
lcpars[["bcost"]] = list(bcost_a, bcost_b, bcost_c)
### Class allocation probabilities
### These are the probabilities of a binary logit model
### apollo_mnl could be used too (with functionality="raw"
### and choice=NA), but explicitly writing the probability
### is easier.
V=list()
V[["class_a"]] = delta_a+pi_LV1_a*LV1
V[["class_b"]] = delta_b+pi_LV1_b*LV1
V[["class_c"]] = delta_c+pi_LV1_c*LV1
classAlloc_settings = list(
alternatives = c(class_a=1, class_b=2, class_c=3),
avail = 1,
choiceVar = NA,
utilities = V
)
lcpars[["pi_values"]] = apollo_classAlloc(classAlloc_settings)
#lcpars[["pi_values"]] = apollo_firstRow(lcpars[["pi_values"]], apollo_inputs)
return(lcpars)
}
# ################################################################# #
#### 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=M1_IntendToUse_NearFuture,
V=zeta_Cons01*LV1,
tau=c(tau_Cons01_1, tau_Cons01_2, tau_Cons01_3, tau_Cons01_4),rows=(task==1),
componentName = "indic_future")
ol_settings2 = list(outcomeOrdered=M2_RecommendUse,
V=zeta_Cons02*LV1,
tau=c(tau_Cons02_1, tau_Cons02_2, tau_Cons02_3, tau_Cons02_4),rows=(task==1),
componentName = "indic_recommend")
ol_settings3 = list(outcomeOrdered=M3_ReadilyAvailableTechn,
V=zeta_Cons03*LV1,
tau=c(tau_Cons03_1, tau_Cons03_2, tau_Cons03_3, tau_Cons03_4),rows=(task==1),
componentName = "indic_available")
ol_settings4 = list(outcomeOrdered=M4_WTSpendMoney,
V=zeta_Cons04*LV1,
tau=c(tau_Cons04_1, tau_Cons04_2, tau_Cons04_3, tau_Cons04_4),rows=(task==1),
componentName = "indic_wtp")
ol_settings5 = list(outcomeOrdered=J1_Savings,
V=zeta_Cons05*LV2,
tau=c(tau_Cons05_1, tau_Cons05_2, tau_Cons05_3, tau_Cons05_4),rows=(task==1),
componentName = "indic_savings")
ol_settings6 = list(outcomeOrdered=J2_PropertyValue,
V=zeta_Cons06*LV2,
tau=c(tau_Cons06_1, tau_Cons06_2, tau_Cons06_3, tau_Cons06_4),rows=(task==1),
componentName = "indic_prop")
ol_settings7 = list(outcomeOrdered=J3_Environment,
V=zeta_Cons07*LV3,
tau=c(tau_Cons07_1, tau_Cons07_2, tau_Cons07_3, tau_Cons07_4),rows=(task==1),
componentName = "indic_env")
ol_settings8 = list(outcomeOrdered=C3_FossilFuels,
V=zeta_Cons08*LV3,
tau=c(tau_Cons08_1, tau_Cons08_2, tau_Cons08_3, tau_Cons08_4),rows=(task==1),
componentName = "indic_ff")
ol_settings9 = list(outcomeOrdered=K1_PV_Feasib,
V=zeta_Cons09*LV4,
tau=c(tau_Cons09_1, tau_Cons09_2, tau_Cons09_3, tau_Cons09_4),rows=(task==1),
componentName = "indic_pv")
ol_settings10 = list(outcomeOrdered=K2_GSHP_Feasib,
V=zeta_Cons10*LV4,
tau=c(tau_Cons10_1, tau_Cons10_2, tau_Cons10_3, tau_Cons10_4),rows=(task==1),
componentName = "indic_gshp")
ol_settings11 = list(outcomeOrdered=K3_TES_Feasib,
V=zeta_Cons11*LV4,
tau=c(tau_Cons11_1, tau_Cons11_2, tau_Cons11_3, tau_Cons11_4),rows=(task==1),
componentName = "indic_tes")
ol_settings12 = list(outcomeOrdered=L1_SeeIt,
V=zeta_Cons12*LV5,
tau=c(tau_Cons12_1, tau_Cons12_2, tau_Cons12_3, tau_Cons12_4),rows=(task==1),
componentName = "indic_see")
ol_settings13 = list(outcomeOrdered=L2_TalkToSomeone,
V=zeta_Cons13*LV5,
tau=c(tau_Cons13_1, tau_Cons13_2, tau_Cons13_3, tau_Cons13_4),rows=(task==1),
componentName = "indic_talk")
ol_settings14 = list(outcomeOrdered=L3_TryIt,
V=zeta_Cons14*LV5,
tau=c(tau_Cons14_1, tau_Cons14_2, tau_Cons14_3, tau_Cons14_4),rows=(task==1),
componentName = "indic_try")
ol_settings15 = list(outcomeOrdered=F1_IndoorEnvironment,
V=zeta_Cons15*LV6,
tau=c(tau_Cons15_1, tau_Cons15_2, tau_Cons15_3, tau_Cons15_4),rows=(task==1),
componentName = "indic_ind")
ol_settings16 = list(outcomeOrdered=F2_Cons_HS,
V=zeta_Cons16*LV6,
tau=c(tau_Cons16_1, tau_Cons16_2, tau_Cons16_3, tau_Cons16_4),rows=(task==1),
componentName = "indic_cons")
### Keep only the first row, as it is the only one that matters
P[["indic_future"]] = apollo_firstRow(apollo_ol(ol_settings1, functionality), apollo_inputs )
P[["indic_recommend"]] = apollo_firstRow(apollo_ol(ol_settings2, functionality), apollo_inputs )
P[["indic_available"]] = apollo_firstRow(apollo_ol(ol_settings3, functionality), apollo_inputs )
P[["indic_wtp"]] = apollo_firstRow(apollo_ol(ol_settings4, functionality), apollo_inputs )
P[["indic_savings"]] = apollo_firstRow(apollo_ol(ol_settings5, functionality), apollo_inputs )
P[["indic_prop"]] = apollo_firstRow(apollo_ol(ol_settings6, functionality), apollo_inputs )
P[["indic_savings"]] = apollo_firstRow(apollo_ol(ol_settings7, functionality), apollo_inputs )
P[["indic_prop"]] = apollo_firstRow(apollo_ol(ol_settings8, functionality), apollo_inputs )
P[["indic_pv"]] = apollo_firstRow(apollo_ol(ol_settings9, functionality), apollo_inputs )
P[["indic_gshp"]] = apollo_firstRow(apollo_ol(ol_settings10, functionality), apollo_inputs )
P[["indic_tes"]] = apollo_firstRow(apollo_ol(ol_settings11, functionality), apollo_inputs )
P[["indic_see"]] = apollo_firstRow(apollo_ol(ol_settings12, functionality), apollo_inputs )
P[["indic_talk"]] = apollo_firstRow(apollo_ol(ol_settings13, functionality), apollo_inputs )
P[["indic_try"]] = apollo_firstRow(apollo_ol(ol_settings14, functionality), apollo_inputs )
P[["indic_ind"]] = apollo_firstRow(apollo_ol(ol_settings15, functionality), apollo_inputs )
P[["indic_cons"]] = apollo_firstRow(apollo_ol(ol_settings16, functionality), apollo_inputs )
### Likelihood of CHOICES
S <- 3
for(s in 1:S){
V = list()
### Class-specific utilities
V[['altA']] = bt1[[s]]*tech1_a+bt2[[s]]*tech2_a+bt3[[s]]*tech3_a+bcost[[s]]*cost_a+bsave[[s]]*savings_a+bemi[[s]]*emission_a+bpay[[s]]*payback_a+bind[[s]]*indoor_a
V[['altB']] = bt1[[s]]*tech1_b+bt2[[s]]*tech2_b+bt3[[s]]*tech3_b+bcost[[s]]*cost_b+bsave[[s]]*savings_b+bemi[[s]]*emission_b+bpay[[s]]*payback_b+bind[[s]]*indoor_b
V[['altC']] = 0
### Define settings for MNL model component that are generic across classes
mnl_settings = list(
alternatives = c(altA=1, altB=2, altC=3),
choiceVar = choice,
utilities = V
)
### Compute within-class choice probabilities using MNL model
P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings, functionality)
### Take product across observation for same individual
P[[paste0("Class_",s)]] = apollo_panelProd(P[[paste0("Class_",s)]], apollo_inputs ,functionality)
}
### Compute latent class model probabilities
lc_settings = list(inClassProb = P[paste0("Class_", 1:S)], classProb=pi_values)
P[["choice"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
P = apollo_combineModels(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 ####
# ################################################################# #
apollo_llCalc(apollo_beta, apollo_probabilities, apollo_inputs)
# ################################################################# #
#### 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)
The output is as follows:
Calculating LL of each model component...Done.
$model
[1] -90178.2
$indic_future
[1] -2413.189
$indic_recommend
[1] -2079.934
$indic_available
[1] -1921.038
$indic_wtp
[1] -1940.897
$indic_savings
[1] -1974.237
$indic_prop
[1] -2039.837
$indic_pv
[1] -1972.208
$indic_gshp
[1] -1979.354
$indic_tes
[1] -2040.902
$indic_see
[1] -2014.449
$indic_talk
[1] -2026.414
$indic_try
[1] -2060.828
$indic_ind
[1] -1921.295
$indic_cons
[1] -1955.527
$Class_1
[1] -14942.26
$Class_2
[1] -22427.91
$Class_3
[1] -16867.93
$choice
[1] -9901.841
>
> # ################################################################# #
> #### MODEL ESTIMATION ####
> # ################################################################# #
>
> model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
Preparing user-defined functions.
Testing likelihood function...
Apollo found a model component of type classAlloc without a componentName. The name was set to "classAlloc" by default.
No coding provided for Ordered model component "indic_future", so assuming outcomeOrdered goes from 1 to 5
Overview of choices for OL model component indic_future:
1 2 3 4 5
Times chosen 75.00 161.00 468.00 378.0 217.00
Percentage chosen overall 5.77 12.39 36.03 29.1 16.71
No coding provided for Ordered model component "indic_recommend", so assuming outcomeOrdered goes from 1 to 5
Overview of choices for OL model component indic_recommend:
1 2 3 4 5
Times chosen 37.00 125.00 473.00 391.0 273.00
Percentage chosen overall 2.85 9.62 36.41 30.1 21.02
No coding provided for Ordered model component "indic_available", so assuming outcomeOrdered goes from 1 to 5
Overview of choices for OL model component indic_available:
1 2 3 4 5
Times chosen 35.00 85.00 400.00 464.00 315.00
Percentage chosen overall 2.69 6.54 30.79 35.72 24.25
No coding provided for Ordered model component "indic_wtp", so assuming outcomeOrdered goes from 1 to 5
Overview of choices for OL model component indic_wtp:
1 2 3 4 5
Times chosen 78 156.00 435.00 390.00 240.00
Percentage chosen overall 6 12.01 33.49 30.02 18.48
No coding provided for Ordered model component "indic_savings", so assuming outcomeOrdered goes from 1 to 5
Overview of choices for OL model component indic_savings:
1 2 3 4 5
Times chosen 17.00 78 286.00 487.00 431.00
Percentage chosen overall 1.31 6 22.02 37.49 33.18
No coding provided for Ordered model component "indic_prop", so assuming outcomeOrdered goes from 1 to 5
Overview of choices for OL model component indic_prop:
1 2 3 4 5
Times chosen 14.00 38.00 250.00 489.00 508.00
Percentage chosen overall 1.08 2.93 19.25 37.64 39.11
No coding provided for Ordered model component "indic_env", so assuming outcomeOrdered goes from 1 to 5
Overview of choices for OL model component indic_env:
1 2 3 4 5
Times chosen 9.00 33.00 218.00 485.00 554.00
Percentage chosen overall 0.69 2.54 16.78 37.34 42.65
No coding provided for Ordered model component "indic_ff", so assuming outcomeOrdered goes from 1 to 5
Overview of choices for OL model component indic_ff:
1 2 3 4 5
Times chosen 28.00 56.00 198.00 421.00 596.00
Percentage chosen overall 2.16 4.31 15.24 32.41 45.88
No coding provided for Ordered model component "indic_pv", so assuming outcomeOrdered goes from 1 to 5
Overview of choices for OL model component indic_pv:
1 2 3 4 5
Times chosen 73.00 121.00 347.00 434.00 324.00
Percentage chosen overall 5.62 9.31 26.71 33.41 24.94
No coding provided for Ordered model component "indic_gshp", so assuming outcomeOrdered goes from 1 to 5
Overview of choices for OL model component indic_gshp:
1 2 3 4 5
Times chosen 103.00 183.00 406.00 358.00 249.00
Percentage chosen overall 7.93 14.09 31.25 27.56 19.17
No coding provided for Ordered model component "indic_tes", so assuming outcomeOrdered goes from 1 to 5
Overview of choices for OL model component indic_tes:
1 2 3 4 5
Times chosen 76.00 142.00 410.00 404.0 267.00
Percentage chosen overall 5.85 10.93 31.56 31.1 20.55
No coding provided for Ordered model component "indic_see", so assuming outcomeOrdered goes from 1 to 5
Overview of choices for OL model component indic_see:
1 2 3 4 5
Times chosen 18.00 36.00 217.00 449.00 579.00
Percentage chosen overall 1.39 2.77 16.71 34.57 44.57
No coding provided for Ordered model component "indic_talk", so assuming outcomeOrdered goes from 1 to 5
Overview of choices for OL model component indic_talk:
1 2 3 4 5
Times chosen 19.00 40.00 209.00 428.00 603.00
Percentage chosen overall 1.46 3.08 16.09 32.95 46.42
No coding provided for Ordered model component "indic_try", so assuming outcomeOrdered goes from 1 to 5
Overview of choices for OL model component indic_try:
1 2 3 4 5
Times chosen 19.00 60.00 262.00 448.00 510.00
Percentage chosen overall 1.46 4.62 20.17 34.49 39.26
No coding provided for Ordered model component "indic_ind", so assuming outcomeOrdered goes from 1 to 5
Overview of choices for OL model component indic_ind:
1 2 3 4 5
Times chosen 25.00 98.00 371.00 485.00 320.00
Percentage chosen overall 1.92 7.54 28.56 37.34 24.63
No coding provided for Ordered model component "indic_cons", so assuming outcomeOrdered goes from 1 to 5
Overview of choices for OL model component indic_cons:
1 2 3 4 5
Times chosen 71.00 186.00 434.00 343.0 265.0
Percentage chosen overall 5.47 14.32 33.41 26.4 20.4
Apollo found a model component of type MNL without a componentName. The name was set to "Class_1" by default.
Setting "avail" is missing, so full availability is assumed.
Overview of choices for MNL model component Class_1:
altA altB altC
Times available 11691.00 11691.00 11691.00
Times chosen 4276.00 4164.00 3251.00
Percentage chosen overall 36.58 35.62 27.81
Percentage chosen when available 36.58 35.62 27.81
Apollo found a model component of type MNL without a componentName. The name was set to "Class_2" by default.
Setting "avail" is missing, so full availability is assumed.
Overview of choices for MNL model component Class_2:
altA altB altC
Times available 11691.00 11691.00 11691.00
Times chosen 4276.00 4164.00 3251.00
Percentage chosen overall 36.58 35.62 27.81
Percentage chosen when available 36.58 35.62 27.81
Apollo found a model component of type MNL without a componentName. The name was set to "Class_3" by default.
Setting "avail" is missing, so full availability is assumed.
Overview of choices for MNL model component Class_3:
altA altB altC
Times available 11691.00 11691.00 11691.00
Times chosen 4276.00 4164.00 3251.00
Percentage chosen overall 36.58 35.62 27.81
Percentage chosen when available 36.58 35.62 27.81
Apollo found a model component of type LC without a componentName. The name was set to "choice" by default.
Summary of class allocation for LC model component choice:
Mean prob.
Class_1 0.5506
Class_2 0.1535
Class_3 0.2958
Class probability for model component "choice" averaged across observations of each individual.
Pre-processing likelihood function...
Preparing workers for multithreading...
Testing influence of parameters................................Error in apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, :
Parameter zeta_Cons05 does not influence the log-likelihood of your model!