Program Code
Program Code
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
lags(`lags')
imax(`imax')
rep(`rep')
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
/*contemporary effects*/
contemporary , ///
rep(`rep')
/*variance coefficients*/
varianceb `varlist' ,
rep(`rep')
indepvarlist(`indepvarlist')
constants(`constants')
ci(`bsn' `dbsn' `bsp' `mc')
/*irf*/
irf ,
`stirf'
lags(`lags')
step(`step')
rep(`rep')
depvars(`depvars')
///
///
///
///
///
///
///
///
///
}
/*fevd*/
fevd ,
step(`step')
rep(`rep')
depvars(`depvars')
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
/*results: matrices*/
if missing("`pooled'") {
mat `b'=vec(B_s0)'
local i = 1 //drop rows with dummies
while `i' <= (`depvars' * `lags' + `imax') * `depvars' {
mat `V_s' = nullmat(`V_s') \
///
V0[`i'..`i' + `depvars' * `lags' - 1, 1...]
local i = `i' + `depvars' * `lags' + `imax'
}
///
macros*/
ivar "`ivar'"
eqnames "`varlist'"
cmd "xtvar"
cmdline `"`e(cmd)' `cmdline'"'
/*display results*/
if missing("`pooled'") {
local txt1="Panel (LSDV) vector autoregression"
local txt2="Group variable: "
local txt3="`e(ivar)'"
}
else {
local txt1="Pooled vector autoregression"
local txt2=""
local txt3=""
}
#delimit ;
di _n in gr "`txt1'"
_col(49) in gr "Number of obs" _col(68) "="
_col(70) in ye %9.0f e(N) ;
di in gr "`txt2'" in ye abbrev("`txt3'",12) in gr
_col(49) "Number of groups" _col(68) "="
_col(70) in ye %9.0g e(N_g) _n ;
di in gr
_col(49) in gr "Obs per group" _col(68) "="
_col(70) in ye %9.0f e(g) ;
#delimit cr
di
_vardisprmse , est(var) small
if missing("`pooled'") {
di "F statistic for F("e(df_m) ","e(df_r) ")"
}
else {
di "F statistic is for F("e(df_m) ","e(df_r) ")"
}
eret display, level(`level')
di
di
mat rownames A0 = `varlist'
mat colnames A0 = `varlist'
di "Contemporary coefficients"
mat list A0, noheader
eret mat A = A0
/*tables & graphs: lists*/
order D* F* step
qui ds
tokenize "`r(varlist)'"
///
legend(off) subtitle(Response: ``r'', position(11)) nodr
aw
local i = `i' + 1
local s = `s' + 2
local r = `r' + 2
}
if "`cit'" == "p" {
di
di in ye `level' "% lower and upper bounds reported; percentile
ci"
}
else {
di
di in ye `level' "% lower and upper bounds reported; standard no
rmal ci"
}
local i = 1
local j = 1
local k = 1
forvalues i = 1/`t' {
local GIRF`j' "`GIRF`j'' IRF`i'"
if `k' == `depvars' {
local j = `j' + 1
local k = 0
}
local k = `k' + 1
}
/*display graphs*/
if missing("`draw'") {
local i = 1
///
name(FEVD_`var', replace) legend(`legen
d')
local j = `j' + 1
}
}
}
}
if _rc != 0
{
di
di "{err}the program produced an error or was stopped"
di "for information type: search rc " _rc
di "clear matrix before running xtvar again"
}
restore
end
//GENERATE LAGGED VARIABLES AND GROUP DUMMIES
capture program drop genlagdum
program define genlagdum
syntax varlist [ ,
///
d(string)
///
timevar(string)
///
panelvar(string)
///
pooled
///
lags(integer 2)
///
]
keep `varlist' `panelvar' `timevar'
/*generate lagged variables*/
foreach var of local varlist {
forvalues i = 1 / `lags' {
qui gen l`i'_`var' = l`i'.`var'
}
}
/*generate group dummies/ constant*/
if missing("`pooled'") {
qui tab `panelvar', gen(`d')
}
else {
gen cons = 1
}
/*drop missings*/
foreach v of var * {
qui drop if missing(`v')
}
end
//LISTS
capture program drop lists
program define lists, rclass
syntax varlist [,
d(string)
pooled
lags(integer 2)
imax(integer 1)
rep(string)
]
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
/*covariance*/
qui mat accum Sigma`rep' = `res', nocons
if missing("`pooled'") {
mat Sigma`rep' = Sigma`rep'/(`obs' - `depvars'*`lags' - `imax')
}
else {
mat Sigma`rep' = Sigma`rep'/(`obs' - `depvars'*`lags' - 1)
}
/*cholesky*/
mat COL`rep' = cholesky(Sigma`rep')
end
//CONTEMPORARY COEFFICIENTS
capture program drop contemporary
program define contemporary
syntax [ ,
///
rep(string)
///
]
///
///
///
///
///
///
///
///
///
///
///
tempname DS AS J C TEMP
/*J matrix for extraction of elements*/
if `lags' > 1 {
mat `J' = I(`depvars'), J(`depvars', `lags' * `depvars' - `depva
rs',0)
}
else {
mat `J' = I(`depvars')
}
/*companion matrix C*/
if `lags' > 1 {
mat `TEMP' = I(`lags' * `depvars' - `depvars'), ///
J(`lags' * `depvars' - `depvars', `depvars', 0)
mat `C' = B_s`rep'' \ `TEMP'
}
else {
mat `C' = B_s`rep''
}
/*response matrix*/
///
///
///
///
///
/*generate dataset*/
qui svmat D`rep'
qui keep if _n <= `step'
keep D*
gen step = _n - 1
/*generate MSE*/
local i = 1
while `i' <= `depvars' {
tempname MSE`rep'`i'
qui gen double `MSE`rep'`i'' = 0
local j = `i'
while `j' <= `depvars' * `depvars' {
qui replace `MSE`rep'`i'' = `MSE`rep'`i'' + sum(D`rep'`j
'^2)
local j = `j' + `depvars'
}
local i = `i' + 1
}
/*FEVD*/
local i = 1
while `i' <= `depvars' {
local j = `i'
while `j' <= `depvars' * `depvars' {
tempname H`j'
qui gen `H`j'' = sum(D`rep'`j'^2) / `MSE`rep'`i''
qui gen FEVD`rep'`j' = `H`j''[_n - 1]
qui replace FEVD`rep'`j' = 0 if FEVD`rep'`j' == .
local fevd "`fevd' FEVD`rep'`j'"
local j = `j' + `depvars'
}
local i = `i' + 1
}
return local fevd "`fevd'"
if `rep' == 0 {
mkmat `fevd', mat(`FEVD') //bs residual matrix
mat D0FEVD = D`rep', `FEVD'
mat D0FEVD = vec(D0FEVD')' //input vector for bstat
}
mat drop D`rep'
end
//PREPARE MC
capture program drop preparemc
program preparemc, rclass
syntax [,
pooled
depvars(integer 1)
obs(integer 1)
reps(integer 200)
indepvarlist(string)
depvarlist(string)
constants(string)
rep(integer 1)
lags(integer 2)
imax(integer 1)
]
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
tempname sim
tempvar ireshape
tempfile simulate
preserve
/*dataset for ci*/
local j = 2 * `step' * `depvars' * `depvars'
forvalues i = 1 / `j' {
local l1 "`l1' IRF`i'"
}
qui postfile `sim' `l1' using "`simulate'", replace
/*display progress*/
di ".", _continue
forvalues i = 1 / `reps' {
local rep = `rep' + 1
if mod(`i',4) == 0 {
if mod(`i',100) == 0 {
di " `i'", _continue
di
}
di ".", _continue
}
/*Monte Carlo loops*/
if "`ci'"=="mc" {
mc ,
`stirf'
`pooled'
lags(`lags')
imax(`imax')
step(`step')
rep(`rep')
depvars(`depvars')
betau(`betau')
reps(`reps')
qui clear
}
/*bootstrapping*/
else {
bootstrapping ,
///
timevar(`timevar')
///
panelvar(`panelvar')
///
resv(`resv')
///
`stirf'
///
`pooled'
///
`ci'
///
rep(`rep')
///
reps(`reps')
///
imax(`imax')
///
tmax(`tmax')
///
obs(`obs')
///
lags(`lags')
///
step(`step')
///
depvars(`depvars')
///
///
///
///
///
///
///
///
///
///
res0(`res0')
///
cres(`cres')
///
constants(`constants')
///
indepvarlist(`indepvarlist')
///
boot(`boot')
}
/*call fevd*/
fevd,
depvars(`depvars')
step(`step')
rep(`rep')
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
preserve
clear
/*draw coefficients*/
varianceb, ///
rep(`rep')
*mata: st_matrix("V`rep'", makesymmetric(st_matrix("V`rep'")))
qui drawnorm `betau', cov(V`rep') n(1)
mkmat `betau', mat(betau)
mat `b_vec' = vec(B0) + betau'
mat drop V`rep'
restore
/*contemporary effects*/
mat COL`rep' = cholesky(Sigma`rep')
if !missing("`stirf'") {
contemporary , ///
rep(`rep')
}
mat drop Sigma`rep'
}
else {
local rep_1 = `rep' - 1
if !missing("`stirf'") {
mat A`rep' = A`rep_1'
}
else {
mat COL`rep' = COL`rep_1'
}
mat `b_vec' = vec(B0) - betau'
mat drop betau
mat drop COL`rep_1'
}
/*coefficients matrix*/
if missing("`pooled'") {
vctomtx
`lags'*`depvars'
`lags'*`depvars'+`imax'+1
`depvars'
`b_vec'
B_s`rep'
///
///
///
///
///
}
else {
vctomtx
///
///
///
///
///
`lags'*`depvars'
`lags'*`depvars'+1+1
`depvars'
`b_vec'
B_s`rep'
}
/*IRF*/
irf,
`stirf'
step(`step')
rep(`rep')
lags(`lags')
depvars(`depvars')
///
///
///
///
///
if `rep' == `reps' {
capt mat drop betau
capt mat drop COL`rep'
mat drop SIGMA Dn
mat drop XpX
}
capt mat drop B_s`rep'
if `odd' == 0 {
capt mat drop A`rep'
capt mat drop COL`rep'
}
end
//BOOTSTRAP
capture program drop bootstrapping
program define bootstrapping
syntax [,
timevar(string)
panelvar(string)
resv(string)
stirf
pooled
bsn
dbsn
bsp
imax(integer 1)
tmax(integer 1)
depvars(integer 1)
res0(string)
obs(integer 1)
rep(integer 1)
lags(integer 2)
cres(string)
constants(string)
reps(integer 200)
indepvarlist(string)
step(integer 8)
boot(string)
]
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
tempfile boot2
tempname nn nn2 H1 H2 INDEPVARLIST VARLIST
local obs_b = `tmax' - `lags'
/*parametric bootstrap*/
if !missing("`bsp'") {
clear
qui drawnorm `res0', cov(Sigma0) n(`obs')
mkmat `res0', mat(RES`rep')
}
/*temporal bootstrap*/
else {
clear
qui set obs `obs_b'
generate long `timevar' = ///
floor((`obs_b') * runiform() + (`lags' + 1)) //random draw of pe
riods
generate `nn' = _n //ordering
joinby `timevar' using "`boot'" //create residual dataset
sort `panelvar' `nn' //same order for all groups
mkmat `cres', mat(RES`rep')
}
mat `VARLIST' = J(`obs', `depvars', .)
if missing("`pooled'") {
mat `H2' = I(`depvars' * `lags') \
///
J(`depvars' + `imax', `depvars' * `lags', 0) //to extract portio
n of H3
mat `INDEPVARLIST' = J(`obs',`lags' * `depvars' + `imax', .)
}
else {
mat `H2' = I(`depvars' * `lags') \
///
J(`depvars' + 1, `depvars' * `lags', 0)
mat `INDEPVARLIST' = J(`obs', `depvars' * `lags' + 1, .)
}
/*generate bootstrap dataset recursively*/
local n = 1
forvalues i = 1(`obs_b')`obs' {
mat `H1' = INDEPVARLIST_CONS[`n',1...]
forvalues j = 1 / `obs_b' {
mat `INDEPVARLIST'[`n',1] = `H1'
mat `VARLIST'[`n',1] = `H1' * B0 + RES`rep'[`n',1...]
mat `H1' = (`VARLIST'[`n',1...], `H1') * `H2' //cut off
old lags and dummies
mat `H1' = (`H1', `INDEPVARLIST'[`n',`depvars' * `lags'
+ 1...]) //add dummies
local n = `n' + 1
}
}
mat drop RES`rep'
svmat `VARLIST'
forvalues i = 1 / `depvars' {
local varlist "`varlist' `VARLIST'`i'"
}
/*lists for estimation*/
lists `varlist' ,
///
`pooled'
///
lags(`lags')
///
imax(`imax')
///
rep(`rep')
local indepvarlist "`r(indepvarlist)'"
mat colnames `INDEPVARLIST' = `indepvarlist' `constants'
svmat `INDEPVARLIST', names(col)
/*double resampling*/
if !missing("`dbsn'") & missing("`bsp'") {
qui save "`boot2'", replace
clear
local obs_b = `imax'
qui set obs `obs_b'
generate long `panelvar' = floor(`imax' * runiform() + 1) //rand
om draw of groups
generate `nn2' = _n //ordering
joinby `panelvar' using "`boot2'" //create residual dataset
sort `nn2' `nn'
}
//estimation
estimation `varlist',
resv(`resv')
depvars(`depvars')
lags(`lags')
rep(`rep')
indepvarlist(`indepvarlist')
constants(`constants')
local res`rep' "`r(res`rep')'"
//covariance matrix
covariance ,
`pooled'
depvars(`depvars')
lags(`lags')
imax(`imax')
obs(`obs')
rep(`rep')
res(`res`rep'')
///
///
///
///
///
///
///
///
///
///
///
///
///
///
`stirf'
lags(`lags')
step(`step')
rep(`rep')
depvars(`depvars')
///
///
///
///