This post is the first in a series that illustrates how to plug code written in another language (like C, C++, or Java) into Stata. This technique is known as writing a plugin or as writing a dynamic-link library (DLL) for Stata.

Plugins can be written for any task, including data management, graphical analysis, or statistical estimation. Per the theme of this series, I discuss plugins for estimation commands.

In this post, I discuss the tradeoffs of writing a plugin, and I discuss a simple program whose calculations I will replace with plugins in subsequent posts.

This is the 29th post in the series **Programming an estimation command in Stata**. See Programming an estimation command in Stata: A map to posted entries for a map to all the posts in this series. Read more…

**estat** commands display statistics after estimation. Many of these statistics are diagnostics or tests used to evaluate model specification. Some statistics are available after all estimation commands; others are command specific.

I illustrate how **estat** commands work and then show how to write a command-specific **estat** command for the **mypoisson** command that I have been developing.

This is the 28th post in the series **Programming an estimation command in Stata**. I recommend that you start at the beginning. See Programming an estimation command in Stata: A map to posted entries for a map to all the posts in this series. Read more…

\(

\newcommand{\xb}{{\bf x}}

\newcommand{\gb}{{\bf g}}

\newcommand{\Hb}{{\bf H}}

\newcommand{\Gb}{{\bf G}}

\newcommand{\Eb}{{\bf E}}

\newcommand{\betab}{\boldsymbol{\beta}}

\)I write ado-commands that estimate the parameters of an exponential conditional mean (ECM) model and a probit conditional mean (PCM) model by nonlinear least squares, using the methods that I discussed in the post Programming an estimation command in Stata: Nonlinear least-squares estimators. These commands will either share lots of code or repeat lots of code, because they are so similar. It is almost always better to share code than to repeat code. Shared code only needs to be changed in one place to add a feature or to fix a problem; repeated code must be changed everywhere. I introduce Mata libraries to share Mata functions across ado-commands, and I introduce wrapper commands to share ado-code.

This is the 27th post in the series **Programming an estimation command in Stata**. I recommend that you start at the beginning. See Programming an estimation command in Stata: A map to posted entries for a map to all the posts in this series.

**Ado-commands for ECM and PCM models**

I now convert the examples of Read more…

\(\newcommand{\xb}{{\bf x}}

\newcommand{\gb}{{\bf g}}

\newcommand{\Hb}{{\bf H}}

\newcommand{\Gb}{{\bf G}}

\newcommand{\Eb}{{\bf E}}

\newcommand{\betab}{\boldsymbol{\beta}}\)I want to write ado-commands to estimate the parameters of an exponential conditional mean (ECM) model and probit conditional mean (PCM) model by nonlinear least squares (NLS). Before I can write these commands, I need to show how to trick **optimize()** into performing the Gauss–Newton algorithm and apply this trick to these two problems.

This is the 26th post in the series **Programming an estimation command in Stata**. I recommend that you start at the beginning. See **Programming an estimation command in Stata: A map to posted entries** for a map to all the posts in this series.

**Gauss–Newton algorithm**

Gauss–Newton algorithms frequently perform better than Read more…

\(\newcommand{\xb}{{\bf x}}

\newcommand{\betab}{\boldsymbol{\beta}}\)Before you use or distribute your estimation command, you should verify that it produces correct results and write a do-file that certifies that it does so. I discuss the processes of verifying and certifying an estimation command, and I present some techniques for writing a do-file that certifies **mypoisson5**, which I discussed in previous posts.

This is the twenty-fifth post in the series **Programming an estimation command in Stata**. I recommend that you start at the beginning. See Programming an estimation command in Stata: A map to posted entries for a map to all the posts in this series.

**Verification versus certification**

Verification is the process of establishing Read more…

I make **predict** work after **mypoisson5** by writing an ado-command that computes the predictions and by having **mypoisson5** store the name of this new ado-command in **e(predict)**. The ado-command that computes predictions using the parameter estimates computed by ado-command **mytest** should be named **mytest_p**, by convention. In the next section, I discuss **mypoisson5_p**, which computes predictions after **mypoisson5**. In section Storing the name of the prediction command in e(predict), I show that storing the name **mypoisson5_p** in **e(predict)** requires only a one-line change to **mypoisson4.ado**, which I discussed in Programming an estimation command in Stata: Adding analytical derivatives to a poisson command using Mata.

This is the twenty-fourth post in the Read more…

\(\newcommand{\xb}{{\bf x}}

\newcommand{\betab}{\boldsymbol{\beta}}\)Using analytically computed derivatives can greatly reduce the time required to solve a nonlinear estimation problem. I show how to use analytically computed derivatives with **optimize()**, and I discuss **mypoisson4.ado**, which uses these analytically computed derivatives. Only a few lines of **mypoisson4.ado** differ from the code for **mypoisson3.ado**, which I discussed in Programming an estimation command in Stata: Allowing for robust or cluster–robust standard errors in a poisson command using Mata.

This is the twenty-third post in the series **Programming an estimation command in Stata**. I recommend that you start at the beginning. See Programming an estimation command in Stata: A map to posted entries for a map to all the posts in this series.

**Analytically computed derivatives for Poisson**

The contribution of the *i*(th) observation to the log-likelihood function for the Poisson maximum-likelihood estimator is Read more…

**mypoisson3.ado** adds options for a robust or a cluster–robust estimator of the variance–covariance of the estimator (VCE) to **mypoisson2.ado**, which I discussed in Programming an estimation command in Stata: Handling factor variables in a poisson command using Mata. **mypoisson3.ado** parses the **vce()** option using the techniques I discussed in Programming an estimation command in Stata: Adding robust and cluster–robust VCEs to our Mata based OLS command. Below, I show how to use **optimize()** to compute the robust or cluster–robust VCE.

I only discuss what is new in the code for **mypoisson3.ado**, assuming that you are familiar with **mypoisson2.ado**.

This is the twenty-second post in the series **Programming an estimation command in Stata**. I recommend that you start at the beginning. See Programming an estimation command in Stata: A map to posted entries for a map to all the posts in this series.

**A poisson command with options for a robust or a cluster–robust VCE**

**mypoisson3** computes Poisson-regression results in Mata. The syntax of the **mypoisson3** command is

**mypoisson3** *depvar* *indepvars* [if] [in] [**,** **vce(**__r__obust | __cl__uster *clustervar***)** __nocons__tant]

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

In the remainder of this post, I discuss Read more…

**mypoisson2.ado** handles factor variables and computes its Poisson regression results in Mata. I discuss the code for **mypoisson2.ado**, which I obtained by adding the method for handling factor variables discussed in Programming an estimation command in Stata: Handling factor variables in optimize() to **mypoisson1.ado**, discussed in Programming an estimation command in Stata: A poisson command using Mata.

This is the twenty-first post in the series **Programming an estimation command in Stata**. I recommend that you start at the beginning. See Programming an estimation command in Stata: A map to posted entries for a map to all the posts in this series.

**A Poisson command with Mata computations**

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

**mypoisson2** *depvar indepvars* [*if*] [*in*] [**,** __nocons__tant]

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

In the remainder of this post, I discuss Read more…

\(

\newcommand{\xb}{{\bf x}}

\newcommand{\betab}{\boldsymbol{\beta}}\)I discuss a method for handling factor variables when performing nonlinear optimization using **optimize()**. After illustrating the issue caused by factor variables, I present a method and apply it to an example using **optimize()**.

This is the twenty post in the series **Programming an estimation command in Stata**. I recommend that you start at the beginning. See Programming an estimation command in Stata: A map to posted entries for a map to all the posts in this series.

**How poisson handles factor variables**

Consider the Poisson regression in which I include a full set of indicator variables created from Read more…