st: Maximum likelihood with loops

 From Olga Lebedeva To statalist@hsphsun2.harvard.edu Subject st: Maximum likelihood with loops Date Fri, 26 Jun 2009 18:16:46 +0200

```
--
________________________________
<br>
```
Olga Lebedeva <br>
```PhD - Candidate in Finance <br>
Universität Mannheim <br>
L9, 1-2 (Zimmer 404), 68131 Mannheim <br>
Tel: 0621-181-2278

```
--- Begin Message ---
 From Olga Lebedeva To statalist@hsphsun2.harvard.edu Subject Maximum likelihood with loops Date Fri, 26 Jun 2009 18:12:48 +0200
```Dear Stata users,

```
I am running into a problem when I need to conduct maximum likelihood estimates on subsamples of the data. I use "if" option in ml model and run loops over different
```subsamples.

My problem is as follows.
```
The model converges in general when I run it on different subsamples one by one (entered manually). However, in order to automatize the procedure, I use loops - and get estimates only for the first subsample of data. when the program moves on to the second subsample, error message 430 "missing values encountered" is displayed. However, the model converges if I start with second subsample but will not converge for the third, fourth, fifth etc.
```
does anyone know what the reson for the problem might be?

ML code:

program pin_lf_02

version 1.2

args lnf x y l k
tempvar mu eps alpha delta P1 P2 P3 P4
quietly {

```
gen double `eps'=exp(`x') gen double `mu'=exp(`y')
```       gen double `alpha'=1/(1+exp(-`l'))
gen double `delta'=1/(1+exp(-`k'))
gen double `P1'= - `eps' + \$ML_y1*ln(`eps') - lngamma(\$ML_y1 + 1)
gen double `P2'= - `eps' + \$ML_y2*ln(`eps') - lngamma(\$ML_y2 + 1)
```
gen double `P3'= -(`mu'+`eps') + \$ML_y2*ln(`mu'+`eps') - lngamma(\$ML_y2 + 1) gen double `P4'=-(`mu'+`eps')+ \$ML_y1*ln(`mu'+`eps') - lngamma(\$ML_y1 + 1) replace `lnf'=ln(exp(ln(1-`alpha')+`P1'+`P2')+exp(ln(`alpha')+ln(`delta')+`P1'+`P3')+exp(ln(`alpha')+ln(1-`delta')+`P4'+`P2'))
```

}

end

forvalues q = 1/20 {

forvalues p = 528/537 {

```
ml model lf pin_lf_02 (x: buy sell = ) (y: buy sell = ) (l: buy sell = ) (k: buy sell = ) if (((monthyear==`p') | (monthyear==`p'+1) | (monthyear==`p'+2))&(num1==`q')), missing technique(nr bhhh)
```ml init /x = 3 /y = 3 /l = 0 /k = 0
* ml check
ml search x -1 6 y -1 5 l -3 3 k -3 3
* ml search

ml maximize, difficult

mat beta`p' = e(b)
replace x1 = el(beta`p',1,1) if (monthyear==`p'+2) & (num1==`q')
replace y1 = el(beta`p',1,2) if monthyear==`p'+2 & (num1==`q')
replace l1 = el(beta`p',1,3) if monthyear==`p'+2 & (num1==`q')
replace k1 = el(beta`p',1,4) if monthyear==`p'+2 & (num1==`q')
}

}

--
________________________________
<br>
```
Olga Lebedeva <br>
```PhD - Candidate in Finance <br>
Universität Mannheim <br>
L9, 1-2 (Zimmer 404), 68131 Mannheim <br>
Tel: 0621-181-2278

```

--- End Message ---