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

📄 reg3.ado

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 ADO
📖 第 1 页 / 共 2 页
字号:
		est local corr `corr'
		est local small `small'
		est local predict reg3_p
		est local method `method'
		est local cmd "reg3"
	}

	/* Display results */

	if "`header'" != "" {
		local noh "*"
	}
	if "`table'" != "" {
		local not "*"
	}
	if "`footer'" != "" {
		local nof "*"
	}

	local testtyp  "  chi2"
	if "`e(small)'" == "small" { 
		local testtyp "F-Stat" 
		local testpfx F
	} 
	else {
		local testtyp "  chi2"
		local testpfx chi2
	}


	local method Three-stage least-squares
	if "`e(method)'" == "ols" | "`e(method)'" == "mvreg" { 
		local method Multivariate
	}
	if "`e(method)'" == "2sls" {
		local method Two-stage least-squares
	}
	if "`e(method)'" == "sure" {
		local method Seemingly unrelated
	}
		
	di
	if "`noh'" == "" {
		di in gr "`method' regression" _c
		if `e(ic)' > 0 { 
			di in gr ", iterated "
		}
		else	di
	}
	matrix dispCns, r
	if "`r(k)'" != "0" { 
		`noh' di in gr _newline "Constraints:"
		`noh' matrix dispCns
	}
	`noh' di in smcl in gr "{hline 70}"
	`noh' di in gr "Equation          Obs  Parms        RMSE    " /*
		*/ _quote "R-sq" _quote "     `testtyp'        P"
	`noh' di in smcl in gr "{hline 70}"
	tokenize `e(eqnames)'
	local i 1
	while "``i''" != "" {
		`noh' di in ye abbrev("``i''",12) /*
			*/ _col(15) %7.0g e(N) %7.0g e(df_m`i') /*
			 */ "   " %9.0g e(rmse_`i') %10.4f e(r2_`i') "  "   /*
			 */ %9.2f e(`testpfx'_`i') %9.4f e(p_`i')
		local i = `i' + 1
	}
	`noh' di in smcl in gr "{hline 70}"
	`noh' di
	`not' estimates display, level(`level')

	if "`e(method)'" == "2sls" | "`e(method)'" == "3sls" {
		`nof' DispVars "Endogenous variables:  " "`e(endog)'" 6 78 green
		`nof' DispVars "Exogenous variables:   " "`e(exog)'"  6 78 green
		`nof' di in smcl in gr "{hline 78}"
	}

end

program define GetDFK2adjust, rclass
	version 8.0
	syntax  ,					/*
	*/	k(numlist integer min=1 max=1 >0)	/*
	*/	keq(numlist integer min=1 max=1 >0)	/*
	*/	colnames(string)			/*
	*/	coleqs(string)				/*
	*/	[					/*
	*/	clist(string)				/*
	*/	]

	local nc : word count `colnames'
	local ne : word count `coleqs'
	if `k' != `nc' {
		di as err "interal error in reg3"
		exit 198
	}
	if `ne' != `nc' {
		di as err "interal error in reg3"
		exit 198
	}

	if `"`clist'"' != "" {
		tempname b T a C
		mat `b' = J(1,`k',1)
		mat colnames `b' = `colnames'
		mat coleq `b' = `coleqs'
		Post4Cns `b'
		matrix makeCns `clist'
		matcproc `T' `a' `C'
		local cols = colsof(`T')
		mat `b' = syminv(`T'*I(`cols')*`T'')
		local k = `k' - diag0cnt(`b')
	}
	return scalar adj = `k' / `keq'
end

program define Post4Cns, eclass
	args b
	tempname v
	mat `v' = `b''*`b'
	eret post `b' `v'
end

/*  Sets the local macros containing equation information in the caller 
 *  Equations may take the form:  
 *
 *           [eqname:] y1 [y2 y3 =] x1 x2 x3 [, noconstant] 
 *
 *  Sets the callers local macros:  eqname, depvars, indvars and constan  */ 
 					/* might form basis of a _parseeq */

program define parseEqn        

	/* see if we have an equation name */
	gettoken token uu : 0, parse(" =:")   /* rare, pull twice if found */
	gettoken token2 : uu, parse(" =:")     /* rare, pull twice if found */
	if index("`token2'", ":") != 0 {
		gettoken token  0 : 0, parse(" =:")      /* sic, to set 0 */
		gettoken token2 0 : 0, parse(" =:")      /* sic, to set 0 */
		c_local eqname  `token'
	} 
	else    c_local eqname 

	/* search just for "=" */
	gettoken token 0 : 0, parse(" =")
	while "`token'" != "=" & "`token'" != "" {
		local depvars `depvars' `token'
		gettoken token 0 : 0, parse(" =")
	}

	if "`token'" == "=" {
		tsunab depvars : `depvars'
		syntax [varlist(ts)] [ , noConstant ]
	} 
	else {				/* assume single depvar */
		local 0 `depvars'
		syntax varlist(ts) [ , noConstant ]
		gettoken depvars varlist : varlist
	}

	c_local depvars `depvars'
	c_local indvars `varlist'
	c_local constan `constan'
end


/*  determine equation name */

program define nameEq, sclass
	args	    eqname	/* user specified equation name
		*/  depvar	/* dependent variable name
		*/  eqlist	/* list of current equation names 
		*/  neq		/* equation number */
	
	if "`eqname'" != "" {
		if index("`eqname'", ".") {
di in red "may not use periods (.) in equation names: `eqname'"
		}
		local eqlist : subinstr local eqlist "`eqname'" "`eqname'", /*
			*/ word count(local count)    /* overkill, but fast */
		if `count' > 0 {
di in red "may not specify duplicate equation names: `eqname'"
			exit 198
		}
		sreturn local eqname `eqname'
		exit
	}
	
	local depvar : subinstr local depvar "." "_", all

	if length("`depvar'") > 32 {
		local depvar "eq`neq'"
	}
	Matches dupnam : "`eqlist'" "`depvar'"
	if "`dupnam'" != "" {
		sreturn local eqname = substr("`neq'`depvar'", 1, 32)
	}
	else {
		sreturn local eqname `depvar'
	}
end


program define IsStop, sclass
	if 	     `"`0'"' == "[" /*
		*/ | `"`0'"' == "," /*
		*/ | `"`0'"' == "if" /*
		*/ | `"`0'"' == "in" /*
		*/ | `"`0'"' == "" {
		sret local stop 1
	}
	else	sret local stop 0
end


/*  Drop all duplicate tokens from list */

program define DropDup   /* <newlist> : <list> */
	args	    newlist	/*  name of macro to store new list
		*/  colon	/*  ":"
		*/  list	/*  list with possible duplicates */

	gettoken token list : list
	while "`token'" != "" {
		local fixlist `fixlist' `token'
		local list : subinstr local list "`token'" "", word all
		gettoken token list : list
	}

	c_local `newlist' `fixlist'
end


/*  Remove all tokens in dirt from full */
 *  Returns "cleaned" full list in cleaned */

program define Subtract   /* <cleaned> : <full> <dirt> */
	args	    cleaned     /*  macro name to hold cleaned list
		*/  colon	/*  ":"
		*/  full	/*  list to be cleaned 
		*/  dirt	/*  tokens to be cleaned from full */
	
	tokenize `dirt'
	local i 1
	while "``i''" != "" {
		local full : subinstr local full "``i''" "", word all
		local i = `i' + 1
	}

	c_local `cleaned' `full'       /* cleans up extra spaces */
end

/*  Returns tokens found in both lists in the macro named by matches.
 *  Duplicates must be duplicated in both lists to be considered
 *  matches a 2nd, 3rd, ... time.  */

program define Matches   
	args	    matches     /*  macro name to hold cleaned list
		*/  colon	/*  ":"
		*/  list1	/*  a list of tokens
		*/  list2	/*  a second list of tokens */

	tokenize `list1'
	local i 1
	while "``i''" != "" {
		local list2 : subinstr local list2 "``i''" "", /*
			*/ word count(local count)
		if `count' > 0 {
			local matlist `matlist' ``i''
		}
		local i = `i' + 1
	}

	c_local `matches' `matlist'
end

/*  Find all occurances in List of tokens in FindList and replace with 
 *  corresponding token from SubsList.  Assumes FindList and SubstList 
 *  have same number of elements.
*/ 

program define Subst, sclass    /*  <NewList> : <List> <FindList> <SubstList> */
	args	    newname	/*  macro name to hold list after replacements
		*/  colon	/*  ":"
		*/  list	/*  varlist with tokens to be replaced
		*/  fndList	/*  list of tokens to be replaced 
		*/  subList	/*  varlist with replacment tokens */

	tokenize `fndList'
	local i 1
	while "``i''" != "" {
		gettoken repltok subList : subList
		local list : subinstr local list "``i''" "`repltok'", word all
		local i = `i' + 1
	}

	c_local `newname' `list'
end


/* Set disturbance covariance matrix structure. */
/* Using syntax would be nice here, but 10 character limit is problematic. */

program define setCorr   /* <corrMacro> : <corrString> */
	args	    corrmac	/*  macro name to contain corr type
		*/  colon	/*  ":"
		*/  corrstr	/*  String containing correlation words */

	if length("`corrstr'") == 0 {
		c_local `corrmac' unstructured
		exit
	}

	if substr("unstructured",1,length("`corrstr'"))=="`corrstr'" {
		c_local `corrmac' unstructured
		exit
	}

	if substr("independent",1,length("`corrstr'"))=="`corrstr'" {
		c_local `corrmac' independent
		exit
	}

	di in red "Unsupported disturbance correlation specified: `corrstr'"
	exit 198
end

/*  Test if equation is identified -- order condition */

program define testIdnt  /* <eqn> <exlist> */
	args	    rhs		/* rhs variables for an equation 
		*/  exlist	/* full list of exogenous variables
		*/  eqname	/*  equation name (for error display)
		*/  depvar      /*  dependent variable (for display) */

	Subtract endog : "`rhs'" "`exlist'"
	local endcnt : word count `endog'
	Subtract exog : "`exlist'" "`rhs'"
	local excnt : word count `exog'
	if `endcnt' > `excnt' {
dis in red "Equation is not identified -- does not meet order conditions"
	DispVars `"    Equation `eqname':  `depvar'  "' "`rhs'" 10 78 red
	DispVars `"    Exogenous variables:   "' "`exlist'" 10 78 red
		exit 481
	}
end

/*  Display a list of variables breaking line nicely */
/*  With new quotes `"..."' no longer need prefix, but kept */

program define DispVars  
	args	     prefix	/* prefix string for first line 
		*/   varlist	/* variable list 
		*/   strtcol	/* starting column for all lines but first 
		*/   maxlen	/* final column for writing 
		*/   color	/* color for writing */

	di in `color' `"`prefix'"' _c
	local curlen = length(`"`prefix'"')

	tokenize `varlist'
	local i = 1
	while "``i''" != "" {
		local len = length("``i''")
		if (`curlen' + `len' + 1) > `maxlen' { 
			di ""
			di _col(`strtcol') _c
			local curlen `strtcol'
		}
		di in `color' "``i'' " _c
		local curlen = `curlen' + `len' + 1
		local i = `i' + 1
	}
	disp ""
end

/*  Element-by-element matrix multiplication.  Result left in <mat1> */

program define mElMult            /* <mat1> <mat2> */
	args mat1 mat2

	local rows = rowsof(`mat1')
	local cols = colsof(`mat1')

	if `rows' != rowsof(`mat2') | `cols' != colsof(`mat2') {
		exit 503
	}

	local i 1
	while `i' <= `rows' {
		local j 1
		while `j' <= `cols' {
			mat `mat1'[`i',`j'] = `mat1'[`i',`j'] * `mat2'[`i',`j']
			local j = `j' + 1
		}
		local i = `i' + 1
	}
