This R code draws individual growth plots as shown in “Applied Longitudinal Data Analysis: Modeling Change and Event Occurrence” by Judith D. Singer and John B. Willett, an excellent book on multilevel modeling and survival analysis.
This code recreates figure 2.5 on page 32 with the caption, “OLS summaries of how individuals change over time. Fitted OLS trajectories superimposed on emperical growth plots for participants in the tolerance study.” The main difference is, by default, ggplot2 draws a 95% confidence region for the regression line: to remove the confidence region, simply add se=FALSE to geom_smooth().
The data shows the change over time in tolerance to deviant behavior for a group adolescents in a youth study. I pull the data from UCLA’s web site, which is an invaluable companion for the book. UCLA gives a similar plot using the lattice package, which is included by default in R 2.14. The data is organized in person-period format, which means each person has multiple rows, and each row represents a unique period (age) for each person.
# read data from UCLA's web site tolerance.pp <- read.table("http://www.ats.ucla.edu/stat/r/examples/alda/data/tolerance1_pp.txt", sep=",", header=T) # load ggplot2 library require(ggplot2) # plot ggplot(tolerance.pp, # data set name aes(age, tolerance)) + # values for horizontal and vertical axes geom_point() + # scatter plot geom_smooth(method=lm) + # regression line with 95% confidence interval facet_wrap(~id) # separately plot each subject by his unique identifier
While I haven’t yet finished reading this rich book, I found this method useful for studying the annual giving patterns of major donors at a non-profit organization. For example, some donors give for years at a low level and suddenly give a large gift, while others gradually increase their annual giving. The method is the same as above except a log coordinate transformation is needed on the Y axis.