Page 1 of 1

Getting NAs in a hybrid model

Posted: 15 Sep 2023, 01:21
by zx9203
Hi Stephane or David,

I wonder why I keep getting NAs in the hybrid model. It worked when I start with a simpler one, with one or two latent variables (FNS1, FNS2). But when I include more, the model results NA for estimates, std error, and so on.

My code are as below:

# ################################################################# #
#### 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 = "rice_Hybrid_with_OL",
modelDescr = "Hybrid choice model on rice choice data, using ordered measurement model for indicators",
indivID = "id",
nCores = 10
)

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

### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv),
### the code would be: database = read.csv("data.csv",header=TRUE)
database = read.csv("D:/2023/3、院长基金/0722 main data/ricedata_main.csv",header=TRUE)

# ################################################################# #
#### ANALYSIS OF CHOICES ####
# ################################################################# #

### Illustration of how to use apollo_choiceAnalysis with user-defined alternatives.
### This is useful in cases where the alternatives in the data differ
### across tasks. The same approach can then also be used with unlabelled data

choiceAnalysis_settings <- list(
alternatives = c(option_1=1, option_2=2, option_3=3, option_4=4),
avail = 1,
choiceVar = database$choice,
explanators = database[,c("edu","age","marry","income","gender")]
)

apollo_choiceAnalysis(choiceAnalysis_settings, apollo_control, database)

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_non_purchase = -1.52,
b_white = -0.05,
b_brown = -0.17,
b_organic = 0.57,
b_brand = 0.13,
b_lowGI = 0.99,
b_lowgluten = 0.98,
b_enhanced = 1.38,
b_price = -0.06,
lambda = 1,
gamma_ricehabit = 0,
gamma_younger = 0,
gamma_bachelor = 0,
gamma_kid = 0,
gamma_highincome = 0,
gamma_gender = 0,
zeta_FNS1 = 0,
zeta_FNS2 = 0,
zeta_FNS3 = 0,
zeta_FNS4 = 0,
zeta_FNS5 = 0,
zeta_FNS6 = 0,
zeta_FNS7 = 0,
zeta_FNS8 = 0,
zeta_FNS9 = 0,
tau_FNS1_1 = -3,
tau_FNS1_2 = -2,
tau_FNS1_3 = -1,
tau_FNS1_4 = 1,
tau_FNS1_5 = 2,
tau_FNS1_6 = 3,
tau_FNS2_1 = -3,
tau_FNS2_2 = -2,
tau_FNS2_3 = -1,
tau_FNS2_4 = 1,
tau_FNS2_5 = 2,
tau_FNS2_6 = 3,
tau_FNS3_1 = -3,
tau_FNS3_2 = -2,
tau_FNS3_3 = -1,
tau_FNS3_4 = 1,
tau_FNS3_5 = 2,
tau_FNS3_6 = 3,
tau_FNS4_1 = -3,
tau_FNS4_2 = -2,
tau_FNS4_3 = -1,
tau_FNS4_4 = 1,
tau_FNS4_5 = 2,
tau_FNS4_6 = 3,
tau_FNS5_1 = -3,
tau_FNS5_2 = -2,
tau_FNS5_3 = -1,
tau_FNS5_4 = 1,
tau_FNS5_5 = 2,
tau_FNS5_6 = 3,
tau_FNS6_1 = -3,
tau_FNS6_2 = -2,
tau_FNS6_3 = -1,
tau_FNS6_4 = 1,
tau_FNS6_5 = 2,
tau_FNS6_6 = 3,
tau_FNS7_1 = -3,
tau_FNS7_2 = -2,
tau_FNS7_3 = -1,
tau_FNS7_4 = 1,
tau_FNS7_5 = 2,
tau_FNS7_6 = 3,
tau_FNS8_1 = -3,
tau_FNS8_2 = -2,
tau_FNS8_3 = -1,
tau_FNS8_4 = 1,
tau_FNS8_5 = 2,
tau_FNS8_6 = 3,
tau_FNS9_1 = -3,
tau_FNS9_2 = -2,
tau_FNS9_3 = -1,
tau_FNS9_4 = 1,
tau_FNS9_5 = 2,
tau_FNS9_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()

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

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

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

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

randcoeff[["LV"]] = gamma_ricehabit * (ricehabit>3) + gamma_younger * (age<2) + gamma_bachelor * edu + gamma_kid * (marry==2|marry==4) + gamma_highincome * (income>1) + gamma_gender * gender + eta

return(randcoeff)
}

# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #

apollo_inputs = apollo_validateInputs()

# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){

### Attach inputs and detach after function exit
apollo_attach(apollo_beta, apollo_inputs)
on.exit(apollo_detach(apollo_beta, apollo_inputs))

### Create list of probabilities P
P = list()