end

/*  sum all of the elements in a matrix */

program define mSumEls            /* <scalar_sum> : <matrix> */
	args sum colon matrix

	local rows = rowsof(`matrix')
	local cols = colsof(`matrix')

	scalar `sum' = 0
	local i 1
	while `i' <= `rows' {
		local j 1
		while `j' <= `cols' {
			scalar `sum' = `sum' + `matrix'[`i',`j']
			local j = `j' + 1
		}
		local i = `i' + 1
	}
end

/*  Check the diagonal of a matrix for missings */

program define chkDiag         /*  <matrix> */
	args mat

	local dim = rowsof(`mat')
	if `dim' != colsof(`mat') {
		exit 503
	}
	
	local i 1
	while `i' <= `dim' {
		if `mat'[`i',`i'] == 0 {
			dis in red "Covariance matrix of errors is singular"
			mat list `mat'
			exit 506
		}
		local i = `i' + 1
	}
end


/*  Apply constraints to the system */

program define Constrn        /* <constraints> <smallsmplstat> <dof> */
	args	     constr     /*  list of constraint numbers
		*/   small	/*  non-blank ==> t-stat, not z-stat
		*/   dof 	/*  degrees of freedom			*/


	tempname A beta C IAR j R Vbeta touse

	matrix makeCns `constr'
	matrix `C' = get(Cns)

	local cdim = colsof(`C')
	local cdim1 = `cdim' - 1

	matrix `R' = `C'[1...,1..`cdim1']
	matrix `A' = syminv(`R'*get(VCE)*`R'')
	local a_size = rowsof(`A')

	scalar `j' = 1
	while `j' <= `a_size' {
		if `A'[`j',`j'] == 0 {
			error 412
		} 
		scalar `j' = `j' + 1
	}
	matrix `A' = get(VCE)*`R''*`A'
	matrix `IAR' = I(colsof(get(VCE))) - `A'*`R'
	matrix `beta' = get(_b) * `IAR'' + `C'[1...,`cdim']'*`A''
	matrix `Vbeta' = `IAR' * get(VCE) * `IAR''

	gen byte `touse' = e(sample)
	if "`small'" == "small" {
		estimates post `beta' `Vbeta' `C', dof(`dof') esample(`touse')
	} 
	else { 
		estimates post `beta' `Vbeta' `C', obs(`dof') esample(`touse')
	}
end

program define setLL, eclass
	args	    EpE		/* accum of residuals  (is modified)*/

	tempname SIGi
	mat `SIGi' = (1/(e(N))) * `EpE'

	est scalar ll = -0.5 * (e(N)*e(k_eq)*ln(2*_pi) +  /*
		*/  e(N)*ln(det(`SIGi')) + e(N)*e(k_eq))

/*  Blunt, inefficient method.
	mat `SIG'  = syminv(`SIGi')
	mElMult `EpE' `SIG'
	mSumEls  `ll_err' : `EpE'
	est scalar ll = -0.5 * (e(N)*e(k_eq)*ln(2*_pi) +  /*
		*/  e(N)*ln(det(`SIGi')) + `ll_err')
*/

end

exit

- Requires double storage for the total number of endogenous variables
  (left hand side or otherwise) + double storage for the errors from each
  equation.

-  Currently contraints are tested after the first stage.  This is
	a matter of convenience, since the full model has not been constructed 
	until that point.  However, it could cause some wasted time if the 
	specified constraints are invalid for the model.

-  Due to the way constraints are applied, constrained models cannot be 
	estimated if the unconstrained model is not identified and has 
	sufficient data.

⌨️ 快捷键说明

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