cnsreg.ado
来自「是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到」· ADO 代码 · 共 180 行
ADO
180 行
*! version 3.2.1 20dec2004
program define cnsreg, eclass byable(onecall)
if _by() {
local BY `"by `_byvars'`_byrc0':"'
}
`BY' _vce_parserun cnsreg, jkopts(eclass) : `0'
if "`s(exit)'" != "" {
exit
}
version 7.0, missing
if replay() {
if `"`e(cmd)'"'~="cnsreg" {
error 301
}
if _by() {
error 190
}
Replay `0'
exit
}
`BY' Estimate `0'
end
program Estimate, eclass byable(recall)
syntax varlist(ts) [if] [in] [aw fw] [, /*
*/ Level(cilevel) /*
*/ Constraints(string) noCONStant ]
if `"`constraints'"'=="" {
di in red "constraints() required"
exit 198
}
if `"`constant'"' == "" {
local cons "_cons"
}
marksample touse
tempname CP YY XY XX XXat Proj_M T Tt a b C TM1 /*
*/ b_dummy V_dummy R r t con_det j k ll
tokenize `varlist'
local y `"`1'"'
mac shift 1
local numcols : word count `*' `cons'
* create and post dummy matrices for mat makeCns
mat `b_dummy' = J(1,`numcols',0)
mat colnames `b_dummy' = `*' `cons'
mat `V_dummy' = J(`numcols',`numcols',0)
mat colnames `V_dummy' = `*' `cons'
mat rownames `V_dummy' = `*' `cons'
mat post `b_dummy' `V_dummy'
mat `V_dummy' = get(VCE)
* create/access constraint matrices
mat makeCns `constraints'
matcproc `T' `a' `C'
qui mat accum `CP' = `y' `*' [`weight'`exp'] if `touse', /*
*/ `constant'
local nobs = `r(N)'
mat `YY' = `CP'[1,1]
mat `XY' = `CP'[2...,1]
mat `XX' = `CP'[2...,2...]
mat `Tt' = `T''
mat `XXat' = `XX'*`a''
mat `TM1' = syminv(`Tt'*(`XX'*`T'))
if diag0cnt(`TM1') {
di in red "constraints insufficient for unique estimate"
error 412
}
* compute T(T'X'WXT)^(-1)T'
mat `Proj_M' = `T'*`TM1'*`Tt'
* compute coefficient vector b
mat `b' = (`Proj_M'*(`XY' - `XXat'))' + `a'
* compute mse and VCE
tempname MSE SSE VCE
mat `SSE' = `YY' - 2*`b'*`XY' + `b'*`XX'*`b''
scalar `MSE' = `SSE'[1,1]
local cdim = colsof(`C')
local cdim1 = `cdim' - 1
local df = `nobs' - colsof(`b') + rowsof(`C')
scalar `MSE' = `MSE'/`df'
mat `VCE' = `MSE'*`Proj_M'
scalar `ll' = -0.5 * r(N) * /*
*/ ( ln(2*_pi) + ln(`SSE'[1,1]/`nobs') + 1 )
/* create R and r before post */
matrix `R' = `C'[1...,1..`cdim1']
matrix `r' = `C'[1...,`cdim']
est post `b' `VCE' `C', obs(`nobs') dof(`df') dep(`y') /*
*/ esample(`touse')
/* Test model = 0 is only sensible when r = 0 or when there
is a single nonzero entry of r that corresponds to a
constraint: _cons = number
*/
matrix `t' = `r''*`r' /* if t = 0, we will test model */
if `t'[1,1] ~= 0 {
/* Find nonzero entry of `r' and check that
there is only one
*/
scalar `j' = 1
scalar `k' = 0
while `j' <= rowsof(`r') {
if `r'[`j',1] ~= 0 {
if `k' == 0 {
scalar `k' = `j'
}
/* else two or more nonzeros */
else scalar `k' = -1
}
scalar `j' = `j' + 1
}
if `k' > 0 & "`constant'" ~= "" {
scalar `k' = -1
}
/* Look for constraint: _cons = number */
if `k' > 0 {
local kk = `k'
matrix `t' = `R'[`kk',1...]
local kc = colnumb(`V_dummy',"_cons")
matrix `t'[1,`kc'] = 0
matrix `t' = `t'*`t''
/* if t = 0, we will test model */
}
}
if `t'[1,1] == 0 {
quietly test `*'
est scalar df_m = r(df)
est scalar F = r(F)
}
else {
est scalar df_m = .
est scalar F = .
}
est scalar N = `nobs'
est scalar df_r = `df'
est scalar rmse = sqrt(`MSE')
est scalar ll = `ll'
est local depvar `"`y'"'
est local predict "tobit_p"
est local cmd "cnsreg"
est local wtype "`weight'"
est local wexp "`exp'"
/* double save in S_E_<stuff> and e() */
global S_E_mdf `e(df_m)'
global S_E_f `e(F)'
global S_E_nobs `e(N)'
global S_E_tdf `e(df_r)'
global S_E_rms `e(rmse)'
global S_E_depv `e(depvar)'
global S_E_cmd "cnsreg"
Replay, level(`level')
end
program Replay
syntax [, Level(cilevel) ]
#delimit ;
di in gr _n "Constrained linear regression"
_col(56) "Number of obs =" in ye %8.0f e(N) ;
if e(df_m) < . {
di in gr _col(56) "F(" in ye %3.0f e(df_m) in gr ","
in ye %6.0f e(df_r) in gr ") =" in ye %8.2f e(F)
_n in gr _col(56) "Prob > F =" in ye %8.4f
fprob(e(df_m),e(df_r),e(F)) ;
} ;
#delimit cr
di in gr _col(56) "Root MSE = " in ye %7.0g e(rmse)
matrix dispCns
est di, level(`level')
end
exit
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?