Dear developers of Apollo, first, thanks for this powerful and flexible package. It’s amazing!
I’m trying to estimate an ordinal logit using second-order latent variables (LVs). I attached de path diagram of the model to explain me better. The idea is to describe how specific groups can change the purchase habits of respondents due to covid 19. Each group is measured through two indicators: knowledge and trust. Each indicator is ordinal on a four-point Likert scale. For example, nat_gov (national government) group is measured with know_natgov and trust_natgov. I specified the ol_settings like this:
ol_settings_know_natgov = list(outcomeOrdered=know_natgov,
V=zeta_know_natgov*nat_gov,
tau=c(tau_1_know_natgov, tau_2_know_natgov, tau_3_know_natgov),
rows=(task==1))
ol_settings_trust_natgov = list(outcomeOrdered=trust_natgov,
V=zeta_trust_natgov*nat_gov,
tau=c(tau_1_trust_natgov, tau_2_trust_natgov, tau_3_trust_natgov),
rows=(task==1))
For another latent group: loc_gov (local government), I did the same, and so on with all the groups:
ol_settings_know_logov = list(outcomeOrdered=know_logov,
V=zeta_know_logov*loc_gov,
tau=c(tau_1_know_logov, tau_2_know_logov, tau_3_know_logov),
rows=(task==1))
ol_settings_trust_logov = list(outcomeOrdered=trust_logov,
V=zeta_trust_logov*loc_gov,
tau=c(tau_1_trust_logov, tau_2_trust_logov, tau_3_trust_logov),
rows=(task==1))
Then, the latent groups are grouped to form another latent group, for example, government (gov) is formed through the latent groups nat_gov and loc_gov. To make this, I used apollo_randCoeff and son on with all the groups:
randcoeff[["gov"]] = gamma_nat_gov * nat_gov + gamma_loc_gov * loc_gov + eta_gov
My utility function is:
ol_settings_change = list(outcomeOrdered=change,
V= asc + beta_gov * gov + beta_loc_rpnd_org * loc_rpnd_org + beta_int_rpnd_org * int_rpnd_org + beta_news * news + beta_pol_fire * pol_fire,
tau=c(tau_1_change, tau_2_change),
rows=(task==1))
P[["change"]] = apollo_ol(ol_settings_change, functionality)
However, the apollo_inputs function output the error: “Error in f(apollo_beta, apollo_inputs) : objeto 'nat_gov' no encontrado”. I suppose that since 'nat_gov' is latent, the package does not understand that it is. I would like to know why?. Thank you. Best regards
IS
_______________________
The full code is:
library(apollo)
database<-read_csv("bd.csv",col_names=TRUE)
apollo_initialise()
apollo_control = list(
modelName ="Ordinal hybrid model Change v0",
modelDescr ="Ordinal hybrid model Change v0",
indivID ="id",
mixing = TRUE,
nCores = 8
)
apollo_beta=c(asc =0,
gamma_nat_gov =0,
gamma_loc_gov =0,
gamma_health_off =0,
gamma_loc_emer_rpnd =0,
gamma_loc_rlf_org =0,
gamma_int_rlf_org =0,
gamma_red_crs =0,
gamma_trad_news =0,
gamma_elec_news =0,
gamma_poli =0,
gamma_fire =0,
zeta_know_natgov =0,
tau_1_know_natgov =-1,
tau_2_know_natgov =0,
tau_3_know_natgov =1,
zeta_trust_natgov =0,
tau_1_trust_natgov =-1,
tau_2_trust_natgov =0,
tau_3_trust_natgov =1,
zeta_know_logov =0,
tau_1_know_logov =-1,
tau_2_know_logov =0,
tau_3_know_logov =1,
zeta_trust_logov =0,
tau_1_trust_logov =-1,
tau_2_trust_logov =0,
tau_3_trust_logov =1,
zeta_know_health =0,
tau_1_know_health =-1,
tau_2_know_health =0,
tau_3_know_health =1,
zeta_trust_health =0,
tau_1_trust_health =-1,
tau_2_trust_health =0,
tau_3_trust_health =1,
zeta_know_localem =0,
tau_1_know_localem =-1,
tau_2_know_localem =0,
tau_3_know_localem =1,
zeta_trust_localem =0,
tau_1_trust_localem =-1,
tau_2_trust_localem =0,
tau_3_trust_localem =1,
zeta_know_localrelief =0,
tau_1_know_localrelief =-1,
tau_2_know_localrelief =0,
tau_3_know_localrelief =1,
zeta_trust_localrelief =0,
tau_1_trust_localrelief =-1,
tau_2_trust_localrelief =0,
tau_3_trust_localrelief =1,
zeta_know_ingo =0,
tau_1_know_ingo =-1,
tau_2_know_ingo =0,
tau_3_know_ingo =1,
zeta_trust_ingo =0,
tau_1_trust_ingo =-1,
tau_2_trust_ingo =0,
tau_3_trust_ingo =1,
zeta_know_redcross =0,
tau_1_know_redcross =-1,
tau_2_know_redcross =0,
tau_3_know_redcross =1,
zeta_trust_redcross =0,
tau_1_trust_redcross =-1,
tau_2_trust_redcross =0,
tau_3_trust_redcross =1,
zeta_know_news =0,
tau_1_know_news =-1,
tau_2_know_news =0,
tau_3_know_news =1,
zeta_trust_news =0,
tau_1_trust_news =-1,
tau_2_trust_news =0,
tau_3_trust_news =1,
zeta_know_social =0,
tau_1_know_social =-1,
tau_2_know_social =0,
tau_3_know_social =1,
zeta_trust_social =0,
tau_1_trust_social =-1,
tau_2_trust_social =0,
tau_3_trust_social =1,
zeta_know_police =0,
tau_1_know_police =-1,
tau_2_know_police =0,
tau_3_know_police =1,
zeta_trust_police =0,
tau_1_trust_police =-1,
tau_2_trust_police =0,
tau_3_trust_police =1,
zeta_know_fire =0,
tau_1_know_fire =-1,
tau_2_know_fire =0,
tau_3_know_fire =1,
zeta_trust_fire =0,
tau_1_trust_fire =-1,
tau_2_trust_fire =0,
tau_3_trust_fire =1,
beta_gov =0,
beta_loc_rpnd_org =-1,
beta_int_rpnd_org =0,
beta_news =1,
beta_pol_fire=0,
tau_1_change=-1,
tau_2_change=0)
apollo_fixed = c("asc")
apollo_draws = list(
interDrawsType="halton",
interNDraws=500,
interUnifDraws=c(),
interNormDraws=c("eta_gov","eta_loc_rpnd_org","eta_int_rpnd_org","eta_news","eta_pol_fire"),
intraDrawsType='',
intraNDraws=0,
intraUnifDraws=c(),
intraNormDraws=c()
)
apollo_randCoeff=function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["gov"]] = gamma_nat_gov * nat_gov + gamma_loc_gov * loc_gov + eta_gov
randcoeff[["loc_rpnd_org"]] = gamma_health_off * health_off + gamma_loc_emer_rpnd * loc_emer_rpnd + gamma_loc_rlf_org * loc_rlf_org + eta_loc_rpnd_org
randcoeff[["int_rpnd_org"]] = gamma_int_rlf_org * int_rlf_org + gamma_red_crs * red_crs + eta_int_rpnd_org
randcoeff[["news"]] = gamma_trad_news * trad_news + gamma_elec_news * elec_news + eta_news
randcoeff[["pol_fire"]] = gamma_poli * poli + gamma_fire * fire + eta_pol_fire
return(randcoeff)
}
apollo_inputs = apollo_validateInputs()
#definir apollo probabilities function
apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
apollo_attach(apollo_beta, apollo_inputs)
on.exit(apollo_detach(apollo_beta, apollo_inputs))
P = list()
### Likelihood of indicators
#####gov
ol_settings_know_natgov = list(outcomeOrdered=know_natgov,
V=zeta_know_natgov*nat_gov,
tau=c(tau_1_know_natgov, tau_2_know_natgov, tau_3_know_natgov),
rows=(task==1))
ol_settings_trust_natgov = list(outcomeOrdered=trust_natgov,
V=zeta_trust_natgov*nat_gov,
tau=c(tau_1_trust_natgov, tau_2_trust_natgov, tau_3_trust_natgov),
rows=(task==1))
ol_settings_know_logov = list(outcomeOrdered=know_logov,
V=zeta_know_logov*loc_gov,
tau=c(tau_1_know_logov, tau_2_know_logov, tau_3_know_logov),
rows=(task==1))
ol_settings_trust_logov = list(outcomeOrdered=trust_logov,
V=zeta_trust_logov*loc_gov,
tau=c(tau_1_trust_logov, tau_2_trust_logov, tau_3_trust_logov),
rows=(task==1))
ol_settings_know_health = list(outcomeOrdered=know_health,
V=zeta_know_health*health_off,
tau=c(tau_1_know_health, tau_2_know_health, tau_3_know_health),
rows=(task==1))
ol_settings_trust_health = list(outcomeOrdered=trust_health,
V=zeta_trust_health*health_off,
tau=c(tau_1_trust_health, tau_2_trust_health, tau_3_trust_health),
rows=(task==1))
ol_settings_know_localem = list(outcomeOrdered=know_localem,
V=zeta_know_localem*loc_emer_rpnd,
tau=c(tau_1_know_localem, tau_2_know_localem, tau_3_know_localem),
rows=(task==1))
ol_settings_trust_localem = list(outcomeOrdered=trust_localem,
V=zeta_trust_localem*loc_emer_rpnd,
tau=c(tau_1_trust_localem, tau_2_trust_localem, tau_3_trust_localem),
rows=(task==1))
ol_settings_know_localrelief = list(outcomeOrdered=know_localrelief,
V=zeta_know_localrelief*loc_rlf_org,
tau=c(tau_1_know_localrelief, tau_2_know_localrelief, tau_3_know_localrelief),
rows=(task==1))
ol_settings_trust_localrelief = list(outcomeOrdered=trust_localrelief,
V=zeta_trust_localrelief*loc_rlf_org,
tau=c(tau_1_trust_localrelief, tau_2_trust_localrelief, tau_3_trust_localrelief),
rows=(task==1))
ol_settings_know_ingo = list(outcomeOrdered=know_ingo,
V=zeta_know_ingo*int_rlf_org,
tau=c(tau_1_know_ingo, tau_2_know_ingo, tau_3_know_ingo),
rows=(task==1))
ol_settings_trust_ingo = list(outcomeOrdered=trust_ingo,
V=zeta_trust_ingo*int_rlf_org,
tau=c(tau_1_trust_ingo, tau_2_trust_ingo, tau_3_trust_ingo),
rows=(task==1))
ol_settings_know_redcross = list(outcomeOrdered=know_redcross,
V=zeta_know_redcross*red_crs,
tau=c(tau_1_know_redcross, tau_2_know_redcross, tau_3_know_redcross),
rows=(task==1))
ol_settings_trust_redcross = list(outcomeOrdered=trust_redcross,
V=zeta_trust_redcross*red_crs,
tau=c(tau_1_trust_redcross, tau_2_trust_redcross, tau_3_trust_redcross),
rows=(task==1))
ol_settings_know_news = list(outcomeOrdered=know_news,
V=zeta_know_news*trad_news,
tau=c(tau_1_know_news, tau_2_know_news, tau_3_know_news),
rows=(task==1))
ol_settings_trust_news = list(outcomeOrdered=trust_news,
V=zeta_trust_news*trad_news,
tau=c(tau_1_trust_news, tau_2_trust_news, tau_3_trust_news),
rows=(task==1))
ol_settings_know_social = list(outcomeOrdered=know_social,
V=zeta_know_social*elec_news,
tau=c(tau_1_know_social, tau_2_know_social, tau_3_know_social),
rows=(task==1))
ol_settings_trust_social = list(outcomeOrdered=trust_social,
V=zeta_trust_social*elec_news,
tau=c(tau_1_trust_social, tau_2_trust_social, tau_3_trust_social),
rows=(task==1))
ol_settings_know_police = list(outcomeOrdered=know_police,
V=zeta_know_police*poli,
tau=c(tau_1_know_police, tau_2_know_police, tau_3_know_police),
rows=(task==1))
ol_settings_trust_police = list(outcomeOrdered=trust_police,
V=zeta_trust_police*poli,
tau=c(tau_1_trust_police, tau_2_trust_police, tau_3_trust_police),
rows=(task==1))
ol_settings_know_fire = list(outcomeOrdered=know_fire,
V=zeta_know_fire*fire,
tau=c(tau_1_know_fire, tau_2_know_fire, tau_3_know_fire),
rows=(task==1))
ol_settings_trust_fire = list(outcomeOrdered=trust_fire,
V=zeta_trust_fire*fire,
tau=c(tau_1_trust_fire, tau_2_trust_fire, tau_3_trust_fire),
rows=(task==1))
P[["indic_know_natgov"]] = apollo_ol(ol_settings_know_natgov, functionality)
P[["indic_trust_natgov"]] = apollo_ol(ol_settings_trust_natgov, functionality)
P[["indic_know_logov"]] = apollo_ol(ol_settings_know_logov, functionality)
P[["indic_trust_logov"]] = apollo_ol(ol_settings_trust_logov, functionality)
P[["indic_know_health"]] = apollo_ol(ol_settings_know_health, functionality)
P[["indic_trust_health"]] = apollo_ol(ol_settings_trust_health, functionality)
P[["indic_know_localem"]] = apollo_ol(ol_settings_know_localem, functionality)
P[["indic_trust_localem"]] = apollo_ol(ol_settings_trust_localem, functionality)
P[["indic_know_localrelief"]] = apollo_ol(ol_settings_know_localrelief, functionality)
P[["indic_trust_localrelief"]] = apollo_ol(ol_settings_trust_localrelief, functionality)
P[["indic_know_ingo"]] = apollo_ol(ol_settings_know_ingo, functionality)
P[["indic_trust_ingo"]] = apollo_ol(ol_settings_trust_ingo, functionality)
P[["indic_know_redcross"]] = apollo_ol(ol_settings_know_redcross, functionality)
P[["indic_trust_redcross"]] = apollo_ol(ol_settings_trust_redcross, functionality)
P[["indic_know_news"]] = apollo_ol(ol_settings_know_news, functionality)
P[["indic_trust_news"]] = apollo_ol(ol_settings_trust_news, functionality)
P[["indic_know_social"]] = apollo_ol(ol_settings_know_social, functionality)
P[["indic_trust_social"]] = apollo_ol(ol_settings_trust_social, functionality)
P[["indic_know_police"]] = apollo_ol(ol_settings_know_police, functionality)
P[["indic_trust_police"]] = apollo_ol(ol_settings_trust_police, functionality)
P[["indic_know_fire"]] = apollo_ol(ol_settings_know_fire, functionality)
P[["indic_trust_fire"]] = apollo_ol(ol_settings_trust_fire, functionality)
ol_settings_change = list(outcomeOrdered=change,
V= asc + beta_gov * gov + beta_loc_rpnd_org * loc_rpnd_org + beta_int_rpnd_org * int_rpnd_org + beta_news * news + beta_pol_fire * pol_fire,
tau=c(tau_1_change, tau_2_change),
rows=(task==1))
P[["change"]] = apollo_ol(ol_settings_change, 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)
}
#estimar el modelo
# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #
### Optional: calculate LL before model estimation
apollo_llCalc(apollo_beta, apollo_probabilities, apollo_inputs)
### 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)
Important: Read this before posting to this forum
- 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.
- 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
- Before asking a question on the forum, users are kindly requested to follow these steps:
- Check that the same issue has not already been addressed in the forum - there is a search tool.
- 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
- Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
- Make sure that R is using the latest official release of Apollo.
- Users can check which version they are running by entering packageVersion("apollo").
- Then check what is the latest full release (not development version) at http://www.ApolloChoiceModelling.com/code.html.
- To update to the latest official version, just enter install.packages("apollo"). To update to a development version, download the appropriate binary file from http://www.ApolloChoiceModelling.com/code.html, and install the package from file
- If the above steps do not resolve the issue, then users should follow these steps when posting a question:
- provide full details on the issue, including the entire code and output, including any error messages
- 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.
ordinal logit using second-order latent variables
ordinal logit using second-order latent variables
- Attachments
-
- path.PNG (68.91 KiB) Viewed 7381 times
-
- Site Admin
- Posts: 1040
- Joined: 24 Apr 2020, 16:29
Re: ordinal logit using second-order latent variables
Hi
the problem is that you have not created nat_gov anywhere. This needs to be included in apollo_randCoeff.
Hope this helps
Stephane
the problem is that you have not created nat_gov anywhere. This needs to be included in apollo_randCoeff.
Hope this helps
Stephane
Re: ordinal logit using second-order latent variables
Thanks for the reply prof Hess, I created the randCoeff nat_gov this:
apollo_draws = list(
interDrawsType="mlhs",
interNDraws=1000,
interUnifDraws=c(),
interNormDraws=c("eta_nat_gov"),
intraDrawsType='',
intraNDraws=0,
intraUnifDraws=c(),
intraNormDraws=c()
)
randcoeff[["nat_gov"]] = eta_nat_gov
However, I got the same Error:
Error in f(apollo_beta, apollo_inputs) : objeto 'nat_gov' no encontrado
apollo_draws = list(
interDrawsType="mlhs",
interNDraws=1000,
interUnifDraws=c(),
interNormDraws=c("eta_nat_gov"),
intraDrawsType='',
intraNDraws=0,
intraUnifDraws=c(),
intraNormDraws=c()
)
randcoeff[["nat_gov"]] = eta_nat_gov
However, I got the same Error:
Error in f(apollo_beta, apollo_inputs) : objeto 'nat_gov' no encontrado
-
- Site Admin
- Posts: 1040
- Joined: 24 Apr 2020, 16:29
Re: ordinal logit using second-order latent variables
Can you please show us the whole code again, not just the extracts
Re: ordinal logit using second-order latent variables
Prof Hess, I solved the problem in my code. It was that I specified the second-order latent variable in the wrong form:
randcoeff[["gov"]] = gamma_nat_gov * nat_gov + gamma_loc_gov * loc_gov
And the correct form is:
randcoeff[["gov"]] = gamma_nat_gov * randcoeff[["nat_gov"]] + gamma_loc_gov * randcoeff[["loc_gov"]].
Also, I deactivated (analyticGrad=FALSE). The code ran without a problem.
Professor, I have another doubt. Should I create an error term of the second-order variable or not? I mean, for example:
Lv1 = gama*x1 + eta1;
Lv2 = gama*x2 + eta2;
Lv3 = Lv1 + Lv2; or
Lv3 = Lv1 + Lv2 + eta3;
Being eta_i a normal random draw.
Thanks for the guidance,
Bests,
IS
randcoeff[["gov"]] = gamma_nat_gov * nat_gov + gamma_loc_gov * loc_gov
And the correct form is:
randcoeff[["gov"]] = gamma_nat_gov * randcoeff[["nat_gov"]] + gamma_loc_gov * randcoeff[["loc_gov"]].
Also, I deactivated (analyticGrad=FALSE). The code ran without a problem.
Professor, I have another doubt. Should I create an error term of the second-order variable or not? I mean, for example:
Lv1 = gama*x1 + eta1;
Lv2 = gama*x2 + eta2;
Lv3 = Lv1 + Lv2; or
Lv3 = Lv1 + Lv2 + eta3;
Being eta_i a normal random draw.
Thanks for the guidance,
Bests,
IS
-
- Site Admin
- Posts: 1040
- Joined: 24 Apr 2020, 16:29
Re: ordinal logit using second-order latent variables
Hi
regarding your question of creating an additional error for LV3, this depends on whether in your model, LV1 and LV2 are used separately from LV3 too as this will determine whether you can identify an additional source of error
Stephane
regarding your question of creating an additional error for LV3, this depends on whether in your model, LV1 and LV2 are used separately from LV3 too as this will determine whether you can identify an additional source of error
Stephane
Re: ordinal logit using second-order latent variables
Thanks, prof very useful your suggestion
Bests
Bests