Stata graphs: Hearts ♥

Asjad Naqvi
The Stata Guide
Published in
8 min readFeb 14, 2021

--

In this guide, learn how to make these heart figures in Stata:

Besides celebrating the 2021 Valentines Day, the aim of this guide is to introduce three elements of programming: utilizing temporary variables or tempvar, random sampling from data, and while loops.

Temporary variables or tempvars allow us to utilize the memory for data handling without generating actual variables. Random sampling is useful for a number of applications, for example, bootstrapping, synthetic controls, power calculations, and understanding concepts like the Law of Large Numbers. The while loops are a key programming feature that allow a code to run till some conditions are met.

The combination of the above three elements helps us generate these heart shape configurations where a set of x points is randomly drawn y times. If we fix x at a small value and draw few samples (small y), then we get a rough-shaped heart shown on the left below. In we increase y, then the shape gets clearer, and if y is large, then we can clearly make out the shape of the heart. Notice the lines on the top. These are the result of randomly picking points on the upper part of the heart and joining them. The outer most layer that encompasses all the points is call an envelope or a convex hull.

--

--

Asjad Naqvi
The Stata Guide

Here you will find stuff on Stata, data visualizations, data wrangling, workflows, and programming.