*! hansen--Bruce Hansen's test for parameter instability *! version 1.0.0 Ken Heinecke May 1994 (STB-20: sts8) * HANSEN.ado Program to perform Bruce Hansen's * Test for parameter instability * Written by Ken Heinecke, May 1994 program define hansen version 3.1 local varlist "req ex" local if "opt pre" local in "opt pre" local options "noCOnstant Current(str) Lags(str) noSAmple Static(str) Regress *" parse "`*'" capture { tempfile __t local savfn "$S_FN" local tsind = 0 quietly { save `__t',replace } if ("`regress'" != "") { local sound "noisily" } if ("`current'" != "") { local c "current(`current')" } if ("`static'" != "") { local s "static(`static')" } if ("`lags'" != "") { local l "lags(`lags')" local tsind=1 } quietly { if(`tsind'==1) { `sound' tsfit `varlist' `if' `in',`l' `c' `s' `constan' `sample' `options' } else { `sound' tsfit `varlist' `if' `in', current(`varlist') `constan' `sample' `options' } local nvar : word count $S_E_vl capture drop _e predict _e `if' `in',res capture drop if _e==. local nobs=_N capture drop _ee gen double _ee = _e*_e sum _ee capture drop _sig2 gen double _sig2=_result(3) parse "$S_E_vl",parse(" ") local i 1 while ("`2'" != "") { gen double _f`i'=`2'*_e local i = `i'+1 mac shift } if ("`constan'" == "") { gen double _f`i'=_e local i = `i'+1 } gen double _f`i' = _ee-_sig2 if ("`constan'" != "") { local number = `nvar' } else { local number = `nvar'+1 } local j 1 while(`j'<=`number') { gen double _s`j'=sum(_f`j') local j = `j'+1 } local i 1 while (`i'<=`number') { local flist ="`flist'" + " _f`i'" local i = `i'+1 } local i 1 while (`i'<=`number') { local slist ="`slist'" + " _s`i'" local i = `i'+1 } mkmat `flist' `slist' /* Create Vi by summing the squares of the elements of the "f" vectors. */ local i 1 local nvar : word count $S_E_vl while (`i'<=`number') { scalar _V`i'=0 local i = `i'+1 } local j=1 while (`j'<=`number') { local i=1 while (`i'<=`nobs') { scalar _V`j'= _V`j' + _f`j'[`i',1]^2 local i = `i' + 1 } local j = `j'+1 } /* Create Li by summing the squares of the elements of the "s" vectors and scaling appropriately. */ local i 1 while (`i'<=`number') { scalar _L`i'=0 local i = `i'+1 } local j=1 while (`j'<=`number') { local i=1 while (`i'<=`nobs') { scalar _L`j'= _L`j' + _s`j'[`i',1]^2 local i = `i' + 1 } local j = `j'+1 } local i 1 while (`i'<=`number') { scalar _L`i'=(1/(`nobs'*_V`i'))*_L`i' scalar drop _V`i' local i = `i'+1 } *** label scalars with variable names parse "$S_E_vl",parse(" ") local n 1 local llist1 "" local llist2 "" local half = int(`nvar'/2) while (`n'<=`half') { scalar `2'=_L`n' scalar drop _L`n' local n = `n'+1 local llist1 = "`llist1'" + " `2'" mac shift } while (`n'<=`nvar'-1) { scalar `2'=_L`n' scalar drop _L`n' local n = `n'+1 local llist2 = "`llist2'" + " `2'" mac shift } if ("`constan'" == "") { scalar _cons = _L`n' scalar drop _L`n' local n = `n'+1 } scalar sigma = _L`n' scalar drop _L`n' if ("`constan'" == "") { local llist2 = "`llist2'" + " _cons sigma" } else { local llist2 = "`llist2'" + " sigma" } noi display " " noi display " " noi display "Individual Statistics" noi display " " noi scalar l `llist1' `llist2' /* Create V for the whole model */ tempname F S V invV mat `F' = _f1,_f2 local i 3 while (`i'<=`number') { mat `F' = `F',_f`i' local i = `i'+1 } mat `S' = _s1,_s2 local j 3 while (`j'<=`number') { mat `S' = `S',_s`j' local j = `j'+1 } mat `V' = J(`number',`number',0) local i=1 tempname temp1 temp2 temp3 temp4 while(`i'<=`nobs') { mat `temp1'=`F'[`i',.] mat `temp2'=`temp1'' mat `temp3'=`temp2'*`temp1' mat `V' = `V' + `temp3' local i = `i'+1 } /* Create L for the whole model */ mat `invV'=inv(`V') mat _Lc=J(1,1,0) local i=1 tempname temp5 temp6 temp7 temp8 while(`i'<=`nobs') { mat `temp5'=`S'[`i',.] mat `temp6'=`temp5'' mat `temp7'=`temp5'*`invV' mat `temp8'= `temp7'*`temp6' mat _Lc = _Lc +`temp8' local i = `i'+1 } local s=1/`nobs' matrix _Lc = `s'*_Lc noi display " " noi display " " noi display "Joint test statistic with `number' degrees of freedom:" noi display " " scalar _Lc = _Lc[1,1] noi scalar l _Lc matrix drop `flist' `slist' drop _all use `__t',clear mac def S_FN "`savfn'" ** end quietly } ** end capture } if (_rc != 0) { local oldrc = _rc quietly use `__t',clear mac def S_FN "`savfn'" exit `oldrc' } end