Bookmark and Share

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


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

Re: st: problems with substitutable expression program for nonlinear regression


From   "Brian P. Poi" <[email protected]>
To   [email protected]
Subject   Re: st: problems with substitutable expression program for nonlinear regression
Date   Fri, 04 Nov 2011 09:16:38 -0500

Sarah Kristina Reuter wrote:
Dear Statalisters,

I apologize for a beginner-question, but I have some problems writing a substitutable expression program for nonlinear regression. The model is:

y = y0 * (1-x^q)

y and x are given, q is to be estimated and y0 is the y-value of a specific data-point, namely the smallest x.

The problems I have are
- “explaining” Stata y0 and
- Stata returns the following error message: “verify that nlreg is a substitutable expression program and that you have specified all options that it requires” and I can’t find the error.

Here is a simple version that does work:

program nlreg, rclass
	version 10

	syntax varlist(min=2 max=2) [aw fw iw] [if] [in]
	marksample touse

	local lhs: word 1 of `varlist'
	local rhs: word 2 of `varlist'

	*specifying y0, even to me that looks complicated
	tempname a
	su `rhs' if `touse', meanonly
	scalar `a' = r(min)
	su `lhs' if `rhs' == `a', meanonly
	scalar y0 = r(min)

	return local eq "`lhs' = y0*(1-`rhs'^{gamma})"
	return local title "`lhs' = y0*(1-`rhs'^gamma)"
end

nl reg: y x

Here I did not make y0 a temporary scalar.  Temporary scalars disappear as soon as a program exits, so -nl- would not know what `y0' was referring to.  My program assumes that there is not a variable in your dataset also named y0.  One solution to that would be to use a temporary scalar for y0, but use Stata's `=...' notation to have Stata substitute the numerical value for y0 before continuing, like this:

	*specifying y0, even to me that looks complicated
	tempname a y0
	su `rhs' if `touse', meanonly
	scalar `a' = r(min)
	su `lhs' if `rhs' == `a', meanonly
	scalar `y0' = r(min)

	return local eq "`lhs' = `=`y0''*(1-`rhs'^{gamma})"
	return local title "`lhs' = `=y0''*(1-`rhs'^gamma)"

In the first version of my program, the title just above the coefficient table will read

y = y0*(1-x^gamma)

while in the second version it will read

y = ###########*(1-x^gamma)

where ########### is whatever number y0 happens to be for your dataset.  You can set the title macro however you want; it just controls that title you see in the output.


Brian P. Poi
Senior Economist
StataCorp LP





Here is my program:

program nlreg, rclass
version 10
syntax varlist(min=2 max=2) [aw fw iw] [if] [in]
tempvar touse
mark `touse' `if' `in'
local lhs: word 1 of `varlist'
local rhs: word 2 of `varlist'
*specifying y0, even to me that looks complicated
tempname a b
su `rhs' if `touse’, meanonly
scalar `a' = r(min)
gen `b’=`lhs’ if `rhs’==`a’
su `b’, meanonly
scalar `y0’=r(min)
return local eq "`lhs' = `y0'*(1-`rhs'^{gamma})"
return local title "`lhs' = `y0'*(1-`rhs'^gamma)"
end

So happy for any suggestions!
Thanks!
Sarah
*
*   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/


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