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 if (!test) { : missing value where TRUE/FALSE needed when adding workInLogs=TRUE in apollo_control

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
Nishihara
Posts: 9
Joined: 19 May 2023, 02:14

Error in if (!test) { : missing value where TRUE/FALSE needed when adding workInLogs=TRUE in apollo_control

Post by Nishihara »

Dear Professor,

I have been keeping having NaN for Standard Errors, so I am trying to add workInLogs=TRUE in apollo_control to see if the issue can be solved.

However, once I add workInLogs=TRUE in apollo_control, I got the error "Error in if (!test) { : missing value where TRUE/FALSE needed" right after the model estimation.

Below is my code and could you please let me know what is the reason for the error?

Code: Select all

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

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "Bangladesh CE",
  modelDescr      = "Mixed logit model on Bangladesh CE data",
  indivID         = "ID",  
  mixing          = TRUE,
  workInLogs = TRUE,
  nCores          = 15,
  outputDirectory = "~/OneDrive - The University of Tokyo/WorldFish/Bangladesh characterization study/DCE"
)


des1 <- rotation.design(attribute.names = list(Efficiency = c ("L0", "L1_breeding", "L2_breeding", "L3_breeding"), Productivity = c ("L0", "L1_productivity", "L2_productivity", "L3_productivity"), Productivity_characteristics = c ("L0", "L1_traits", "L2_traits", "L3_traits"), Pond_management = c ("L0", "L1_management", "L2_management", "L3_management"), Fingerling_cost = c ("500", "1000", "1500", "2000")), nalternatives = 2, nblocks = 1, row.renames = FALSE, randomize = TRUE, seed =6965)

des2 <- rotation.design(attribute.names = list(Efficiency = c ("0", "1", "2", "3"), Productivity = c ("0", "1", "2", "3"), Productivity_characteristics = c ("0", "1", "2", "3"), Pond_management = c ("0", "1", "2", "3"), Fingerling_cost = c ("500", "1000", "1500", "2000")), nalternatives = 2, nblocks = 1, row.renames = FALSE, randomize = TRUE, seed =6965)

alt <- des2$alternatives  
alt1 <- alt$alt.1 
# Add "1" after variables' name
names(alt1)[names(alt1) == "Efficiency"] <- paste0("Efficiency1")
names(alt1)[names(alt1) == "Productivity"] <- paste0("Productivity1")
names(alt1)[names(alt1) == "Productivity_characteristics"] <- paste0("Productivity_characteristics1")
names(alt1)[names(alt1) == "Pond_management"] <- paste0("Pond_management1")
names(alt1)[names(alt1) == "Fingerling_cost"] <- paste0("Fingerling_cost1")

alt2 <- alt$alt.2
names(alt2)[names(alt2) == "Efficiency"] <- paste0("Efficiency2")
names(alt2)[names(alt2) == "Productivity"] <- paste0("Productivity2")
names(alt2)[names(alt2) == "Productivity_characteristics"] <- paste0("Productivity_characteristics2")
names(alt2)[names(alt2) == "Pond_management"] <- paste0("Pond_management2")
names(alt2)[names(alt2) == "Fingerling_cost"] <- paste0("Fingerling_cost2")

alt.all <- cbind(alt1,alt2)

alt.all <- alt.all[,-c(1,3,9:11)]


input1=read.csv("60.csv")

num <- length(unique(input1$ID))
alt.all.expand <- alt.all[rep(seq_len(nrow(alt.all)), times = num), ]

desmat1 <- make.design.matrix(choice.experiment.design = des1, optout = TRUE, categorical.attributes = c("Efficiency", "Productivity", "Productivity_characteristics", "Pond_management"), continuous.attributes = c("Fingerling_cost"), unlabeled = TRUE)

dataset <- make.dataset(respondent.dataset = input1, choice.indicators = c("q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15", "q16"), design.matrix = desmat1)
choice <- dataset[,c(1,3:5)]
choice <- subset(choice,RES=="TRUE")

database <- cbind(choice,alt.all.expand)
database$Fingerling_cost1 <- as.numeric(as.character(database$Fingerling_cost1))
database$Fingerling_cost2 <- as.numeric(as.character(database$Fingerling_cost2))


### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc = 0,
                sigma_asc = 0,
                mu_Efficiency_L0    = 0,
                sigma_Efficiency_L0 = 0,
                mu_Efficiency_L1    = 0,
                sigma_Efficiency_L1 = 0,
                mu_Efficiency_L2    = 0,
                sigma_Efficiency_L2 = 0,
                mu_Efficiency_L3    = 0,
                sigma_Efficiency_L3 = 0,
                mu_Productivity_L0    = 0,
                sigma_Productivity_L0 = 0,
                mu_Productivity_L1    = 0,
                sigma_Productivity_L1 = 0, 
                mu_Productivity_L2    = 0,
                sigma_Productivity_L2 = 0, 
                mu_Productivity_L3    = 0,
                sigma_Productivity_L3 = 0, 
                mu_Productivity_characteristics_L0    = 0,
                sigma_Productivity_characteristics_L0 = 0,
                mu_Productivity_characteristics_L1    = 0,
                sigma_Productivity_characteristics_L1 = 0,
                mu_Productivity_characteristics_L2    = 0,
                sigma_Productivity_characteristics_L2 = 0,
                mu_Productivity_characteristics_L3    = 0,
                sigma_Productivity_characteristics_L3 = 0,
                mu_Pond_management_L0    = 0,
                sigma_Pond_management_L0 = 0,
                mu_Pond_management_L1    = 0,
                sigma_Pond_management_L1 = 0,
                mu_Pond_management_L2    = 0,
                sigma_Pond_management_L2 = 0,
                mu_Pond_management_L3    = 0,
                sigma_Pond_management_L3 = 0,
                mu_log_Fingerling_cost    = -3,
                sigma_log_Fingerling_cost = 1
                )

