I am learning how to use Stata's maximum-likelihood maximization capabilities, with a view to writing my own ml routine to maximize a relatively complex likelihood function. In order to wrap my head around how "ml" works, I have written a very simple lf-type program to perform linear regression under homoskedasticity, called lrm_lf1. For reasons that will become clear when I talk about the second program, I have split the usual `xb' argument where the regressors go into two arguments, xb1 and xb2. This program reproduces exactly the results of a regression using the canned "reg" routine, as you can see in the screenshot below.

I've also written a program (lrm_lf2) that is identical to lrm_lf2, except for the fact that the equation for xb2 only has a constant (in lrm_lf1 I put the variable "headroom" in that equation, without a constant). I assign headroom to a global called $headroom, and put " `xb2' * $headroom " into the log-likelihood function in the program in place of `xb2'. This shouldn't make any difference to the results: in both lrm_lf1 and lrm_lf2, the regression specification is as follows: y_i = b0 + b1*mpg + b2*headroom + e_i, with e_i normally distributed with variance sigma^2. However, lrm_lf2 doesn't produce the same estimates for the coefficients on mpg and headroom, as you can see in the second screenshot, below.

Code for the programs and for the regressions that produce the output in the attached images is below. Any idea why the two programs produce different results?
I've also written a program (lrm_lf2) that is identical to lrm_lf2, except for the fact that the equation for xb2 only has a constant (in lrm_lf1 I put the variable "headroom" in that equation, without a constant). I assign headroom to a global called $headroom, and put " `xb2' * $headroom " into the log-likelihood function in the program in place of `xb2'. This shouldn't make any difference to the results: in both lrm_lf1 and lrm_lf2, the regression specification is as follows: y_i = b0 + b1*mpg + b2*headroom + e_i, with e_i normally distributed with variance sigma^2. However, lrm_lf2 doesn't produce the same estimates for the coefficients on mpg and headroom, as you can see in the second screenshot, below.
Code for the programs and for the regressions that produce the output in the attached images is below. Any idea why the two programs produce different results?
Code:
** Defining the programs ** cap program drop lrm_lf1 program define lrm_lf1 args lnf xb1 xb2 sigma qui replace `lnf' = ln(normalden($ML_y1, `xb1' + `xb2', `sigma')) end cap program drop lrm_lf2 program define lrm_lf2 args lnf xb1 xb2 sigma qui replace `lnf' = ln(normalden($ML_y1, `xb1' + `xb2' * $headroom , `sigma')) end sysuse auto.dta, clear ** Output in screenshot 1 below ** ml model lf lrm_lf1 (price = mpg) (headroom, nocons) /sigma ml maximize reg price mpg headroom global headroom = headroom ** Output in screenshot 2 below ** ml model lf lrm_lf2 (price = mpg) /headroom /sigma ml maximize reg price mpg headroom
Comment