Bookmark and Share

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: two-step nonlinear least squares estimation

From   Le Wang <>
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


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'))


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


Thanks again for your help!

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index