Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: two-step nonlinear least squares estimation

 From Le Wang To statalist@hsphsun2.harvard.edu Subject st: two-step nonlinear least squares estimation Date Tue, 7 Sep 2010 14:54:56 -0400

```Hi there,

I have a question regarding -nl- command. Thank you for your time.

I would like to estimate the following model in two steps, say, y =
beta_0*x + beta_1*exp( (z*gamma_0) *theta)

1) Given theta, gamma_0 is obtained from the regression of x on
z*theta; generate predicted values from this estimated equation, xhat.
2) Then estimate the model replacing (z*gamma_0) with the predicted
values, xhat, above via nonlinear least squares estimation.

I was able to estimate this model using -optimize()- in Mata, but
needed to code the variance-covariance matrix myself. I was wondering
if this could be done using -nl- command. For the second step, it is
straightforward. But for the first step, -nl- program doesn't seem to
behave the way as the -optimize()- command does; Below is my codes,
Stata says that "nltest returned 100, verify that nltest is a function
evaluator program". But I can't quite figure out where the codes went
wrong.

----------------------------------------------------------

clear
set more off
set seed 123456
set obs 1000

* Simulate the data
g x = rnormal()
g z = rchi2(1)
g v = rnormal()

* The data generating process is different from what we are trying
to estimate
g y = 1 + x + z + v

* The main program
cap program drop nltest
program nltest
version 11
syntax varlist(min=3 max=3) [if],at(name)
local y : word 1 of `varlist'
local x : word 2 of `varlist'
local z : word 3 of `varlist'

tempname b0 b1 theta
scalar `b0' = `at'[1,1]
scalar `b1' = `at'[1,2]
scalar `theta' = `at'[1,3]

tempvar xhat ztheta
g `ztheta' = `z'*`theta'

reg `x' `ztheta'
predict `xhat'

// return the function value
replace `y1' = `b0'*`x1' + `b1'*exp(`theta'*`xhat'))

end

* Estimation
nl test @ y x z, nparameters(3)

----------------------------------------------------------

Thanks again for your help!

Best,
Le
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```