Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple ML (lf) program doesn't produce same results as canned routine

    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.
    Click image for larger version

Name:	lrm_lf1.PNG
Views:	1
Size:	23.5 KB
ID:	1670622


    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.
    Click image for larger version

Name:	lrm_lf2.PNG
Views:	1
Size:	22.5 KB
ID:	1670623



    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
    Last edited by Graham Mazeine; 23 Jun 2022, 10:26.

  • #2
    The problem is on this line
    Code:
    global headroom = headroom
    Because rather than saying I want global headroom to have the value "headroom", it is actually capturing the first value of that variable.
    You should type it as

    Code:
    global headroom headroom
    Also, it isn't good practice, unless you have very strong reasons, to use globals when passing information across programs.
    F

    Comment


    • #3
      You're a lifesaver, Fernando. I've modified the code and it now matches the results from the canned routine exactly. Thank you very much.

      Comment

      Working...
      X