### 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 = "mlhs",
  interNDraws    = 500,
  interUnifDraws = c(),
  interNormDraws = c("draws_Efficiency_L0","draws_Efficiency_L1","draws_Efficiency_L2","draws_Efficiency_L3","draws_Productivity_L0","draws_Productivity_L1","draws_Productivity_L2","draws_Productivity_L3","draws_Productivity_characteristics_L0","draws_Productivity_characteristics_L1","draws_Productivity_characteristics_L2","draws_Productivity_characteristics_L3","draws_Pond_management_L0","draws_Pond_management_L1","draws_Pond_management_L2","draws_Pond_management_L3","draws_Fingerling_cost","draws_asc"),
  intraDrawsType = "mlhs",
  intraNDraws    = 0,
  intraUnifDraws = c(),
  intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  randcoeff[["b_Efficiency_L0"]] =mu_Efficiency_L0 + sigma_Efficiency_L0 * draws_Efficiency_L0 
  randcoeff[["b_Efficiency_L1"]] =mu_Efficiency_L1 + sigma_Efficiency_L1 * draws_Efficiency_L1 
  randcoeff[["b_Efficiency_L2"]] =mu_Efficiency_L2 + sigma_Efficiency_L2 * draws_Efficiency_L2 
  randcoeff[["b_Efficiency_L3"]] =mu_Efficiency_L3 + sigma_Efficiency_L3 * draws_Efficiency_L3 
  randcoeff[["b_Productivity_L0"]] =  mu_Productivity_L0 + sigma_Productivity_L0 * draws_Productivity_L0
  randcoeff[["b_Productivity_L1"]] =  mu_Productivity_L1 + sigma_Productivity_L1 * draws_Productivity_L1
  randcoeff[["b_Productivity_L2"]] =  mu_Productivity_L2 + sigma_Productivity_L2 * draws_Productivity_L2
  randcoeff[["b_Productivity_L3"]] =  mu_Productivity_L3 + sigma_Productivity_L3 * draws_Productivity_L3
  randcoeff[["b_Pond_management_L0"]] =  mu_Pond_management_L0 + sigma_Pond_management_L0 * draws_Pond_management_L0 
  randcoeff[["b_Pond_management_L1"]] =  mu_Pond_management_L1 + sigma_Pond_management_L1 * draws_Pond_management_L1 
  randcoeff[["b_Pond_management_L2"]] =  mu_Pond_management_L2 + sigma_Pond_management_L2 * draws_Pond_management_L2 
  randcoeff[["b_Pond_management_L3"]] =  mu_Pond_management_L3 + sigma_Pond_management_L3 * draws_Pond_management_L3 
  randcoeff[["b_Productivity_characteristics_L0"]] =  mu_Productivity_characteristics_L0 + sigma_Productivity_characteristics_L0 * draws_Productivity_characteristics_L0 
  randcoeff[["b_Productivity_characteristics_L1"]] =  mu_Productivity_characteristics_L1 + sigma_Productivity_characteristics_L1 * draws_Productivity_characteristics_L1 
  randcoeff[["b_Productivity_characteristics_L2"]] =  mu_Productivity_characteristics_L2 + sigma_Productivity_characteristics_L2 * draws_Productivity_characteristics_L2 
  randcoeff[["b_Productivity_characteristics_L3"]] =  mu_Productivity_characteristics_L3 + sigma_Productivity_characteristics_L3 * draws_Productivity_characteristics_L3 
  randcoeff[["b_Fingerling_cost"]] =  -exp(mu_log_Fingerling_cost + sigma_log_Fingerling_cost * draws_Fingerling_cost) 
  randcoeff[["ec"]] =  sigma_asc * draws_asc
  
  return(randcoeff)
}

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

apollo_inputs = apollo_validateInputs()

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

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
  
  ### Function initialisation: do not ange the following three commands
  ### AareaaFingerling_cost inputs and detaFingerling_cost 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_seareaings, order is irrelevant
  V = list()
  V[['NB']]  = asc + ec
  V[['alt1']]  = b_Efficiency_L0 * (Efficiency1==0) + b_Efficiency_L1 * (Efficiency1==1) + b_Efficiency_L2 * (Efficiency1==2) +b_Efficiency_L3 * (Efficiency1==3) + 
    b_Productivity_L0 * (Productivity1==0)+b_Productivity_L1 * (Productivity1==1) +  b_Productivity_L2 * (Productivity1==2)+ b_Productivity_L3 * (Productivity1==3) + 
    b_Pond_management_L0 * (Pond_management1==0) +b_Pond_management_L1 * (Pond_management1==1) + b_Pond_management_L2 * (Pond_management1==2)+b_Pond_management_L3 * (Pond_management1==3) + 
    b_Productivity_characteristics_L0 * (Productivity_characteristics1==0) +b_Productivity_characteristics_L1 * (Productivity_characteristics1==1) + b_Productivity_characteristics_L2 * (Productivity_characteristics1==2)+b_Productivity_characteristics_L3 * (Productivity_characteristics1==3) + 
    b_Fingerling_cost * Fingerling_cost1 
  V[['alt2']]  = b_Efficiency_L0 * (Efficiency2==0) + b_Efficiency_L1 * (Efficiency2==1) + b_Efficiency_L2 * (Efficiency2==2) +b_Efficiency_L3 * (Efficiency2==3) + 
    b_Productivity_L0 * (Productivity2==0) + b_Productivity_L1 * (Productivity2==1) +  b_Productivity_L2 * (Productivity2==2)+ b_Productivity_L3 * (Productivity2==3) + 
    b_Pond_management_L0 * (Pond_management2==0) +  b_Pond_management_L1 * (Pond_management2==1) + b_Pond_management_L2 * (Pond_management2==2)+b_Pond_management_L3 * (Pond_management2==3) + 
    b_Productivity_characteristics_L0 * (Productivity_characteristics2==0) +b_Productivity_characteristics_L1 * (Productivity_characteristics2==1) + b_Productivity_characteristics_L2 * (Productivity_characteristics2==2)+b_Productivity_characteristics_L3 * (Productivity_characteristics2==3) + 
    b_Fingerling_cost * Fingerling_cost2
  
  
  ### Define seareaings for MNL model component
  mnl_seareaings = list(
    alternatives  = c(alt1="1", alt2="2", NB="3"),
    avail         = list(alt1=1, alt2=1, NB=1),
    choiceVar     = ALT,
    utilities     = V
  )
  
  ### Compute probabilities using MNL model
  P[["model"]] = apollo_mnl(mnl_seareaings, 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                                            ####
# ################################################################# #


model = apollo_estimate(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs, estimate_settings = list(bootstrapSE=5))
Thank you very much in advance for you kind help.
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: Error in if (!test) { : missing value where TRUE/FALSE needed when adding workInLogs=TRUE in apollo_control

Post by dpalma »

Hi,

I cannot see any syntax mistake in your code, but I am under the impression that you are over-specifying your model. Remember that for categorical variables with L levels, you need L-1 coefficients. For example, if attribute efficiency hast 4 levels (0, 1, 2, and 3), then you should only include coefficients (betas) for levels 1, 2, 3 (or any other three levels), so that the excluded level becomes the base.

You might also want to read
  • Walker, J. (2002) Mixed logit (or logit kernel) model: Dispelling misconceptions of identification. Transportation Research Record 1805.
Where the author discusses the identification of random-parameter mixed logits models with categorical variables.

As a general recommendation, I would strongly advice you start your modelling with a simple MNL, and only when you are happy with your MNL model, you move to a mixed logit. Otherwise, it is much more difficult to diagnose the source of any issue.

Best wishes
David
Nishihara
Posts: 9
Joined: 19 May 2023, 02:14

Re: Error in if (!test) { : missing value where TRUE/FALSE needed when adding workInLogs=TRUE in apollo_control

Post by Nishihara »

Hi David,

Thank you very much for your help and suggestions. The model worked after I excluded coefficients with L0 in each attribute, but still got the same error when I tried to add workInLogs=TRUE. Since there is no more NaN in standard error, I do not add this in the pollo_control.

However, I have a new issue coming out when I used a subset of the database:
WARNING: Singular Hessian, cannot calculate s.e.
Could not write hessian to a file.
WARNING: Some eigenvalues of the Hessian are complex, indicating that the Hessian is not symmetrical.


I do not understand why the issue happen because I have run various subsets of the whole database and most of them worked well and the results were fine. Could you please let me know what is the issue behind the error?

Please see the code:

Code: Select all

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

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "Bangladesh CE",
  modelDescr      = "Mixed logit model on Bangladesh CE data",
  indivID         = "ID",  
  mixing          = TRUE,
 # workInLogs = TRUE,
  nCores          = 15,
  outputDirectory = "~/OneDrive - The University of Tokyo/WorldFish/Bangladesh characterization study/DCE"
)


des1 <- rotation.design(attribute.names = list(Efficiency = c ("L0", "L1_breeding", "L2_breeding", "L3_breeding"), Productivity = c ("L0", "L1_productivity", "L2_productivity", "L3_productivity"), Productivity_characteristics = c ("L0", "L1_traits", "L2_traits", "L3_traits"), Pond_management = c ("L0", "L1_management", "L2_management", "L3_management"), Fingerling_cost = c ("500", "1000", "1500", "2000")), nalternatives = 2, nblocks = 1, row.renames = FALSE, randomize = TRUE, seed =6965)

des2 <- rotation.design(attribute.names = list(Efficiency = c ("0", "1", "2", "3"), Productivity = c ("0", "1", "2", "3"), Productivity_characteristics = c ("0", "1", "2", "3"), Pond_management = c ("0", "1", "2", "3"), Fingerling_cost = c ("500", "1000", "1500", "2000")), nalternatives = 2, nblocks = 1, row.renames = FALSE, randomize = TRUE, seed =6965)

alt <- des2$alternatives  
alt1 <- alt$alt.1 
# Add "1" after variables' name
names(alt1)[names(alt1) == "Efficiency"] <- paste0("Efficiency1")
names(alt1)[names(alt1) == "Productivity"] <- paste0("Productivity1")
names(alt1)[names(alt1) == "Productivity_characteristics"] <- paste0("Productivity_characteristics1")
names(alt1)[names(alt1) == "Pond_management"] <- paste0("Pond_management1")
names(alt1)[names(alt1) == "Fingerling_cost"] <- paste0("Fingerling_cost1")

alt2 <- alt$alt.2
names(alt2)[names(alt2) == "Efficiency"] <- paste0("Efficiency2")
names(alt2)[names(alt2) == "Productivity"] <- paste0("Productivity2")
names(alt2)[names(alt2) == "Productivity_characteristics"] <- paste0("Productivity_characteristics2")
names(alt2)[names(alt2) == "Pond_management"] <- paste0("Pond_management2")
names(alt2)[names(alt2) == "Fingerling_cost"] <- paste0("Fingerling_cost2")

alt.all <- cbind(alt1,alt2)

alt.all <- alt.all[,-c(1,3,9:11)]


input1=read.csv("80.csv")

num <- length(unique(input1$ID))
alt.all.expand <- alt.all[rep(seq_len(nrow(alt.all)), times = num), ]

desmat1 <- make.design.matrix(choice.experiment.design = des1, optout = TRUE, categorical.attributes = c("Efficiency", "Productivity", "Productivity_characteristics", "Pond_management"), continuous.attributes = c("Fingerling_cost"), unlabeled = TRUE)

dataset <- make.dataset(respondent.dataset = input1, choice.indicators = c("q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15", "q16"), design.matrix = desmat1)
choice <- dataset[,c(1,3:5)]
choice <- subset(choice,RES=="TRUE")

database <- cbind(choice,alt.all.expand)
database$Fingerling_cost1 <- as.numeric(as.character(database$Fingerling_cost1))
database$Fingerling_cost2 <- as.numeric(as.character(database$Fingerling_cost2))


### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc = 0,
                sigma_asc = 0,
                #mu_Efficiency_L0    = 0,
                #sigma_Efficiency_L0 = 0,
                mu_Efficiency_L1    = 0,
                sigma_Efficiency_L1 = 0,
                mu_Efficiency_L2    = 0,
                sigma_Efficiency_L2 = 0,
                mu_Efficiency_L3    = 0,
                sigma_Efficiency_L3 = 0,
                #mu_Productivity_L0    = 0,
                #sigma_Productivity_L0 = 0,
                mu_Productivity_L1    = 0,
                sigma_Productivity_L1 = 0, 
                mu_Productivity_L2    = 0,
                sigma_Productivity_L2 = 0, 
                mu_Productivity_L3    = 0,
                sigma_Productivity_L3 = 0, 
                #mu_Productivity_characteristics_L0    = 0,
               #sigma_Productivity_characteristics_L0 = 0,
                mu_Productivity_characteristics_L1    = 0,
                sigma_Productivity_characteristics_L1 = 0,
                mu_Productivity_characteristics_L2    = 0,
                sigma_Productivity_characteristics_L2 = 0,
                mu_Productivity_characteristics_L3    = 0,
                sigma_Productivity_characteristics_L3 = 0,
                #mu_Pond_management_L0    = 0,
                #sigma_Pond_management_L0 = 0,
                mu_Pond_management_L1    = 0,
                sigma_Pond_management_L1 = 0,
                mu_Pond_management_L2    = 0,
                sigma_Pond_management_L2 = 0,
                mu_Pond_management_L3    = 0,
                sigma_Pond_management_L3 = 0,
                mu_log_Fingerling_cost    = -10,
                sigma_log_Fingerling_cost = 1
                )

### 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 = "mlhs",
  interNDraws    = 500,
  interUnifDraws = c(),
  interNormDraws = c("draws_Efficiency_L0","draws_Efficiency_L1","draws_Efficiency_L2","draws_Efficiency_L3","draws_Productivity_L0","draws_Productivity_L1","draws_Productivity_L2","draws_Productivity_L3","draws_Productivity_characteristics_L0","draws_Productivity_characteristics_L1","draws_Productivity_characteristics_L2","draws_Productivity_characteristics_L3","draws_Pond_management_L0","draws_Pond_management_L1","draws_Pond_management_L2","draws_Pond_management_L3","draws_Fingerling_cost","draws_asc"),
  intraDrawsType = "mlhs",
  intraNDraws    = 0,
  intraUnifDraws = c(),
  intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
 # randcoeff[["b_Efficiency_L0"]] =mu_Efficiency_L0 + sigma_Efficiency_L0 * draws_Efficiency_L0 
  randcoeff[["b_Efficiency_L1"]] =mu_Efficiency_L1 + sigma_Efficiency_L1 * draws_Efficiency_L1 
  randcoeff[["b_Efficiency_L2"]] =mu_Efficiency_L2 + sigma_Efficiency_L2 * draws_Efficiency_L2 
  randcoeff[["b_Efficiency_L3"]] =mu_Efficiency_L3 + sigma_Efficiency_L3 * draws_Efficiency_L3 
#  randcoeff[["b_Productivity_L0"]] =  mu_Productivity_L0 + sigma_Productivity_L0 * draws_Productivity_L0
  randcoeff[["b_Productivity_L1"]] =  mu_Productivity_L1 + sigma_Productivity_L1 * draws_Productivity_L1
  randcoeff[["b_Productivity_L2"]] =  mu_Productivity_L2 + sigma_Productivity_L2 * draws_Productivity_L2
  randcoeff[["b_Productivity_L3"]] =  mu_Productivity_L3 + sigma_Productivity_L3 * draws_Productivity_L3
#  randcoeff[["b_Pond_management_L0"]] =  mu_Pond_management_L0 + sigma_Pond_management_L0 * draws_Pond_management_L0 
  randcoeff[["b_Pond_management_L1"]] =  mu_Pond_management_L1 + sigma_Pond_management_L1 * draws_Pond_management_L1 
  randcoeff[["b_Pond_management_L2"]] =  mu_Pond_management_L2 + sigma_Pond_management_L2 * draws_Pond_management_L2 
  randcoeff[["b_Pond_management_L3"]] =  mu_Pond_management_L3 + sigma_Pond_management_L3 * draws_Pond_management_L3 
#  randcoeff[["b_Productivity_characteristics_L0"]] =  mu_Productivity_characteristics_L0 + sigma_Productivity_characteristics_L0 * draws_Productivity_characteristics_L0 
  randcoeff[["b_Productivity_characteristics_L1"]] =  mu_Productivity_characteristics_L1 + sigma_Productivity_characteristics_L1 * draws_Productivity_characteristics_L1 
  randcoeff[["b_Productivity_characteristics_L2"]] =  mu_Productivity_characteristics_L2 + sigma_Productivity_characteristics_L2 * draws_Productivity_characteristics_L2 
  randcoeff[["b_Productivity_characteristics_L3"]] =  mu_Productivity_characteristics_L3 + sigma_Productivity_characteristics_L3 * draws_Productivity_characteristics_L3 
  randcoeff[["b_Fingerling_cost"]] =  -exp(mu_log_Fingerling_cost + sigma_log_Fingerling_cost * draws_Fingerling_cost) 
  randcoeff[["ec"]] =  sigma_asc * draws_asc
  
  return(randcoeff)
}

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

apollo_inputs = apollo_validateInputs()

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

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
  
  ### Function initialisation: do not ange the following three commands
  ### AareaaFingerling_cost inputs and detaFingerling_cost 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_seareaings, order is irrelevant
  V = list()
  V[['NB']]  = asc + ec
  V[['alt1']]  = #b_Efficiency_L0 * (Efficiency1==0) + 
    b_Efficiency_L1 * (Efficiency1==1) + b_Efficiency_L2 * (Efficiency1==2) +b_Efficiency_L3 * (Efficiency1==3) + 
    #b_Productivity_L0 * (Productivity1==0)+
    b_Productivity_L1 * (Productivity1==1) +  b_Productivity_L2 * (Productivity1==2)+ b_Productivity_L3 * (Productivity1==3) + 
    #b_Pond_management_L0 * (Pond_management1==0) +
    b_Pond_management_L1 * (Pond_management1==1) + b_Pond_management_L2 * (Pond_management1==2)+b_Pond_management_L3 * (Pond_management1==3) + 
    #b_Productivity_characteristics_L0 * 
    (Productivity_characteristics1==0) +b_Productivity_characteristics_L1 * (Productivity_characteristics1==1) + b_Productivity_characteristics_L2 * (Productivity_characteristics1==2)+b_Productivity_characteristics_L3 * (Productivity_characteristics1==3) + 
    b_Fingerling_cost * Fingerling_cost1 
  V[['alt2']]  = #b_Efficiency_L0 * (Efficiency2==0) + 
    b_Efficiency_L1 * (Efficiency2==1) + b_Efficiency_L2 * (Efficiency2==2) +b_Efficiency_L3 * (Efficiency2==3) + 
    #b_Productivity_L0 * (Productivity2==0) + 
    b_Productivity_L1 * (Productivity2==1) +  b_Productivity_L2 * (Productivity2==2)+ b_Productivity_L3 * (Productivity2==3) + 
    #b_Pond_management_L0 * (Pond_management2==0) + 
    b_Pond_management_L1 * (Pond_management2==1) + b_Pond_management_L2 * (Pond_management2==2)+b_Pond_management_L3 * (Pond_management2==3) + 
    #b_Productivity_characteristics_L0 * (Productivity_characteristics2==0) +
    b_Productivity_characteristics_L1 * (Productivity_characteristics2==1) + b_Productivity_characteristics_L2 * (Productivity_characteristics2==2)+b_Productivity_characteristics_L3 * (Productivity_characteristics2==3) + 
    b_Fingerling_cost * Fingerling_cost2
  
  
  ### Define seareaings for MNL model component
  mnl_seareaings = list(
    alternatives  = c(alt1="1", alt2="2", NB="3"),
    avail         = list(alt1=1, alt2=1, NB=1),
    choiceVar     = ALT,
    utilities     = V
  )
  
  ### Compute probabilities using MNL model
  P[["model"]] = apollo_mnl(mnl_seareaings, 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                                            ####
# ################################################################# #


model = apollo_estimate(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs, estimate_settings = list(maxIterations=500))

# ################################################################# #
#### MODEL OUTPUTS                                               ####
# ################################################################# #

# ----------------------------------------------------------------- #
#---- FORMAareaED OUTPUT (TO SCREEN)                               ----
# ----------------------------------------------------------------- #

#apollo_loadModel("Bangladesh CE")

apollo_modelOutput(model, list(printPVal=2))
The analysis process:

Code: Select all

Preparing user-defined functions.

Testing likelihood function...
WARNING: Some alternatives are never chosen in your data! 

Overview of choices for MNL model component :
                                    alt1    alt2   NB
Times available                  2848.00 2848.00 2848
Times chosen                     1363.00 1485.00    0
Percentage chosen overall          47.86   52.14    0
Percentage chosen when available   47.86   52.14    0


Pre-processing likelihood function...
Creating cluster...
Preparing workers for multithreading...

Testing influence of parameters
Starting main estimation
Initial function value: -3172.244 
Initial gradient value:
                                  asc                             sigma_asc                      mu_Efficiency_L1 
                        -898.12464673                            0.60247464                          247.82390458 
                  sigma_Efficiency_L1                      mu_Efficiency_L2                   sigma_Efficiency_L2 
                           0.40296422                          205.51154774                           -0.12951902 
                     mu_Efficiency_L3                   sigma_Efficiency_L3                    mu_Productivity_L1 
                         261.14084146                            0.08599178                          181.96102404 
                sigma_Productivity_L1                    mu_Productivity_L2                 sigma_Productivity_L2 
                           0.31548968                          246.50811170                           -0.92025085 
                   mu_Productivity_L3                 sigma_Productivity_L3    mu_Productivity_characteristics_L1 
                         241.66953891                           -0.18763944                          249.81547187 
sigma_Productivity_characteristics_L1    mu_Productivity_characteristics_L2 sigma_Productivity_characteristics_L2 
                           0.31394483                          271.27301106                            0.16333415 
   mu_Productivity_characteristics_L3 sigma_Productivity_characteristics_L3                 mu_Pond_management_L1 
                         288.08167235                           -0.42207228                          135.51665475 
             sigma_Pond_management_L1                 mu_Pond_management_L2              sigma_Pond_management_L2 
                          -0.57343394                          278.36027352                            0.35072962 
                mu_Pond_management_L3              sigma_Pond_management_L3                mu_log_Fingerling_cost 
                         404.95631725                            0.10745842                          -58.47532263 
            sigma_log_Fingerling_cost 
                         -19.30127185 
initial  value 3172.243768 
iter   2 value 2093.974250
iter   3 value 2064.029718
iter   4 value 2052.527634
iter   5 value 2036.289079
iter   6 value 2025.199930
iter   7 value 2018.120940
iter   8 value 2014.950515
iter   9 value 2013.220728
iter  10 value 2006.239214
iter  11 value 2005.786043
iter  12 value 2003.023501
iter  13 value 1996.532479
iter  14 value 1989.203864
iter  15 value 1973.578307
iter  16 value 1969.908518
iter  17 value 1959.489245
iter  18 value 1954.016367
iter  19 value 1942.816514
iter  20 value 1921.091618
iter  21 value 1902.404140
iter  22 value 1900.247512
iter  23 value 1895.706176
iter  24 value 1883.707908
iter  25 value 1881.739804
iter  26 value 1880.472173
iter  27 value 1875.427722
iter  28 value 1868.623212
iter  29 value 1867.316408
iter  30 value 1866.090265
iter  31 value 1863.196618
iter  32 value 1862.156467
iter  33 value 1859.046071
iter  34 value 1854.577735
iter  35 value 1850.949785
iter  36 value 1848.193785
iter  37 value 1845.326027
iter  38 value 1840.500518
iter  39 value 1837.271612
iter  40 value 1834.694981
iter  41 value 1833.990316
iter  42 value 1832.411691
iter  43 value 1831.402344
iter  44 value 1829.231663
iter  45 value 1828.015831
iter  46 value 1826.423753
iter  47 value 1825.531542
iter  48 value 1824.486251
iter  49 value 1823.918253
iter  50 value 1823.564910
iter  51 value 1823.343887
iter  52 value 1822.846944
iter  53 value 1822.406672
iter  54 value 1822.319895
iter  55 value 1822.083120
iter  56 value 1822.014738
iter  57 value 1821.984485
iter  58 value 1821.967490
iter  59 value 1821.624354
iter  60 value 1821.470220
iter  61 value 1821.441009
iter  62 value 1821.386830
iter  63 value 1821.318234
iter  64 value 1821.294613
iter  65 value 1821.272409
iter  66 value 1821.231710
iter  67 value 1821.167521
iter  68 value 1821.105906
iter  69 value 1820.931594
iter  70 value 1820.781755
iter  71 value 1820.674282
iter  72 value 1820.626664
iter  73 value 1820.561573
iter  74 value 1820.440204
iter  75 value 1820.273141
iter  76 value 1820.166582
iter  77 value 1820.116583
iter  78 value 1820.079058
iter  79 value 1820.053555
iter  80 value 1820.010706
iter  81 value 1819.982624
iter  82 value 1819.954037
iter  83 value 1819.939179
iter  84 value 1819.900237
iter  85 value 1819.882585
iter  86 value 1819.876970
iter  87 value 1819.869801
iter  88 value 1819.835026
iter  89 value 1819.810516
iter  90 value 1819.619762
iter  91 value 1819.056053
iter  92 value 1817.505934
iter  93 value 1817.420536
iter  94 value 1817.289469
iter  95 value 1817.196616
iter  96 value 1817.158202
iter  97 value 1817.140572
iter  98 value 1817.131762
iter  99 value 1817.123660
iter 100 value 1817.120057
iter 101 value 1817.116867
iter 102 value 1817.112643
iter 103 value 1817.105182
iter 104 value 1817.085732
iter 105 value 1817.062784
iter 106 value 1817.051040
iter 107 value 1817.045395
iter 108 value 1817.037677
iter 109 value 1817.031578
iter 110 value 1817.020779
iter 111 value 1816.997341
iter 112 value 1816.969359
iter 113 value 1816.926489
iter 114 value 1816.922175
iter 115 value 1816.905094
iter 116 value 1816.868209
iter 117 value 1816.714437
iter 118 value 1816.581554
iter 119 value 1816.512863
iter 120 value 1816.299641
iter 121 value 1816.162376
iter 122 value 1814.044826
iter 123 value 1803.748500
iter 124 value 1792.758986
iter 125 value 1779.978504
iter 126 value 1777.705829
iter 127 value 1770.919361
iter 128 value 1755.215027
iter 129 value 1740.409094
iter 130 value 1723.946344
iter 131 value 1713.703888
iter 132 value 1711.799731
iter 133 value 1711.765939
iter 134 value 1711.623548
iter 135 value 1711.620873
iter 136 value 1711.600764
iter 137 value 1711.572177
iter 138 value 1711.531603
iter 139 value 1710.868337
iter 140 value 1710.620717
iter 141 value 1710.219927
iter 142 value 1707.459119
iter 143 value 1707.321648
iter 144 value 1707.071490
iter 145 value 1706.861952
iter 146 value 1706.765362
iter 147 value 1706.732992
iter 148 value 1706.706123
iter 149 value 1706.679349
iter 150 value 1706.651748
iter 151 value 1706.611720
iter 152 value 1706.578541
iter 153 value 1706.494319
iter 154 value 1706.310475
iter 155 value 1706.196726
iter 156 value 1706.125288
iter 157 value 1706.103003
iter 158 value 1706.045850
iter 159 value 1706.009130
iter 160 value 1705.994551
iter 161 value 1705.943534
iter 162 value 1705.928337
iter 163 value 1705.909026
iter 164 value 1705.897255
iter 165 value 1705.874148
iter 166 value 1705.859953
iter 167 value 1705.842801
iter 168 value 1705.828142
iter 169 value 1705.824926
iter 170 value 1705.815029
iter 171 value 1705.811804
iter 172 value 1705.805510
iter 173 value 1705.794620
iter 174 value 1705.785569
iter 175 value 1705.782580
iter 176 value 1705.776762
iter 177 value 1705.758518
iter 178 value 1705.731494
iter 179 value 1705.612077
iter 180 value 1705.348048
iter 181 value 1705.216809
iter 182 value 1705.186961
iter 183 value 1704.951652
iter 184 value 1704.951439
iter 185 value 1704.950870
iter 186 value 1704.949595
iter 187 value 1704.948571
iter 188 value 1704.948272
iter 189 value 1704.948068
iter 190 value 1704.947869
iter 191 value 1704.947593
iter 192 value 1704.947278
iter 193 value 1704.947087
iter 194 value 1704.947061
iter 195 value 1704.946993
iter 196 value 1704.946933
iter 197 value 1704.946862
iter 198 value 1704.946741
iter 199 value 1704.946586
iter 200 value 1704.946533
iter 201 value 1704.946346
iter 202 value 1704.946229
iter 203 value 1704.946181
iter 204 value 1704.946145
iter 205 value 1704.946108
iter 206 value 1704.945937
iter 207 value 1704.945685
iter 208 value 1704.945608
iter 209 value 1704.945581
iter 210 value 1704.944753
iter 211 value 1704.942777
iter 212 value 1704.939806
iter 213 value 1704.925269
iter 214 value 1704.923824
iter 215 value 1704.920974
iter 216 value 1704.918968
iter 217 value 1704.909896
iter 218 value 1704.881301
iter 219 value 1704.881169
iter 220 value 1704.849388
iter 221 value 1704.835008
iter 222 value 1704.829182
iter 223 value 1704.822520
iter 224 value 1704.817389
iter 225 value 1704.814877
iter 226 value 1704.813608
iter 227 value 1704.812758
iter 228 value 1704.812310
iter 229 value 1704.811832
iter 230 value 1704.811448
iter 231 value 1704.810991
iter 232 value 1704.810500
iter 233 value 1704.808539
iter 234 value 1704.804676
iter 235 value 1704.803566
iter 236 value 1704.802056
iter 237 value 1704.801899
iter 238 value 1704.800854
iter 239 value 1704.800322
iter 240 value 1704.798835
iter 241 value 1704.797873
iter 242 value 1704.796211
iter 243 value 1704.793244
iter 244 value 1704.792806
iter 245 value 1704.792311
iter 246 value 1704.789504
iter 247 value 1704.786745
iter 248 value 1704.785815
iter 249 value 1704.781324
iter 250 value 1704.779973
iter 251 value 1704.753716
iter 252 value 1704.733305
iter 253 value 1704.668375
iter 254 value 1704.578055
iter 255 value 1704.535634
iter 256 value 1704.520568
iter 257 value 1704.503679
iter 258 value 1704.465214
iter 259 value 1704.439113
iter 260 value 1704.436351
iter 261 value 1704.433875
iter 262 value 1704.430826
iter 263 value 1704.427701
iter 264 value 1704.424849
iter 265 value 1704.423424
iter 266 value 1704.421356
iter 267 value 1704.419709
iter 268 value 1704.417369
iter 269 value 1704.416310
iter 270 value 1704.415851
iter 271 value 1704.415350
iter 272 value 1704.414621
iter 273 value 1704.414224
iter 274 value 1704.413525
iter 275 value 1704.413435
iter 276 value 1704.413130
iter 277 value 1704.412544
iter 278 value 1704.411951
iter 279 value 1704.411507
iter 280 value 1704.410880
iter 281 value 1704.410757
iter 282 value 1704.410723
iter 283 value 1704.410430
iter 284 value 1704.408818
iter 285 value 1704.408791
iter 286 value 1704.406433
iter 287 value 1704.402845
iter 288 value 1704.401771
iter 289 value 1704.387924
iter 290 value 1704.365566
iter 291 value 1704.305405
iter 292 value 1704.293027
iter 293 value 1704.259765
iter 294 value 1704.225316
iter 295 value 1704.130853
iter 296 value 1703.951741
iter 297 value 1703.867415
iter 298 value 1703.860175
iter 299 value 1703.851799
iter 300 value 1703.846309
iter 301 value 1703.843601
iter 302 value 1703.840886
iter 303 value 1703.840133
iter 304 value 1703.839280
iter 305 value 1703.838270
iter 306 value 1703.837176
iter 307 value 1703.836017
iter 308 value 1703.834920
iter 309 value 1703.834443
iter 310 value 1703.834112
iter 311 value 1703.833876
iter 312 value 1703.833605
iter 313 value 1703.833027
iter 314 value 1703.832975
iter 315 value 1703.832785
iter 316 value 1703.832383
iter 317 value 1703.832209
iter 318 value 1703.832055
iter 319 value 1703.831993
iter 320 value 1703.831761
iter 321 value 1703.831540
iter 322 value 1703.831480
iter 323 value 1703.831396
iter 324 value 1703.831343
iter 325 value 1703.831290
iter 326 value 1703.831168
iter 327 value 1703.830343
iter 328 value 1703.828577
iter 329 value 1703.823262
iter 330 value 1703.819289
iter 331 value 1703.818360
iter 332 value 1703.818173
iter 333 value 1703.817587
iter 334 value 1703.817242
iter 335 value 1703.816971
iter 336 value 1703.816862
iter 337 value 1703.816764
iter 338 value 1703.816692
iter 339 value 1703.816623
iter 340 value 1703.816388
iter 341 value 1703.816213
iter 342 value 1703.816167
iter 343 value 1703.816128
iter 344 value 1703.816089
iter 344 value 1703.816067
iter 345 value 1703.816039
iter 346 value 1703.816006
iter 347 value 1703.815973
iter 347 value 1703.815965
iter 347 value 1703.815959
final  value 1703.815959 
converged
Additional convergence test using scaled estimation. Parameters will be scaled by their current estimates and additional
  iterations will be performed.
initial  value 1703.815959 
iter   2 value 1703.733133
iter   3 value 1703.584810
iter   4 value 1703.298383
iter   5 value 1703.290150
iter   6 value 1703.284739
iter   7 value 1703.280118
iter   8 value 1703.235892
iter   9 value 1703.199981
iter  10 value 1703.183935
iter  11 value 1703.143452
iter  12 value 1703.137490
iter  13 value 1703.129785
iter  14 value 1703.101547
iter  15 value 1703.062093
iter  16 value 1703.054693
iter  17 value 1703.044301
iter  18 value 1702.991431
iter  19 value 1702.972682
iter  20 value 1702.963493
iter  21 value 1702.952577
iter  22 value 1702.936912
iter  23 value 1702.921202
iter  24 value 1702.868935
iter  25 value 1702.808651
iter  26 value 1702.759857
iter  27 value 1702.698061
iter  28 value 1702.638987
iter  29 value 1702.635433
iter  30 value 1702.604644
iter  31 value 1702.558933
iter  32 value 1702.533191
iter  33 value 1702.506170
iter  34 value 1702.493803
iter  35 value 1702.475412
iter  36 value 1702.452572
iter  37 value 1702.434410
iter  38 value 1702.413123
iter  39 value 1702.386352
iter  40 value 1702.330710
iter  41 value 1702.163241
iter  42 value 1702.161720
iter  43 value 1702.160382
iter  44 value 1702.092708
iter  45 value 1702.035656
iter  46 value 1702.004235
iter  47 value 1701.993727
iter  48 value 1701.967704
iter  49 value 1701.958037
iter  50 value 1701.954850
iter  51 value 1701.953672
iter  52 value 1701.952588
iter  53 value 1701.945395
iter  54 value 1701.929140
iter  55 value 1701.928067
iter  56 value 1701.927517
iter  57 value 1701.923171
iter  58 value 1701.912767
iter  59 value 1701.911033
iter  60 value 1701.908548
iter  61 value 1701.904588
iter  62 value 1701.900791
iter  63 value 1701.894893
iter  64 value 1701.886205
iter  65 value 1701.881717
iter  66 value 1701.874681
iter  67 value 1701.870730
iter  68 value 1701.865395
iter  69 value 1701.844950
iter  70 value 1701.833871
iter  71 value 1701.827298
iter  72 value 1701.802890
iter  73 value 1701.794926
iter  74 value 1701.780197
iter  75 value 1701.769970
iter  76 value 1701.761214
iter  77 value 1701.752655
iter  78 value 1701.748994
iter  79 value 1701.748740
iter  80 value 1701.743779
iter  81 value 1701.739485
iter  82 value 1701.738262
iter  83 value 1701.736576
iter  84 value 1701.733572
iter  85 value 1701.730996
iter  86 value 1701.722934
iter  87 value 1701.714454
iter  88 value 1701.708140
iter  89 value 1701.704803
iter  90 value 1701.700570
iter  91 value 1701.696987
iter  92 value 1701.689725
iter  93 value 1701.687662
iter  94 value 1701.685240
iter  95 value 1701.683663
iter  96 value 1701.680313
iter  97 value 1701.677569
iter  98 value 1701.674171
iter  99 value 1701.669575
iter 100 value 1701.662963
iter 101 value 1701.656189
iter 102 value 1701.654165
iter 103 value 1701.648976
iter 104 value 1701.508957
iter 105 value 1701.395524
iter 106 value 1701.392765
iter 107 value 1701.391193
iter 108 value 1701.389545
iter 109 value 1701.386907
iter 110 value 1701.381769
iter 111 value 1701.377346
iter 112 value 1701.366255
iter 113 value 1701.346322
iter 114 value 1701.332740
iter 115 value 1701.329543
iter 116 value 1701.322038
iter 117 value 1701.311102
iter 118 value 1701.296013
iter 119 value 1701.287189
iter 120 value 1701.286884
iter 121 value 1701.284716
iter 122 value 1701.274985
iter 123 value 1701.268002
iter 124 value 1701.262691
iter 125 value 1701.212330
iter 126 value 1701.084228
iter 127 value 1701.081411
iter 128 value 1701.033754
iter 129 value 1701.008427
iter 130 value 1701.006383
iter 131 value 1700.940684
iter 132 value 1700.925007
iter 133 value 1700.906762
iter 134 value 1700.904327
iter 135 value 1700.894569
iter 136 value 1700.880667
iter 137 value 1700.880134
iter 138 value 1700.874937
iter 139 value 1700.868404
iter 140 value 1700.867077
iter 141 value 1700.865040
iter 142 value 1700.863009
iter 143 value 1700.860633
iter 144 value 1700.858467
iter 145 value 1700.858036
iter 146 value 1700.857927
iter 147 value 1700.857805
iter 148 value 1700.856200
iter 149 value 1700.855415
iter 150 value 1700.855164
iter 151 value 1700.854111
iter 152 value 1700.853207
iter 153 value 1700.852072
iter 154 value 1700.848696
iter 155 value 1700.848449
iter 156 value 1700.846218
iter 157 value 1700.845394
iter 158 value 1700.843935
iter 159 value 1700.841710
iter 160 value 1700.838118
iter 161 value 1700.837275
iter 162 value 1700.836873
iter 163 value 1700.836825
iter 163 value 1700.836803
iter 164 value 1700.836566
iter 165 value 1700.836519
iter 166 value 1700.836493
iter 167 value 1700.836442
iter 168 value 1700.836372
iter 169 value 1700.835785
iter 170 value 1700.834946
iter 171 value 1700.832892
iter 172 value 1700.831008
iter 173 value 1700.830283
iter 174 value 1700.829251
iter 175 value 1700.826662
iter 176 value 1700.824858
iter 177 value 1700.824672
iter 178 value 1700.821313
iter 179 value 1700.819064
iter 180 value 1700.817604
iter 181 value 1700.810073
iter 182 value 1700.806310
iter 183 value 1700.805662
iter 184 value 1700.802088
iter 185 value 1700.797188
iter 186 value 1700.791242
iter 187 value 1700.786846
iter 188 value 1700.779266
iter 189 value 1700.778206
iter 190 value 1700.771538
iter 191 value 1700.769386
iter 192 value 1700.765727
iter 193 value 1700.760684
iter 194 value 1700.758043
iter 195 value 1700.757257
iter 196 value 1700.755159
iter 197 value 1700.753988
iter 198 value 1700.751957
iter 199 value 1700.750217
iter 200 value 1700.748045
iter 201 value 1700.746263
iter 202 value 1700.743913
iter 203 value 1700.742260
iter 204 value 1700.739979
iter 205 value 1700.738518
iter 206 value 1700.735325
iter 207 value 1700.729542
iter 208 value 1700.725403
iter 209 value 1700.724159
iter 210 value 1700.722298
iter 211 value 1700.720393
iter 212 value 1700.718357
iter 213 value 1700.716379
iter 214 value 1700.712281
iter 215 value 1700.706579
iter 216 value 1700.703989
iter 217 value 1700.703878
iter 218 value 1700.700757
iter 219 value 1700.699791
iter 220 value 1700.698716
iter 221 value 1700.614208
iter 222 value 1700.579889
iter 223 value 1700.524670
iter 224 value 1700.508505
iter 225 value 1700.506379
iter 226 value 1700.486710
iter 227 value 1700.485663
iter 228 value 1700.485257
iter 229 value 1700.484647
iter 230 value 1700.484556
iter 231 value 1700.483707
iter 232 value 1700.481290
iter 233 value 1700.478002
iter 234 value 1700.477190
iter 235 value 1700.476538
iter 236 value 1700.475964
iter 237 value 1700.475173
iter 238 value 1700.474399
iter 239 value 1700.474200
iter 240 value 1700.473642
iter 241 value 1700.471537
iter 242 value 1700.469873
iter 243 value 1700.465697
iter 244 value 1700.464070
iter 245 value 1700.462098
iter 246 value 1700.457583
iter 247 value 1700.453565
iter 248 value 1700.451964
iter 249 value 1700.445224
iter 250 value 1700.443416
iter 251 value 1700.439915
iter 252 value 1700.432417
iter 253 value 1700.430511
iter 254 value 1700.428709
iter 255 value 1700.428057
iter 256 value 1700.424477
iter 257 value 1700.422030
iter 258 value 1700.421981
iter 259 value 1700.417563
iter 260 value 1700.414994
iter 261 value 1700.413439
iter 262 value 1700.412173
iter 263 value 1700.409545
iter 264 value 1700.408622
iter 265 value 1700.406926
iter 266 value 1700.402570
iter 267 value 1700.400679
iter 268 value 1700.398679
iter 269 value 1700.395912
iter 270 value 1700.395065
iter 271 value 1700.392325
iter 272 value 1700.390809
iter 273 value 1700.389087
iter 274 value 1700.387955
iter 275 value 1700.387830
iter 276 value 1700.385087
iter 277 value 1700.383395
iter 278 value 1700.380257
iter 279 value 1700.379085
iter 280 value 1700.311215
iter 281 value 1700.269727
iter 282 value 1700.269319
iter 283 value 1700.269041
iter 284 value 1700.268620
iter 285 value 1700.267991
iter 286 value 1700.267449
iter 287 value 1700.267026
iter 288 value 1700.265390
iter 289 value 1700.262374
iter 290 value 1700.261659
iter 291 value 1700.261031
iter 292 value 1700.258998
iter 293 value 1700.256737
iter 294 value 1700.256120
iter 295 value 1700.255473
iter 296 value 1700.255373
iter 297 value 1700.254431
iter 298 value 1700.252346
iter 299 value 1700.251189
iter 300 value 1700.249098
iter 301 value 1700.247612
iter 302 value 1700.243880
iter 303 value 1700.240011
iter 304 value 1700.234426
iter 305 value 1700.199613
iter 306 value 1700.172834
iter 307 value 1700.159648
iter 308 value 1700.159498
iter 309 value 1700.159291
iter 310 value 1700.145229
iter 311 value 1700.144142
iter 312 value 1700.143763
iter 313 value 1700.143538
iter 314 value 1700.120874
iter 315 value 1700.119349
iter 316 value 1700.117233
iter 317 value 1700.116690
iter 318 value 1700.116013
iter 319 value 1700.115210
iter 320 value 1700.114948
iter 321 value 1700.100173
iter 322 value 1700.081945
iter 323 value 1700.079046
iter 324 value 1700.076157
iter 325 value 1700.069634
iter 326 value 1700.065738
iter 327 value 1700.063526
iter 328 value 1700.063113
iter 329 value 1700.063008
iter 330 value 1700.062880
iter 331 value 1700.062546
iter 332 value 1700.061852
iter 333 value 1700.060651
iter 334 value 1700.059426
iter 335 value 1700.058864
iter 336 value 1700.058765
iter 337 value 1700.030634
iter 338 value 1700.004974
iter 339 value 1700.004945
iter 339 value 1700.004939
iter 339 value 1700.004936
final  value 1700.004936 
converged

Estimated parameters:
                                         Estimate
asc                                    -4.723e+04
sigma_asc                             5532.135069
mu_Efficiency_L1                        -0.740938
sigma_Efficiency_L1                     -0.718399
mu_Efficiency_L2                         0.054839
sigma_Efficiency_L2                     -0.013480
mu_Efficiency_L3                        -0.678335
sigma_Efficiency_L3                     -0.043756
mu_Productivity_L1                      -0.415698
sigma_Productivity_L1                    0.269392
mu_Productivity_L2                       0.661461
sigma_Productivity_L2                    0.013316
mu_Productivity_L3                       0.684210
sigma_Productivity_L3                    0.002062
mu_Productivity_characteristics_L1       1.009493
sigma_Productivity_characteristics_L1   -0.003522
mu_Productivity_characteristics_L2       0.749483
sigma_Productivity_characteristics_L2    0.003575
mu_Productivity_characteristics_L3       0.352720
sigma_Productivity_characteristics_L3    0.012300
mu_Pond_management_L1                    0.437618
sigma_Pond_management_L1                 0.004471
mu_Pond_management_L2                    1.378491
sigma_Pond_management_L2                -0.125306
mu_Pond_management_L3                    1.636650
sigma_Pond_management_L3                 1.025703
mu_log_Fingerling_cost                 -17.501871
sigma_log_Fingerling_cost               10.223907

Final LL: -1700.0049

Calculating log-likelihood at equal shares (LL(0)) for applicable models...
Calculating log-likelihood at observed shares from estimation data (LL(c)) for applicable models...
Calculating LL of each model component...
Calculating other model fit measures
INFORMATION: Your model took more than 10 minutes to estimate, so it was saved to file ~/OneDrive - The University of
  Tokyo/WorldFish/Bangladesh characterization study/DCE/Bangladesh CE_model.rds before calculating its covariance matrix. If
  calculation of the covariance matrix fails or is stopped before finishing, you can load the model up to this point using
  apollo_loadModel. You may also want to inspect the approximate BHHH standard errors shown above to determine whether you
  wish to continue this process. 
Computing covariance matrix using analytical gradient.
 0%....25%....50%....75%...100%
WARNING: Singular Hessian, cannot calculate s.e. 
Could not write hessian to a file.
WARNING: Some eigenvalues of the Hessian are complex, indicating that the Hessian is not symmetrical. 
The results:

Code: Select all

Model run by Jie using Apollo 0.2.9 on R 4.3.0 for Darwin.
www.ApolloChoiceModelling.com

Model name                                  : Bangladesh CE
Model description                           : Mixed logit model on Bangladesh CE data
Model run at                                : 2023-05-22 21:56:26.714904
Estimation method                           : bfgs
Model diagnosis                             : successful convergence
Optimisation diagnosis                      : Non-symmetrical hessian
     hessian properties                     : Complex eigenvalues
     maximum eigenvalue                     : NA
Number of individuals                       : 178
Number of rows in database                  : 2848
Number of modelled outcomes                 : 2848

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

LL(start)                                   : -3172.24
LL at equal shares, LL(0)                   : -3128.85
LL at observed shares, LL(C)                : -1971.47
LL(final)                                   : -1700
Rho-squared vs equal shares                  :  0.4567 
Adj.Rho-squared vs equal shares              :  0.4477 
Rho-squared vs observed shares               :  0.1377 
Adj.Rho-squared vs observed shares           :  0.1245 
AIC                                         :  3456.01 
BIC                                         :  3622.73 

Estimated parameters                        : 28
Time taken (hh:mm:ss)                       :  00:17:40.28 
     pre-estimation                         :  00:01:37.2 
     estimation                             :  00:13:16.38 
          initial estimation                :  00:06:39.56 
          estimation after rescaling        :  00:06:36.81 
     post-estimation                        :  00:02:46.7 
Iterations                                  :  692  
     initial estimation                     :  351 
     estimation after rescaling             :  341 

Unconstrained optimisation.

Estimates:
                                         Estimate        s.e.   t.rat.(0)  p(2-sided)    Rob.s.e. Rob.t.rat.(0)  p(2-sided)
asc                                    -4.723e+04          NA          NA          NA          NA            NA          NA
sigma_asc                             5532.135069          NA          NA          NA          NA            NA          NA
mu_Efficiency_L1                        -0.740938          NA          NA          NA          NA            NA          NA
sigma_Efficiency_L1                     -0.718399          NA          NA          NA          NA            NA          NA
mu_Efficiency_L2                         0.054839          NA          NA          NA          NA            NA          NA
sigma_Efficiency_L2                     -0.013480          NA          NA          NA          NA            NA          NA
mu_Efficiency_L3                        -0.678335          NA          NA          NA          NA            NA          NA
sigma_Efficiency_L3                     -0.043756          NA          NA          NA          NA            NA          NA
mu_Productivity_L1                      -0.415698          NA          NA          NA          NA            NA          NA
sigma_Productivity_L1                    0.269392          NA          NA          NA          NA            NA          NA
mu_Productivity_L2                       0.661461          NA          NA          NA          NA            NA          NA
sigma_Productivity_L2                    0.013316          NA          NA          NA          NA            NA          NA
mu_Productivity_L3                       0.684210          NA          NA          NA          NA            NA          NA
sigma_Productivity_L3                    0.002062          NA          NA          NA          NA            NA          NA
mu_Productivity_characteristics_L1       1.009493          NA          NA          NA          NA            NA          NA
sigma_Productivity_characteristics_L1   -0.003522          NA          NA          NA          NA            NA          NA
mu_Productivity_characteristics_L2       0.749483          NA          NA          NA          NA            NA          NA
sigma_Productivity_characteristics_L2    0.003575          NA          NA          NA          NA            NA          NA
mu_Productivity_characteristics_L3       0.352720          NA          NA          NA          NA            NA          NA
sigma_Productivity_characteristics_L3    0.012300          NA          NA          NA          NA            NA          NA
mu_Pond_management_L1                    0.437618          NA          NA          NA          NA            NA          NA
sigma_Pond_management_L1                 0.004471          NA          NA          NA          NA            NA          NA
mu_Pond_management_L2                    1.378491          NA          NA          NA          NA            NA          NA
sigma_Pond_management_L2                -0.125306          NA          NA          NA          NA            NA          NA
mu_Pond_management_L3                    1.636650          NA          NA          NA          NA            NA          NA
sigma_Pond_management_L3                 1.025703          NA          NA          NA          NA            NA          NA
mu_log_Fingerling_cost                 -17.501871          NA          NA          NA          NA            NA          NA
sigma_log_Fingerling_cost               10.223907          NA          NA          NA          NA            NA          NA
Thank you again for your help.
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: Error in if (!test) { : missing value where TRUE/FALSE needed when adding workInLogs=TRUE in apollo_control

Post by dpalma »

Hi,

About workInLogs, we are looking into it, but for your case, I do not think you need it, so I would avoid using it.

The current problem is probably caused by some remnant identification issue. However, it is very hard to diagnose it without access to your data. If you can, send your data and model script to D.Palma [at] leeds.ac.uk . I will look at it and then I can provide more detailed advice.

Best wishes
David
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: Error in if (!test) { : missing value where TRUE/FALSE needed when adding workInLogs=TRUE in apollo_control

Post by dpalma »

Hi Jie,

I got your data and script. Your model is not identified.

The problem is that no one chooses alternative NB, as detailed below:

Code: Select all

WARNING: Some alternatives are never chosen in your data! 

Overview of choices for MNL model component :
                                    alt1    alt2   NB
Times available                  1168.00 1168.00 1168
Times chosen                      623.00  545.00    0
Percentage chosen overall          53.34   46.66    0
Percentage chosen when available   53.34   46.66    0
As no one chooses NB, you cannot estimate parameter asc, because it could go to -infinity. Think about it this way, if asc = -infinity, then no one would choose NB ever. So the optimisation algorithm tries to make asc more and more negative, without bound. Similarly, the ec component also is not identifiable. If you either remove alternative NB from your model, or fix asc and sigma_asc to zero, then you can estimate your model.

Best wishes
David
Nishihara
Posts: 9
Joined: 19 May 2023, 02:14

Re: Error in if (!test) { : missing value where TRUE/FALSE needed when adding workInLogs=TRUE in apollo_control

Post by Nishihara »

Dear David,

Thank you very much for your kind help.

Best wishes,
Jie
Post Reply