(
newcommand{betab}{boldsymbol{beta}}
newcommand{xb}{{bf x}}
newcommand{yb}{{bf y}}
newcommand{Xb}{{bf X}}
)I present the right way to write a Stata estimation command that implements the atypical least-squares (OLS) estimator by explaining the code. I exploit ideas that I launched in earlier #StataProgramming posts. Particularly, I construct on Programming an estimation command in Stata: Utilizing Stata matrix instructions and capabilities to compute OLS objects, wherein I recalled the OLS formulation and confirmed the right way to compute them utilizing Stata matrix instructions and capabilities and on
Programming an estimation command in Stata: A primary ado command, wherein I launched some ado-programming ideas. Though I introduce some native macro tips that I exploit on a regular basis, I additionally construct on Programing an estimation command in Stata: The place to retailer your stuff.
That is the sixth submit within the collection Programming an estimation command in Stata. I like to recommend that you simply begin originally. See Programming an estimation command in Stata: A map to posted entries for a map to all of the posts on this collection.
Native macro tips
I exploit numerous native macro tips in my ado-files. On this part, I illustrate ones that I exploit within the instructions that I develop on this submit. In each ado-file that I write, I ask questions on lists of variable names saved in native macros. I steadily use the prolonged macro capabilities and the gettoken command to ask these questions and retailer the ends in a neighborhood macro.
The syntax for storing the results of an prolonged macro operate in a neighborhood macro is
native localname : extended_fcn
Under, I exploit the prolonged macro operate phrase rely to rely the variety of parts within the listing and retailer the outcome within the native macro rely.
Instance 1: Storing and extracting the results of an prolonged macro operate
. native rely : phrase rely a b c . show "rely accommodates `rely'" rely accommodates 3
There are lots of prolonged macro capabilities, however I illustrate simply the one I exploit on this submit; sort assist prolonged fcn for an entire listing.
A token is a component in a listing. I steadily use the gettoken command to separate lists aside. The gettoken command has the syntax
gettoken localname1 [localname2] : localname3
gettoken shops the primary token within the listing saved within the native macro localname3 into the native macro localname1. If the non-obligatory localname2 is specified, the remaining tokens are saved within the native macro localname2.
I exploit gettoken to retailer the primary token saved in mylist into the native macro first, whose contents I subsequently extract and show.
Instance 2: Utilizing gettoken to retailer first token solely
. native mylist y x1 x2 . show "mylist accommodates `mylist'" mylist accommodates y x1 x2 . gettoken first : mylist . show "first accommodates `first'" first accommodates y
Now, I exploit gettoken to retailer the primary token saved in mylist into the native macro first and the remaining tokens into the native macro left. I subsequently extract and show the contents of first and left.
Instance 3: Utilizing gettoken to retailer first and remaining tokens
. gettoken first left: mylist . show "first accommodates `first'" first accommodates y . show "left accommodates `left'" left accommodates x1 x2
I steadily need to enhance the worth of a neighborhood macro by some fastened quantity, say, (3). I now illustrate an answer that I exploit.
Instance 4: Native macro replace
. native p = 1 . native p = `p' + 3 . show "p is now `p'" p is now 4
When the replace worth, also referred to as the increment worth, is (1), we are able to use the increment operator, as under:
Instance 5: Native macro replace
. native p = 1 . native ++p . show "p is now `p'" p is now 2
A primary model of myregress
The code in myregress1 implements a model of the OLS formulation. I exploit myregress1 in instance 6. Under instance 6, I focus on the code and the output.
*! model 1.0.0 23Oct2015 program outline myregress1, eclass model 14 syntax varlist show "The syntax command places the variables specified by the " show " consumer into the native macro varlist" show " varlist accommodates `varlist'" gettoken depvar : varlist show "The dependent variable is `depvar'" matrix accum zpz = `varlist' show "matrix accum types Z'Z" matrix listing zpz native p : phrase rely `varlist' native p = `p' + 1 matrix xpx = zpz[2..`p', 2..`p'] matrix xpy = zpz[2..`p', 1] matrix xpxi = syminv(xpx) matrix b = (xpxi*xpy)' matrix rating double xbhat = b generate double res = (`depvar' - xbhat) generate double res2 = res^2 summarize res2 native N = r(N) native sum = r(sum) native s2 = `sum'/(`N'-(`p'-1)) matrix V = `s2'*xpxi ereturn submit b V ereturn native cmd "myregress1" ereturn show finish
Instance 6: myregress1 output
. sysuse auto
(1978 Vehicle Knowledge)
. myregress1 value mpg trunk
The syntax command places the variables specified by the
consumer into the native macro varlist
varlist accommodates value mpg trunk
The dependent variable is value
(obs=74)
matrix accum types Z'Z
symmetric zpz[4,4]
value mpg trunk _cons
value 3.448e+09
mpg 9132716 36008
trunk 6565725 20630 15340
_cons 456229 1576 1018 74
Variable | Obs Imply Std. Dev. Min Max
-------------+---------------------------------------------------------
res2 | 74 6674851 1.30e+07 11.24372 9.43e+07
------------------------------------------------------------------------------
| Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
mpg | -220.1649 65.59262 -3.36 0.001 -348.7241 -91.6057
trunk | 43.55851 88.71884 0.49 0.623 -130.3272 217.4442
_cons | 10254.95 2349.084 4.37 0.000 5650.83 14859.07
------------------------------------------------------------------------------
Listed below are my feedback on the code and the output in instance 6.
- Line 2 specifies that myregress1 is an e-class command that shops its ends in e().
- Traces 5–8 illustrate that the syntax command shops the names of the variables specified by the consumer within the native macro varlist. This conduct can also be illustrated in instance 6.
- Line 10 makes use of the gettoken command to retailer the primary variable identify saved within the native macro varlist within the native macro depvar. Line 11 shows this identify and the utilization is illustrated in instance 6.
- Line 13 makes use of matrix accum to place ((Xb’Xb)) and ((Xb’yb)) right into a Stata matrix named zpz, as mentioned in Programming an estimation command in Stata: Utilizing Stata matrix instructions and capabilities to compute OLS objects and additional illustrated in strains 14–15 and instance 6.
- Line 17 shops the variety of variables within the native macro varlist into the native macro p.
- Line 18 increments the native macro p by (1) to account for the fixed time period included by matrix accum by default.
- Traces 20–23 extract ((Xb’Xb)) and ((Xb’yb)) from zpz and put the vector of level estimates (widehat{betab}) into the Stata row vector b.
- Line 25 places (Xbwidehat{betab}) into the variable xbhat.
- Traces 26 and 27 calculate the residuals and the squared residuals, respectively.
- Traces 28–32 calculate the estimated variance-covariance matrix of the estimator (VCE) from the sum of squared residuals.
- Line 33 shops b and V into e(b) and e(V), respectively.
- Line 34 shops the identify of the estimation command (myregress1) in e(cmd).
- Line 35 produces an ordinary Stata output desk from the ends in e(b) and e(V).
myregress1 accommodates code to assist illustrate the way it works, and it makes use of hard-coded names for international objects like Stata variables and Stata matrices. Customers don’t need to see the output from the illustration strains, so that they should be eliminated. Customers don’t need their international Stata matrices overwritten by a command they use, which is what myregress1 would do to a matrix named zpz, xpx, xpxi, b, or V.
The code in myregress2 fixes these issues.
*! model 2.0.0 26Oct2015 program outline myregress2, eclass model 14 syntax varlist gettoken depvar : varlist tempname zpz xpx xpy xpxi b V tempvar xbhat res res2 quietly matrix accum `zpz' = `varlist' native p : phrase rely `varlist' native p = `p' + 1 matrix `xpx' = `zpz'[2..`p', 2..`p'] matrix `xpy' = `zpz'[2..`p', 1] matrix `xpxi' = syminv(`xpx') matrix `b' = (`xpxi'*`xpy')' quietly matrix rating double `xbhat' = `b' generate double `res' = (`depvar' - `xbhat') generate double `res2' = (`res')^2 quietly summarize `res2' native N = r(N) native sum = r(sum) native s2 = `sum'/(`N'-(`p'-1)) matrix `V' = `s2'*`xpxi' ereturn submit `b' `V' ereturn native cmd "myregress2" ereturn show finish
- Line 8 makes use of tempname to place protected names into the native macros zpz, xpx, xpy, xpxi, b, and V.
- Line 9 makes use of tempvar to place protected names into the native macros xbhat, res, res2.
- Traces 11, 14–18, and 25–26 use the protected names within the native macros created by tempname as a substitute of the hard-coded names for the matrices.
- Traces 18–20 use the protected names within the native macros created by tempvar as a substitute of the hard-coded names for the variables it creates.
The output under exhibits the output produced by myregress2.
Instance 7: myregress2 output
. myregress2 value mpg trunk
------------------------------------------------------------------------------
| Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
mpg | -220.1649 65.59262 -3.36 0.001 -348.7241 -91.6057
trunk | 43.55851 88.71884 0.49 0.623 -130.3272 217.4442
_cons | 10254.95 2349.084 4.37 0.000 5650.83 14859.07
------------------------------------------------------------------------------
Completed and undone
After reviewing some tips with native macros that I exploit in a lot of the ado-files that I write, I mentioned two variations of an ado-command that implements the (OLS) estimator. Within the subsequent submit, I prolong this command in order that the consumer might request a strong VCE, or that the fixed time period be suppressed, or each.
