mypoisson2 computes Poisson regression results in Mata. The syntax of the mypoisson2 command is

mypoisson2depvar indepvars [if] [in] [,noconstant]

where indepvars can contain factor variables or time-series variables.

In the remainder of this post, I discuss the code for mypoisson2.ado. I recommend that you click on the filename to download the code. To avoid scrolling, view the code in the do-file editor, or your favorite text editor, to see the line numbers.

As with programs that I have previously discussed, there is an ado part and Mata part. Lines 2–56 are the ado part; they define mypoisson2 and the subroutine getcinfo. Lines 58–133 are the Mata part; they define the Mata work function mywork() used in mypoisson2, the makeCt function used in mywork(), and the evaluator function plleval2() used in mywork().

The ado-command mypoisson2 has the following parts:

Lines 5–11 parse what the user typed, identify the sample, and create temporary names for Stata objects used in the computations or returned by our Mata work function.

Lines 13–15 use the subroutine getcinfo to get information about the user-specified covariates and then store this information in the local cnames and a Stata matrix.

Lines 17–18 call the Mata work function.

Lines 20–30 post the results returned by the Mata work function to e().

Line 32 displays the results.

The Mata function mywork() has the following parts:

Lines 60–65 parse the arguments.

Lines 67–69 declare vectors, matrices, and scalars that are local to mywork().

Lines 71–90 compute the results.

Lines 92–95 copy the computed results to Stata, using the names that were passed as arguments.

I now discuss the ado-code in some detail, focusing on only the aspects that are new to mypoisson2.ado.

The subroutine getcinfo encapsulates the computations performed in examples 3, 4, and 5 in Programming an estimation command in Stata: Handling factor variables in optimize(). getcinfo uses _rmcoll to identify which covariates must be omitted, stores the names of the covariates to be omitted in the local macro cnames, then uses _ms_omit_info to create a vector containing a 1 for omitted variables and a 0 otherwise. getcinfo puts cnames into r(cnames) and the vector identifying the omitted variables into r(mo).

Lines 14–15 store the information put into r() by getcinfo in the local macro cnames and the Stata vector whose name is contained in the local macro mo. Lines 23–25 use cnames to put row names on the vector of point estimates and row and column names on the estimated variance–covariance matrix of the estimator (VCE). Line 18 passes the vector to mywork().

I now discuss the Mata code in some detail, again focusing on only the new aspects. Line 79 gets the constraint matrix Ct needed to handle any omitted variables from makeCt(). Lines 98–121 define makeCt(), which encapsulates the computations that form Cm in example 6 in Programming an estimation command in Stata: Handling factor variables in optimize(). Line 86 uses optimize_init_constraints() to put Ct in the optimize() object. Ct contains a matrix with zero rows when there are no constraints, and putting a constraint matrix with zero rows into the optimize() object tells optimize() that there are no constraints.

The output in examples 1 and 2 confirms that mypoisson2 produces the same results as poisson when a full set of indicator variables is included in a model with a constant term.