📄 nl_7.ado
字号:
*/ "cannot calculate derivatives"
exit 481
}
if `logtsf' {
* could use dln(f(x))/dx =
* (1/f(x)) df(x)/dx here
replace `J`j''=ln( /*
*/ (`YHATi'-`lnlsq')/(`YHAT'-`lnlsq') /*
*/ ) / `incr' if `touse'
}
else replace `J`j''= (`YHATi'-`YHAT')/`incr' /*
*/ if `touse'
* replace param j with nonincremented value
global ``j'' = `old_pj'
local j = `j'+1
}
/*
Update parameter estimates and test for convergence
by max proportional iterative change in each param.
See Gallant p 29.
*/
reg `RESID' `Jnames' `exp' if `touse', nocons
scalar `ssr' = .
scalar `f' = 1
* name is ``j''
* parameter value is ${``j''}
while (`ssr' > `old_ssr') { /* backup loop */
local cnvrge 1 /* parameter convergence flag */
if (`f'!=1) {
local j 1
while `j' <= `np' {
global ``j'' `op`j''
local j=`j'+1
}
}
local j 1
while `j' <= `np' {
local op`j' ${``j''}
global ``j'' = `op`j''+`f'*_coef[`J`j'']
if `f'*abs(_coef[`J`j'']) > /*
*/ `eps'*(abs(`op`j'')+`tau') {
local cnvrge 0
}
local j=`j'+1
}
cap estimates hold `hest'
cap `version' nl`func' `YHAT' `fargs' , /*
*/ `options'
if _rc {
local rc = _rc
if "$T_nlferr" != "" {
noi di
noi di in red "$T_nlferr"
exit 498
}
else {
noi di in red "`funcerr'"
}
cap estimates unhold `hest'
exit `rc'
}
cap estimates unhold `hest'
cap assert `YHAT'<. if `touse'
if _rc==0 {
if `logtsf' {
replace `RESID' = `YVAR' - /*
*/ log(`YHAT'-`lnlsq')/*
*/ if `touse'
}
else replace `RESID' = `YVAR'-`YHAT' /*
*/ if `touse'
est hold `dbeta'
reg `RESID' `Z' `exp' if `touse', nocons
scalar `ssr' = e(rss)*`gm2'
est unhold `dbeta'
if "`ssr'"=="" {
scalar `ssr' = . /* bug fix */
}
}
scalar `f'=`f'/2
} /* backup loop */
if "`log'"=="" {
if "`trace'"!="" {
local trcol "_col(42)"
local trnl "_n"
}
noi di in gr _col(16) `trcol' /*
*/ "`scaled'residual SS = " /*
*/ in ye %9.0g `old_ssr' `trnl'
}
if abs(`old_ssr'-`ssr') > /*
*/ `eps'*(`old_ssr'+`tau') {
local cnvrge 0
}
scalar `old_ssr' = `ssr'
local done `cnvrge'
local its = `its'+1
if `its' >= `iterate' { local done 1 }
}
/*
End of main loop.
Check if sd(any derivative vector) = 0, i.e. that
that parameter is a 'regression constant'
to reasonable accuracy.
*/
local j 1
local const 0
local consj 0
while `j' <= `np' {
* name is ``j''
* value is ${``j''}
qui sum `J`j''
if sqrt(r(Var)) < `eps'*(abs(r(mean))+`tau') {
local const 1
local consj `j'
}
local j = `j'+1
}
/*
If the non-linear model appears to have no constant,
regress y on 0 thru the origin to calc its
(uncorrected) SS as the residual SS, otherwise use
the usual corrected SS.
*/
if `const' == 0 {
reg `YVAR' `Z' `exp' if `touse', nocons
local dftot = `nobs'
scalar `sstot' = e(rss)*`gm2'
}
else {
local dftot = `nobs'-1
scalar `sstot' = `dftot'*`vary'*`gm2'
}
/*
Perform reg using final residuals (last ssr is still valid),
calc ANOVA table and display results.
*/
qui reg `RESID' `Jnames' `exp' if `touse', nocons
local dfm = e(df_m)-`const'
tempname bvec VCE
mat `bvec' = get(_b)
mat `VCE' = get(VCE)
* mat `VCE' = (e(df_r) / (e(df_r)+1)) * `VCE'
mat rownames `VCE' = `params'
mat colnames `VCE' = `params'
mat colnames `bvec' = `params'
local k 1
while `k'<=`np' {
mat `bvec'[1,`k'] = ${``k''}
local k=`k'+1
}
local tdf = `dftot' - `dfm'
est post `bvec' `VCE', /*
*/ obs(`nobs') dof(`tdf') dep(`yname') esamp(`touse')
est scalar df_m = `dfm'
est scalar df_r = `tdf'
est scalar df_t = `dftot'
est scalar mss = `sstot'-`ssr'
est scalar mms = e(mss)/e(df_m)
est scalar msr = `ssr'/e(df_r)
est scalar rmse = sqrt(e(msr))
est scalar F = e(mms)/e(msr)
est scalar r2 = 1-`ssr'/`sstot'
est scalar r2_a = 1-(1-e(r2))*`dftot'/e(df_r)
/*
calc -2(log likelihood) allowing for possible transformation
(formula may need rechecking, esp when weights present)
*/
est scalar dev = `nobs'*(1+log(2*_pi*`ssr'/`nobs')-`mnlnwt')
/* Double saves */
global S_E_dfm "`e(df_m)'"
global S_E_dfr "`e(df_r)'" /* Royston original */
global S_E_tdf "`e(df_r)'" /* new standard */
global S_E_ssm "`e(mss)'"
global S_E_msm "`e(mms)'"
global S_E_msr "`e(msr)'"
global S_E_sdr "`e(rmse)'"
global S_E_f "`e(F)'"
global S_E_rsq "`e(rsq)'"
global S_E_rsqa "`e(rsqa)'"
global S_E_devi "`e(dev)'"
} /* capture */
if _rc {
local rc=_rc
capture est unhold `dbeta'
exit `rc'
}
if "`leave'"~="" {
local j 1
while "``j''"~="" {
rename `J`j'' ``j''
local j = `j' + 1
}
}
est scalar converge = `cnvrge'
est scalar N = `nobs'
est scalar rss = `ssr'
est scalar df_t = `dftot'
est scalar tss = `sstot'
est local depvar "`yname'"
est scalar cj = `consj'
est local params "`params'"
est local function "`func'"
est local version "`version'"
est scalar gm_2 = `gm2'
est scalar k = `np'
est local f_args "`fargs'"
est local options "`options'"
est local title "`title'"
est local title_2 "`title2'"
est scalar log_t = `logtsf'
est scalar lnlsq = `lnlsq'
est scalar ic = `its'-1
est local predict nl_p_7
est local cmd "nl" /* must be last */
/* Double saves */
global S_E_cnvr "`e(converge)'"
global S_E_nobs "`e(N)'"
global S_E_ssr "`e(rss)'"
global S_E_dft "`e(df_t)'"
global S_E_sst "`e(tss)'"
global S_E_depv "`e(depvar)'"
global S_E_cj "`e(cj)'"
global S_E_rhs "`e(params)'"
global S_E_fcn "`e(function)'"
global S_E_np "`e(k)'"
global S_E_fvl "`e(f_args)'"
global S_E_fops "`e(options)'"
global S_E_ttl "`e(title)'"
global S_E_ttl2 "`e(title_2)'"
global S_E_lts "`e(log_t)'"
global S_E_lnl2 "`e(lnlsq)'"
global S_E_gm2 "`e(gm_2)'"
global S_E_cmd "`e(cmd)'" /* must be last */
if "`disp'" == "" {
NLout `level'
}
end
program define NLout
args level
global S_1 e(N) /* Number of observations */
global S_2 e(mss) /* model sum of squares */
global S_3 e(df_m) /* model degrees of freedom */
global S_4 e(rss) /* residual sum of squares */
global S_5 e(df_r) /* residual degrees of freedom */
global S_6 e(F) /* model F-statistic */
global S_7 e(r2) /* R-square */
global S_8 e(r2_a) /* adjusted R-square */
global S_9 e(rmse) /* residual root mean square */
global S_10 e(dev) /* residual deviance (-2 * log likelihood) */
global S_11 e(gm_2) /* geom mean (y-k)]^2 if lnlsq(k), else 1 */
global S_12 e(converge) /* 0 if convergence failed, 1 otherwise */
#delimit ;
di in gr _n " Source {c |} SS df MS"
_col(54) "Number of obs = " in ye %9.0f e(N) ;
di in gr "{hline 13}{c +}{hline 30}"
in gr _col(54) "F(" %3.0f e(df_m) "," %6.0f e(df_r)
") = " in ye %9.2f e(F) ;
di in gr " Model {c |} " in ye %11.0g e(mss) " " %5.0f e(df_m)
" " %11.0g e(mms)
in gr _col(54) "Prob > F = "
in ye %9.4f Ftail(e(df_m),e(df_r),e(F)) ;
di in gr " Residual {c |} " in ye %11.0g e(rss) " " %5.0f e(df_r)
" " %11.0g e(msr)
in gr _col(54) "R-squared = " in ye %9.4f e(r2) ;
di in gr "{hline 13}{c +}{hline 30}"
in gr _col(54) "Adj R-squared = " in ye %9.4f e(r2_a) ;
di in gr " Total {c |} " in ye %11.0g e(tss) " " %5.0f e(df_t)
" " %11.0g e(tss)/e(df_t)
in gr _col(54) "Root MSE = "
in ye %9.0g e(rmse) ;
di in gr _col(54) "Res. dev. = " in ye %9.0g e(dev) ;
#delimit cr
/*
Display coefficients, standard errors and ci's
*/
tempname invt
scalar `invt' = invttail(e(df_r), (1-`level'/100)/2)
di in gr "`e(title)'"
if "`e(title_2)'" != "" {
di "`e(title_2)'"
}
est di, level(`level')
if e(cj) ~= 0 {
local word : word `e(cj)' of `e(params)'
di in gr /*
*/ "* Parameter `word' taken as constant term in model & ANOVA table"
}
di in gr /*
*/ " (SEs, P values, CIs, and correlations are asymptotic approximations)"
if e(converge) ~= 1 {
error 430
}
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -