Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: nonlinear regression with restrictions


From   Ed Blackburne <[email protected]>
To   [email protected]
Subject   Re: st: nonlinear regression with restrictions
Date   Wed, 06 Dec 2006 14:30:45 -0600

Michael,

I have attached two pieces of code that I believe solves your problem. Please review and see if it works for you. Also, I would appreciate any feedback to this thread from the Stata power users who know of a better (i.e. more efficient) method of solving this problem. I especially prefer not to use globals, but I can't see another way around the problem.



using the infamous auto data, the code will estimate a model of

price=a1+a2*trunk+a3*weight+a4*(1-dk)*((b^2 - 2 * b * displacement + displacement^2)
where dk is defined as by you below.

You can, of course, use any variable as the threshold variable.


After copying the files to your working directory, the output is

. michael
(1978 Automobile Data)

initial: log likelihood = -681.03832
alternative: log likelihood = -681.03317
rescale: log likelihood = -675.90404
Iteration 0: log likelihood = -675.90404 Iteration 1: log likelihood = -675.37653 Iteration 2: log likelihood = -675.34488 Iteration 3: log likelihood = -675.34441 Iteration 4: log likelihood = -675.34441
------------------------------------------------------------------------------
Threshold nonlinear estimation
Threshold value of displacement = 224.98074 with standard error = 26.895476
------------------------------------------------------------------------------

Source | SS df MS Number of obs = 74
-------------+------------------------------ F( 3, 70) = 17.13
Model | 268847078 3 89615692.7 Prob > F = 0.0000
Residual | 366218318 70 5231690.26 R-squared = 0.4233
-------------+------------------------------ Adj R-squared = 0.3986
Total | 635065396 73 8699525.97 Root MSE = 2287.3

------------------------------------------------------------------------------
price | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
trunk | -65.54658 84.54591 -0.78 0.441 -234.1681 103.0749
weight | 1.207276 .5366726 2.25 0.028 .1369165 2.277636
term | .1762172 .0445175 3.96 0.000 .0874298 .2650046
_cons | 2875.371 1294.126 2.22 0.030 294.3173 5456.424
------------------------------------------------------------------------------


The first piece of code is a simple do file that defines the problem, etc

=====================================================
capture program drop michael
program define michael
webuse auto, clear
tempname b V
global dep_var price
global indep_var trunk weight
global thresh displacement
generate double term=0
global term term
ml model d0 michael_ml /gamma, max
matrix b=e(b)
matrix V=e(V)
scalar `b'=b[1,1]
scalar `V'=V[1,1]
scalar `V'=sqrt(`V')
di _n in smcl in gr "{hline 78}"
di in gr "Threshold nonlinear estimation"
di in gr "Threshold value of $thresh = " in ye `b' in gr " with standard error = " in ye `V'
di in smcl in gr "{hline 78}"

regress $dep_var $indep_var $term
end

===============================================================

The michael_ml procedure is an ado file that uses the undocumented ml_hold option of Stata's ml command

===============================================================
capture program drop michael_ml
program define michael_ml
args todo b lnf
tempname beta
marksample touse
mleval `beta' = `b', scalar
quie replace term=0
quie replace term=(`beta'^2-2*`beta'* $thresh + $thresh ^2) if $thresh > `beta'
ml hold
quie regress $dep_var $indep_var term
ml unhold
scalar `lnf'=e(ll)
end

================================================================





Michael Stobernack wrote:

Dear all,

I would like to run the following nonlinear regression:

Y = a1 + a2 * X +a3 * (1 - dk) * (b**2 - 2 * b * x + x**2) + u

with dk = 1 if x <= b and dk = 0 if x > b

That is, there is a variable (dk) which depends on a parameter (b) which itself
is still to be estimated.

The background is: I would like to get a linear regression between x=0 and x=b,
but a nonlinear regression for x>b. And I don't know the value of b in advance.
The regression should estimate the value of b.

I would be very glad about any hints how to do this in Stata.

Best regards,

Michael

*
* 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/
*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index