UCLA Academic Technology Services HomeServicesClassesContactJobs

Stata FAQ
How can I visualize longitudinal data in Stata?

Longitudinal data are data containing measurements on subjects at multiple times.  Visualizing longitudinal data without loss of data can be difficult, but there are several ways to do so in Stata. Both Stata command xtline and Stata user-written command profileplot allow you to generate plots with time on the horizontal axis and levels of an outcome on the vertical axis, connecting the related points (points from a single subject or from a specified group) with a line. This type of plot allows you to trace the levels of the outcome variable over time for a given subject and can often reveal larger patterns that may be of interest. Which command is appropriate for your dataset depends on the shape of your dataset. 

If your dataset is "wide" (each time point is represented as its own variable and the measured responses are the values found in the multiple time variables), then profileplot will allow you to visualize your data without reconfiguring it. The dataset below contains four measurements on five subjects. These data are in wide form, and we can see the plot generated by profileplot.

     +------------------------------------+
     | id   time1   time2   time3   time4 |
     |------------------------------------|
  1. |  1      23      25      25      30 |
  2. |  2      14      17      20      23 |
  3. |  3      32      33      34      24 |
  4. |  4      33      35      42      50 |
  5. |  5       5      17      22      22 |
     +------------------------------------+
profileplot time1 time2 time3 time4, by(id)

For details on how to obtain and use profileplot, see Stata FAQ: How can I do a profile plot in Stata?.

If you dataset is "long" (there is one variable indicating time and another variable with the measured response from given time value), then xtline will allow you to visualize your data without reconfiguring it.  The dataset below contains the same four measurements on five subjects as the dataset set seen above, but here the data are in long form. We can see the plot generated by xtline.

     +----------------+
     | id   time    y |
     |----------------|
  1. |  1      1   23 |
  2. |  1      2   25 |
  3. |  1      3   25 |
  4. |  1      4   30 |
  5. |  2      1   14 |
     |----------------|
  6. |  2      2   17 |
  7. |  2      3   20 |
  8. |  2      4   23 |
  9. |  3      1   32 |
 10. |  3      2   33 |
     |----------------|
 11. |  3      3   34 |
 12. |  3      4   24 |
 13. |  4      1   33 |
 14. |  4      2   35 |
 15. |  4      3   42 |
     |----------------|
 16. |  4      4   50 |
 17. |  5      1    5 |
 18. |  5      2   17 |
 19. |  5      3   22 |
 20. |  5      4   22 |
     +----------------+
xtline y, t(time) i(id) overlay

For details on how to use xtline, see Stata FAQ: How do I use xtline in Stata?.


How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


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.