温馨提示：定期 清理浏览器缓存，可以获得最佳浏览体验。

**目录**

The Stata Blog » Programming an estimation command in Stata: A map to posted entries

The Stata Blog » Programming an estimation command in Stata: Nonlinear least-squares estimators

15 January 2016 David M. Drukker, Executive Director of Econometrics

^{[1]}

I have posted a series of entries about programming an estimation command in Stata. They are best read in order. The comprehensive list below allows you to read them from first to last at your own pace.

Programming estimators in Stata: Why you should

To help you write Stata commands that people want to use, I illustrate how Stata syntax is predictable and give an overview of the estimation-postestimation structure that you will want to emulate in your programs.Programming an estimation command in Stata: Where to store your stuff

I discuss the difference between scripts and commands, and I introduce some essential programming concepts and constructions that I use to write the scripts and commands.Programming an estimation command in Stata: Global macros versus local macros

I discuss a pair of examples that illustrate the differences between global macros and local macros.Programming an estimation command in Stata: A first ado-command

I discuss the code for a simple estimation command to focus on the details of how to implement an estimation command. The command that I discuss estimates the mean by the sample average. I begin by reviewing the formulas and a do-file that implements them. I subsequently introduce ado-file programming and discuss two versions of the command. Along the way, I illustrate some of the postestimation features that work after the command.Programming an estimation command in Stata: Using Stata matrix commands and functions to compute OLS objects

I present the formulas for computing the ordinary least-squares (OLS) estimator, and I discuss some do-file implementations of them. I discuss the formulas and the computation of independence-based standard errors, robust standard errors, and cluster–robust standard errors. I introduce the Stata matrix commands and matrix functions that I use in ado-commands that I discuss in upcoming posts.Programming an estimation command in Stata: A first command for OLS

I show how to write a Stata estimation command that implements the OLS estimator by explaining the code.Programming an estimation command in Stata: A better OLS command

I use the**syntax**command to improve the command that implements the OLS estimator that I discussed in Programming an estimation command in Stata: A first command for OLS. I show how to require that all variables be numeric variables and how to make the command accept time-series operated variables.Programming an estimation command in Stata: Allowing for sample restrictions and factor variables

I modify the OLS command discussed in Programming an estimation command in Stata: A better OLS command to allow for sample restrictions, to handle missing values, to allow for factor variables, and to deal with perfectly collinear variables.Programming an estimation command in Stata: Allowing for options

I make three improvements to the command that implements the OLS estimator that I discussed in Programming an estimation command in Stata: Allowing for sample restrictions and factor variables. First, I allow the user to request a robust estimator of the variance–covariance of the estimator. Second, I allow the user to suppress the constant term. Third, I store the residual degrees of freedom in**e(df_r)**so that**test**will use the*t*or*F*distribution instead of the normal or chi-squared distribution to compute the*p*-value of Wald tests.Programming an estimation command in Stata: Using a subroutine to parse a complex option

I make two improvements to the command that implements the OLS estimator that I discussed in Programming an estimation command in Stata: Allowing for options. First, I add an option for a cluster–robust estimator of the variance–covariance of the estimator (VCE). Second, I make the command accept the modern syntax for either a robust or a cluster–robust estimator of the VCE. In the process, I use subroutines in my ado-program to facilitate the parsing, and I discuss some advanced parsing tricks.Programming an estimation command in Stata: Mata 101

I introduce Mata, the matrix programming language that is part of Stata.Programming an estimation command in Stata: Mata functions

I show how to write a function in Mata, the matrix programming language that is part of Stata.Programming an estimation command in Stata: A first ado-command using Mata

I discuss a sequence of ado-commands that use Mata to estimate the mean of a variable. The commands illustrate a general structure for Stata-Mata programs.Programming an estimation command in Stata: Computing OLS objects in Mata

I present the formulas for computing the OLS estimator and show how to compute them in Mata. This post is a Mata version of Programming an estimation command in Stata: Using Stata matrix commands and functions to compute OLS objects. I discuss the formulas and the computation of independence-based standard errors, robust standard errors, and cluster–robust standard errors.Programming an estimation command in Stata: An OLS command using Mata

I discuss a command that computes OLS results in Mata, paying special attention to the structure of Stata programs that use Mata work functions.Programming an estimation command in Stata: Adding robust and cluster–robust VCEs to our Mata-based OLS command

I show how to use the undocumented command _vce_parse to parse the options for robust or cluster–robust estimators of the**VCE**. I then discuss myregress12.ado, which performs its computations in Mata and computes an**IID**-based, a robust, or a cluster–robust estimator of the**VCE**.Programming an estimation command in Stata: A review of nonlinear optimization using Mata

I review the theory behind nonlinear optimization and get some practice in Mata programming by implementing an optimizer in Mata. This post is designed to help you develop your Mata programming skills and to improve your understanding of how the Mata optimization suites**optimize()**and**moptimize()**work.Programming an estimation command in Stata: Using optimize() to estimate Poisson parameters

I show how to use**optimize()**in Mata to maximize a Poisson log-likelihood function and to obtain estimators of the**VCE**based on**IID**observations or on robust methods.Programming an estimation command in Stata: A poisson command using Mata 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**OLS**results in Mata, as I discussed in Programming an estimation command in Stata: An OLS command using Mata.Programming an estimation command in Stata: Handling factor variables in optimize()

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()**.Programming an estimation command in Stata: Handling factor variables in a poisson command using Mata

**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.Programming an estimation command in Stata: Allowing for robust or cluster–robust standard errors in a poisson command using Mata

**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. I show how to use**optimize()**to compute the robust or cluster–robust**VCE**.Programming an estimation command in Stata: Adding analytical derivatives to a poisson command using Mata

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.Programming an estimation command in Stata: Making predict work

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)**.Programming an estimation command in Stata: Certifying your command

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.Programming an estimation command in Stata: Nonlinear least-squares estimators

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.Programming an estimation command in Stata: Consolidating your code

I write ado-commands that estimate the parameters of an exponential conditional mean model and a probit conditional mean 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.Programming an estimation command in Stata: Writing an estat postestimation command

**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.

连享会-直播课上线了！

http://lianxh.duanshu.com

免费公开课：

直击面板数据模型 - 连玉君，时长：1小时40分钟 Stata 33 讲 - 连玉君, 每讲 15 分钟. 部分直播课 课程资料下载 (PPT，dofiles等)

支持回看，所有课程可以随时购买观看。

专题 | 嘉宾 | 直播/回看视频 |
---|---|---|

⭐ 最新专题 ⭐ |
DSGE, 因果推断, 空间计量等 | |

⭕ Stata数据清洗 |
游万海 | 直播, 2 小时，已上线 |

研究设计 | 连玉君 | 我的特斯拉-实证研究设计，-幻灯片- |

面板模型 | 连玉君 | 动态面板模型，-幻灯片- |

面板模型 | 连玉君 | 直击面板数据模型 [免费公开课，2小时] |

Note: 部分课程的资料，PPT 等可以前往 连享会-直播课 主页查看，下载。

## 关于我们

**Stata连享会**由中山大学连玉君老师团队创办，定期分享实证分析经验。直播间 有很多视频课程，可以随时观看。连享会-主页 和 知乎专栏，300+ 推文，实证分析不再抓狂。 **公众号推文分类：**计量专题 | 分类推文 | 资源工具。推文分成**内生性**|**空间计量**|**时序面板**|**结果输出**|**交乘调节**五类，主流方法介绍一目了然：DID, RDD, IV, GMM, FE, Probit 等。**公众号关键词搜索/回复**功能已经上线。大家可以在公众号左下角点击键盘图标，输入简要关键词，以便快速呈现历史推文，获取工具软件和数据下载。常见关键词：`课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法`

等

连享会小程序：扫一扫，看推文，看视频……

扫码加入连享会微信群，提问交流更方便

✏ 连享会学习群-常见问题解答汇总：

✨ https://gitee.com/arlionn/WD

Posts by David M. Drukker, Executive Director of Econometrics: *https://blog.stata.com/author/ddrukker/*

04/26
专题课程
1017

04/21
专题课程
1634

12/05
计量专题
57302

10/10
专题课程
44569

05/24
Python-R-Matlab
11

05/23
Stata绘图
43

05/22
Stata绘图
86

05/21
数据分享
110

05/20
专题课程
53

05/19
Stata绘图
118