📄 arima.ado
字号:
/*
Don't think this is needed, since we always stick
sigma at the end of `b0' -- see about 25 or 30 lines
down.
else {
qui sum `u' if `touse'
mat `b0' = nullmat(`b0') , sqrt(`r(Var)'*(r(N)-1)/r(N))
}
*/
/* name stripe, eqns, and inits for mar mma */
foreach d in `mdeltas' {
if `d' == 1 & "`ar'`ma'" != "" { continue }
if `d' == 1 {
AddStrip colnams : "`colnams'" "`mar_`d''" ARMA ar
AddStrip colnams : "`colnams'" "`mma_`d''" ARMA ma
}
else {
AddStrip colnams : "`colnams'" "`mar_`d''" ARMA`d' ar
AddStrip colnams : "`colnams'" "`mma_`d''" ARMA`d' ma
}
if "`mar_`d''" != "" {
local arterms "l(`mar_`d'').`u'"
local multeqns `multeqns' /*
*/ (AR`d'_terms: `yprefix' = `arterms', nocons )
local yprefix
}
if "`mma_`d''" != "" {
local materms "l(`mma_`d'').`e_t'"
local multeqns `multeqns' /*
*/ (MA`d'_terms: `yprefix' = `materms', nocons )
local yprefix
}
mat `T' = J(1, `:word count `mar_`d'' `mma_`d''', 0)
mat `b0' = nullmat(`b0') , `T'
capture confirm numeric variable `e_t'
if _rc {
qui gen double `e_t' = `u'
}
}
local colnams `colnams' sigma:_cons
local sigeqn "(SIGMA: `yprefix' = )"
local yprefix
mat `b0' = nullmat(`b0'), `sigma'
/* Report time gaps */
tsreport if `touse', report `detail'
local gaps `r(N_gaps)'
if `gaps' > 0 {
di in bl "(note: filtering over missing observations)"
}
qui count if `touse'
local nobs `r(N)'
/* End starting values and matrix stripe */
/* Maximize -- ML */
/* Handle constraints, if any */
if "`constraints'" != "" {
/* make constraints using
* dummy matrices */
tempname b V T a C
/* set full model */
ml model rdu0 arima_dr `xbeqn' `areqn' `maeqn' `multeqns' /*
*/ `sigeqn', `techniq' missing nopreserve collinear /*
*/ `vce'
mat `b' = $ML_b
local eqnames : coleq `b' /* for saved results */
mat colnames `b' = `colnams'
mat `V' = `b'' * `b'
mat post `b' `V'
capture mat makeCns `constraints'
if _rc {
local rc = _rc
di in red "Constraints invalid:"
mat makeCns `constraints'
exit _rc
}
matcproc `T' `a' `C'
global TT `T' /* globals for LL evaluator */
global Ta `a'
global Tstripe : colfullnames $ML_b
/* constrain b0 */
if "`from'" != "" & "`armab0'" == "" {
_mkvec `b0', from(`from') first /*
*/ colnames(`colnams') error("from()")
}
mat `b0' = (`b0' - `a') * `T'
/* for constrained model,
* just feed ml first varnames
* of unconstrained model */
tempname one
qui gen byte `one' = . in 1
mat `b' = $ML_b[1, 1..colsof(`b0')]
local cnsvars : colnames `b'
local cnsvars : subinstr local cnsvars "_cons" "`one'", all w
/* give ML constrained model */
local xbeqn "(`dep_m' = `cnsvars', nocons)"
local areqn
local maeqn
local multeqns
local sigeqn
}
/* Set initial values */
if "`from'" != "" & "`armab0'" == "" & "`constraints'" == "" {
_mkvec `b0', from(`from') first colnames(`colnams') /*
*/ error("from()")
}
local from "`b0', copy"
/* Fit the model */
noi ml model rdu0 arima_dr /*
*/ `xbeqn' `areqn' `maeqn' `multeqns' `sigeqn' /*
*/ [`weight'`exp'] if `touse' , obs(`nobs') /*
*/ title(ARIMA regression) /*
*/ init(`from') `log' `score' `robust' /*
*/ `stdopts' `mlopts' `techniq' gtolerance(`gtolerance') /*
*/ `bracket' search(off) missing maximize nooutput /*
*/ nopreserve collinear `vce'
if "`constraints'" != "" { /* put problem back in original space */
mat `b' = get(_b) * `T'' + `a'
mat `V' = `T' * e(V) * `T''
mat post `b' `V' `C', noclear
}
/* Fix-up names on matrices */
mat `b0' = e(b)
est scalar sigma = `b0'[1, colsof(`b0')]
if "`constraints'" == "" {
local eqnames : coleq `b0' /* for saved results */
}
mat colnames `b0' = `colnams'
mat repost _b=`b0', rename
/* Model Chi */
if "`ind_m'" != "" {
tsrevar `dep_m', list
qui test [`r(varlist)']
local accum accum
}
foreach i of global Tseasons {
if `i' == 1 {
qui test [ARMA], `accum'
local accum accum
}
else {
qui test [ARMA`i'], `accum'
local accum accum
}
}
/* Saved results */
est scalar p = e(p)
est scalar df_m = r(df)
est scalar chi2 = r(chi2)
if "$Tp0" != "" { est matrix P0 $Tp0 }
if "$Tstate0" != "" {
mat `T' = $Tstate0' * $Tstate0
if `T'[1,1] != 0 { est matrix Xi0 $Tstate0 }
}
local ct : word count `ind_m'
est scalar k1 = `ct' + ("`constant'" == "")
sum `timevar' if `touse' , meanonly
est scalar tmin = r(min)
est scalar tmax = r(max)
local fmt : format `timevar'
est local tmins = trim(string(r(min), "`fmt'"))
est local tmaxs = trim(string(r(max), "`fmt'"))
est scalar k_eq = ("`ind_m'"!=""|"`constant'"=="") + /*
*/ `:word count `mdeltas'' + 1
est scalar N_gaps = `gaps'
est scalar ar_max = `maxmar'
est scalar ma_max = `maxmma'
foreach m in mma mar {
forvalues i = 1/8 {
if "``m'`i'_n'" != "" {
est local `m'`i' ``m'`i'_n'
}
}
}
est local seasons $Tseasons
if "$Tbadstic" != "" { est local unsta "possibly nonstationary" }
est local cnslist `constraints'
est local eqnames `eqnames'
est local diffuse `diffuse'
est local ar `ar'
est local ma `ma'
est local estat_cmd arima_estat
est local predict arima_p
est local tech `technique'
if "`e(vcetype)'" == "Robust" {
est local vcetype `e(vcetype2)' Semi-robust
}
if "`e(vce)'" == "oim" {
est local vcetype "OIM"
}
est local cmd "arima"
/* Results */
Display , `coef' `head' level(`level')
mac drop T*
end
program define Display
syntax [, Level(cilevel) noCOEF noHEAD ]
if "`head'" == "" { _tsheadr }
if "`coef'" == "" {
local k = e(k_eq) - 1
estimates display , neq(`k') plus level(`level')
_diparm sigma, level(`level')
di in smcl in gr "{hline 13}{c BT}{hline 64}"
}
if "`e(unsta)'" != "" {
di in blue "attempts were made to step into nonstationary " /*
*/ "regions during optimization"
di in blue "solution constrained to be stationary, but " /*
*/ "may not be global maximum likelihood"
di in blue "may wish to re-run model with -diffuse- option"
}
end
program define DropDup /* Remove duplicate tokens from a sorted list */
args listfix /* a macro name to hold the the fixed list
*/ colon /* :
*/ list /* original list */
tokenize `list'
local prev `1'
local i 2
while "``i''" != "" {
if "``i''" == "`prev'" {
local ``i''
}
else local prev ``i''
local i = `i' + 1
}
c_local `listfix' `*'
end
program define AddStrip
args stripe /* macro to contain full matrix names
*/ colon /* :
*/ stripe0 /* current contents of strip
*/ laglist /* list of lags
*/ eqname /* equation name for this part of stripe
*/ varname /* variable name for this part of stripe */
tokenize `laglist'
local i 1
while "``i''" != "" {
local part `part' `eqname':L``i''.`varname'
local i = `i' + 1
}
c_local `stripe' `stripe0' `part'
end
program define FixARMA
args b ar ma
tempname bsum
scalar `bsum' = 0
/* AR terms */
local nar : word count `ar'
local i 1
while `i' <= `nar' {
scalar `bsum' = `bsum' + `b'[1, `i']
local i = `i' + 1
}
if abs(`bsum') > .95 {
mat `b'[1,1] = (.95 / abs(`bsum')) * `b'[1,1..`nar']
}
/* MA terms */
local nma : word count `ma'
local narma = `nma' + `nar'
local i = `nar' + 1
while `i' <= `narma' {
if abs(`b'[1, `i']) > 0.95 {
mat `b'[1, `i'] = .95 * `b'[1, `i'] / abs(`b'[1, `i'])
}
local i = `i' + 1
}
end
program define MinLag /* minlag : numlist1 numlist2 ... numlistN */
args minlag colon
mac shift 2
local i 1
numlist "`*'", sort
local nlist `r(numlist)'
gettoken minval : nlist
c_local `minlag' `minval'
end
program define MaxLagS /* sumlag : N "nlist1" "nlist2" ... "nlistN" */
args maxlag colon k_list
mac shift 3
local maxsum 0
local i 1
while `i' <= `k_list' {
if "``i''" != "" {
local lastel : word count `i'
local last : word `lastel' of `i'
local maxsum = `maxsum' + `last'
}
local i = `i' + 1
}
c_local `maxlag' `maxsum'
end
program define MaxLag /* maxlag : numlist1 numlist2 ... numlistN */
args maxlag colon
mac shift 2
if "`*'" == "" {
c_local `maxlag' 0
exit
}
numlist "`*'", sort
local lastel : word count `r(numlist)'
local lastval : word `lastel' of `r(numlist)'
c_local `maxlag' `lastval'
end
/* Obtain whitened residuals of a series from an "infinite" AR regression
* regression. Used to get a disturbance estimate that can be used for
* the MA component of a regression; a'la Monfort & Gourieux TS p 188
*/
program define Monfort
args u_var /* variable to hold whitened resids
*/ colon /* :
*/ e_var /* variable with ARMA resids
*/ minlag /* minimum lag
*/ maxlag /* smallest maximum lag value
*/ inter /* true interval of time variable
*/ touse /* touse variable */
/* search for a reasonble lag for infinite AR representation */
/* a space problem here since k doubles are created in regress. */
tempvar touse2
qui gen byte `touse2' = . in 1
local vir 1
local done 0
local k = 30 /* would prefer 40 */
while ! `done' & `k' > 0 {
if `k' > 10 {
local usemin = min(`minlag', `inter')
}
else local usemin `minlag'
local usemax = max(`maxlag', `usemin'+`k'*`inter')
qui replace `touse2' = `touse'
capture markout `touse2' `e_var' /*
*/ l(`usemin'(`inter')`usemax').`e_var'
if !_rc {
qui count if `touse2'
if r(N) > 2*`k' { local done 1 }
}
else if `vir' {
di in blue "(note: insufficient memory or " /*
*/ "observations to estimate usual"
di in blue "starting values [1])"
local vir 0
}
local k = `k' - 5
}
/* Obtain near white u's from "infinite" AR regress */
while `usemax' > `usemin' {
capture regress `e_var' l(`usemin'(`inter')`usemax').`e_var' /*
*/ if `touse'
local hold_rc = _rc
if !`hold_rc' {
capture drop `u_var'
qui predict double `u_var' if `touse', res
exit
}
if `vir' {
di in blue "(note: insufficient memory or " /*
*/ "observations to estimate usual"
di in blue "starting values [2])"
local vir 0
}
local usemax = `usemax' - 1
}
if `hold_rc'==2000 {
di in red "insufficient observations"
exit 2000
}
di in red "insufficient memory or observations to estimate " /*
*/ "starting values"
exit `hold_rc'
end
program define ErrCheck
if _rc == 111 | _rc == 2000 {
di in blue "insufficient observations"
exit _rc
}
if _rc {
error _rc
exit
}
end
program define SetConst
end
exit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -