code from the book Practical Data Analysis