Summary: I would be very grateful for any ideas about commands that achieve the following goal: When a maximum likelihood algorithm fails to converge, STOP the algorithm, and execute the NEXT line of code in the do file. "capture" causes the maximum likelihood algorithm to CONTINUE iterations after the error, which I do not want.
Details:
I am estimating a latent class logit model using the "lclogit" command (http://www.stata-journal.com/sjpdf.h...iclenum=st0312). I want to evaluate whether the parameter estimates are sensitive to starting values (i.e. determine whether the algorithm converged to a local or global maximum in the likelihood function). One rudimentary way to evaluate this is to run the model using multiple "seed" values (which control the starting values) and look at the associated log likelihoods.
If there is never an error in the maximum likelihood algorithm, the code below can accomplish this task. Here, the dependent variable is "y", the independent variables are "x1" and "x2," the identifier for choice scenarios is "choiceid," the identifier for choice makers is "agentid," and the desired number of latent classes is 4. I am evaluating the model for seed 1 and seed 2, and storing the seed and log likelihood values for each step in the "forvalues" loop to matrix "A."
Unfortunately, sometimes the lclogit algorithm does not converge for certain seeds. For example, my output is below for seed 1.
...
Iteration 14: log likelihood = -2131.9913
cannot compute an improvement -- flat region encountered
r(430);
When this error occurs, I would like Stata to STOP the maximum likelihood estimation, and execute the NEXT line of code in the do file (i.e. "local s = e(seed)..."). Placing "capture" before the lclogit command (as shown below) does not accomplish this goal. "Capture" causes Stata to proceed with additional maximum likelihood iterations, rather than execute the next line of code. (see output below).
...
Iteration 14: log likelihood = -2131.9961
Iteration 15: log likelihood = -2130.3187
Iteration 16: log likelihood = -2129.1172
Iteration 17: log likelihood = -2128.2234
...
I'm apologize that I don't have a sample data set to make this code reproducible. Hopefully, my question is generic enough that data is not needed.
Thank you!
Details:
I am estimating a latent class logit model using the "lclogit" command (http://www.stata-journal.com/sjpdf.h...iclenum=st0312). I want to evaluate whether the parameter estimates are sensitive to starting values (i.e. determine whether the algorithm converged to a local or global maximum in the likelihood function). One rudimentary way to evaluate this is to run the model using multiple "seed" values (which control the starting values) and look at the associated log likelihoods.
If there is never an error in the maximum likelihood algorithm, the code below can accomplish this task. Here, the dependent variable is "y", the independent variables are "x1" and "x2," the identifier for choice scenarios is "choiceid," the identifier for choice makers is "agentid," and the desired number of latent classes is 4. I am evaluating the model for seed 1 and seed 2, and storing the seed and log likelihood values for each step in the "forvalues" loop to matrix "A."
Code:
forvalues ii = 1/2 {lclogit y x1 x2 x3, group(choiceid) id(agentid) nclasses(4) seed(`ii') local s = e(seed) local ll = e(ll) matrix A = nullmat(A) \ `s', `ll'}
...
Iteration 14: log likelihood = -2131.9913
cannot compute an improvement -- flat region encountered
r(430);
When this error occurs, I would like Stata to STOP the maximum likelihood estimation, and execute the NEXT line of code in the do file (i.e. "local s = e(seed)..."). Placing "capture" before the lclogit command (as shown below) does not accomplish this goal. "Capture" causes Stata to proceed with additional maximum likelihood iterations, rather than execute the next line of code. (see output below).
Code:
forvalues ii = 1/2 {capture lclogit y x1 x2 x3, group(choiceid) id(agentid) nclasses(4) seed(`ii')local s = e(seed)local ll = e(ll)matrix A = nullmat(A) \ `s', `ll'}
Iteration 14: log likelihood = -2131.9961
Iteration 15: log likelihood = -2130.3187
Iteration 16: log likelihood = -2129.1172
Iteration 17: log likelihood = -2128.2234
...
I'm apologize that I don't have a sample data set to make this code reproducible. Hopefully, my question is generic enough that data is not needed.
Thank you!