Dear statalist users,
I'm trying to fit a nonlinear model using the nl command. The nlfcn is a bit
complex and cannot easily be expressed in a single
replace `1' = etc.
command. Therefore, I thought it would be best to exploit 'tempvars' in the
nlfcn-body, to hold intermediate results, that can be plugged in in the
final "replace `1' = etc." command. To give a simple example of what I mean:
input x y
-3 10
-2 3.5
-1 1
0 0.1
1 1
2 4
3 10
end
program nlparabolic
version 8
if "`1'" == "?" {
global S_1 "a b c"
global a = 0
global b = 0
global c = 0
exit
}
tempvar quadratic
gen `quadratic' = $a + $b*x + $c*x^2
replace `1' = `quadratic'
end
nl parabolic y
The tempvar "quadratic" contains the nlfnc-values to be placed in `'1'.
However, the nl parameter estimates change quite a lot depending on the
initial values of the 3 parameters a, b and c, as specified in the global
statements. Also, the corresponding standard errors vary considerably for
different initial parameter values. This problem is solved if, instead of
the
replace `1' = `quadratic'
the statement
replace `1' = $a + $b*x + $c*x^2
is used (as in this simple example of course would be straightforward to
do). This last replace command also gives exactly the right
parameter-estimates (i.e., the same ones as would be obtained using "regress
y x xsquare") and also the right standard errors, all these NOT depending on
the initial values. Therefore, is it correct to conclude that, in the 'body'
of the nlfnc specified, tempvars or permanent vars cannot be used to hold
intermediate results and that, as a consequence, the nl-function has to be
specified in it's full complexity in the single "replace `1' = etc."
command?
My second question is: can the nl command be used to perform maximum
likelihood estimation? I'm aware of the ml command in Stata (which of course
is preferable when performing ML estimation) but I'm wondering if nl could
be faster then ml, and if it's possible to do ML estimation with nl in the
first place.
Best regards, Ben.
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/