⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _vecu.ado

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 ADO
📖 第 1 页 / 共 4 页
字号:
*! version 1.1.10  09mar2005
program define _vecu, eclass sort
	version 8.1

local m1 = _N
	syntax varlist(ts numeric) [if] [in]		///
		,					///
		[   					///
		LTOLerance(numlist max=1 )		///
		TOLerance(numlist max=1)		///
		ITERate(numlist max=1 integer )		///
		Trend(string)				///
		Lags(numlist integer max=1 >0 <`m1')	///
		SIndicators(varlist numeric)    	///
		Seasonal				///
		VRank					///
		VEst					///
		Rank(integer 1 )			///
		noTRace					///
		Max					///
		Ic					///
		level99					///
		levela					///
		BConstraints(string)			///
		AConstraints(string)			///
		AFrom(name)				///
		BFrom(name)				///
		noLOg					///
		btrace					///
		toltrace				///
		noreduce				///
		]

// vrank is switch for running vecrank
// vest is switch for running vec

	_vectparse ,`trend'	
	local trend "`r(trend)'"
	
	local trends : word count `trend'
	if `trends' > 1 {
		di as err "only one {cmd:trendtype} may be "	///
			"specified in {cmd:trend()}"
		exit 198
	}	

	if "`seasonal'" != "" & "`trend'" == "none" {
		di as err "{cmd:seasonal} cannot be specified "	///
			"with {cmd:trend(none)}"
		exit 198	
	}

	if "`seasonal'" != "" & "`trend'" == "rconstant" {
		di as err "{cmd:seasonal} cannot be specified "	///
			"with {cmd:trend(rconstant)}"
		exit 198	
	}

	if "`sindicators'" != "" & "`trend'" == "none" {
		di as err "{cmd:sindicators()} cannot be specified "	///
			"with {cmd:trend(none)}"
		exit 198	
	}

	if "`sindicators'" != "" & "`trend'" == "rconstant" {
		di as err "{cmd:sindicators()} cannot be specified "	///
			"with {cmd:trend(rconstant)}"
		exit 198	
	}


	if "`lags'" == "" {
		local lags 2
	}	
		
	qui tsset
	local tvar "`r(timevar)'"
	local tsfmt "`r(tsfmt)'"
	local stype "`r(unit1)'"

	if "`r(timevar)'" == "" {
		di as err "dataset is not properly {hlp:tsset}"
		exit 498
	}	

	marksample touse

	tempvar touse2
	qui gen byte `touse2' = (`touse' & L`lags'.`touse'<.)
	local p = `lags'
	local pm1 = `p' - 1

	if "`sindicators'" != "" & "`seasonal'" != "" {	
		di as err "sindicators() and seasonal may not both "	///
			"be specified"
		exit 498
	}

//	parse  alpha and beta constraints 

	if "`vest'" == "" & "`aconstraints'`bconstraints'" != "" {
		di as err "constraints are not allowed when determining " ///
			"the cointegrating rank"
		exit 498
	}



	markout `touse' `tvar'

// deal with collinearity 

	qui _rmcoll `varlist' if `touse'
	local rm_vlist "`r(varlist)'"
	local clean : list rm_vlist == varlist
	if `clean' != 1 {
		di as err "the endogenous variables are perfectly collinear"
		di as err "{p 4 4 2}a list of noncollinear variables "	///
			"is{p_end}"
		di as err "{p 8 8 2}`rm_vlist'{p_end}"
		exit 459
	}	

	if "`reduce'" == "" {
		vec_rmcoll `varlist' , touse(`touse') p(`p') rmcoll
		local varlist "`r(varlist)'"
		local p = r(p)
		local pm1 = `p' - 1
		local reduced "`r(reduced)'"
	
		vec_rmcoll `varlist' , touse(`touse') p(`p')
		local varlist "`r(varlist)'"
		local p = r(p)
		local pm1 = `p' - 1
		local reduced "`reduced' `r(reduced)'"

		vec_rmcoll `varlist' , touse(`touse') p(`p') ownonly
		local varlist "`r(varlist)'"
		local p = r(p)
		local pm1 = `p' - 1
		local reduced "`reduced' `r(reduced)'"

		local reduced : list uniq reduced
	}
	else {

	}

	if "`sindicators'" != "" {
		_rmcoll `sindicators'
		local sindicators "`r(varlist)'"
	}

	local ck_vlist0 "`varlist' `sindicators'"
	qui _rmcoll `ck_vlist0'
	local ck_vlist1 "`r(varlist)'"

	local ck_vlist2 : list ck_vlist0 == ck_vlist1
	if `ck_vlist2' != 1 {
		di as err "{p 0 4 4}at least one of the "	///
			"seasonal indicator variables is "	///
			"perfectly collinear with at least "	///
			"one of the dependent variables{p_end}"
		exit 498	
	}



// z2base is defined here to get e(sample) right now

	if `pm1' > 0 {
		tsunab z2base : l(1/`pm1').d.(`varlist') 
		markout `touse' `z2base'
	}
	else {
		markout `touse' d.(`varlist')
		local z2base
	}


	local K : word count `varlist' 
	local Kp1 = `K' + 1

	
	if "`vest'" != "" {
		if `rank' >= `K' {
			di as err "rank() must be less than `K'"
			exit 498
		}	
		forvalues i=1/`rank' {
			capture confirm new variable _ce`i'
			if _rc > 0 {
				di as txt "dropping _ce`i'"
				drop _ce`i'
			}	
		}

	}

	qui sum `tvar' if `touse'

	local tmin = r(min)
	local tmax = r(max)

	local tmins : di `tsfmt' `tmin'
	local tmaxs : di `tsfmt' `tmax'

// create seasonal tempvars if needed 

	if "`seasonal'" != "" {

//  never include a full set
		if "`trend'" == "none" {
			local tconstant 
		}
		else {
			local tconstant 
		}

		_vecmksi , stype(`stype') timevar(`tvar')	///
			touse(`touse') `tconstant'

		local sindicators "`r(vlist)'"
		global S_DROP_sindicators "`r(vlist)'"
		local s_names "`r(vlist)'"	
		local ns = r(ns)

	}
	else {
		if "`sindicators'" != "" { 
			local s_names "`sindicators'"	
			local ns : word count `sindicators'
		}
		else {
			local ns 0
		}	
	}
	

	if "`r(panelvar)'" != "" {
		qui sum `r(panelvar)' if `touse' 
		if r(min) < r(max) {
			di as err "sample contains more than one panel"
			exit 498
		}
	}
	
	qui tsreport if `touse' ==1 	

	if r(N_gaps) > 0 {
		di as err "the sample has gaps"
		exit 498
	}	
	

// form model and make auxillary variables


	if "`trend'" == "none" {
		local dt ""
		local dt_names ""
		
		local nz1 = `K'
		local nz2 = (`K'*`pm1' + `ns')
		local tterms = `ns'
	}

	if "`trend'" == "rconstant" {
		tempvar mu 
		qui gen double `mu' = 1 if `touse'
		local z1ex `mu'
		local z1exn _cons

		local nz1 = `K'+1
		local nz2 = (`K'*`pm1' + `ns')
		local tterms = `ns'
	}

	if "`trend'" == "constant" {
		tempvar mu
		qui gen double `mu' = 1
		local dt `mu'
		local dt_names "_cons"

		local nz1 = `K'
		local nz2 = (`K'*`pm1' + 1 + `ns')
		local tterms = `ns' + 1
	}
	
	if "`trend'" == "rtrend" {
		tempvar mu 
		qui gen double `mu' = 1 if `touse'

		_vecmktrend if `touse', nvecyet tvar(`tvar') 	///
			tmin(`tmin')

		local dt `mu'
		local z1ex _trend
		local z1exn _trend
		local dt_names "_cons"

		local nz1 = `K' + 1
		local nz2 = (`K'*`pm1' + 1 + `ns')
		local tterms = `ns' + 1
	}

	if "`trend'" == "trend" {
		tempvar mu 
		qui gen double `mu' = 1 if `touse'
		_vecmktrend if `touse', nvecyet tvar(`tvar') 	///
			tmin(`tmin')
		local dt _trend `mu'
		local dt_names "_trend _cons"

		local nz1 = `K' 
		local nz2 = (`K'*`pm1' + 2 + `ns')
		local tterms = `ns' + 2
	}


/* make varlists for Z_0t, Z_1t, Z_2t */

	tsunab z0t : d.(`varlist')		// z0t contains K vars

	tsunab endog_vars : `varlist'	
	local endog_names : subinstr local endog_vars "." "_" , all		

	tsunab z1tb : l.(`varlist') 
	local z1t `z1tb' `z1ex'			// z1t contains K vars
						// or K+1 when 
						// trend == "rtrend"
						// or trend == "rconstant"

	local z1tn `z1tb' `z1exn'		// names for z1t
	
	local K1 : word count `z1t'

	local z2t `z2base' `sindicators' `dt' 
	local z2t_names  `z2base' `s_names' `dt_names'
				// z2t contains K2 = (p-1)*K + m vars

	local K2 : word count `z2t'

	local z3t `sindicators' `dt' 
	local z3t_names  `s_names' `dt_names'
				// z3t contains K3 =  m vars
	local K3 : word count `z3t'

	local parms_smp =(`K'*`nz2'+(`K'+`nz1'-`rank')*`rank')
	local df_smp = int(`parms_smp'/`K')

	qui count if `touse' > 0
	if r(N) <= `df_smp'  {
		local smp_obs = r(N)
		di as err "there are too many parameters in the model"
		di as err "{p}the model uses up `df_smp' "	///
			"degrees of freedom, but there are "	///
			"only `smp_obs' observations{p_end}"
		exit 498
	}	


	if `K2' > 0 {
// Partial-out Z2

		local i 0
		foreach var of local z0t {
			local ++i
			tempvar r0t_`i' 
			qui regress `var' `z2t' if `touse', nocons
			qui predict double `r0t_`i'' if `touse', res
			local r0t `r0t' `r0t_`i''
		}

		local i 0
		foreach var of local z1t {
			local ++i
			tempvar r1t_`i' 
			qui regress `var' `z2t' if `touse', nocons
			qui predict double `r1t_`i'' if `touse', res
			local r1t `r1t' `r1t_`i''
		}
	}
	else {
		local r0t `z0t'
		local r1t `z1t'
	}

// Get s?? matrices and T

	tempname s00 s01 s10 s11 T
	Getsmats , r0t(`r0t') r1t(`r1t') touse(`touse')		///
		s00(`s00') s01(`s01') s10(`s10') s11(`s11')  	///
		t(`T') k(`K')

/* pieces used by optimization with and without constraints */

	forvalues j = 1/`rank' {
		local ce_lab `ce_lab' _ce`j'
	}

	foreach nm of local ce_lab {
		local Lce_lab "`Lce_lab' L.`nm' "
	}

// do unconstrained Johansen method


	tempname alpha omega lam beta

	capture noi Johansen , alpha(`alpha') beta(`beta') 	///
		omega(`omega') lam(`lam') s10(`s10') 		///
		s01(`s01') s11(`s11') s00(`s00')
	if _rc > 0 {
		DerrMsg
		exit _rc
	}	
	

// diffvarlist holds the first differenced varlist of endogenous variables
// diffvarlist2 holds the equation name version of diffvarlist

	tsunab tsvarlist : `varlist'
	tsunab diffvarlist : D.(`tsvarlist')
	tsunab lagvarlist : L.(`tsvarlist')

// this method assumes that there is only period in each token
	local diffvarlist2 : subinstr local diffvarlist "." "_" , all
	local tsvarlist2 : subinstr local tsvarlist "." "_" , all

	mat colnames `omega' = `tsvarlist'
	mat rownames `omega' = `tsvarlist'

	tempname ll llvals smat1 smat2 llcons parms
	forvalues i = 1/`K' {
		mat `llvals' = (nullmat(`llvals') \ ln(1-`lam'[1,`i']) )
		mat `parms'  = (nullmat(`parms') \ 		///
			(`K'*`nz2'+(`K'+`nz1'-`i')*`i') )
	}

	mat `llvals' = (0 \ `llvals' )
	mat `parms'  = (`K'*`nz2' \ `parms')' 

	ltriang , mat(`smat1') dimen(`Kp1') value(1)

	scalar `llcons' = 						///
		(- .5*`T'*`K'*ln(2*_pi)-.5*`T'*ln(det(`s00'))-.5*`T'*`K')
	mat `ll' = J(`Kp1',1,1)*`llcons'
	
	mat `ll' = `ll' -.5*`T'*`smat1'*`llvals' 
	mat `ll' = `ll''

	tempname aic bic hqic 
	
	matrix `aic'  = J(1,`K'+1,.)
	matrix `bic'  = J(1,`K'+1,.)
	matrix `hqic' = J(1,`K'+1,.)

	MKic `aic' `bic' `hqic' `ll' `parms' `T'


	if "`trend'" == "rconstant" | "`trend'" == "rtrend" {
		mat `lam' = `lam'[1,1..colsof(`lam')-1]
		mat `alpha' = `alpha'[1...,1..colsof(`alpha')-1]
	}	
	
	if "`vest'" == "" {


		tempname tracem maxm ctrace
	
		mat `maxm' = -1*`T'*ln(1-`lam'[1,1])
		mat `ctrace' = J(`K',1,1)

		forvalues i = 2/`K' {
			local r1 = `i' -1
			local r2 = `K'-`i'+1
			
			mat `maxm' = `maxm', -1*`T'*ln(1-`lam'[1,`i'])
			mat `ctrace'   = ( `ctrace', (J(`r1', 1, 0) \ 	///
				J( `r2',1,1) ) )
	
		}
	
		mat `tracem' = `maxm'*`ctrace'
	
// compute rank selected by different methods
	
		tempname cv95 cv99 cvc95 cvc99
		_vecgetcv `trend'
		mat `cv95' = r(cv95)
		mat `cv99' = r(cv99)
	
		_vecgtn `trend'
		local trendnumber = r(trendnumber)
	
	
		local cvcols = colsof(`tracem') 
	
		forvalues i = `cvcols'(-1)1 {
			mat `cvc95' = (nullmat(`cvc95'), 	///
				`cv95'[`i',`trendnumber'] )
			mat `cvc99' = (nullmat(`cvc99'),	///
				`cv99'[`i',`trendnumber'] )
			
		}
	
	
// find rank selected by Johansen sequential trace tests
	
		foreach cval in 95 99 {
			local r_`cval' = -1 
			local row`cval' = 0
			while (`r_`cval'' < 0 & `row`cval'' < `cvcols' ) {
				local ++row`cval'
				if `tracem'[1,`row`cval''] <=		///
					`cvc`cval''[1,`row`cval''] {
					local r_`cval' = `row`cval''
				}
			}
		
			if `r_`cval'' < 0 local r_`cval' = .
		}
	
// find rank selected by IC 
	
		tempname icmin
	
		foreach icf in bic hqic {
			scalar `icmin' = ``icf''[1,1]
			local r_`icf' 1
			forvalues i = 2/`cvcols' {
				if ``icf''[1,`i'] < `icmin' {
					scalar `icmin' = ``icf''[1,`i']
					local r_`icf' `i'
				}	
			}
		}
	
		local cnames0
		local Km1 = `K' - 1
		forvalues i = 0/`Km1'  {
			local cnames0 "`cnames0' `i'_ce "
		}

		local cnames1
		forvalues i = 1/`K'  {
			local cnames1 "`cnames1' `i'_ce "
		}

		local cnames2
		forvalues i = 0/`K'  {
			local cnames2 "`cnames2' `i'_ce "
		}

		matrix rownames `maxm' = max
		matrix rownames `tracem' = trace
		matrix rownames `lam' = eigenvalues

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -