⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nl.hlp

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 HLP
📖 第 1 页 / 共 2 页
字号:
{pstd}
Using a substitutable expression is the easiest way to define your 
nonlinear function.  Substitutable expressions are just like any other
mathematical expression in Stata, except that the parameters of your 
model are bound in braces.  There are three rules to follow:

{phang2}
1.  Parameters of the model are bound in braces: {cmd:{c -(}b0{c )-}},
{cmd:{c -(}param{c )-}}, etc.

{phang2}
2.  Initial values are given by including an equal sign and the initial
value inside the braces:  {cmd:{c -(}b1=1.267{c )-}}, 
{cmd: {c -(}gamma=3{c )-}}, etc.  If you do not specify an initial 
value, that parameter is initialized to zero.  The {cmd:initial()} 
option overrides initial values provided in substitutable 
expressions.

{phang2}
3.  Linear combinations can be included using the notation 
{cmd:{c -(}}{it:eqname}{cmd::}{it:varlist}{cmd:{c )-}}:

{pmore3}
{cmd:{c -(}xb:mpg price weight{c )-}} is equivalent to{p_end}
{pmore3}
{cmd:{c -(}xb_mpg{c )-}*mpg + }
{cmd:{c -(}xb_price{c )-}*price + }
{cmd:{c -(}xb_weight{c )-}*weight}


{title:Examples}

{phang2}
1.  To fit the model

{pmore3}
y = alpha + beta*x^gamma

{pmore2}
where alpha, beta, and gamma are parameters and a starting value for 
gamma is one, you would type

{pmore3}
{cmd:. nl (y = {c -(}alpha{c )-} + {c -(}beta{c )-}*x^{c -(}gamma=1{c )-})}

{phang2}
2.  To regress y on a constant and the reciprocal of x you could do

{pmore3}
{cmd:. nl (y = {b0} + {b1} / x), initial(b0 2 b1 3)}

{pmore2}
which obviates the need to generate a new variable equal to 1/x 
before calling {cmd:regress}.  Here b0 is initialized to two and
b1 is initialized to three.


{title:Some commonly used models}

{pstd} 
The following models are used so frequently that they are built into {cmd:nl}.

{pmore}Exponential regression with one asymptote:{p_end}

{phang3}{cmd:exp3} {space 3} y = b0 + b1*b2^x{p_end}
{phang3}{cmd:exp2} {space 3} y = {space 4} b1*b2^x{p_end}
{phang3}{cmd:exp2a} {space 2} y = {space 4} b1*(1-b2^x){p_end}

{pmore}Logistic function (symmetric sigmoid shape)(*):{p_end}

{p 12 24 2}{cmd:log4} {space 3} y = b0 + b1/(1 + exp(-b2*(x-b3))){p_end}
{p 12 24 2}{cmd:log3} {space 3} y = {space 4} b1/(1 + exp(-b2*(x-b3))){p_end}

{pmore}Gompertz function (asymmetric sigmoid shape):{p_end}

{p 12 24 2}{cmd:gom4} {space 3} y = b0 + b1*exp(-exp(-b2*(x-b3))){p_end}
{p 12 24 2}{cmd:gom3} {space 3} y = {space 4} b1*exp(-exp(-b2*(x-b3)))

{pmore}(*) not to be confused with logistic regression

{pstd}
To use any of these, you type

{pmore}
{cmd:. nl} {it:model} {cmd::} {it:depvar} {it:indepvar}

{pstd}
For example,

{pmore}
{cmd:. nl exp3: y x}{p_end}
{pmore}
{cmd:. nl gom3: response dosage}

{pstd}
Initial values are chosen automatically, though you can override the 
defaults by using the {cmd:initial()} option.


{title:Substitutable expression programs {hline 2} {it:sexp_prog}s}

{pstd}
If you use the same nonlinear function repeatedly, then you can write a 
substitutable expression program so that you do not have to retype the 
expression every time.  The first two letters of the program name must 
by {cmd:nl}.  The {cmd:nl}{it:sexp_prog} is to accept a {it:varlist}, an 
{cmd:if} {it:exp}, and, optionally, weights.  The program will then 
return a substitutable expression in the r-class macro {cmd:r(eq)} and, 
optionally, a title in {cmd:r(title)}.

{pstd}
The outline of a {cmd:nl}{it:sexp_prog} program is

{p 8 14 2}{cmd:program nl}{it:sexp_prog}{cmd:, rclass}{p_end}
{p 12 18 2}{cmd:syntax }{it:varlist} {cmd:[aw fw iw]} {cmd:if}{p_end}
{p 12 18 2}{it:(obtain initial parameters if desired)}{p_end}
{p 12 18 2}{cmd:return local eq "}{it:<sexp>}{cmd:"}{p_end}
{p 12 18 2}{cmd:return local title "}{it:title}{cmd:"}{p_end}
{p 8 14 2}{cmd:end}


{title:Example}

{pmore}
Returning to the model

{pmore2}
y = alpha + beta*x^gamma

{pmore}
one way to obtain initial values is to let gamma = 1 and then run a 
regression of x on y to obtain alpha and beta.  The substitutable 
expression program is

{p 12 18 2}{cmd:program nlmyreg, rclass}{p_end}
{p 16 22 2}{cmd:syntax varlist(min=2 max=2) [aw fw iw] if}{p_end}
{p 16 22 2}{cmd:local lhs: word 1 of `varlist'}{p_end}
{p 16 22 2}{cmd:local rhs: word 2 of `varlist'}{p_end}
{p 16 22 2}{cmd:regress `lhs' `rhs' [`weight'`exp'] `if'}{p_end}
{p 16 22 2}{cmd:tempname a b}{p_end}
{p 16 22 2}{cmd:scalar `a' = _b[_cons]}{p_end}
{p 16 22 2}{cmd:scalar `b' = _b[`rhs']}{p_end}
{p 16 22 2}{cmd:return local eq "`lhs' = {alpha=`=`a''}+{beta=`=`b''}*`rhs'^{gamma=1}"}{p_end}
{p 16 22 2}{cmd:return local title "`lhs' = alpha+beta*`rhs'^gamma"}{p_end}
{p 12 18 2}{cmd:end}{p_end}

{pmore}
To fit your model, you type

{p 12 18 2}{cmd:. nl myreg: y x}

{pmore}
(There is a space between {cmd:nl} and {cmd:myreg}, even though the 
program is named {cmd:nlmyreg}.)

{pstd}
Note that the substitutable expression does not need to account for 
weights or the {cmd:if} {it:exp}, though you do need to use them in 
obtaining initial values.  Also note that the substitutable expression is 
not bound in parentheses, unlike when typing it in interactively.


{title:Function evaluator programs {hline 2} {it:func_prog}s}

{pstd}
If your function is particularly complex, then you may find that writing 
a single substitutable expression is impractical.  In those cases, you 
can write a function evaluator program instead.  Whenever {cmd:nl} needs 
to evaluate your function, it calls your program with a vector of 
parameters.  Your program then fills in the dependent variable with 
function values.

{pstd}
Function evaluator programs must accept a {it:varlist}, an {cmd:if} 
{it:exp}, and an option named {cmd:at} that accepts the name of a 
matrix.  It may optionally accept weights as well.  The outline of a 
{cmd:nl}{it:func_prog} program is

{p 8 14 2}{cmd:program nl}{it:func_prog}{p_end}
{p 12 18 2}{cmd:syntax} {it:varlist} {cmd:[aw fw iw] if, at(name)}{p_end}
{p 12 18 2}{cmd:local lhs: word 1 of `varlist'}{p_end}
{p 12 18 2}{cmd:local rhs: subinstr local varlist "`lhs'" "", word}{p_end}
{p 12 18 2}{it:(evaluate the function at at)}{p_end}
{p 12 18 2}{cmd:replace `lhs' = }{it: <the function values>} {cmd:`if'}{p_end}
{p 8 14 2}{cmd:end}

{pstd}
When evaluating your function, remember to restrict the estimation 
sample using {cmd:`if'}.  Also, remember to include the weights if 
using commands such as {cmd:summarize} or {cmd:regress} if you intend 
to do weighted estimation.


{pstd}
{title:Example}

{pmore}
The CES production function can be written

{pmore2}
ln Q = b0 - 1/rho*ln{delta*K^-rho + (1-delta)*L^-rho}

{pmore}
where Q denotes output and b0, rho, and delta are parameters to be 
estimated.  The function evaluator program is

{p 12 18 2}{cmd:program nlces}{p_end}
{p 16 22 2}{cmd:syntax varlist(min=3 max=3) [aw fw iw] if, at(name)}{p_end}
{p 16 22 2}{cmd:local logout: word 1 of `varlist'}{p_end}
{p 16 22 2}{cmd:local capital: word 2 of `varlist'}{p_end}
{p 16 22 2}{cmd:local labor: word 3 of `varlist'}{p_end}
{p 16 22 2}{cmd:// Retrieve parameters out of at}{p_end}
{p 16 22 2}{cmd:tempname b0 rho delta}{p_end}
{p 16 22 2}{cmd:scalar `b0' = `at'[1,1]}{p_end}
{p 16 22 2}{cmd:scalar `rho' = `at'[1,2]}{p_end}
{p 16 22 2}{cmd:scalar `delta' = `at'[1,3]}{p_end}
{p 16 22 2}{cmd:// Some temporary variables}{p_end}
{p 16 22 2}{cmd:tempvar kterm lterm}{p_end}
{p 16 22 2}{cmd:generate double `kterm' = `delta'*`capital'^(-1*`rho') `if'}{p_end}
{p 16 22 2}{cmd:generate double `lterm' = (1-`delta')*`labor'^(-1*`rho') `if'}{p_end}
{p 16 22 2}{cmd:// Now fill in dependent variable}{p_end}
{p 16 22 2}{cmd:replace `logout' = `b0' - 1/`rho'*ln(`kterm'+`lterm') `if'}{p_end}
{p 12 18 2}{cmd:end}

{pmore}
Any of the following methods can be used to estimate the parameters:

{phang3}
1.  This method uses b0 = 0 as an initial value by default:

{p 16 22 2}
{cmd:. nl ces @ logout capital labor, parameters(b0 rho delta) initial(rho 1 delta 0.5)}

{phang3}
2.  This method initializes b0 to 2, rho to 1, and delta to 0.5.  Since we don't
give parameter names, nl names them b1, b2, and b3

{p 16 22 2}
{cmd: . nl ces @ logout capital labor, nparameters(3) initial(b1 2 b2 1 b3 0.5)} 

{phang3}
3.  This method sets up a vector holding the initial values:

{p 16 22 2}
{cmd: . matrix ivals = (2, 1, 0.5)}{p_end}
{p 16 22 2}
{cmd: . nl ces @ logout capital labor, parameters(b0 rho delta) initial(ivals)}

{p 16 22 2}
or

{p 16 22 2}
{cmd: . nl ces @ logout capital labor, nparameters(3) initial(ivals)}


{title:Also see}

{psee}
Manual:  {bf:[R] nl}

{psee}
Online:  {help nl postestimation};{break}
{helpb ml}, {helpb nlcom}, {helpb regress}
{p_end}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -