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.

Error in model estimation for LC model with class allocation and covariates

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
dasham
Posts: 3
Joined: 20 May 2021, 14:38

Error in model estimation for LC model with class allocation and covariates

Post by dasham »

Hello,

I am running a model based on Example 20 (http://www.apollochoicemodelling.com/fi ... output.txt). When I estimate the model, however, I get an error: Error in if (any(round(classprobsum, 10) != 1)) stop("Class allocation probabilities in 'classProb' for model component \"", :
missing value where TRUE/FALSE needed

I am not so sure how to solve this and I have not found a similar post in this forum..I have the latest apollo package installed. This is the code I am using up to the post-processing:

# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
### Clear memory
rm(list = ls())

### Load Apollo library
library(apollo)


### Initialise code
apollo_initialise()

apollo_control = list(
modelName ="SHEDS_LC",
modelDescr ="LC model with class allocation model on SHEDS data and with covariates",
indivID ="id",
nCores = 3
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
##effects coding
library(dplyr)

data <- read.csv("/Users/mihail0000/Documents/HB mixed logit apollo/choice_apollo.csv",header=TRUE)

##alternative 1
## ref level is ownership L4
data <- mutate(data, ownershipL1.1 = ifelse(ownership1 == 0, 1, 0))
data <- mutate(data, ownershipL2.1 = ifelse(ownership1 == 1, 1, 0))
data <- mutate(data, ownershipL3.1 = ifelse(ownership1 == 2, 1, 0))
data <- mutate(data, ownershipL1.1 = ifelse(ownership1 == 3, -1, ownershipL1.1))
data <- mutate(data, ownershipL2.1 = ifelse(ownership1 == 3, -1, ownershipL2.1))
data <- mutate(data, ownershipL3.1 = ifelse(ownership1 == 3, -1, ownershipL3.1))

## ref level is mobility L2
data <- mutate(data, mobilityL1.1 = ifelse(mobility1 == 0, 1, 0))
data <- mutate(data, mobilityL1.1 = ifelse(mobility1 == 1, -1, mobilityL1.1))
data <- mutate(data, mobilityL3.1 = ifelse(mobility1 == 2, 1, 0))
data <- mutate(data, mobilityL3.1 = ifelse(mobility1 == 1, -1, mobilityL3.1))

## ref level is shared space L1
data <- mutate(data, sharedspaceL2.1 = ifelse(sharedspace1 == 0, -1, 1))

## alternative 2
## ref level is ownership L4
data <- mutate(data, ownershipL1.2 = ifelse(ownership2 == 0, 1, 0))
data <- mutate(data, ownershipL2.2 = ifelse(ownership2 == 1, 1, 0))
data <- mutate(data, ownershipL3.2 = ifelse(ownership2 == 2, 1, 0))
data <- mutate(data, ownershipL1.2 = ifelse(ownership2 == 3, -1, ownershipL1.2))
data <- mutate(data, ownershipL2.2 = ifelse(ownership2 == 3, -1, ownershipL2.2))
data <- mutate(data, ownershipL3.2 = ifelse(ownership2 == 3, -1, ownershipL3.2))

## ref level is mobility L2
data <- mutate(data, mobilityL1.2 = ifelse(mobility2 == 0, 1, 0))
data <- mutate(data, mobilityL1.2 = ifelse(mobility2 == 1, -1, mobilityL1.2))
data <- mutate(data, mobilityL3.2 = ifelse(mobility2 == 2, 1, 0))
data <- mutate(data, mobilityL3.2 = ifelse(mobility2 == 1, -1, mobilityL3.2))

## ref level is shared space L1
data <- mutate(data, sharedspaceL2.2 = ifelse(sharedspace2 == 0, -1, 1))


library("haven")
demos <- read_dta("/Users/mihail0000/Desktop/SHEDS data 0410.dta")
fullPED <- merge(data, demos, by="id", all.x=TRUE)

database = fullPED

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_1 = 0,
asc_2 = 0,
b_ownershipL1_a = 0,
b_ownershipL2_a = 0,
b_ownershipL3_a = 0,
b_mobilityL1_a = 0,
b_mobilityL3_a = 0,
b_sharedspaceL2_a = 0,
delta_a = 0,
gamma_city_a = 0,
gamma_linc_a = 0,
gamma_educ_a = 0,
gamma_sex_a = 0,
gamma_tenant_a = 0,
gamma_house_a = 0,
gamma_age_a = 0,
gamma_car_a = 0,
gamma_ger_a = 0,
gamma_household3_a = 0,
gamma_biospheric_a = 0,
gamma_altruistic_a = 0,
gamma_egoistic_a = 0,
gamma_hedonic_a = 0,
gamma_intentions_a = 0,
gamma_descrip_a = 0,
gamma_injunctive_a = 0,
gamma_coop_a = 0,
gamma_adopter_a = 0,
gamma_PVe_a = 0,
gamma_PVh_a = 0,
b_ownershipL1_b = 0,
b_ownershipL2_b = 0,
b_ownershipL3_b = 0,
b_mobilityL1_b = 0,
b_mobilityL3_b = 0,
b_sharedspaceL2_b = 0,
delta_b = 0,
gamma_city_b = 0,
gamma_linc_b = 0,
gamma_educ_b = 0,
gamma_sex_b = 0,
gamma_tenant_b = 0,
gamma_house_b = 0,
gamma_age_b = 0,
gamma_car_b = 0,
gamma_ger_b = 0,
gamma_household3_b = 0,
gamma_biospheric_b = 0,
gamma_altruistic_b = 0,
gamma_egoistic_b = 0,
gamma_hedonic_b = 0,
gamma_intentions_b = 0,
gamma_descrip_b = 0,
gamma_injunctive_b = 0,
gamma_coop_b = 0,
gamma_adopter_b = 0,
gamma_PVe_b = 0,
gamma_PVh_b = 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("asc_2", "gamma_city_b", "gamma_linc_b", "gamma_educ_b", "gamma_sex_b", "gamma_tenant_b", "gamma_house_b", "gamma_age_b", "gamma_car_b", "gamma_ger_b", "gamma_household3_b", "gamma_biospheric_b", "gamma_altruistic_b", "gamma_egoistic_b", "gamma_hedonic_b", "gamma_intentions_b", "gamma_descrip_b", "gamma_injunctive_b", "gamma_coop_b", "gamma_adopter_b", "gamma_PVe_b", "gamma_PVh_b")

# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS ####
# ################################################################# #

apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["beta_ownershipL1"]] = list(b_ownershipL1_a, b_ownershipL1_b)
lcpars[["beta_ownershipL2"]] = list(b_ownershipL2_a, b_ownershipL2_b)
lcpars[["beta_ownershipL3"]] = list(b_ownershipL3_a, b_ownershipL3_b)
lcpars[["beta_mobilityL1"]] = list(b_mobilityL1_a, b_mobilityL1_b)
lcpars[["beta_mobilityL3"]] = list(b_mobilityL3_a, b_mobilityL3_b)
lcpars[["beta_sharedspaceL2"]] = list(b_sharedspaceL2_a, b_sharedspaceL2_b)


V=list()
V[["class_a"]] = delta_a + gamma_city_a*city + gamma_linc_a*linc + gamma_educ_a*educyears + gamma_sex_a*sex + gamma_tenant_a*tenant + gamma_house_a*house + gamma_age_a*age_corr + gamma_car_a*car + gamma_ger_a*ger + gamma_household3_a*household_3plus + gamma_biospheric_a*biospheric_values + gamma_altruistic_a*altruistic_values +gamma_egoistic_a*egoistic_values + gamma_hedonic_a*hedonic_values + gamma_intentions_a*intentions_1 + gamma_descrip_a*descrip_norms + gamma_injunctive_a*injunctive + gamma_coop_a*ped_coop +gamma_adopter_a*ped_adopter + gamma_PVe_a*accomPVe + gamma_PVh_a*accomPVh
V[["class_b"]] = delta_b + gamma_city_b*city + gamma_linc_b*linc + gamma_educ_b*educyears + gamma_sex_b*sex + gamma_tenant_b*tenant + gamma_house_b*house + gamma_age_b*age_corr + gamma_car_b*car + gamma_ger_b*ger + gamma_household3_b*household_3plus + gamma_biospheric_b*biospheric_values + gamma_altruistic_b*altruistic_values +gamma_egoistic_b*egoistic_values + gamma_hedonic_b*hedonic_values + gamma_intentions_b*intentions_1 + gamma_descrip_b*descrip_norms + gamma_injunctive_b*injunctive + gamma_coop_b*ped_coop +gamma_adopter_b*ped_adopter + gamma_PVe_b*accomPVe + gamma_PVh_b*accomPVh

mnl_settings = list(
alternatives = c(class_a=1, class_b=2),
avail = 1,
choiceVar = NA,
V = V
)
lcpars[["pi_values"]] = apollo_mnl(mnl_settings, functionality="raw")

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

### Define settings for MNL model component that are generic across classes
mnl_settings = list(
alternatives = c(alt1=1, alt2=2),
avail = list(alt1=1, alt2=1),
choiceVar = choice
)

### Loop over classes
s=1
while(s<=2){

### Compute class-specific utilities
V=list()
V[['alt1']] = asc_1 + beta_ownershipL1[[s]]*ownershipL1.1 + beta_ownershipL2[[s]]*ownershipL2.1 + beta_ownershipL3[[s]]*ownershipL3.1 + beta_mobilityL1[[s]]*mobilityL1.1 + beta_mobilityL3[[s]]*mobilityL3.1+ beta_sharedspaceL2[[s]]*sharedspaceL2.1
V[['alt2']] = asc_2 + beta_ownershipL1[[s]]*ownershipL1.2 + beta_ownershipL2[[s]]*ownershipL2.2 + beta_ownershipL3[[s]]*ownershipL3.2 + beta_mobilityL1[[s]]*mobilityL1.2 + beta_mobilityL3[[s]]*mobilityL3.2+ beta_sharedspaceL2[[s]]*sharedspaceL2.2


mnl_settings$V = V
mnl_settings$componentName = paste0("Class_",s)

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

s=s+1}

### Compute latent class model probabilities
lc_settings = list(inClassProb = P, classProb=pi_values)
P[["model"]] = apollo_lc(lc_settings, apollo_inputs, functionality)

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


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

#apollo_beta=apollo_searchStart(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)
#apollo_outOfSample(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)

### Estimate model
model = apollo_estimate(apollo_beta, apollo_fixed,
apollo_probabilities, apollo_inputs,
estimate_settings=list(writeIter=FALSE))

### Show output in screen
apollo_modelOutput(model)

### Save output to file(s)
apollo_saveOutput(model)
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Error in model estimation for LC model with class allocation and covariates

Post by stephanehess »

Hi

if you could share your code and data with me via e-mail, then I'll look into this for you.

One (unrelated) thing is that your current specification is overspecified. You cannot estimate both delta_a and delta_b

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
dasham
Posts: 3
Joined: 20 May 2021, 14:38

Re: Error in model estimation for LC model with class allocation and covariates

Post by dasham »

Hi Stephane,

Thanks, I have sent it through via email.

Dasha
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Error in model estimation for LC model with class allocation and covariates

Post by stephanehess »

The problem happens as some of the variables you use in the class allocation model have some entries equal to NA. Please have a look e.g. at linc.

But we will improve the error message
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply