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

📄 _varfcast.ado

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 ADO
📖 第 1 页 / 共 2 页
字号:
*! version 1.3.7  01apr2005
program define _varfcast
	version 8.0
	syntax ,  [STep(numlist max=1 integer >0)		/*
		*/ Dynamic(string) 				/*
		*/ noSE 					/*
		*/ BS						/*
		*/ BSP 						/*
		*/ Reps(numlist >0 integer max=1)		/*
		*/ BSCentile					/*
		*/ noDOts					/*
		*/ Level(cilevel)	  			/*
		*/ ESTimates(string)				/*
		*/ CLear					/*
		*/ SAving(string) ]

/* To deal with timeseries operators in depvar names
   there are three parallel lists
	e(endog) contains names (possibly with ts ops) that
		were specified by user of var
		this list is a valid varlist on lhs but
		not on rhs
	e(eqnames) contains names with "." after any
		ts op replaced by "_"
		these names are valid for creating names that 
		can be used on lhs or rhs.  However, variables
		with these names do not exist.  But this list
		does contain the list of eq names.
	endog_ts contains names of temporary variables generated
		from e(endog)  The variables in tmpvarlist are
		used in creating the dynamic forecasts.  At the
		end the variables in tmpvarlist are renamed and
		labeled.
   note that matrix `b_ts' is created by from e(b) by replacing all 
   	endogenous variables by tempvar name via 
		op_colnm `b_ts' orig tempname
	This replacement method accounts for all timeseries operators 
	on lhs and rhs.
*/	

	if "`bscentile'" != "" & "`bs'`bsp'" == "" {
		di as err "{cmd:bscentile} cannot be specified "	/*
			*/ "without either {cmd:bs} or {cmd:bsp}"
		exit 198
	}	

	if "`saving'" != "" & "`bs'`bsp'" == "" {
		di as err "{cmd:saving()} cannot be specified "	/*
			*/ "without either {cmd:bs} or {cmd:bsp}"
		exit 198
	}	


	if "`dots'" != "" & "`bs'`bsp'" == "" {
		di as err "{cmd:nodots} must be specified with "	/*
			*/ "either {cmd:bs} or {cmd:bsp}"
		exit 198
	}	


	if "`reps'" != "" & "`bs'`bsp'" == "" {
		di as err "{cmd:reps()} must be specified with "	/*
			*/ "either {cmd:bs} or {cmd:bsp}"
		exit 198
	}	

	if "`reps'" == "" {
		local reps 200
	}	


	local steps `step'

	local fbegin `dynamic'
	if "`steps'"== "" {
		local steps 1
	}	

	if `reps' <= 0 {
		di as err "reps() must specify a strictly positive " /*
			*/ "integer"
		exit 198
	}	
	if "`fbegin'" != "" {
		local fbdate = `fbegin'
		capture confirm integer number `fbdate'
		if _rc > 0 {
			di as err "fbegin() must specify an integer"
			exit 198
		}	
	}	
	
	if `steps' <=0 {
		di as err "steps() must specify a strictly positive "/*
			*/"integer"
		exit 198
	}	
	if "`bs'`bsp'" != "" {
		if "`bs'" != "" & "`bsp'" != "" {
			di as err "specify {cmd:bs} or {cmd:bsp}, not both"
			exit 198
		}	
	}	

	if `"`saving'"' != "" {
		gettoken bsfile replace : saving , parse(",")
		local tcnt : word count `bsfile'
		if `tcnt' != 1 {
			di as err "saving(`saving') not valid"
			exit 198
		}	
		local replace : subinstr local replace "," ""
		local replace : subinstr local replace " " "", all
		local tcnt : word count `replace'
		if `tcnt' == 1 {
			if "`replace'" != "replace"  {
				di as err "saving(`saving') not valid"
				exit 198
			}
		}	
		else {
			if `tcnt' != 0  {
				di as err "saving(`saving') not valid"
				exit 198
			}	
		}
	}	
	else {
		tempfile bsfile 
	}	


	tempname pest
	tempvar esamp
	qui _estimates hold `pest', copy restore nullok varname(`esamp')

	if "`estimates'" == "" {
		local estimates .
	}

	capture est restore `estimates'
	if _rc > 0 {
		di as err "cannot restore estimates(`estimates')"
		exit 498
	}	

	_cknotsvaroi varfcast compute

	if "`e(cmd)'" != "var" & "`e(cmd)'" != "svar" {
	di as error "{help varfcast##|_new:varfcast} only works " 	/*
		*/ " with estimates from {help var##|_new:var} and "	/*
			*/ "{help svar##|_new:svar}"
		exit 198
	}

	if "`e(cmd)'" == "svar" {
		local svar _var
	}

/* if e(bigf)=="nobigf" then nose is implicitly specified. if nose
   is not specified, set it with a warning message */
   	
	if "`e(bigf`svar')'" != "" & "`se'" == "" {
		di as txt "nose implicitly specified, nobigf on the " /*
			*/ "estimated VAR implies nose " 
		local se "nose"
	}	
   

	if "`clear'" != "" {
		local _varfcastnames : char _dta[_varfcastnames]
		foreach v of local _varfcastnames {
			capture drop `v'
		}
		char define _dta[_varfcastnames]
	}

	tempname bf0
	capture mat `bf0' = e(bf`svar')
	if _rc > 0 & "`se'" == "" {
		di as txt "could not assign e(bf), nose implicitly "	/*
			*/ "specified"
		local se nose	
	}	

	if matmissing(`bf0') & "`se'" != "nose" {
		di as txt "e(bf) contains missing values, nose "	/*
			*/ "implicitly specified"
		local se nose	
	}	
	
	
	/* check that fbegin is at least maxlags+1 into the sample */
	if "`fbegin'" != "" {
		if `fbegin' < e(tmin) {
			di as err "dynamic() cannot specify a time prior to "/*
				*/ "the beginning of the esimation sample"
			exit 198
		}
	}	
	capture tsset 
	local t "`r(timevar)'"

	if "`r(panelvar)'" != "" {
		di as error "this command does not work with panel data"
		exit 198 
	}	

	local eqlist "`e(eqnames`svar')'"
	local endog "`e(endog`svar')'"
	local exog  "`e(exog`svar')'"
	local lags  "`e(lags`svar')'"
	tempvar obsvar

	local neqs : word count `eqlist'

/* make endog_ts and b_ts that are safe with ts op on rhs */
	tempname b_ts
	mat `b_ts' = e(b`svar')
	local i 1

	qui count if e(sample) ==1
	if r(N) == 0 {
		di as err "e(sample) is never equal to 1"
		exit 498
	}	

	foreach v of local endog {
		local vt : subinstr local v "." "_"
		tempvar tv`i'
		qui gen double `tv`i'' = `v' if e(sample)
		op_colnm `b_ts' `v' `tv`i''
		local endog_ts "`endog_ts' `tv`i'' "
		local i = `i' + 1
	}
	
	qui gen `obsvar'=_n if e(sample)==1
	qui sum `obsvar'
	local lastob=r(max)
	local leastob=r(min)

	if "`fbegin'" != "" {
		tempvar gbob gbob2 
		tempname max1 max2
		gen `gbob'=cond(`t'==`fbegin'-1,_n,0)
		gen `gbob2'=sum(`gbob')
		qui sum `gbob'
		local max1=r(max)
		qui sum `gbob2'
		local max2=r(max)
					/* note since `t' is tsset, this 
					 can never happen, but just in 
					 case  refuse to it */
		if `max1' != `max2' {
			di as error "`fbegin' is not a valid date for this data"
			exit 198
		}	

		
		if `max1' <= `lastob' {
			if "`se'"  == "" {
				di as txt "since `fbegin' is in the "/*
					*/ "estimation sample, nose is " /*
					*/ "implicitly specified"
			}		
			local se "nose"	
		}

		if `max1' < `leastob' + e(mlag`svar')-1 {
			di as err "forecast must begin " e(mlag`svar') /*
				*/ " or more periods into estimation sample"
				exit 198
		}
		if `max1' > `lastob'+1  {
			di as err "forecast must begin no later than one" /*
				*/ " period after estimation period."
				exit 198
		}

		local lastob = `max1'
	}
		
/* make call to tsfill or tsappend */	

	local obs2add=-1*(_N-(`lastob'+`steps'))

	if `obs2add' <= 0 {
		tsfill
	}
	else {
		tsappend , add(`obs2add') 
	}
	qui replace `esamp' = 0 if `esamp' >= .
	if "`estimates'" != "." {
		qui replace _est_`estimates' = 0 	/*
			*/ if _est_`estimates' >= .
	}		

	if "`exog'" != "" {

		if "`se'" == "" {
			local se "nose"
			di as txt "asymptotic standard error not available " /*
				*/ "with exogenous variables"
		}		
	}

	fcast2 `endog_ts' , steps(`steps') lastob(`lastob') b_ts(`b_ts')


/* rename forecasted variables  */
	if "`fbegin'" == "" {
		local fbegin = `lastob' + 1
	}	
	
	local i 1
	foreach v of local eqlist {
		local vorig : word `i' of `endog'
		local dyn  "dyn(`fbegin')"
		local lbl "forecasted `vorig', `dyn'"
		qui rename `tv`i'' `v'_f
		label variable `v'_f "`lbl'"
		local i = `i' + 1
	}

/* now compute and insert standard errors */
	if "`bs'" != "" | "`bsp'" != "" {



/* check that there is sufficient memory for bs data */

/* size of bs dataset */

		local bsobs = (`steps')*`reps'

/* bwidth includes width which 3 for int step, 15*8 for 15 doubles
 * 15*3 for 3 15 character strings, and 4 for pointer overhead for each
 * observation
 */
		local bswidth = (8*(2+`neqs')+4)
		
		local bssize = `bsobs'*`bswidth'

/* only do check if !Small Stata */

		if "$S_FLAVOR" != "Small" {
qui memory

local free_m = r(M_data)-_N*(r(size_ptr)+r(width)+5*8)
local talloc = .01*(ceil(r(M_total)/(1024^2)*100))
local moreal = .01*(ceil((`bssize'-`free_m')/(1024^2)*100))

if `bssize' >= `free_m' {

	if `talloc' == 1 {
		local mys1 
	} 
	else {
		local mys1  s
	}

	if `moreal' == 1 {
		local mys2 
	} 
	else {
		local mys2  s
	}

	di as err "insufficient memory allocated for "	/*
		*/ "bootstrapped dataset"
	di as err "{p 0 4}there are `free_m' bytes free, "/*
		*/ "but the bootstrapped dataset will "	/*
		*/ "require `bssize' bytes{p_end}"
	di as err "{p 0 4}At present you have about "	///
		"`talloc' megabyte`mys1' "   		///
		"allocated and you need approximately "	///
		"`moreal' additional megabytes{p_end}"
	
	exit 901
}	
		}

		preserve
		tempname fsamp
		qui gen byte `fsamp' =e(sample)
		qui replace `fsamp' =1 if _n>=`lastob' /*
			*/ &  _n <= `lastob'+`steps'
		noi _varbsf using "`bsfile'", reps(`reps') step(`steps') /*
			*/ lastob(`lastob') `bsp' `replace' /*
			*/ fsamp(`fsamp') `dots'
		qui drop _all
		qui use "`bsfile'"
		if "`bscentile'" != "" {
			local lcent=100*(1-.01*`level')/2
			local ucent=`level' + `lcent'

			forvalues i = 1(1)`steps' {
				foreach v of local eqlist {
					tempname `v'L`i' `v'U`i' /*
						*/ `v'STD`i'
					qui centile `v'_f if /* 
						*/ step == `i', cent(`lcent')
					scalar ``v'L`i''=r(c_1)		
					qui centile `v'_f if /*
						*/ step == `i', cent(`ucent')
					scalar ``v'U`i''=r(c_1)	

					qui sum `v'_f if step == `i'
					scalar ``v'STD`i'' = r(sd)

				}
			}
		}
		else {
			local alpha = 1-.5*(100-`level')/100
			local std_alpha = invnorm(`alpha')
			forvalues i = 1(1)`steps' {
				foreach v of local eqlist {
					tempname `v'L`i' `v'U`i' /*
						*/ `v'STD`i'

					qui sum `v'_f if step == `i'
					scalar ``v'STD`i'' = r(sd)

					scalar ``v'L`i''= -1*`std_alpha'* /*
						*/ ``v'STD`i''
					scalar ``v'U`i''= `std_alpha'* /*
						*/ ``v'STD`i''

				}
			}
		}
		
		restore
		forvalues i = 1(1)`steps' {
			foreach v of local eqlist {
				if `i' == 1 {
					qui gen double `v'_f_L = .  
					qui gen double `v'_f_U = .
					qui gen double `v'_f_se = .
				}	

				if "`bscentile'" != "" {
					qui replace `v'_f_L = ``v'L`i'' /*
						*/ if _n==`lastob'+`i'
					qui replace `v'_f_U = ``v'U`i'' /*
						*/ if _n==`lastob'+`i'
					qui replace `v'_f_se = ``v'STD`i'' /*
						*/ if _n==`lastob'+`i'
				}
				else {
					qui replace `v'_f_L = `v'_f + 	/*
						*/ ``v'L`i''		/*
						*/ if _n==`lastob'+`i'
					qui replace `v'_f_U = `v'_f  + 	/*
						*/ ``v'U`i'' 		/*
						*/ if _n==`lastob'+`i'
					qui replace `v'_f_se = ``v'STD`i'' /*
						*/ if _n==`lastob'+`i'

				}
			}
		}		
		

		local i 1
		foreach v of local eqlist {

label variable `v'_f_L `"`=strsubdp("`level'")'% lower bound:`v'_f"'
label variable `v'_f_U `"`=strsubdp("`level'")'% upper bound:`v'_f"'
			label variable `v'_f_se "s.e. for `v'_f"
		
			local i = `i' + 1
		}
	
	}
	else {
		if "`se'" == "" {

/* make endog_ts and b_ts that are safe with ts op on rhs from e(bf) */
		tempname bf_ts
		mat `bf_ts' = e(bf`svar')
		local i 1

			foreach v of local endog {
				local vt : subinstr local v "." "_"
				tempvar tv`i'
				qui gen double `tv`i'' = `v' if e(sample)
				op_colnm `bf_ts' `v' `tv`i''
				local endogbf_ts "`endogbf_ts' `tv`i'' "
				local i = `i' + 1
			}

⌨️ 快捷键说明

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