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.

Negative Std Dev, NAs when hessianRoutine="maxLik"

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
anhntl
Posts: 13
Joined: 02 Feb 2021, 09:40

Negative Std Dev, NAs when hessianRoutine="maxLik"

Post by anhntl »

Dear Professors,
I am estimating in Apollo package and find it very flexible and strong package. I enjoy estimating DCE with it.
However, I am encountering some problem when estimating MIXL and I would like to ask for your advice if possible:

1. I got negative standard deviation and negative t ratio: Is it something wrong or error with my estimation, and how to solve it? Or can I keep the result and change the sign of std dev.
(I read the manual file already but I am still not clear about it and what makes this problem)

2. If I am using estimate_settings=list(hessianRoutine="maxLik"), I got much of NAs. But if using numDeriv, then the covariance can be calculated without NA. I would like to ask the differences between the two, and which case is more suitable for my model.

3. Since I used mlogit and gmnl package before, I found that there are different results to Apollo when I am estimating same MIXL models with same halton draws. (For MNL model, they get similar results). I found from result screen that number of iterations is different, but why it makes so different value of parameters and why they make different iterations?

This is my code. Starting value is taking from MNL estimation.

Thank you very much.


################ MIXL #################

apollo_initialise()
apollo_control = list(
modelName ="SimpleMIXL",
modelDescr ="SimpleHH",
indivID ="id",
mixing=TRUE,panelData=TRUE,
nCores=3
)
database<- read.csv("D:\\Research\\Theses\\Second Chapter\\Working\\Data\\Final\\apollo_household.csv", header=T)
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c( mu.b.width=1, sigma.b.width=0.1,# sigma.hsk.h=0.1,

mu.b.access=1,mu.b.tax=1,
sigma.b.access=0.1,sigma.b.tax=0.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()

### Read in starting values for at least some parameters from existing model output file
apollo_beta=apollo_readBeta(apollo_beta,apollo_fixed,"MNL_Test",overwriteFixed = FALSE)

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

### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "halton",
interNDraws = 50,
interUnifDraws = c(),
interNormDraws = c("draws.width","draws.access","draws.tax"),
intraDrawsType = "halton",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["b.width"]] = mu.b.width + sigma.b.width * draws.width

randcoeff[["b.access"]] = mu.b.access + sigma.b.access * draws.access
randcoeff[["b.tax"]] = mu.b.tax + sigma.b.tax * draws.tax
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 change the following three commands
### 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()

### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['alt.1']] = b.access* access.1+b.width*width.1
V[['alt.2']] = b.access* access.2+b.width*width.2+
b.tax* tax.2
V[['alt.3']] = b.width*width.3+
b.access* access.3+b.tax* tax.3

### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt.1=1, alt.2=2,alt.2=3),
avail = list(alt.1=1, alt.2=1,alt.2=1),
choiceVar = choice,
V = V
)

### Compute probabilities using MNL model
P[['model']] = apollo_mnl(mnl_settings, 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(hessianRoutine="maxLik"))

apollo_modelOutput(model)
apollo_saveOutput(model)



My mlogit code:
hh<- read.csv("D:\\Research\\Theses\\Second Chapter\\Working\\Data\\Final\\apollo_household.csv", header=T)
hh$chid <- 1:nrow(hh)
m.hh <- dfidx(hh, idx = list(c("chid", "id")),
choice = "choice", varying = 171:185, sep = ".")
mxl.hh<- mlogit(formula = choice ~ tax + width + access | 0, data = m.hh,
rpar = c(tax = "n", width = "n", access = "n"),
R = 50, panel = TRUE, print.level = 3)

summary(mxl.hh)

My gmnl code:
simple=gmnl(choice~tax+access+width|0,
data=mpool.hh, model="mixl", R=50, panel=TRUE, #correlation = TRUE,
ranp=c(access="n",
width="n", tax="n"), print.init = 1)
stephanehess
Site Admin
Posts: 1002
Joined: 24 Apr 2020, 16:29

Re: Negative Std Dev, NAs when hessianRoutine="maxLik"

Post by stephanehess »

Hi

1. Negative estimates for the standard deviation are not a problem. Please have a look at the frequently asked questions section of the manual or website (http://www.apollochoicemodelling.com/faq.html) to understand why this happens and why it is not a problem. Here is the answer:
Q: Why is my estimate of the standard deviation negative?
This happens if a random coefficient is coded as randCoeff[["beta"]]=mu+sigma*draws where draws is a random variate that is symmetrical around zero.
Q: So should I constrain the standard deviation to be positive?
There is no reason for doing so. The results will be the same if the random variate draws is symmetrical around zero. Imposing constraints will also make estimation harder. And of course, if a user wants to allow for correlation between individual coefficients, then the parameters multiplying the draws need to be able to be positive or negative.
2. We tend to find numDeriv more reliable as a routine in R, but if you update to later versions of Apollo, it will use the most appropriate routine itself.

3. It is difficult to answer this question without knowing how big the differences were you received. Differences can arise for example due to different starting values. Some software also drops the first few draws with Halton, which may again lead to differences in results. 50 draws is of course also a low number in that context.

Best wishes

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply