\(

\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}}\)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…

\(

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

\newcommand{\betab}{\boldsymbol{\beta}}\)I discuss **mypoisson1**, which computes Poisson-regression results in Mata. The code in **mypoisson1.ado** is remarkably similar to the code in **myregress11.ado**, which computes ordinary least-squares (OLS) results in Mata, as I discussed in Programming an estimation command in Stata: An OLS command using Mata.

I build on previous posts. I use the structure of Stata programs that use Mata work functions that I discussed previously in Programming an estimation command in Stata: A first ado-command using Mata and Programming an estimation command in Stata: An OLS command using Mata. You should be familiar with Read more…

\(

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

\newcommand{\betab}{\boldsymbol{\beta}}\)I show how to use **optimize()** in Mata to maximize a Poisson log-likelihood function and to obtain estimators of the variance–covariance of the estimator (**VCE**) based on independent and identically distributed (**IID**) observations or on robust methods.

This is the eighteenth 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.

**Using optimize()**

There are many optional choices that one may make when solving a nonlinear optimization problem, but there are very few that one must make. The **optimize*()** functions in Mata handle this problem by making a set of default choices for you, requiring that you specify a few things, and allowing you to change any of the default choices.

When I use **optimize()** to solve a Read more…

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

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

\newcommand{\yb}{{\bf y}}

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

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

\newcommand{\thetab}{\boldsymbol{\theta}}

\newcommand{\Xb}{{\bf X}}

\)I review the theory behind nonlinear optimization and get more practice in Mata programming by implementing an optimizer in Mata. In real problems, I recommend using the **optimize()** function or **moptimize()** function instead of the one I describe here. In subsequent posts, I will discuss **optimize()** and **moptimize()**. This post will help you develop your Mata programming skills and will improve your understanding of how **optimize()** and **moptimize()** work.

This is the seventeenth 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 quick review of nonlinear optimization**

We want to maximize a real-valued function \(Q(\thetab)\), where \(\thetab\) is a \(p\times 1\) vector of parameters. Minimization is done by maximizing \(-Q(\thetab)\). We require that \(Q(\thetab)\) is twice, continuously differentiable, so that we can use a second-order Taylor series to approximate \(Q(\thetab)\) in a neighborhood of the point \(\thetab_s\),

\[

Q(\thetab) \approx Q(\thetab_s) + \gb_s'(\thetab -\thetab_s)

+ \frac{1}{2} (\thetab -\thetab_s)’\Hb_s (\thetab -\thetab_s)

\tag{1}

\]

where \(\gb_s\) is the \(p\times 1\) vector of first derivatives of \(Q(\thetab)\) evaluated at \(\thetab_s\) and \(\Hb_s\) is the \(p\times p\) matrix of second derivatives of \(Q(\thetab)\) evaluated at \(\thetab_s\), known as the Hessian matrix.

Nonlinear maximization algorithms start with Read more…

I show how to use the undocumented command _vce_parse to parse the options for robust or cluster-robust estimators of the variance-covariance of the estimator (**VCE**). I then discuss **myregress12.ado**, which performs its computations in Mata and computes **VCE** estimators based on independently and identically distributed (**IID**) observations, robust methods, or cluster-robust methods.

**myregress12.ado** performs ordinary least-squares (**OLS**) regression, and it extends **myregress11.ado**, which I discussed in Programming an estimation command in Stata: An OLS command using Mata. To get the most out of this post, you should be familiar with Programming an estimation command in Stata: Using a subroutine to parse a complex option and Programming an estimation command in Stata: Computing OLS objects in Mata.

This is the sixteenth 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.

**Parsing the vce() option**

I used ado-subroutines to simplify the parsing of the options **vce(robust)** and **vce(cluster** *cvarname***)** in **myregress10.ado**; see Programming an estimation command in Stata: Using a subroutine to parse a complex option. Part of the point was to Read more…