📄 boxcox.ado
字号:
while "`1'" != "" {
global T_notr : subinstr global T_notr /*
*/ "`1'" "", word
#delimit ;
ml model rdu0 "boxco_l LHS"
(ntrans: `dep' = )
if `touse' $T_wtexp, technique(nr)
search(off) `mlopts'
`start' max `loglr' ;
#delimit cr
scalar `lltr'=2*(`llf'-e(ll))
scalar `pv' = 1-chi2(1, `lltr' )
matrix `chi2' = nullmat(`chi2') , /*
*/ cond(`lltr'>=., -1, `lltr')
matrix `p' = nullmat(`p') , /*
*/ cond(`pv'>=., -1, `pv')
matrix `df' = nullmat(`df') , 1
local pname `pname' `1'
local pcol `pcol' Notrans
local c2name `c2name' `1'
local c2col `c2col' Notrans
global T_notr "`notr'"
macro shift
}
tokenize $T_parm
while "`1'" != "" {
global T_parm : subinstr global /*
*/ T_parm "`1'" "", word
#delimit ;
ml model rdu0 "boxco_l LHS"
(ntrans: `dep' = )
if `touse' $T_wtexp, technique(nr)
search(off) `mlopts'
`start' max `loglr' ;
#delimit cr
scalar `lltr'=2*(`llf'-e(ll))
scalar `pv' = 1-chi2(1, `lltr' )
matrix `chi2' = nullmat(`chi2') , /*
*/ cond(`lltr'>=., -1, `lltr')
matrix `p' = nullmat(`p') , /*
*/ cond(`pv'>=., -1, `pv')
matrix `df' = nullmat(`df') , 1
local pname `pname' `1'
local pcol `pcol' Notrans
local c2name `c2name' `1'
local c2col `c2col' Notrans
global T_parm "`parm'"
macro shift
}
matrix colnames `p'= `pname'
matrix coleq `p' = `pcol'
matrix colnames `chi2'= `c2name'
matrix coleq `chi2' = `c2col'
matrix colnames `df'= `c2name'
matrix coleq `df' = `c2col'
}
/* CALL POST PROGRAM */
BasePost `touse' "`model'" `bf' `Vf' `bpar' `lsig'
if "`lrtest'" != "" {
est matrix chi2m `chi2'
est matrix pm `p'
est matrix df `df'
}
if "$T_nocns" == "" & "`LL0'" != "" {
local llt=2*(`llf'-`LL0')
est scalar ll0=`LL0'
est scalar df_r=`df0'
est scalar chi2=`llt'
}
est scalar ll=`llf'
est scalar chi2_t1=`chit1'
est scalar p_t1 =`p_t1'
est scalar ll_t1=`ll_t1'
est scalar chi2_tm1=`chitm1'
est scalar p_tm1 =`p_tm1'
est scalar ll_tm1=`ll_tm1'
est scalar chi2_t0=`chit0'
est scalar p_t0 =`p_t0'
est scalar ll_t0=`ll_t0'
est scalar ic=`ic'
est scalar rc=`rc'
/* est local wtype="`wt'" */
est local wtype="`weight'"
if "`wt'" != "" {
est local wexp="`exp'"
}
est local depvar "`dep'"
est local lrtest "`lrtest'"
est local chi2type "LR"
est local predict "boxco_p"
est local model "`model'"
if "`ntrans'"=="" & "$T_nocns" != "" {
est local ntrans ""
}
else est local ntrans "yes"
est local cmd "boxcox"
macro drop T_*
Display, level(`level')
exit
}
/* END LHS ONLY MODEL */
/* THIS SECTION DOES FULL MODEL WITH COMMON LAMBDA OR
LAMBDA ON THE RHS AND THETA ON THE LHS */
tempname bvec bvec2 sig
global T_bvec "`bvec'"
global T_sig "`sig'" /* use the globals to work on
the matrix and the scalar in the
evaluator program */
if "`model'" == "theta" {
local lrhs "/theta"
local evalpgm "Theta"
}
else {
local evalpgm "Lambda"
}
/* Get starts for full model */
if `lstart' != 1 {
di as txt "lstart only valid for LHS or RHS model" /*
*/ "--option ignored"
}
if "`from'" != "" {
local starts "init(`from')"
}
else {
if "`model'"=="theta" {
matrix `bvec'= (1, 1)
}
else {
matrix `bvec'= 1
}
local starts "init(`bvec', copy)"
}
di as txt _n "Fitting full model"
ml model rdu0 "boxco_l `evalpgm'" (ntrans: `dep' = ) /*
*/ `lrhs' if `touse' $T_wtexp, technique(nr) /*
*/ search(off) `mlopts' /*
*/ `starts' max `log'
tempname llf bf Vf bpar lsig
scalar `llf' = e(ll)
local ic=e(ic)
local rc=e(rc)
local wt "`e(wtype)'"
local wex "`e(wexp)'"
matrix `Vf'=e(V)
matrix `bpar'=e(b)
/* make call to evaluator with converged lambda */
global ML_y1 `dep'
global ML_samp `touse'
tempname lamc bc
matrix `bc'=e(b)
tempvar lltmp
gen double `lltmp' = 0
boxco_l `evalpgm' `lltmp' `bc' `lltmp'
matrix `bf'=$T_bvec
scalar `lsig' = $T_sig
macro drop ML_y1
macro drop ML_samp
/* Do LR tests lambda=theta=-1,0,1 Note Lambda and Theta
models have same restricted LL in this case */
tempname chit1 p_t1 chitm1 p_tm1 chit0 p_t0 ll_t1 ll_tm1 ll_t0
lincompT `ll_t1' `touse' /* program for computing LL
when theta=1*/
scalar `chit1'=2*(`llf'-`ll_t1')
scalar `p_t1'=1-chi2(1, `chit1')
invcompT `ll_tm1' `touse' /* program for computing LL
when theta=-1 */
scalar `chitm1'=2*(`llf'-`ll_tm1')
scalar `p_tm1'=1-chi2(1, `chitm1')
logcompT `ll_t0' `touse' /* program for computing LL
when theta =0 */
scalar `chit0'=2*(`llf'-`ll_t0')
scalar `p_t0'=1-chi2(1, `chit0')
if "`lrtest'" != "" {
di _n as txt "Fitting comparison models for LR tests"
tempname p chi2 df
local firstlr 1
local notr "$T_notr"
local parm "$T_parm"
tempname lltr pv
tokenize $T_notr
while "`1'" != "" {
global T_notr : subinstr global T_notr /*
*/ "`1'" "", word
#delimit ;
ml model rdu0 "boxco_l `evalpgm'"
(ntrans: `dep' = ) `lrhs'
if `touse' $T_wtexp, technique(nr)
search(off) `mlopts'
`start' max `loglr' ;
#delimit cr
scalar `lltr'=2*(`llf'-e(ll))
scalar `pv' = 1-chi2(1, `lltr' )
matrix `chi2' = nullmat(`chi2') , /*
*/ cond(`lltr'>=., -1, `lltr')
matrix `p' = nullmat(`p') , cond(`pv'>=., -1, `pv')
matrix `df' = nullmat(`df') , 1
local pname `pname' `1'
local pcol `pcol' Notrans
local c2name `c2name' `1'
local c2col `c2col' Notrans
global T_notr "`notr'"
macro shift
}
local n : word count $T_parm
tokenize $T_parm
while "`1'" != "" {
global T_parm : subinstr global /*
*/ T_parm "`1'" "", word
if `n'==1 & "`model'"=="theta" {
di as txt _n "Comparison model " /*
*/ "for `1' is left-hand " /*
*/ "side only."
di as txt "Lambda is not " /*
*/ "identified in the "
di as txt "restricted model "
local dfs 2
#delimit ;
ml model rdu0 "boxco_l LHS"
(ntrans: `dep' = ) if `touse'
$T_wtexp, technique(nr)
search(off) `mlopts'
`start' max `loglr' ;
#delimit cr
}
else {
local dfs 1
#delimit ;
ml model rdu0 "boxco_l `evalpgm'"
(ntrans: `dep' = )
`lrhs' if `touse'
$T_wtexp, technique(nr)
search(off) `mlopts' `start'
max `loglr' ;
#delimit cr
}
scalar `lltr'=2*(`llf'-e(ll))
scalar `pv' = 1-chi2(`dfs', `lltr' )
matrix `chi2' = nullmat(`chi2') , /*
*/ cond(`lltr'>=., -1, `lltr')
matrix `p' = nullmat(`p') , cond(`pv'>=., -1, `pv')
matrix `df' = nullmat(`df') , 1
local pname `pname' `1'
local pcol `pcol' Notrans
local c2name `c2name' `1'
local c2col `c2col' Notrans
global T_parm "`parm'"
macro shift
}
matrix colnames `p'= `pname'
matrix coleq `p' = `pcol'
matrix colnames `chi2'= `c2name'
matrix coleq `chi2' = `c2col'
matrix colnames `df'= `c2name'
matrix coleq `df' = `c2col'
}
/* CALL POST PROGRAM */
BasePost `touse' "`model'" `bf' `Vf' `bpar' `lsig'
if "`lrtest'" != "" {
est matrix chi2m `chi2'
est matrix pm `p'
est matrix df `df'
}
if "$T_nocns" == "" {
local llt=2*(`llf'-`LL0')
est scalar df_r=`df0'
est scalar chi2=`llt'
est scalar ll0=`LL0'
}
est scalar ll=`llf'
est scalar chi2_t1=`chit1'
est scalar p_t1 =`p_t1'
est scalar ll_t1=`ll_t1'
est scalar chi2_tm1=`chitm1'
est scalar p_tm1 =`p_tm1'
est scalar ll_tm1=`ll_tm1'
est scalar chi2_t0=`chit0'
est scalar p_t0 =`p_t0'
est scalar ll_t0=`ll_t0'
est scalar ic=`ic'
est scalar rc=`rc'
/* est local wtype="`wt'" */
est local wtype="`weight'"
if "`wt'" != "" {
est local wexp="`exp'"
}
est local depvar "`dep'"
est local lrtest "`lrtest'"
est local chi2type "LR"
est local predict "boxco_p"
if "`ntrans'"=="" & "$T_nocns" != "" {
est local ntrans ""
}
else {
est local ntrans "yes"
}
est local model "`model'"
est local cmd "boxcox"
macro drop T_*
/* Call display programs */
Display, level(`level')
end
program define mparse, sclass
sret clear
syntax, [LHSonly RHSonly LAMbda THETA ]
if "`lhsonly'`rhsonly'`lambda'`theta'" == "" {
sret local model "lhsonly"
}
else {
local sum = ("`lhsonly'" != "") + ("`rhsonly'" != "") /*
*/ + ("`lambda'" != "") + ("`theta'" != "")
if `sum' > 1 {
di as err /*
*/ "model specifications are mutually exlusive: specify only one model"
exit 198
}
sret local model "`lhsonly'`rhsonly'`lambda'`theta'"
}
end
program define bhead
di
tempname p
local dft= e(df_m)-e(df_r)
#delimit ;
di _col(51) as txt "Number of obs" _col(67) "= " _col(68)
%10.0g as res e(N);
di _col(51) as txt "LR chi2(" as res `dft' as txt ")"
_col(67) "=" _col(69) %10.2f as res e(chi2);
scalar `p' = 1-chi2(`dft',e(chi2) );
di as txt "Log likelihood = " as res e(ll) _col(51) as txt "Prob > chi2"
_col(67) "=" _col(69) as res %10.3f `p';
di " ";
#delimit cr
end
program define btitle
args level
local cil `=string(`level')'
local cil `=length("`cil'")'
#delimit ;
di as txt "{hline 13}{c TT}{hline 64}" ;
di %12s as txt abbrev("`e(depvar)'",12) " {c |}"
as txt _col(12) " Coef. Std. Err. z P>|z|"
_col(`=61-`cil'') `"[`=strsubdp("`level'")'% Conf. Interval]"';
di as txt "{hline 13}{c +}{hline 64}" ;
#delimit cr
end
program define cdisp
di as txt "{hline 13}{c BT}{hline 64}"
di " "
di as txt "Estimates of scale-variant parameters"
di as txt "{hline 13}{c TT}{hline 14}
di as txt _col(14) "{c |} Coef."
tempname b
matrix `b'=e(b)
local eqs : coleq `b'
local names : colnames `b'
local cnt 1
tokenize `eqs'
if "`1'" == "Notrans" {
di as txt "{hline 13}{c +}{hline 14}"
di as res "Notrans" _col(14) as txt "{c |}"
}
while "`1'" == "Notrans" {
local coef : word `cnt' of `names'
di as txt %12s abbrev("`coef'",12) " {c |} " /*
*/ as res %9.0g `b'[1,`cnt']
local cnt = `cnt' +1
macro shift
}
if "`1'" == "Trans" {
di as txt "{hline 13}{c +}{hline 14}"
di as res "Trans" _col(14) as txt "{c |}"
}
while "`1'" == "Trans" {
local coef : word `cnt' of `names'
di as txt %12s abbrev("`coef'",12) " {c |} " /*
*/ as res %9.0g `b'[1,`cnt']
local cnt = `cnt' +1
macro shift
}
while "`1'" != "sigma" {
local cnt = `cnt' +1
macro shift
}
if "`1'" == "sigma" {
di as txt "{hline 13}{c +}{hline 14}"
di as txt %12s "/sigma" " {c |} " as res /*
*/ %9.0g `b'[1,`cnt']
di as txt "{hline 13}{c BT}{hline 14}"
}
end
program define cdisplr
di as txt "{hline 13}{c BT}{hline 64}"
di " "
di as txt "Estimates of scale-variant parameters"
di as txt "{hline 13}{c TT}{hline 47}"
di as txt _col(14) "{c |} Coef." _col(27) /*
*/ " chi2(df)" /*
*/ _col(36) " P>chi2(df)" /*
*/ _col(52) "df of chi2"
tempname b chi p df
matrix `b'=e(b)
matrix `chi'=e(chi2m)
matrix `p'=e(pm)
matrix `df'=e(df)
local eqs : coleq `b'
local names : colnames `b'
local cnt 1
local pcnt 1
tokenize `eqs'
if "`1'" == "Notrans" {
di as txt "{hline 13}{c +}{hline 47}"
di as res "Notrans" _col(14) as txt "{c |}"
}
while "`1'" == "Notrans" {
local coef : word `cnt' of `names'
if "`coef'" == "_cons" {
di as txt %12s abbrev("`coef'",12) " {c |}" /*
*/ _col(17) as res %9.0g `b'[1,`cnt']
}
else {
di as txt %12s abbrev("`coef'",12) " {c |} " /*
*/ as res %9.0g `b'[1,`cnt'] " " %9.3f /*
*/ `chi'[1,`pcnt'] " " %7.3f /*
*/ `p'[1,`pcnt'] _col(54) %2.0f `df'[1,`pcnt']
local pcnt = `pcnt' +1
}
local cnt = `cnt' +1
macro shift
}
if "`1'" == "Trans" {
di as txt "{hline 13}{c +}{hline 47}"
di as res "Trans" _col(14) as txt "{c |}"
}
while "`1'" == "Trans" {
local coef : word `cnt' of `names'
di as txt %12s abbrev("`coef'",12) " {c |} " /*
*/ as res %9.0g `b'[1,`cnt'] " " %9.3f /*
*/ `chi'[1,`pcnt'] " " %7.3f /*
*/ `p'[1,`pcnt'] _col(54) %2.0f `df'[1,`pcnt']
local cnt = `cnt' +1
local pcnt = `pcnt' +1
macro shift
}
while "`1'" != "sigma" {
local cnt = `cnt' +1
macro shift
}
if "`1'" == "sigma" {
di as txt "{hline 13}{c +}{hline 47}"
di as txt %12s "/sigma" _col(14) "{c |} " /*
*/ as res %9.0g `b'[1,`cnt']
di as txt "{hline 13}{c BT}{hline 47}"
}
end
program define BasePost, eclass
args touse model bf Vf bpar lsig
tempname b1 b V V1 newb
matrix `b1'=`bf'
local size = colsof(`b1')
local eqnames ""
local names ""
local cnt 1
local first 1
tokenize $T_notr
while "`1'" != "" {
local eqnames `eqnames' Notrans
local names `names' `1'
if `first'==1 {
matrix `newb'=`b1'[1,`cnt']
local first 0
}
else matrix `newb'=`newb',`b1'[1,`cnt']
macro shift
local cnt =`cnt' +1
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -