📄 arima.ado
字号:
*! version 7.0.6 11apr2005
program define arima, eclass byable(recall)
version 7, missing
/* limits, etc. */
local gtol .05 /* gradient tolerance */
local infty 1e9 /* diffuse P_0 diagonal */
if replay() {
if "`e(cond)'" != "" {
arch `0'
exit
}
if "`e(cmd)'" != "arima" {
noi di in red "results of arima not found"
exit 301
}
Display `0'
exit
}
syntax [varlist(ts)] [if] [in] [iw] [, /*
*/ AR(numlist int ascend >0) /*
*/ ARIMA(numlist int min=3 max=3 >-1) /*
*/ SARIMA(numlist int min=4 max=4 >-1) /*
*/ BHHH BHHHQ BFGS /*
*/ BHHHBfgs(numlist min=2 max=2 integer >=0 <99999) /*
*/ BHHHDfp(numlist min=2 max=2 integer >=0 <99999) /*
*/ noBRacket Constraints(string) noCOEF /*
*/ CONDition noConstant DIffuse DFP DETail /*
*/ FROM(string) GTOLerance(real `gtol') noHEAD HESsian /*
*/ Interval(integer 1) Level(cilevel) noLOg /*
*/ MA(numlist int ascend >0) /*
*/ MAR1(string) MAR2(string) MAR3(string) MAR4(string) /*
*/ MAR5(string) MAR6(string) MAR7(string) MAR8(string) /*
*/ MMA1(string) MMA2(string) MMA3(string) MMA4(string) /*
*/ MMA5(string) MMA6(string) MMA7(string) MMA8(string) /*
*/ MLOpts(string) P0(string) /*
*/ NATIVE NR OPG Robust SCore(passthru) SAVEspace /*
*/ STATE0(string) TECHnique(string) VCE(string) /*
*/ * ]
if _by() {
_byoptnotallowed score() `"`score'"'
}
/* if conditional, just use arch */
if "`condition'" != "" {
if "`mar1'`mma1'`sarima'" != "" {
di as error "may not use condition with seasonal models"
exit 198
}
arch `0'
est local seasons "1"
est local estat_cmd arima_estat
est local predict arima_p
MaxLag maxar : `e(ar)'
MaxLag maxma : `e(ma)'
local r = max(`maxar', `maxma' + 1)
tempname p0 b
mat `b' = e(b)
mat `p0' = `b'[1, colnumb(`b',"SIGMA2:_cons")] * I(`r')
est matrix P0 `p0'
est scalar ar_max = `maxar'
est scalar ma_max = `maxma'
est scalar sigma = sqrt(_b[SIGMA2:_cons])
local eqnames `e(eqnames)'
local ct : word count `e(ar)'
local i 1
while `i' <= `ct' {
local eqnames : subinstr local eqnames "ARMA" "AR1_terms"
local i = `i' + 1
}
local eqnames : subinstr local eqnames "ARMA" "MA1_terms", all
local eqnames : subinstr local eqnames "HET" "SIGMA"
est local eqnames `eqnames'
exit
}
/* Handle arima() option */
if "`arima'" != "" {
if "`ar'`ma'" != "" {
di in red "ar() and ma() not allowed with arima()"
exit 198
}
tokenize `arima'
local p `1'
local d `2'
local q `3'
tokenize `varlist'
local varlist
local i 1
while "``i''" != "" {
local varlist `varlist' D`d'.``i''
local i = `i' + 1
}
if `p' != 0 {
numlist "1/`p'"
local ar `r(numlist)'
}
if `q' != 0 {
numlist "1/`q'"
local ma `r(numlist)'
}
}
if "`sarima'" != "" {
if "`mma1'`mar1'" != "" {
di as error "mma() and mar() not allowed with sarima()"
exit 198
}
tokenize `sarima'
local sp `1'
local sd `2'
local sq `3'
local ss `4'
if `ss' <= 0 {
di as error "sarima() seasonality term must be positive"
exit 198
}
/* S4S4 is NOT the same as S8! */
local seasop
forvalues i = 1/`sd' {
local seasop "S`ss'`seasop'"
}
tokenize `varlist'
local varlist
local i 1
while "``i''" != "" {
if "`seasop'" != "" {
local varlist `varlist' `seasop'.``i''
}
else {
local varlist `varlist' ``i''
}
local i = `i' + 1
}
if `sp' != 0 {
numlist "1/`sp'"
local sar `r(numlist)'
}
if `sq' != 0 {
numlist "1/`sq'"
local sma `r(numlist)'
}
if "`sar'" != "" {
local mar1 "`sar', `ss'"
}
if "`sma'" != "" {
local mma1 "`sma', `ss'"
}
}
mac drop T*
/* Other syntax errors */
if "`robust'" != "" & "`opg'" != "" {
local opg
di in blue "(note: option opg ignored when " /*
*/ "specified with robust)"
}
if "`diffuse'" != "" & "`state0'`p0'" != "" {
di in red "diffuse may not be specified with state() or p0()"
exit 198
}
/* Handle ML options */
mlopts stdopts, `options'
local vcetype `hessian' `opg' `native' `robust'
local nvce : word count `vcetype'
if "`vce'" != "" {
local vcetype `vcetype' vce(`vce')
}
if (`nvce' + (`"`vce'"' != "")) > 1 {
di in red "options `:list retok vcetype' may not be combined"
exit 198
}
else local vce `vce' `hessian' `opg' `native'
if "`technique'" == "" { /* undoc*/
local tmeth `bhhh' `bhhhq' `bfgs' `dfp' `nr'
local ct : word count `tmeth'
local ct = `ct' + ("`bhhhbfgs'" != "") + ("`bhhhdfp'" != "")
if `ct' == 1 {
if "`bhhhbfgs'" != "" {
if "`vce'`robust'" == "" {
local vce opg
}
gettoken c1 c2 : bhhhbfgs
local tmeth `"bhhh `c1' bfgs `c2'"'
}
if "`bhhhdfp'" != "" {
if "`vce'`robust'" == "" {
local vce opg
}
gettoken c1 c2 : bhhhdfp
local tmeth `"bhhh `c1' dfp `c2'"'
}
local techniq "technique(`tmeth')"
}
else if `ct' == 0 {
if "`vce'`robust'" == "" {
local vce opg
}
local techniq "technique(bhhh 5 bfgs 10)"
}
else {
di in red "may only specify one optimization method"
di in red "from: bhhh bfgs dfp nr bhhhbfgs() bhhhdfp()"
exit 198
}
}
else local techniq "technique(`technique')"
if "`vce'" != "" {
local vce vce(`vce')
}
if "`bracket'" == "" {
local bracket bracket
}
else local bracket
/* other special parsing */
if "`from'" == "armab0" {
local armab0 "armab0"
local from
}
marksample touse /* set sample */
_ts timevar panvar if `touse', sort onepanel
qui tsset
markout `touse' `timevar' `panvar'
if "`savespace'" != "" {
preserve
tsrevar `varlist' , list
keep `r(varlist)' `touse' `timevar' `panvar'
qui tsset
}
/* Remove collinearity */
_rmcoll `varlist' if `touse', `constant'
local varlist `r(varlist)'
gettoken dep_m ind_m : varlist
MaxLag maxmar : `ar' /* track max lags */
MaxLag maxmma : `ma'
/* mma#() and mar#() options */
if "`ar'`ma'" != "" {
local mdeltas 1
}
foreach m in mma mar {
forvalues i = 1/8 {
if "``m'`i''" != "" {
gettoken lst delta : `m'`i' , parse(,)
gettoken c delta : delta , parse(,)
local delta = trim("`delta'")
capture confirm integer number `delta'
local rc1 = _rc
capture numlist "`lst'" , ascending /*
*/ integer min(1)
if _rc | `rc1' {
di as error `"`m'(``m'`i'') invalid"'
exit 198
}
local t : subinstr local mdeltas "`delta'" /*
*/ "" , word count(local ct)
if ! `ct' {
local mdeltas `mdeltas' `delta'
}
if "``m'_`delta'_d'" != "" {
di as error ///
"cannot define multiple `m' terms with same seasonality"
exit 198
}
MaxLag t : `lst'
local `m'_`delta'_d `delta'
local max`m' = `max`m'' + `delta'*`t'
local `m'_`delta' `r(numlist)'
/* Reformatted for later eret macro */
local `m'`i'_n "`r(numlist)', `delta'"
}
else { /* No more terms */
continue, break
}
}
}
global Tseasons `mdeltas'
/* handle special priming options */
if "`diffuse'`state0'`p0'"!="" {
local r = max(`maxmar', `maxmma' + 1)
tempname p0m state0m
}
if "`diffuse'" != "" { /* diffuse prior */
global Tstate0 `state0m'
global Tp0 `p0m'
mat $Tstate0 = J(`r', 1, 0)
mat $Tp0 = `infty' * I(`r')
}
if "`state0'" != "" { /* user supplied state (Xi) */
global Tstate0 `state0m'
local r = max(`maxmar', `maxmma' + 1)
capture confirm number `state0'
if _rc {
if rowsof(`state0')!=`r' | colsof(`state0')!=1 {
if `r' != 1 { local s "s" }
di in red "matrix `state0' must " /*
*/ "have `r' row`s' and 1 column"
exit 198
}
mat $Tstate0 = `state0'
}
else mat $Tstate0 = J(`r', 1, `state0')
}
if "`p0'" != "" { /* user supplied P_(1|0) */
global Tp0 `p0m'
local r = max(`maxmar', `maxmma' + 1)
capture confirm number `p0'
if _rc {
if colsof(`p0')!=`r' | rowsof(`p0')!=`r' {
if `r' != 1 {
local s "s"
}
else local s
di in red "matrix `p0' must have `r' row`s' " /*
*/ " and `r' column`s'"
exit 198
}
mat $Tp0 = `p0'
}
else mat $Tp0 = `p0' * I(`r')
}
/* check matsize, ^2 for init cond. */
if "$Tp0" == "" {
local m = max(`maxmar', `maxmma' + 1)
local matsize : set matsize
if `matsize' < `m'^2 {
di in red "matsize too small, must be max(AR, MA+1)^2"
di in red "use -diffuse- option or type -help matsize-"
exit 908
}
}
/* expand TS ops in depvar for kalman */
tsrevar `dep_m'
if "`dep_m'" != "`r(varlist)'" {
tempvar depvar
global Tdepvar `depvar'
qui gen double $Tdepvar = `dep_m' if `touse'
}
else global Tdepvar `dep_m'
tempvar u e_t /* disturbance temp vars */
/* Starting values, matrix stripe
* and ML equations */
local yprefix `dep_m'
/* Xb */
tempname b0 T
if "`ind_m'" != "" | "`constant'" == "" {
/* starting values */
capture regress `dep_m' `ind_m' if `touse' , `constant'
ErrCheck
qui predict double `u' if `touse' , res
mat `b0' = e(b)
local sigma = sqrt(e(rmse)^2*(e(df_r)/e(N)))
/* ML equation */
if index("`dep_m'", ".") == 0 {
local xbeqn "(`dep_m': `yprefix' = `ind_m', `constant')"
}
else local xbeqn "(eq1: `yprefix' = `ind_m', `constant')"
local yprefix
/* maintain name stripe */
mat coleq `b0' = `dep_m'
local colnams : colfullnames `b0'
}
else {
qui gen double `u' = `dep_m'
qui sum `dep_m'
local sigma = sqrt(r(Var))
}
/* AR, MA */
if "`ar'`ma'" != "" {
/* could combine w/ mdeltas
* except intial values */
if "`ar'" != "" { local arterms "l(`ar').`u'" }
if "`ma'" != "" { local materms "l(`ma').`e_t'" }
if "`armab0'" == "" {
/* Get residuals from AR representation of errors */
/* won't do mma mar -- start vals will be hard for those models -- use 0*/
MinLag minlag : `ar' `ma'
MaxLagS maxlag : 2 `ar' `ma'
Monfort `e_t' : `u' `minlag' `maxlag' `interval' `touse'
/* Get consistent estimates of AR and MA parameters */
capture regress `u' `arterms' `materms' /*
*/ if `touse', nocons
if !_rc {
mat `T' = e(b)
FixARMA `T' "`ar'" "`ma'"
mat `b0' = nullmat(`b0') , `T'
local sigma = sqrt(e(rmse)^2*(e(df_r)/e(N)))
}
else local armab0b "armab0"
}
if "`armab0'`armab0b'" != "" {
local narma : word count `ar' `ma'
mat `T' = J(1, `narma', 0)
mat `b0' = nullmat(`b0') , `T'
if "`armab0b'" == "" { qui gen double `e_t' = `u' }
}
/* maintain name stripe */
AddStrip colnams : "`colnams'" "`ar'" ARMA ar
AddStrip colnams : "`colnams'" "`ma'" ARMA ma
/* ML equation */
if "`ar'" != "" {
local areqn "(AR1_terms: `yprefix' = `arterms', nocons )"
local yprefix
}
if "`ma'" != "" {
local maeqn "(MA1_terms: `yprefix' = `materms', nocons )"
local yprefix
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -