Back

Presentation

On the shoulders of giants: Writing wrapper commands in Stata

Sebastian Kripfganz

7 September 2023

Session

For repeated tasks, it is convenient to use commands with simple syntax that carry out more complicated tasks under the hood.

These can be data management and visualization tasks or statistical analyses. Many of these tasks are variations or special cases of more versatile approaches. Instead of reinventing the wheel, wrapper commands build on the existing capabilities by “wrapping” around other commands. For example, certain types of graphs might require substantial effort when building them from scratch using Stata's graph twowaycommands, but this process can be automated with a dedicated command. Similarly, many estimators for specific models are special cases of more general estimation techniques, such as maximum likelihood or generalized method of moments estimators. A wrapper command can be used to translate relatively simple syntax into the more complex syntax of Stata's ml or gmm commands, or even directly into the underlying optimize() or moptimize() Mata functions. Many official Stata commands can be regarded as wrapper commands, and often there is a hierarchical wrapper structure with multiple layers. For example, most commands for mixed-effects estimation of particular models are wrappers for the general meglm command, which itself just wraps around the undocumented _me_estimate command, which then calls gsem, which in turn initiates the estimation with the ml package. The main purpose of the higher-layer wrappers is typically syntax parsing. With every layer the initially simple syntax is translated into the more general syntax of the lower-layer command, but the user only needs to be concerned with the basic syntax of the lop-layer command. Similarly, community-contributed commands often wrap around official or other community-contributed commands. They may even wrap around packages written for other programming environments, such as Python.

In this presentation, I discuss different types of wrapper commands and focus on practical aspects of their implementation. I illustrate these ideas with two of my own commands. The new spxtivdfreg wrapper adds a spatial dimension to the xtivdfreg command (Kripfganz and Sarafidis 2021) for defactored instrumental-variables estimation of large panel-data models with common factors. The xtdpdgmmfe wrapper provides a simplified syntax for the GMM estimation of linear dynamic fixed-effects panel-data models with the xtdpdgmm command.

Speaker

Sebastian Kripfganz