### Likelihood of indicators
ol_settings1 = list(outcomeOrdered = FNS1,
V = zeta_FNS1*LV,
tau = list(tau_FNS1_1, tau_FNS1_2, tau_FNS1_3, tau_FNS1_4, tau_FNS1_5, tau_FNS1_6),
rows = (task==1|task==2),
componentName = "indic_FNS1")
ol_settings2 = list(outcomeOrdered = FNS1,
V = zeta_FNS2*LV,
tau = list(tau_FNS2_1, tau_FNS2_2, tau_FNS2_3, tau_FNS2_4, tau_FNS2_5, tau_FNS2_6),
rows = (task==1|task==2),
componentName = "indic_FNS2")
ol_settings3 = list(outcomeOrdered = FNS3,
V = zeta_FNS3*LV,
tau = list(tau_FNS3_1, tau_FNS3_2, tau_FNS3_3, tau_FNS3_4, tau_FNS3_5, tau_FNS3_6),
rows = (task==1|task==2),
componentName = "indic_FNS3")
ol_settings4 = list(outcomeOrdered = FNS4,
V = zeta_FNS4*LV,
tau = list(tau_FNS4_1, tau_FNS4_2, tau_FNS4_3, tau_FNS4_4, tau_FNS4_5, tau_FNS4_6),
rows = (task==1|task==2),
componentName = "indic_FNS4")
ol_settings5 = list(outcomeOrdered = FNS5,
V = zeta_FNS5*LV,
tau = list(tau_FNS5_1, tau_FNS5_2, tau_FNS5_3, tau_FNS5_4, tau_FNS5_5, tau_FNS5_6),
rows = (task==1|task==2),
componentName = "indic_FNS5")
ol_settings6 = list(outcomeOrdered = FNS6,
V = zeta_FNS6*LV,
tau = list(tau_FNS6_1, tau_FNS6_2, tau_FNS6_3, tau_FNS6_4, tau_FNS6_5, tau_FNS6_6),
rows = (task==1|task==2),
componentName = "indic_FNS6")
ol_settings7 = list(outcomeOrdered = FNS7,
V = zeta_FNS7*LV,
tau = list(tau_FNS7_1, tau_FNS7_2, tau_FNS7_3, tau_FNS7_4, tau_FNS7_5, tau_FNS7_6),
rows = (task==1|task==2),
componentName = "indic_FNS7")
ol_settings8 = list(outcomeOrdered = FNS8,
V = zeta_FNS8*LV,
tau = list(tau_FNS8_1, tau_FNS8_2, tau_FNS8_3, tau_FNS8_4, tau_FNS8_5, tau_FNS8_6),
rows = (task==1|task==2),
componentName = "indic_FNS8")
ol_settings9 = list(outcomeOrdered = FNS9,
V = zeta_FNS9*LV,
tau = list(tau_FNS9_1, tau_FNS9_2, tau_FNS9_3, tau_FNS9_4, tau_FNS9_5, tau_FNS9_6),
rows = (task==1|task==2),
componentName = "indic_FNS9")
P[["indic_FNS1"]] = apollo_ol(ol_settings1, functionality)
P[["indic_FNS2"]] = apollo_ol(ol_settings2, functionality)
P[["indic_FNS3"]] = apollo_ol(ol_settings3, functionality)
P[["indic_FNS4"]] = apollo_ol(ol_settings4, functionality)
P[["indic_FNS5"]] = apollo_ol(ol_settings5, functionality)
P[["indic_FNS6"]] = apollo_ol(ol_settings6, functionality)
P[["indic_FNS7"]] = apollo_ol(ol_settings7, functionality)
P[["indic_FNS8"]] = apollo_ol(ol_settings8, functionality)
P[["indic_FNS9"]] = apollo_ol(ol_settings9, functionality)

### Likelihood of choices
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['option_1']] = b_white * (white_1==0) + b_brown * (white_1==2) + b_organic * organic_1 + b_brand * brand_1 + b_lowGI * (health_1==1) + b_lowgluten * (health_1==2) + b_enhanced * (health_1==3) + b_price * price_1 + lambda * LV
V[['option_2']] = b_white * (white_2==0) + b_brown * (white_2==2) + b_organic * organic_2 + b_brand * brand_2 + b_lowGI * (health_2==1) + b_lowgluten * (health_2==2) + b_enhanced * (health_2==3) + b_price * price_2 + lambda * LV
V[['option_3']] = b_white * (white_3==0) + b_brown * (white_3==2) + b_organic * organic_3 + b_brand * brand_3 + b_lowGI * (health_3==1) + b_lowgluten * (health_3==2) + b_enhanced * (health_3==3) + b_price * price_3 + lambda * LV
V[['option_4']] = asc_non_purchase

### Define settings for MNL model component
mnl_settings = list(
alternatives = c('option_1'=1, 'option_2'=2, 'option_3'=3, 'option_4'=4),
avail = 1,
choiceVar = choice,
utilities = V,
componentName = "choice"
)

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

### Likelihood of the whole model
P = apollo_combineModels(P, apollo_inputs, functionality)

### Take product across observation for same individual
P = apollo_panelProd(P, apollo_inputs, functionality)

### Average across inter-individual draws
P = apollo_avgInterDraws(P, apollo_inputs, functionality)

### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}

# ################################################################# #
#### MODEL 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)

Could you please suggest what to do to solve the problem?

Best,
Xian

Re: Getting NAs in a hybrid model

Posted: 18 Sep 2023, 08:25
by stephanehess
Hi

can you show us the results? There could be many reasons.

Also, please note that your model only has 1 latent variable (LV). What you are referring to are the indicators, not the latent variables

Stephane

Re: Getting NAs in a hybrid model

Posted: 18 Sep 2023, 09:55
by zx9203
Dear Stephane,

I'm sorry that this post should be deleted because I have asked you the same question in another part of the forum.

Thank you pointing out my misunderstanding about latent variable.

Best,
Xian