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

📄 adjust.ado

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 ADO
📖 第 1 页 / 共 3 页
字号:
*! version 2.2.4  17mar2005
program adjust
	version 6.0, missing

* Syntax:
* adjust [var[= #] [var[= #] ...]] [if exp] [in range] [, by(varlist)
*      generate(var [var]) {xb [se|stdf] | pr | exp} ci level(#) vertical
*      equation(##|name) nooffset replace label() selabel() cilabel() nokey 
*      noheader format(%fmt) <tabdisp options>]
*
* Where the allowed tabdisp options are: center left cellwidth(#) csepwidth(#)
* scsepwidth(#) stubwidth(#)

	/* Check the estimation command we follow */
	if "`e(cmd2)'" != "" {
		local ecmd "`e(cmd2)'"
	}
	else {
		local ecmd "`e(cmd)'"
	}

	if "`ecmd'"=="binreg" {
		local elinkt "`e(linkt)'"
	}

	CheckCmd `ecmd'
	local edvar "`e(depvar)'"
	local edcnt : word count `edvar'
	if `edcnt' > 1 {
		/* multiple depvars --- list equation instead */
		local edvar
	}

	/* Non standard parsing due to [var[= #][var[= #]...]]
	   This next command splits it into two parts: the varlist part 
	   (com = s(part1)) and the rest (opt = s(part2)).
	*/
	SplitIt `*'
	local com "`s(part1)'"
	local opt "`s(part2)'"

	/* standard parsing for the opt part of the command */
	local maxby 7    /* 7 because of tabdisp */
	local 0 "`opt'"
	#delimit ;
	syntax [if] [in] [, BY(varlist min=1 max=`maxby') Generate(string)
		XB Pr EXP SE STDF CI Level(cilevel) VERTical
		EQuation(string) noOFFset REPLACE LABel(string) SELABel(string)
		CILABel(string) noKEY noHEADer CENter Left Format(string)
		CELLWidth(passthru) STUBWidth(passthru) CSEPwidth(passthru)
		SCSEPwidth(passthru)
	] ;
	#delimit cr

	/* Initial marking out of the -if- and -in- */
	marksample touse

	/* Non standard parsing of the varlist */
	ParseVar `com'
	local covars "`s(vnames)'" /* Covariates to be set */
	local covals "`s(values)'" /* Values to set the covariates or "mean" */

	/* Pull all the tabdisp options into one local macro */
	local tabopts "`center' `left' `cellwid' `stubwid' `csepwid' `scsepwi'"

	/* Take care of format --- default %8.0g */
	if "`format'" == "" {
		local format "%8.0g"
	}
	quietly di `format' 0	/* an error check on the format */

	/* Check equation() option */
	CheckEq `"`equatio'"'
	local equatio "`r(eqname)'"
	if "`equatio'" != "" { local eqopt "equation(`equatio')" }

	/* Get the variable names from the estimation coef. vector */
	GetbName "`ecmd'" `"`equatio'"'
	local bnames "`r(names)'"

	/* Check that the covars are in bnames */
	CheckVar "`covars'" "`bnames'"

	/* take care of case where no -by()- was specified */
	if "`by'" == "" {
		tempvar oneby
		gen str1 `oneby' = " "
		label var `oneby' "All"
		local by `oneby'
	}

	/* Create various lists of variables */
	CheckBy `by'
	Subtract "`by'" "`covars'"
	local bynotc "`r(list)'" /* the by vars not in covars list */
	Subtract "`by'" "`bynotc'"
	local byinc "`r(list)'"  /* the by vars in the covars list */
	Subtract "`bnames'" "`covars' `by'"
	local asis "`r(list)'"   /* variables left as is */

	/* mark the variables --- -if- and -in- were marked earlier */
	markout `touse' `bnames'
	markout `touse' `by' , strok
	qui count if `touse'
	if r(N) == 0 { error 2000 }  /* need observations */

	/* Check a bunch of options and determine labels and pr command */
	CheckOpt "`xb'" "`pr'" "`exp'" "`se'" "`stdf'" "`ci'" /*
		*/ "`label'" "`selabel'" "`cilabel'" "`ecmd'" "`level'" /*
		*/ "`elinkt'"
	local esttype "`r(esttype)'"
	local errtype "`r(errtype)'"
	local prprog  "`r(prprog)'"
	local label   "`r(label)'"
	local selabel "`r(selabel)'"
	local cilabel "`r(cilabel)'"

	/* Take care of the generate() option */
	CheckGen "`generat'" "`errtype'" "`replace'"
	local ngen "`r(ngen)'"  /* number of generate variables */
	local gen1 "`r(gen1)'"  /* 1st generate var (for xb or pr) */
	local gen2 "`r(gen2)'"  /* 2nd generate var (for stdp or stdf) */

	/* preliminary set up if we are generating variables */
	if `ngen' > 0 { /* find sort order and make id -- for future merge */
		local sorted : sortedby
		tempvar id
		gen long `id' = _n
		qui compress `id'
	}

	/* make a copy of the by vars that are also covars */
	if "`asis'" != "" {
		local newby "`bynotc'"
	}
	tokenize "`byinc'"
	local i 1
	while "``i''" != "" {
		tempvar tmp`i'
		gen `tmp`i'' = ``i''
		if "`asis'" != "" {
			local newby "`newby' `tmp`i''"
		}
		local i = `i' + 1
	}

	/* cut data to the touse sample */
	preserve
	qui keep if `touse'

	/* set the covars equal to the covals */
	SetCovs "`covars'" "`covals'"
	local thevals "`r(values)'"

	/* check that if there is an offset in the model it is constant within
	   each table cell or that the -nooffset- option of -adjust- was
	   specified.
	*/
	qui sort `by'
	if "`e(offset)'" != "" & "`offset'" == "" {
		local offnam `e(offset)'
		if substr("`offnam'",1,3) == "ln(" {
			local offnam=substr("`offnam'",4,length("`offnam'")-4)
		}
		cap by `by': assert `offnam'==`offnam'[_n-1] if _n > 1
		if (_rc != 0) {
			di as err /*
			    */ "offset (`e(offset)') not constant within by()"
			exit 198
		}
	}

	/* get the individual predictions (and errors if required) */
	/* For this the asis variables are left completely as is while the
	   covars have been set to their mean (or other specified values). */
	if `ngen' > 0 { /* get individual predictions */
		/* always start with xb */
		qui _predict `gen1', xb `eqopt' `offset'
		/* if -pr- or -exp- then prprog the xb values in gen1 */
		`prprog' `gen1'
		label var `gen1' "`label'"
	}
	if `ngen' > 1 { /* get individual error (stdp or stdf) */
		if "`errtype'" == "" {
			qui _predict `gen2', stdp `eqopt' `offset'
		}
		else {
			qui _predict `gen2', `errtype' `eqopt' `offset'
		}
		label var `gen2' "`selabel'"
	}


	/* Take care of asis variables. --- For the Table the asis variables
           are replaced with their mean within each cell defined by the by
           variables. */
	if "`asis'" != "" {
		qui sort `newby'
		tokenize "`asis'"
		local i 1
		while "``i''" != "" {
			/* make a copy of the asis var -- to restore later */
			tempvar asis`i'
			gen `asis`i'' = ``i''
			/* replace asis variable with mean for each by cell */
			qui by `newby' : replace ``i'' = sum(``i'')/_N
			/* _N ok here because no missing values */
			qui by `newby' : replace ``i'' = ``i''[_N]
			local i = `i' + 1
		}
	}


	/* Do predictions with all substitutions in force */
	/* We could have collapsed down to one observation per table cell
	   at this point except that we need to get the data back in good
           shape in case we are merging in generated variables later.  The
           collapsing to one observation per cell is done inside DoTable */
	tempvar myxb myse
	qui _predict `myxb', xb `eqopt' `offset'    /* always use xb */
	if "`errtype'" == "" {
		qui _predict `myse', stdp `eqopt' `offset'
	}
	else {
		qui _predict `myse', `errtype' `eqopt' `offset'
	}


	/* restore the original asis variables */
	if "`asis'" != "" {
		tokenize "`asis'"
		local i 1
		while "``i''" != "" {
			qui replace ``i'' = `asis`i''
			qui drop `asis`i''
			local i = `i' + 1
		}
	}

	/* restore the by vars that are also covars to their original values */
	tokenize "`byinc'"
	local i 1
	while "``i''" != "" {
		qui replace ``i'' = `tmp`i''
		qui drop `tmp`i''
		local i = `i' + 1
	}

	/* display some table header info */
	if "`header'" != "noheader" {
		Header "`generat'" "`asis'" "`by'" "`covars'" "`covals'" /*
			*/ "`thevals'" "`edvar'" "`equatio'" "`ecmd'"
	}

	/* create and display the table of results */
	DoTable "`key'" "`by'" "`myxb'" "`myse'" `"`label'"' `"`selabel'"' /*
		*/ "`esttype'" "`errtype'" "`ci'" `"`cilabel'"' "`level'" /*
		*/ "`prprog'" "`vertica'" "`tabopts'" "`format'" "`replace'"

	/* Don't restore if we are replacing the data with the Table */
	if "`replace'" != "" {
		restore, not
	}
	/* If we generated vars then merge them in original sorted order */
	else if `ngen' > 0 {
		keep `id' `generat'
		sort `id'
		tempfile hold
		qui save "`hold'"
		restore, preserve
		sort `id'
		tempvar junk
		merge `id' using "`hold'" , _merge(`junk')
		drop `junk'
		if "`sorted'" != "" { sort `sorted' }
		restore, not
	}
end


* CheckBy checks the by() option variables which are passed on 
* the command line for repeats.
program CheckBy /* <byvars> */
	Repeats `*'
	if "`r(replist)'" != "" {
		di in red "`r(replist)': specified twice in the by option"
		exit 198
	}
end


* CheckCmd is passed a string which should be the name of the estimation
* command last run.  This program checks if it is empty or one of the 
* unsupported estimation commands.
program CheckCmd  /* <estcmd> */
	if "`*'"=="" {
		di in red "This command must follow an estimation command"
		exit 301
	}
	/* check if the estimation command is one of the unsupported ones */
	if "`*'"=="areg" | "`*'"=="canon" | "`*'"=="nl" | "`*'"=="pca" {
		di in red "This command currently not allowed following `*'"
		exit 301
	}

/* Why this command will not follow certain estimation commands:

areg -- the variable specified in the -absorb()- required option does not
	have it's betas computed and so that variable is not handled as we
	might think by -predict-.  If we allowed this command the results 
	produced would not be what is expected.

nl   -- not designed to work with nonlinear estimation.

pca, canon  -- not typical estimation commands.

There are others that are not allowed (such as factor) and get trapped in one
way or another along the way (usually by _predict).
*/
end


* CheckEq checks if the equation() option was given and if so if it is valid.
* Also if the person passed in the equation number instead of name we find 
* the name.  We pass back the name or null. 
program CheckEq /* <equation option> */ , rclass
	args eqarg offset

	if `"`eqarg'"'=="" { /* user didn't specify equation() option */
		tempname bmat
		mat `bmat' = e(b)
		local names : coleq `bmat' , quote
		tokenize `"`names'"'
		if `"`1'"' == "_" {  /* there are no equation names */
			ret local eqname
		}
		else { /* return 1st eq. name since user didn't specify */
			ret local eqname `"`1'"'
		}
	}
	else { /* user specified equation() option */
		tempvar tmp
		/* This _predict to see if equation() option is valid */
		qui capture _predict `tmp' in 1, equation(`eqarg')
		if _rc {
			di in red `"equation(`eqarg') invalid"'
			exit 198
		}
		if substr(trim(`"`eqarg'"'),1,1) == "#" { /* specified eq. # */
			local eqnum = substr(trim(`"`eqarg'"'),2,.)
			/* double check that it is one number */
			qui capture confirm integer number `eqnum'
			if _rc {
				di in red `"equation(`eqarg') invalid"'
				exit 198
			}
			/* `eqnum' has equation number --- we want the name */
			tempname bmat
			mat `bmat' = e(b)
			local names : coleq `bmat' , quote
			tokenize `"`names'"'
			local i 1
			local k 1
			while `"``i''"' != "" & `k' <= `eqnum' {
				if `k' == `eqnum' {
					ret local eqname `"``i''"'
				}
				else {
					local j = `i' + 1
					while `"``j''"' == `"``i''"' {
						local j = `j' + 1
					}
					local i = `j'
				}
				local k = `k' + 1
			}
		}
		else { /* User gave equation name */
			if index(`"`eqarg'"',",") != 0 {
				/* If more than one equation name */
				di in red `"equation(`eqarg') invalid"'
				exit 198
			}
			ret local eqname `"`eqarg'"'
		}
	}
end


* CheckGen checks the generate() vars and returns the number of vars in
* r(ngen), the first var name in r(gen1), and the second in r(gen2).
program CheckGen  /* <genvars> <etype> <replace> */ , rclass
	args genvars etype replace

	if "`replace'" != "" & "`genvars'" != "" {
		di in red "cannot specify both generate() and replace"
		exit 198
	}

	local ngen 0
	if "`genvars'" != "" {
		confirm new variable `genvars'
		local ngen : word count `genvars'
		if `ngen' > 2 {
			di in red "one or two variables allowed in generate()"
			exit 198
		}
		local gen1 : word 1 of `genvars'
		if `ngen' == 2 { local gen2 : word 2 of `genvars' }
	}

⌨️ 快捷键说明

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