------------------------------------------------------------------------------- help for spagplot -------------------------------------------------------------------------------Spaghetti Plot
spagplot yvar xvar [if exp] [in range], id(idvar) [nofit] [noover] [graph options]
where yvar xvar are the Y and X variables you wish to plot and idvar is the variable that identifies a single observation.
NOTE! Spagplot makes the assumption that the first X variable for subject i+1 is greater than the last X variable for subject "i" and this is not always true. When this is false, the lines for two subjects will get connected.
Description
spagplot allows you to "spaghetti plot", i.e. a plot of an X variable and the predicted values for the Y variable forming a regression line of the predicted values of Y by X. A separate regression line is plotted for each observation indicated by the idvar variable.
If you specify the nofit option, the actual y values (not the fitted y values will be plotted.
If you specify the noover option, the overall regression line between y and x will be suppressed.
Examples
We will show a number of examples from a data file which contains alcohol use alcuse at 3 ages, age 14, 15, and 16 for a number of children (identified by the variable id. The age variable has been centered around age 14 and is named {id:age_14}. We first show how to obtain the data file.
. use https://stats.idre.ucla.edu/stat/stata/examples/alda/data/alcohol1_pp
This example shows a basic spaghetti plot of alcohol use by age, broken down by id.
. spagplot alcuse age_14 , id( id )
This example just performs the plot for the first 30 observations.
. spagplot alcuse age_14 in 1/30, id( id )
This example shows the plot just for the ids below 9.
. spagplot alcuse age_14 if id <= 9, id( id )
This example adds a title as a graph option.
. spagplot alcuse age_14 , id( id ) ytitle("Predicted Alcohol Use")
This example adds a title as a graph option, and changes the scheme to sj.
. spagplot alcuse age_14 , id( id ) ytitle("Predicted Alcohol Use") scheme(sj)
This example also changes the note from the default note.
. spagplot alcuse age_14 , id( id ) ytitle("Predicted Alcohol Use") scheme(sj) > note("My custom note")
This example removes the note from the graph entirely.
. spagplot alcuse age_14 , id( id ) ytitle("Predicted Alcohol Use") scheme(sj) > note("")
Author
Statistical Consulting Group Institute for Digital Research and Education, UCLA idrestat@ucla.edu
Also see
Manual: [R] graph
Online: help for graph