Stata FAQ
How do I use xtline in Stata?

Stata 9 introduced the xtline command. Xtline allows you to generate linear plots for panel data. We will show a number of examples from a data file which contains a measurement of alcohol use, alcuse, taken at ages 14, 15, and 16 for 82 children (identified by the variable id). We use Stata graph scheme s2mono, which produces plots in grayscale, because publications often require monochromatic plots.

First, we read in the data file.

use http://www.ats.ucla.edu/stat/stata/examples/alda/data/alcohol1_pp, clear

This first example shows a line connecting the three time points broken down by id (one plot per child). We indicate that our time variable is age with t(age) and our panel variable is id with i(id). The outcome that we wish to examine, alcohol use, will appear on the vertical axis. The time variable we specify will appear on the horizontal axis. A separate plot will be created for each different id value.

xtline alcuse if id < 10, t(age) i(id) scheme(s2mono)

This example generates plots for the first 9 children's observations in the file (id < 10).

Suppose we are interested in seeing all of the above lines in one plot.  To do this, we add overlay to our command.

xtline alcuse if id < 10, overlay t(age) i(id) legend(off) scheme(s2mono)

We can also plot fitted lines using the xtline command. In the next example, we regress alcuse on age interacted with id. The predicted values from this regression can then be plotted.

xi: reg alcuse i.id*age if id < 10
predict p
xtline p if id < 10, overlay t(age) i(id) legend(off) scheme(s2mono)

 

How to cite this page

Report an error on this page or leave a comment

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.