100% found this document useful (1 vote)
24 views

The Book of R: A First Course in Programming and Statistics 1st Edition Tilman M. Davies all chapter instant download

Programming

Uploaded by

hankesafiadb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
24 views

The Book of R: A First Course in Programming and Statistics 1st Edition Tilman M. Davies all chapter instant download

Programming

Uploaded by

hankesafiadb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Download the Full Version of textbook for Fast Typing at textbookfull.

com

The Book of R: A First Course in Programming and


Statistics 1st Edition Tilman M. Davies

https://textbookfull.com/product/the-book-of-r-a-first-
course-in-programming-and-statistics-1st-edition-tilman-m-
davies/

OR CLICK BUTTON

DOWNLOAD NOW

Download More textbook Instantly Today - Get Yours Now at textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

A First Course in Statistics for Signal Analysis Wojbor A.


Woyczy■ski

https://textbookfull.com/product/a-first-course-in-statistics-for-
signal-analysis-wojbor-a-woyczynski/

textboxfull.com

Statistics for Technology A Course in Applied Statistics


Third Edition Chatfield

https://textbookfull.com/product/statistics-for-technology-a-course-
in-applied-statistics-third-edition-chatfield/

textboxfull.com

Mathematical Statistics With Applications in R (Third


Edition) Kandethody M. Ramachandran

https://textbookfull.com/product/mathematical-statistics-with-
applications-in-r-third-edition-kandethody-m-ramachandran/

textboxfull.com

A First Course in the Finite Element Method Si Edition


Martin

https://textbookfull.com/product/a-first-course-in-the-finite-element-
method-si-edition-martin/

textboxfull.com
A First Course in Predictive Control, Second Edition
Rossiter

https://textbookfull.com/product/a-first-course-in-predictive-control-
second-edition-rossiter/

textboxfull.com

A course in categorical data analysis First Edition


Leonard

https://textbookfull.com/product/a-course-in-categorical-data-
analysis-first-edition-leonard/

textboxfull.com

A First Course in Sobolev Spaces Giovanni Leoni

https://textbookfull.com/product/a-first-course-in-sobolev-spaces-
giovanni-leoni/

textboxfull.com

A First Course in Functional Analysis 1st Edition Orr


Moshe Shalit

https://textbookfull.com/product/a-first-course-in-functional-
analysis-1st-edition-orr-moshe-shalit/

textboxfull.com

A First Course in Enumerative Combinatorics 1st Edition


Carl G. Wagner

https://textbookfull.com/product/a-first-course-in-enumerative-
combinatorics-1st-edition-carl-g-wagner/

textboxfull.com
THE BOOK OF
A COMPLETE

R
INTRODUCTION
TO R AND
DATA ANALYSIS

THE BOOK OF R
The Book of R is a comprehensive, beginner-friendly • Statistical concepts like exploratory data analysis,
guide to R, the world’s most popular programming probabilities, hypothesis tests, and regression
language for statistical analysis. Even if you have modeling, and how to execute them in R A F I R S T C O U R S E I N
no programming experience and little more than a
• How to access R’s thousands of functions, libraries, P R O G R A M M I N G A ND S T A T I S T I C S
grounding in the basics of mathematics, you’ll find
and data sets
everything you need to begin using R effectively for
statistical analysis. • How to draw valid and useful conclusions from
your data TILMAN M. DAVIES
You’ll start with the basics, like how to handle data
and write simple programs, before moving on to more • How to create publication-quality graphics of your
advanced topics, like producing statistical summaries of results
your data and performing statistical tests and modeling.
Combining detailed explanations with real-world
You’ll even learn how to create impressive data visual-
examples and exercises, this book will provide you
izations with R’s basic graphics tools and contributed
with a solid understanding of both statistics and the
packages, like ggplot2 and ggvis, as well as inter-
depth of R’s functionality. Make The Book of R your
active 3D visualizations using the rgl package.
doorway into the growing world of data analysis.
Dozens of hands-on exercises (with downloadable
ABOUT THE AUTHOR
solutions) take you from theory to practice, as you
learn: Tilman M. Davies is a lecturer at the University of Otago
in New Zealand, where he teaches statistics and R at
• The fundamentals of programming in R, including
all university levels. He has been programming in R for
how to write data frames, create functions, and use
10 years and uses it in all of his courses.
variables, statements, and loops

T H E F I N E ST I N G E E K E N T E RTA I N M E N T ™
w w w.nostarch.com

$49.95 ($57.95 CDN) DAVIES


“ I L I E F L AT .”
This book uses a durable binding that won’t snap shut.
STATISTICAL SOFTWARE
COMPUTERS/MATHEMATICAL &
SHELVE IN:
The Book of R
The Book of R
A First Course in
Programming and Statistics

b y T il ma n M . D a v i e s

San Francisco
The Book of R. Copyright © 2016 by Tilman M. Davies.

All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.

Printed in USA

First printing

20 19 18 17 16â•…â•… 1 2 3 4 5 6 7 8 9

ISBN-10: 1-59327-651-6
ISBN-13: 978-1-59327-651-5

Publisher: William Pollock


Production Editor: Riley Hoffman
Cover Illustration: Josh Ellingson
Interior Design: Octopod Studios
Developmental Editor: Liz Chadwick
Technical Reviewer: Debbie Leader
Copyeditor: Kim Wimpsett
Compositor: Riley Hoffman
Proofreader: Paula Fleming
Indexer: BIM Creatives, LLC

For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:

No Starch Press, Inc.


245 8th Street, San Francisco, CA 94103
phone: 415.863.9900; info@nostarch.com
www.nostarch.com

Library of Congress Cataloging-in-Publication Data

Names: Davies, Tilman M., author.


Title: The book of R : a first course in programming and statistics / by
Tilman M. Davies.
Description: San Francisco : No Starch Press, [2016] | Includes
bibliographical references and index.
Identifiers: LCCN 2015035305| ISBN 9781593276515 | ISBN 1593276516
Subjects: LCSH: R (Computer program language) | Computer programming. |
Statistics--Data processing.
Classification: LCC QA76.73.R3 D38 2016 | DDC 519.50285--dc23
LC record available at http://lccn.loc.gov/2015035305

No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other
product and company names mentioned herein may be the trademarks of their respective owners. Rather
than use a trademark symbol with every occurrence of a trademarked name, we are using the names only
in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.

The information in this book is distributed on an “As Is” basis, without warranty. While every precaution
has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any
liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or
indirectly by the information contained in it.
Prof. Dr. Dr. Jochen Weniger, 1925–2015
Ein Wissenschaftler. Mein Opa.
BRIEF CONTENTS

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii

PART I: THE LANGUAGE

Chapter 1: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3


Chapter 2: Numerics, Arithmetic, Assignment, and Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Chapter 3: Matrices and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Chapter 4: Non-numeric Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Chapter 5: Lists and Data Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Chapter 6: Special Values, Classes, and Coercion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Chapter 7: Basic Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Chapter 8: Reading and Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

PART II: PROGRAMMING

Chapter 9: Calling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165


Chapter 10: Conditions and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Chapter 11: Writing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Chapter 12: Exceptions, Timings, and Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

PART III: STATISTICS AND PROBABILITY

Chapter 13: Elementary Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261


Chapter 14: Basic Data Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Chapter 15: Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Chapter 16: Common Probability Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331

PART IV: STATISTICAL TESTING AND MODELING

Chapter 17: Sampling Distributions and Confidence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367


Chapter 18: Hypothesis Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Chapter 19: Analysis of Variance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Chapter 20: Simple Linear Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Chapter 21: Multiple Linear Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Chapter 22: Linear Model Selection and Diagnostics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527

PART V: ADVANCED GRAPHICS

Chapter 23: Advanced Plot Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575


Chapter 24: Going Further with the Grammar of Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Chapter 25: Defining Colors and Plotting in Higher Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Chapter 26: Interactive 3D Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691

Appendix A: Installing R and Contributed Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737


Appendix B: Working with RStudio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
Reference List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767

viii Brief Contents


CONTENTS IN DETAIL

PREFACE xxiii

ACKNOWLEDGMENTS xxv

INTRODUCTION xxvii
A Brief History of R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviii
Part I: The Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviii
Part II: Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
Part III: Statistics and Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
Part IV: Statistical Testing and Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
Part V: Advanced Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxx
For Students . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxx
For Instructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi

PART I
THE LANGUAGE

1
GETTING STARTED 3
1.1 Obtaining and Installing R from CRAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Opening R for the First Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.1 Console and Editor Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.2 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2.3 Working Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2.4 Installing and Loading R Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2.5 Help Files and Function Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2.6 Third-Party Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3 Saving Work and Exiting R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3.1 Workspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3.2 Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4 Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.1 Coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4.2 Math and Equation References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.4.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Exercise 1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2
NUMERICS, ARITHMETIC, ASSIGNMENT, AND VECTORS 17
2.1 R for Basic Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.1.1 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.1.2 Logarithms and Exponentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.1.3 E-Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Exercise 2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.2 Assigning Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Exercise 2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.3 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.3.1 Creating a Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.3.2 Sequences, Repetition, Sorting, and Lengths . . . . . . . . . . . . . . . . . . . . . . . 24
Exercise 2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.3.3 Subsetting and Element Extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Exercise 2.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.3.4 Vector-Oriented Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Exercise 2.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

3
MATRICES AND ARRAYS 39
3.1 Defining a Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.1.1 Filling Direction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.1.2 Row and Column Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.1.3 Matrix Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.2 Subsetting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.2.1 Row, Column, and Diagonal Extractions . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.2.2 Omitting and Overwriting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Exercise 3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.3 Matrix Operations and Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.3.1 Matrix Transpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.3.2 Identity Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3.3.3 Scalar Multiple of a Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.3.4 Matrix Addition and Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.3.5 Matrix Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.3.6 Matrix Inversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Exercise 3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.4 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.4.1 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.4.2 Subsets, Extractions, and Replacements . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Exercise 3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

4
NON-NUMERIC VALUES 59
4.1 Logical Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.1.1 TRUE or FALSE? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
4.1.2 A Logical Outcome: Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . 60
Exercise 4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

x Contents in Detail
4.1.3 Multiple Comparisons: Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . 64
Exercise 4.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
4.1.4 Logicals Are Numbers! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
4.1.5 Logical Subsetting and Extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Exercise 4.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
4.2 Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
4.2.1 Creating a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
4.2.2 Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
4.2.3 Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
4.2.4 Substrings and Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Exercise 4.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
4.3 Factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
4.3.1 Identifying Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
4.3.2 Defining and Ordering Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
4.3.3 Combining and Cutting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Exercise 4.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

5
LISTS AND DATA FRAMES 89
5.1 Lists of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
5.1.1 Definition and Component Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
5.1.2 Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
5.1.3 Nesting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Exercise 5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
5.2 Data Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
5.2.1 Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
5.2.2 Adding Data Columns and Combining Data Frames . . . . . . . . . . . . . . . . 98
5.2.3 Logical Record Subsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Exercise 5.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

6
SPECIAL VALUES, CLASSES, AND COERCION 103
6.1 Some Special Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
6.1.1 Infinity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
6.1.2 NaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Exercise 6.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
6.1.3 NA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
6.1.4 NULL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Exercise 6.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
6.2 Understanding Types, Classes, and Coercion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
6.2.1 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
6.2.2 Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
6.2.3 Is-Dot Object-Checking Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
6.2.4 As-Dot Coercion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Exercise 6.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

Contents in Detail xi
7
BASIC PLOTTING 127
7.1 Using plot with Coordinate Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
7.2 Graphical Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
7.2.1 Automatic Plot Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
7.2.2 Title and Axis Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
7.2.3 Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
7.2.4 Line and Point Appearances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
7.2.5 Plotting Region Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
7.3 Adding Points, Lines, and Text to an Existing Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Exercise 7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
7.4 The ggplot2 Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
7.4.1 A Quick Plot with qplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
7.4.2 Setting Appearance Constants with Geoms . . . . . . . . . . . . . . . . . . . . . . . . 141
7.4.3 Aesthetic Mapping with Geoms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Exercise 7.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

8
READING AND WRITING FILES 147
8.1 R-Ready Data Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
8.1.1 Built-in Data Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
8.1.2 Contributed Data Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
8.2 Reading in External Data Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
8.2.1 The Table Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
8.2.2 Spreadsheet Workbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
8.2.3 Web-Based Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
8.2.4 Other File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
8.3 Writing Out Data Files and Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
8.3.1 Data Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
8.3.2 Plots and Graphics Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
8.4 Ad Hoc Object Read/Write Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Exercise 8.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

PART II
PROGRAMMING

9
CALLING FUNCTIONS 165
9.1 Scoping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
9.1.1 Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
9.1.2 Search Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
9.1.3 Reserved and Protected Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Exercise 9.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

xii Contents in Detail


9.2 Argument Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
9.2.1 Exact . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
9.2.2 Partial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
9.2.3 Positional . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
9.2.4 Mixed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
9.2.5 Dot-Dot-Dot: Use of Ellipses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Exercise 9.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

10
CONDITIONS AND LOOPS 179
10.1 if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
10.1.1 Stand-Alone Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
10.1.2 else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
10.1.3 Using ifelse for Element-wise Checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Exercise 10.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
10.1.4 Nesting and Stacking Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
10.1.5 The switch Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Exercise 10.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
10.2 Coding Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
10.2.1 for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Exercise 10.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
10.2.2 while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Exercise 10.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
10.2.3 Implicit Looping with apply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Exercise 10.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
10.3 Other Control Flow Mechanisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
10.3.1 Declaring break or next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
10.3.2 The repeat Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Exercise 10.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

11
WRITING FUNCTIONS 215
11.1 The function Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
11.1.1 Function Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
11.1.2 Using return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Exercise 11.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
11.2 Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
11.2.1 Lazy Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
11.2.2 Setting Defaults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
11.2.3 Checking for Missing Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
11.2.4 Dealing with Ellipses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Exercise 11.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
11.3 Specialized Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
11.3.1 Helper Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
11.3.2 Disposable Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
11.3.3 Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Exercise 11.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

Contents in Detail xiii


12
EXCEPTIONS, TIMINGS, AND VISIBILITY 241
12.1 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
12.1.1 Formal Notifications: Errors and Warnings . . . . . . . . . . . . . . . . . . . . . . . . 242
12.1.2 Catching Errors with try Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Exercise 12.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
12.2 Progress and Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
12.2.1 Textual Progress Bars: Are We There Yet? . . . . . . . . . . . . . . . . . . . . . . . . . 249
12.2.2 Measuring Completion Time: How Long Did It Take? . . . . . . . . . . . . . . . 250
Exercise 12.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
12.3 Masking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
12.3.1 Function and Object Distinction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
12.3.2 Data Frame Variable Distinction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255

PART III
STATISTICS AND PROBABILITY

13
ELEMENTARY STATISTICS 261
13.1 Describing Raw Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
13.1.1 Numeric Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
13.1.2 Categorical Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
13.1.3 Univariate and Multivariate Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
13.1.4 Parameter or Statistic? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Exercise 13.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
13.2 Summary Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
13.2.1 Centrality: Mean, Median, Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
13.2.2 Counts, Percentages, and Proportions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Exercise 13.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
13.2.3 Quantiles, Percentiles, and the Five-Number Summary . . . . . . . . . . . . . . 274
13.2.4 Spread: Variance, Standard Deviation, and the Interquartile Range . . 275
Exercise 13.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
13.2.5 Covariance and Correlation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
13.2.6 Outliers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Exercise 13.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287

14
BASIC DATA VISUALIZATION 289
14.1 Barplots and Pie Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
14.1.1 Building a Barplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
14.1.2 A Quick Pie Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
14.2 Histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294

xiv Contents in Detail


14.3 Box-and-Whisker Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
14.3.1 Stand-Alone Boxplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
14.3.2 Side-by-Side Boxplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
14.4 Scatterplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
14.4.1 Single Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
14.4.2 Matrix of Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Exercise 14.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306

15
PROBABILITY 309
15.1 What Is a Probability? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
15.1.1 Events and Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
15.1.2 Conditional Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
15.1.3 Intersection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
15.1.4 Union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
15.1.5 Complement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Exercise 15.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
15.2 Random Variables and Probability Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
15.2.1 Realizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
15.2.2 Discrete Random Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
15.2.3 Continuous Random Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
15.2.4 Shape, Skew, and Modality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Exercise 15.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327

16
COMMON PROBABILITY DISTRIBUTIONS 331
16.1 Common Probability Mass Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
16.1.1 Bernoulli Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
16.1.2 Binomial Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Exercise 16.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
16.1.3 Poisson Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Exercise 16.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
16.1.4 Other Mass Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
16.2 Common Probability Density Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
16.2.1 Uniform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Exercise 16.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
16.2.2 Normal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Exercise 16.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
16.2.3 Student’s t-distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
16.2.4 Exponential . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Exercise 16.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
16.2.5 Other Density Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362

Contents in Detail xv
PART IV
STATISTICAL TESTING AND MODELING

17
SAMPLING DISTRIBUTIONS AND CONFIDENCE 367
17.1 Sampling Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
17.1.1 Distribution for a Sample Mean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
17.1.2 Distribution for a Sample Proportion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Exercise 17.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
17.1.3 Sampling Distributions for Other Statistics . . . . . . . . . . . . . . . . . . . . . . . . . 377
17.2 Confidence Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
17.2.1 An Interval for a Mean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
17.2.2 An Interval for a Proportion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
17.2.3 Other Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
17.2.4 Comments on Interpretation of a CI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Exercise 17.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383

18
HYPOTHESIS TESTING 385
18.1 Components of a Hypothesis Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
18.1.1 Hypotheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
18.1.2 Test Statistic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
18.1.3 p-value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
18.1.4 Significance Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
18.1.5 Criticisms of Hypothesis Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
18.2 Testing Means . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
18.2.1 Single Mean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Exercise 18.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
18.2.2 Two Means . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Exercise 18.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
18.3 Testing Proportions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
18.3.1 Single Proportion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
18.3.2 Two Proportions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Exercise 18.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
18.4 Testing Categorical Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
18.4.1 Single Categorical Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
18.4.2 Two Categorical Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Exercise 18.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
18.5 Errors and Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
18.5.1 Hypothesis Test Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
18.5.2 Type I Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
18.5.3 Type II Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Exercise 18.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
18.5.4 Statistical Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Exercise 18.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433

xvi Contents in Detail


19
ANALYSIS OF VARIANCE 435
19.1 One-Way ANOVA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
19.1.1 Hypotheses and Diagnostic Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
19.1.2 One-Way ANOVA Table Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
19.1.3 Building ANOVA Tables with the aov Function . . . . . . . . . . . . . . . . . . . . . 440
Exercise 19.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
19.2 Two-Way ANOVA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
19.2.1 A Suite of Hypotheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
19.2.2 Main Effects and Interactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
19.3 Kruskal-Wallis Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Exercise 19.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449

20
SIMPLE LINEAR REGRESSION 451
20.1 An Example of a Linear Relationship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
20.2 General Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
20.2.1 Definition of the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
20.2.2 Estimating the Intercept and Slope Parameters . . . . . . . . . . . . . . . . . . . . . 454
20.2.3 Fitting Linear Models with lm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
20.2.4 Illustrating Residuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
20.3 Statistical Inference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
20.3.1 Summarizing the Fitted Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
20.3.2 Regression Coefficient Significance Tests . . . . . . . . . . . . . . . . . . . . . . . . . . 459
20.3.3 Coefficient of Determination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
20.3.4 Other summary Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
20.4 Prediction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
20.4.1 Confidence Interval or Prediction Interval? . . . . . . . . . . . . . . . . . . . . . . . . . 461
20.4.2 Interpreting Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
20.4.3 Plotting Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
20.4.4 Interpolation vs. Extrapolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
Exercise 20.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
20.5 Understanding Categorical Predictors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
20.5.1 Binary Variables: k = 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
20.5.2 Multilevel Variables: k > 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
20.5.3 Changing the Reference Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
20.5.4 Treating Categorical Variables as Numeric . . . . . . . . . . . . . . . . . . . . . . . . 478
20.5.5 Equivalence with One-Way ANOVA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Exercise 20.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482

21
MULTIPLE LINEAR REGRESSION 485
21.1 Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
21.2 Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
21.2.1 Extending the Simple Model to a Multiple Model . . . . . . . . . . . . . . . . . . . 487

Contents in Detail xvii


21.2.2 Estimating in Matrix Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
21.2.3 A Basic Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
21.3 Implementing in R and Interpreting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
21.3.1 Additional Predictors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
21.3.2 Interpreting Marginal Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
21.3.3 Visualizing the Multiple Linear Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
21.3.4 Finding Confidence Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
21.3.5 Omnibus F-Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
21.3.6 Predicting from a Multiple Linear Model . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Exercise 21.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
21.4 Transforming Numeric Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
21.4.1 Polynomial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
21.4.2 Logarithmic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
21.4.3 Other Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Exercise 21.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
21.5 Interactive Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
21.5.1 Concept and Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
21.5.2 One Categorical, One Continuous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
21.5.3 Two Categorical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
21.5.4 Two Continuous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
21.5.5 Higher-Order Interactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Exercise 21.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525

22
LINEAR MODEL SELECTION AND DIAGNOSTICS 527
22.1 Goodness-of-Fit vs. Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
22.1.1 Principle of Parsimony . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
22.1.2 General Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
22.2 Model Selection Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
22.2.1 Nested Comparisons: The Partial F-Test . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
22.2.2 Forward Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
22.2.3 Backward Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
22.2.4 Stepwise AIC Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
Exercise 22.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
22.2.5 Other Selection Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
22.3 Residual Diagnostics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
22.3.1 Inspecting and Interpreting Residuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
22.3.2 Assessing Normality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
22.3.3 Illustrating Outliers, Leverage, and Influence . . . . . . . . . . . . . . . . . . . . . . . 555
22.3.4 Calculating Leverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
22.3.5 Cook’s Distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
22.3.6 Graphically Combining Residuals, Leverage, and Cook’s Distance . . . 563
Exercise 22.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
22.4 Collinearity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
22.4.1 Potential Warning Signs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
22.4.2 Correlated Predictors: A Quick Example . . . . . . . . . . . . . . . . . . . . . . . . . . 569

xviii Contents in Detail


PART V
ADVANCED GRAPHICS

23
ADVANCED PLOT CUSTOMIZATION 575
23.1 Handling the Graphics Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
23.1.1 Manually Opening a New Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
23.1.2 Switching Between Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
23.1.3 Closing a Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
23.1.4 Multiple Plots in One Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
23.2 Plotting Regions and Margins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
23.2.1 Default Spacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
23.2.2 Custom Spacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
23.2.3 Clipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
23.3 Point-and-Click Coordinate Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
23.3.1 Retrieving Coordinates Silently . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
23.3.2 Visualizing Selected Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
23.3.3 Ad Hoc Annotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
Exercise 23.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
23.4 Customizing Traditional R Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
23.4.1 Graphical Parameters for Style and Suppression . . . . . . . . . . . . . . . . . . . 592
23.4.2 Customizing Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
23.4.3 Customizing Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
23.5 Specialized Text and Label Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
23.5.1 Font . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
23.5.2 Greek Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
23.5.3 Mathematical Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
23.6 A Fully Annotated Scatterplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
Exercise 23.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606

24
GOING FURTHER WITH THE GRAMMAR OF GRAPHICS 609
24.1 ggplot or qplot? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
24.2 Smoothing and Shading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
24.2.1 Adding LOESS Trends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
24.2.2 Constructing Smooth Density Estimates . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
24.3 Multiple Plots and Variable-Mapped Facets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
24.3.1 Independent Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
24.3.2 Facets Mapped to a Categorical Variable . . . . . . . . . . . . . . . . . . . . . . . . . 619
Exercise 24.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
24.4 Interactive Tools in ggvis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
Exercise 24.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628

Contents in Detail xix


25
DEFINING COLORS AND PLOTTING IN HIGHER DIMENSIONS 631
25.1 Representing and Using Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
25.1.1 Red-Green-Blue Hexadecimal Color Codes . . . . . . . . . . . . . . . . . . . . . . . . 632
25.1.2 Built-in Palettes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
25.1.3 Custom Palettes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636
25.1.4 Using Color Palettes to Index a Continuum . . . . . . . . . . . . . . . . . . . . . . . . 637
25.1.5 Including a Color Legend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
25.1.6 Opacity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
25.1.7 RGB Alternatives and Further Functionality . . . . . . . . . . . . . . . . . . . . . . . . . 645
Exercise 25.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
25.2 3D Scatterplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
25.2.1 Basic Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
25.2.2 Visual Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
Exercise 25.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
25.3 Preparing a Surface for Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653
25.3.1 Constructing an Evaluation Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654
25.3.2 Constructing the z-Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
25.3.3 Conceptualizing the z-Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
25.4 Contour Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
25.4.1 Drawing Contour Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
25.4.2 Color-Filled Contours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
Exercise 25.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
25.5 Pixel Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
25.5.1 One Grid Point = One Pixel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
25.5.2 Surface Truncation and Empty Pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
Exercise 25.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676
25.6 Perspective Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679
25.6.1 Basic Plots and Angle Adjustment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679
25.6.2 Coloring Facets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682
25.6.3 Rotating with Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686
Exercise 25.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687

26
INTERACTIVE 3D PLOTS 691
26.1 Point Clouds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
26.1.1 Basic 3D Cloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692
26.1.2 Visual Enhancements and Legends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
26.1.3 Adding Further 3D Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694
Exercise 26.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
26.2 Bivariate Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699
26.2.1 Basic Perspective Surface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700
26.2.2 Additional Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
26.2.3 Coloring by z Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703
26.2.4 Dealing with the Aspect Ratio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
Exercise 26.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706

xx Contents in Detail
26.3 Trivariate Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
26.3.1 Evaluation Coordinates in 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
26.3.2 Isosurfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710
26.3.3 Example: Nonparametric Trivariate Density . . . . . . . . . . . . . . . . . . . . . . . 715
26.4 Handling Parametric Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
26.4.1 Simple Loci . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
26.4.2 Mathematical Abstractions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725
Exercise 26.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732

A
INSTALLING R AND CONTRIBUTED PACKAGES 737
A.1 Downloading and Installing R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737
A.2 Using Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
A.2.1 Base Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
A.2.2 Recommended Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740
A.2.3 Contributed Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740
A.3 Updating R and Installed Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746
A.4 Using Other Mirrors and Repositories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
A.4.1 Switching CRAN Mirror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
A.4.2 Other Package Repositories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748
A.5 Citing and Writing Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748
A.5.1 Citing R and Contributed Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748
A.5.2 Writing Your Own Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749

B
WORKING WITH RSTUDIO 751
B.1 Basic Layout and Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752
B.1.1 Editor Features and Appearance Options . . . . . . . . . . . . . . . . . . . . . . . . . 752
B.1.2 Customizing Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753
B.2 Auxiliary Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754
B.2.1 Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754
B.2.2 Package Installer and Updater . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
B.2.3 Support for Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 756
B.2.4 Markup, Document, and Graphics Tools . . . . . . . . . . . . . . . . . . . . . . . . . . 756

REFERENCE LIST 761

INDEX 767

Contents in Detail xxi


PREFACE

The aim of The Book of R: A First Course in Programming


and Statistics is to provide a relatively gentle yet infor-
mative exposure to the statistical software environ-
ment R, alongside some common statistical analyses,
so that readers may have a solid foundation from
which to eventually become experts in their own right. Learning to use
and program in a computing language is much the same as learning a new
spoken language. At the beginning, it is often difficult and may even be
daunting—but total immersion in and active use of the language is the
best and most effective way to become fluent.
Many beginner-style texts that focus on R can generally be allocated to
one of two categories: those concerned with computational aspects (that is,
syntax and general programming tools) and those with statistical modeling
and analysis in mind, often one particular type. In my experience, these
texts are extremely well written and contain a wealth of useful information
but better suit those individuals wanting to pursue fairly specific goals from
the outset. This text seeks to combine the best of both worlds, by first focus-
ing on only an appreciation and understanding of the language and its style
and subsequently using these skills to fully introduce, conduct, and inter-
pret some common statistical practices. The target audience is, quite simply,
anyone who wants to gain a foothold in R as a first computing language,
perhaps with the ultimate goal of completing their own statistical analyses.
This includes but is certainly not limited to undergraduates, postgraduates,
academic researchers, and practitioners in the applied sciences with little
or no experience in programming or statistics in general. A basic under-
standing of elementary mathematical behavior (for example, the order of
operations) and associated operators (for example, the summation symbol
Σ) is desirable, however.
In view of this, The Book of R can be used purely as a programming text
to learn the language or as an introductory statistical methods book with
accompanying instruction in R. Though it is not intended to represent an
exhaustive dictionary of the language, the aim is to provide readers with a
comfortable learning tool that eliminates the kind of foreboding many have
voiced to me when they have considered learning R from scratch. The fact
remains that there are usually many different ways to go about any given
task—something that holds true for most so-called high-level computer lan-
guages. What this text presents reflects my own way of thinking about learn-
ing and programming in R, which I approach less as a computer scientist
and more as an applied data analyst.
In part, I aim to provide a precursor and supplement to the work in The
Art of R Programming: A Tour of Statistical Software Design, the other R text pub-
lished by No Starch Press (2011), written by Professor Norman Matloff (Uni-
versity of California, Davis). In his detailed and well-received book, Professor
Matloff comes at R from a computer science angle, that is, treating it as a
programming language in its own right. As such, The Art of R Programming
provides some of the best descriptions of R’s computational features I’ve
yet to come across (for example, running external code such as C from R
programs, handling and manipulating R’s memory allocations, and formal
debugging strategies). Noteworthy, however, is the fact that some previous
experience and knowledge of programming in general goes a long way to
appreciating some of these more advanced features. It is my hope that my
text will not only provide this experience but do so in R itself at a comfort-
able pace, with statistical analyses as the supplementary motivation.
This text, which serves as a “traveler’s guide” as we backpack our way
through R country, was born out of a three-day introductory R workshop I
began teaching at the University of Otago in New Zealand. The emphasis
is on active use of the software, with each chapter containing a number of
code examples and practice exercises to encourage interaction. For those
readers not part of a workshop, just fire up your computer, grab a drink and
a comfy chair, and start with Chapter 1.

Tilman M. Davies
Dunedin, New Zealand

xxiv Preface
ACKNOWLEDGMENTS

First, I must of course acknowledge the creators and


maintainers of R, both past and present. Whether
or not any prominent contributor ever picks up
this book (perhaps as a handy stabilizer for a wonky
piece of furniture), they should be reminded of the
appreciation that many, including myself, have for the incredible role R
plays as a data analysis tool worldwide and a glowing example of what can be
achieved with free software. R has transformed how academic and industry
statisticians, and many others, make contributions to research and data anal-
ysis. This doesn’t refer just to their respective pursuits but also to the abil-
ity to make computational functionality readily available so that others may
perform the same analyses. I’m just one voice of many to echo this, and The
Book of R essentially represents my own professional love letter to all things
R. The best way to profess this sentiment is to encourage and educate both
students and professionals in the analytic freedom R provides and hence
perpetuate the enthusiasm many already hold for the software.
The team at No Starch Press is undoubtedly an absolute pleasure to
work with. If “informal professionalism” is actually a thing, it’s exactly how
I like to work and exactly how they guided the progress of what turned out
to be a large, time-consuming project. Thanks to Bill Pollock and Tyler
Ortman for taking on this project. Thanks especially to those at the coalface
of my drafts, specifically Liz Chadwick and Seph Kramer for picking through
and streamlining the language and exposition in numerous TEX files, and
to those who turned the collection of these files into a cohesive, polished
book, headed by Riley Hoffman. Dr. Debbie Leader, a senior university tutor
in statistics at Massey University (Palmerston North, New Zealand), can’t
escape my appreciation for the scrutiny she applied to the R code and sta-
tistical content, improving the clarity and interpretation of the core focus
of the book. Comments from other reviewers and early readers were also
instrumental in shaping the finished product.
A special mention must be made of Professor Martin Hazelton at Massey
University. Once my undergraduate lecturer during my freshman year at the
University of Western Australia; then my graduate and doctoral supervisor
in New Zealand, where I followed him; and now my ongoing collaborator,
mentor, and friend, Martin is pretty much solely responsible for my interest
and career in statistics. Although my scribblings mightn’t include all the big
fancy Oxford words he’d use, it’s only with this education that I’m now in a
position to write this book. With it, I hope I can inspire my own students and
any others wanting to learn more about statistical programming in the same
way he inspired me.
Last, but certainly not least, my immediate family in Perth and my
extended family in Germany must be thanked for their ongoing and unwa-
vering love and support through many years. My New Zealand family must
also be thanked, especially Andrea, who would often put up with a glowing
Apple logo instead of my face on the other end of the couch as I continued
tapping away on the manuscript after work, only pausing to reach for a beer
(or if something juicy was happening on Shortland Street—Google it). The
cat, Sigma, was always nearby at these moments too, ensuring her insertion
in a couple of exercises in this book.
I suppose all this could be summarized in one word, though:

Cheers.

xxvi Acknowledgments
INTRODUCTION

R plays a key role in a wide variety of


research and data analysis projects because
it makes many modern statistical methods,
both simple and advanced, readily available
and easy to use. It’s true, however, that a beginner to
R is often new to programming in general. As a begin-
ner, you must not only learn to use R for your specific
data analysis goals but also learn to think like a pro-
grammer. This is partly why R has a bit of a reputation
for being “hard”—but rest assured, that really isn’t
the case.

A Brief History of R
R is based heavily on the S language, first developed in the 1960s and 1970s
by researchers at Bell Laboratories in New Jersey (for an overview, see, for
example, Becker et al., 1988). With a view to embracing open source soft-
ware, R’s developers—Ross Ihaka and Robert Gentleman at the Univer-
sity of Auckland in New Zealand—released it in the early 1990s under the
Exploring the Variety of Random
Documents with Different Content
sang. Pour quelles raisons accepterait-on plusieurs maîtres ? Caillou
se refuse à ce servage. Voilà pourquoi, cédant aussi à ses instincts
de gourmandise et de rapine, il dévore chaque jour, dans leur plus
verte jeunesse, les pommes et les poires de l’oncle, espoir d’une
récolte future. Comme il en jette dédaigneusement les débris à
travers les allées, il n’est pas difficile de constater le crime :
— Ne mens pas, Caillou ! Tu montes sur les arbres et tu cueilles
les fruits ?
— Oui, dit Caillou. Tout en haut des arbres, je monte !
Il avoue fièrement, considérant que c’est là un exploit digne de sa
propre estime et d’une inscription dans les ouvrages dont il se repaît.
On le gronde, on le punit, on lui fait jurer :
— Tu ne cueilleras plus de pommes, Caillou ? Promets !
— Je n’en cueillerai plus, dit Caillou, maussade.
— Ni de poires, ni de pêches ?
Caillou promet encore, et il a gardé ça de son ancienne
honnêteté : il sait tenir parole. Seulement, le lendemain, pêchers,
poiriers, pommiers présentent un spectacle hideux et sans
précédent dans l’histoire des désastres de l’arboriculture. Caillou n’a
plus « cueilli », cela ne fait aucun doute. Mais on ne lui avait pas
défendu de grimper aux arbres. Il a donc grimpé, comme d’habitude,
et mangé le plus de fruits qu’il a pu, en laissant les trognons
attachés à leur tige. Les arbres sont déshonorés, et Caillou
s’applaudit d’avoir à la fois respecté sa parole et acquis la certitude
qu’on parlera de lui à travers les siècles : car il est maintenant avide
de gloire, même de la plus détestable.
A l’aube suivante, l’oncle part pour la chasse. Il aime cette
distraction, il éprouve aussi le besoin de s’éloigner le plus souvent
possible de cette maison qui est sienne, devenue pleine d’une
agitation choquante et de drames intimes. Il siffle son chien, et
Caillou demande d’un air innocent :
— Tu t’en vas, mon oncle ?
Mais le chien, dès qu’il a vu Caillou, fait un bond d’épouvante, et
s’enfuit, s’enfuit à l’autre bout du pays, pour échapper à son
persécuteur. Car Caillou est son bourreau ; Caillou, pour satisfaire
ses instincts de primitif, développés par ses lectures, en a fait parfois
un lion, parfois un ours, parfois un loup. Il l’a pris au piège, il l’a tué
dans des combats tumultueux, il l’a ramené dans son antre de
Robinson victorieux, lié aux quatre pattes, il l’a traîné sur le gravier
rude comme un cadavre pantelant ; et le bon chien s’est changé en
une pauvre bête apeurée qui n’a plus confiance dans la bonté des
hommes, qui sait qu’ils peuvent faire du mal quand on ne leur fait
rien. Caillou ne traiterait pas de la même façon Jupiter, le chat, parce
que Jupiter a des griffes. Mais il l’utilise cependant à sa manière, il le
transforme en un tigre redoutable en l’élevant trente fois par jour à la
hauteur de la cage du serin. Et le serin, le cœur battant, les plumes
ébouriffées à la vue de l’ennemi héréditaire, se jette contre les
barreaux de la cage, s’y brise le bec, s’y écorche les pattes, en perd
le boire et le manger.
C’est ainsi que Caillou fait « des expériences » et désorganise
l’intérieur pacifique, ordonné, solennel, de l’oncle Jules. Et plus il est
admonesté, morigéné, châtié, plus il se considère comme une
victime des puissances, plus il se pénètre de l’idée, puisée dans une
littérature qui l’abuse, qu’il n’est pas comme les autres, qu’il a le droit
de se venger, qu’il ne se venge pas assez, et que ses malheurs sont
dignes d’une éternelle mémoire. Il est devenu un romantique, et
comme tous les romantiques, on ne peut même plus le prendre par
les sentiments : les duretés qu’il subit légitiment à ses yeux tous les
actes de révolte.
— Songe à la passion de Jésus-Christ ! lui dit un jour
imprudemment sa mère.
— Ça n’a duré que trois jours, répond Caillou. Et moi, voilà trois
ans qu’on m’embête !
Voilà trois ans qu’il est le plus heureux petit garçon du monde,
mais il se croirait déshonoré de l’admettre. Il n’y a plus qu’à
l’emmener, le maître de la maison n’en peut plus. Enfin voilà ses
hôtes partis, l’oncle respire. Mais la précieuse cuisinière vient lui
rendre son tablier.
— Je ne veux plus rester dans cette maison, dit-elle, on n’y a pas
d’égards pour moi : monsieur Caillou, avant de partir, a fourré des
colimaçons plein mon lit !
Et elle ajoute :
— Je dois dire aussi à monsieur que son serin est mort.
— Mort ! fait l’oncle. Et de quoi ?
— Je n’en sais rien. De peur peut-être !
C’est ainsi que le pauvre oncle reste seul dans sa maison
dévastée, sans cuisinière, sans chien, sans canari. La jeunesse a
passé là comme une passion ; elle n’a rien laissé derrière elle que
des ruines. Et l’oncle se sent décidément très vieux, très fini, tout à
fait incapable de s’accoutumer à ceux qui n’ont pas son âge ; il
songe amèrement qu’il ne pardonnera jamais à Caillou. Il est bien
vieux en effet, puisqu’il a oublié ce que c’est que l’âge ingrat des
petits garçons : celui auquel ils commencent à s’opposer, par la
révolte, et pour prendre conscience de leur personnalité, à un
monde dont ils se font une idée aussi intolérable qu’elle est fausse.

La mère de Caillou est désespérée. Malgré toute la sagesse et


toute l’expérience qu’elles ont pu acquérir, toutes les mères
nourrissent la même illusion : elles se figurent que leurs petits
resteront toujours petits. Et Caillou maintenant lui apparaît comme
une espèce de monstre ! Elle envisage toutes sortes de projets,
même celui de le mettre au collège, pour qu’il y apprenne la
discipline, le respect, et qu’il parvienne à se persuader qu’il faut
« faire comme tout le monde ». Car enfin, il faut que Caillou
commence « son éducation ». J’assiste à ses angoisses, mais je ne
puis les partager. Caillou traverse une crise, mais je sais bien qu’au
fond il est toujours le même Caillou. Et je ne voudrais point qu’on me
le changeât, sous prétexte de le corriger.
— Croyez bien, lui dis-je, que je ne nourris nulle haine contre les
maîtres de mon enfance. Je sais toutefois que je suis sorti de leurs
mains façonné de si étrange manière que j’ai mis des années avant
de me retrouver moi-même : et ce furent des années perdues. Il y
avait un jeune canard, une fois…
— C’est un apologue.
— Peut-être. Mais je vous assure qu’il s’agit d’un canard
véritable, auquel il arriva malheur pendant la grande inondation.
» D’abord ce canard ne fut qu’un œuf, ce qui n’a rien que de très
naturel. Monsieur Giscard, tonnelier rue des Ursins, derrière Notre-
Dame, l’avait choisi un jour dans une terrine, à la campagne, chez
un de ses amis, nourrisseur.
» — Qu’est-ce que c’est donc que celui-là ? avait-il demandé.
» Et en effet, il était plus gros que les autres, un peu plus long, et
quand on le mettait entre son œil et la lumière, il prenait une teinte
vert pâle, tandis que les autres paraissaient roses.
» — Celui-là ? répondit le nourrisseur : c’est un œuf de cane.
» Alors monsieur Giscard avait demandé la permission de
l’emporter, disant que puisqu’il avait des poules, il pourrait le faire
couver.
» Voilà par quelles suites de circonstances ce canard naquit rue
des Ursins. Sa coquille crevée, il fut d’abord une masse oblongue,
qui sous les ailes d’une vieille poule se dorlotait dans du poil jaune,
précurseur des plumes. Il était en somme assez semblable aux
petits poulets, ses frères de hasard, sauf pour les pattes et le bec,
qu’il avait plus larges ; et son corps avait encore la forme de l’œuf
dont il était sorti. Puis son pennage poussa, il grandit et devint un
canard véritable, fort heureux de sa condition et de ses entours.
» Car la nourriture s’offrait devant lui, savoureuse, abondante et
variée. C’était du pain en miettes, des écuellées de rogatons gras,
aussi les entrailles d’autres oiseaux, telles que madame Giscard les
jetait sur le sol ; et le canard, dans son ignorance ingénue, les
dévorait. Au delà de la cour qui servait d’atelier, une fois passée la
vieille porte cochère garnie d’énormes clous à têtes de diamant,
s’étendait une région très vaste, qui fournissait encore des choses
bonnes à manger. C’était la vue des Ursins même, irrégulière,
tortueuse, resserrée à l’un des bouts, semblable en vérité à un
boyau, avec une vieille chapelle toute ruineuse dont les pierres
disjointes offraient en été aux appétits des vers, des mouches, des
insectes vivants. A l’une de ses extrémités s’ouvrait la rue de la
Colombe, qui mène à un édifice énorme, très haut sur le ciel, avec
deux tours et un clocher riche en corneilles ; à l’autre, c’était un
espace plat et large, bordé par un mur de pierre. Les hommes
appellent l’édifice « Notre-Dame », et l’espace plat, fermé d’une
muraille, ils le nomment « le quai ». A certaines heures, ces rues
sont presque vides ; on peut s’y aventurer, on y trouve des oranges
mâchées, des rognures de viande, mille débris d’un goût délicieux.
Mais la petite troupe des poulets et du canard n’allait jamais vers le
quai.
» — Qu’est-ce qu’il y a, de ce côté ? demanda un jour le canard,
pour s’instruire.
» — L’eau ! répondit la vieille poule,
» Sa voix était si pénétrée d’horreur que le canard n’insista pas. Il
connut donc que l’eau, c’était le mal. Cependant il eut la curiosité de
savoir ce que faisaient les ouvriers de monsieur Giscard. Tout le jour
leurs marteaux de bois dur et leurs doloires luisantes retentissaient
et grinçaient sur les muids, les bordelaises, les feuillettes. Le canard
apprit que ces vaisseaux ronds et creux devaient plus tard recevoir
des liquides ; il les considéra donc comme des remparts destinés à
contenir une matière analogue à l’eau, perfide et dangereuse. Il
buvait pourtant au ruisseau de la rue. Mais, imitant ses compagnons,
il évitait de se mouiller les pattes.
» Un jour, les hommes dirent entre eux : « Elle monte ! » Le
canard n’entendit pas leurs paroles, mais il remarqua leur agitation.
Ils aveuglèrent les soupiraux avec du ciment et construisirent un mur
devant la porte. Le canard crut d’abord que c’était pour l’empêcher
de sortir, car les animaux, comme les humains, penchent
communément à croire que les choses qu’on fait, on les fait pour
eux, ou contre eux. Et le canard s’égaya de cette sottise. Il n’avait
jamais volé. Il ne savait même pas que ses ailes, perpétuellement
rognées, pouvaient servir à voler, mais il se sentait sûr de franchir,
quand il le voudrait, cette ridicule barrière.
» Il y avait dans la cour une vieille mangeoire destinée à des
chevaux depuis longtemps disparus. C’était là que perchaient les
jeunes poules, tandis que celles qui couvaient demeuraient
d’habitude, la nuit comme le jour, dans une cuve pleine de paille ; et
c’est à celles-là que se joignait le canard, qui n’aimait pas à grimper.
Vers le milieu de la nuit, il entendit des bruits singuliers. On criait,
plus loin que la maison, dans la rue :
» — L’eau monte, elle monte toujours ! Déménagez, vous ne
pouvez rester ici !
» Le canard ne savait pas que ces cris venaient de barques
montées par des soldats. Ce qui le surprit davantage, c’est que la
cuve dans laquelle il sommeillait semblait se mouvoir assez
doucement, au lieu de demeurer à sa place ordinaire ; et à la lueur
du croissant de la lune, il s’aperçut que la mangeoire, sur laquelle
étaient perchées les jeunes poules, paraissait maintenant moins
élevée au-dessus de sa tête. Les animaux ont des rêves aussi bien
que les hommes : cela sans doute était un rêve ! Cependant la vieille
Houdan qui l’avait couvé, au moment où la cuve se rapprocha de la
mangeoire, battit désespérément de ses pauvres ailes, et sauta. Le
canard fit comme elle, par esprit d’imitation. Puis il se rendormit
tranquillement, car les oiseaux, dès que le soleil est couché, ne
peuvent tenir leurs yeux ouverts.
» Mais ils s’éveillèrent dès l’aube, et le jour naissant lui montra un
spectacle étrange. A la place du sol qu’il avait piétiné la veille, plat et
très dur, d’une belle couleur grise, et fertile en nourriture, il n’y avait
plus sous ses pieds qu’une étendue jaunâtre d’un élément inconnu.
Au premier abord on eût pensé que c’était solide, mais vers les coins
et les parois de la cour, ça se gonflait, ça clapotait ; c’était sournois,
inquiétant, insidieux. La vieille poule dit, avec un frisson d’effroi :
» — C’est l’eau !
» Et le canard éprouva un sentiment d’angoisse bien plus
déchirant que celui qui pénétrait ses voisins parce que c’était une loi
inculquée dès son enfance, et non pas son instinct, qui le retenait à
sa place. L’eau lui faisait envie, il la désirait d’une manière à la fois
vague et puissante ; pourtant elle lui faisait peur par principe. Il
demanda timidement :
» — Est-ce que c’est vrai, absolument vrai, qu’on ne peut pas
descendre là-dessus et se tenir debout, tout en vie ?
» Il était naturel qu’il posât la question sous cette forme puisqu’il
ne savait pas qu’il y a un acte qui s’appelle nager. Mais on ne lui
répondit que par un regard atroce. Non, ça ne se pouvait pas !
Quelques poules essayèrent, prenant leur élan, de sauter par-
dessus la clôture de la cour. L’une d’elles y réussit, mais on l’entendit
retomber, de l’autre côté, dans l’élément mortel qui remplissait la
rue. Les autres se noyèrent dans la cour même, après s’être brisé le
bec contre les pierres qu’elles avaient tenté de franchir.
» — Qu’est-ce qu’il faut faire ? interrogea le canard.
» — Attendre, répondit la vieille poule.
» Ayant toujours vécu parmi les hommes, elle ne connaissait rien
que ce qui venait d’eux ; le salut ou la mort : ils dispensaient tout.
Mais les hommes ne vinrent pas. Les oiseaux, qui digèrent très vite,
souffrent et meurent très rapidement de la faim. Le canard et la
poule, trop faibles pour rester perchés, se blottirent au fond de la
mangeoire.
…» Après l’inondation, monsieur Giscard revint.
» — Les poules ont dû mourir, dit-il, mais c’est le canard qui doit
s’en donner !
» Mais le canard avait subi le sort des camarades. Couché près
de la poule, sur le dos, avec cette taie sur les yeux ouverts qu’ont les
oiseaux morts, il dressait vers le ciel ses pattes palmées faites pour
ramer l’eau.
» Monsieur Giscard fut si émerveillé par ce spectacle qu’il oublia
de regretter la perte.
» — Tout de même, dit-il, tout de même… celui-là, il avait de
l’éducation ! »

La maman de Caillou se mit à rire.


— Vous croyez que je plaisante, lui dis-je, mais c’est une histoire
qui est arrivée, et je plains beaucoup ce canard : si vous voulez bien
y réfléchir une petite minute, il fut assassiné par persuasion.
Toutefois vous pouvez aussi accepter ce récit comme une fable, et
supposer que ce canard, c’est vous, moi, ou notre voisin et Caillou
lui-même, si on lui donne trop tôt, et comme on la donne, hélas, ce
qu’on est convenu d’appeler « de l’éducation ». Car ça consiste
généralement, chez nous, à faire perdre aux petits Français leur
personnalité et leurs instincts. Je vous engage à vous méfier :
Caillou n’y gagnerait absolument rien.

Juillet 1909 — Février 1911.

FIN
TABLE

PREMIÈRE RENCONTRE 1
CAILLOU ET LES FEMMES 19
LES CHIENS ET LA GLOIRE 51
A LA CAMPAGNE 63
SA PUDEUR 81
LES AMIS DE CAILLOU 95
LE CIRQUE 133
LA GOURMANDISE 143
L’ŒUF DE CHEVAL 153
CAILLOU ET SON PÈRE 165
DU SENTIMENT DE LA PROPRIÉTÉ 175
LE MYSTÈRE 187
L’OPÉRATION 217
L’AUBE DE L’AGE INGRAT 229

E. GREVIN — IMPRIMERIE DE LAGNY — 4830-7-14.


*** END OF THE PROJECT GUTENBERG EBOOK CAILLOU ET
TILI ***

Updated editions will replace the previous one—the old editions will
be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States copyright in
these works, so the Foundation (and you!) can copy and distribute it
in the United States without permission and without paying copyright
royalties. Special rules, set forth in the General Terms of Use part of
this license, apply to copying and distributing Project Gutenberg™
electronic works to protect the PROJECT GUTENBERG™ concept
and trademark. Project Gutenberg is a registered trademark, and
may not be used if you charge for an eBook, except by following the
terms of the trademark license, including paying royalties for use of
the Project Gutenberg trademark. If you do not charge anything for
copies of this eBook, complying with the trademark license is very
easy. You may use this eBook for nearly any purpose such as
creation of derivative works, reports, performances and research.
Project Gutenberg eBooks may be modified and printed and given
away—you may do practically ANYTHING in the United States with
eBooks not protected by U.S. copyright law. Redistribution is subject
to the trademark license, especially commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the free


distribution of electronic works, by using or distributing this work (or
any other work associated in any way with the phrase “Project
Gutenberg”), you agree to comply with all the terms of the Full
Project Gutenberg™ License available with this file or online at
www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand, agree
to and accept all the terms of this license and intellectual property
(trademark/copyright) agreement. If you do not agree to abide by all
the terms of this agreement, you must cease using and return or
destroy all copies of Project Gutenberg™ electronic works in your
possession. If you paid a fee for obtaining a copy of or access to a
Project Gutenberg™ electronic work and you do not agree to be
bound by the terms of this agreement, you may obtain a refund from
the person or entity to whom you paid the fee as set forth in
paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only be


used on or associated in any way with an electronic work by people
who agree to be bound by the terms of this agreement. There are a
few things that you can do with most Project Gutenberg™ electronic
works even without complying with the full terms of this agreement.
See paragraph 1.C below. There are a lot of things you can do with
Project Gutenberg™ electronic works if you follow the terms of this
agreement and help preserve free future access to Project
Gutenberg™ electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright law in
the United States and you are located in the United States, we do
not claim a right to prevent you from copying, distributing,
performing, displaying or creating derivative works based on the
work as long as all references to Project Gutenberg are removed. Of
course, we hope that you will support the Project Gutenberg™
mission of promoting free access to electronic works by freely
sharing Project Gutenberg™ works in compliance with the terms of
this agreement for keeping the Project Gutenberg™ name
associated with the work. You can easily comply with the terms of
this agreement by keeping this work in the same format with its
attached full Project Gutenberg™ License when you share it without
charge with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the terms
of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.

1.E. Unless you have removed all references to Project Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project Gutenberg™
work (any work on which the phrase “Project Gutenberg” appears, or
with which the phrase “Project Gutenberg” is associated) is
accessed, displayed, performed, viewed, copied or distributed:
This eBook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this eBook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning of
this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1 with
active links or immediate access to the full terms of the Project
Gutenberg™ License.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or expense
to the user, provide a copy, a means of exporting a copy, or a means
of obtaining a copy upon request, of the work in its original “Plain
Vanilla ASCII” or other form. Any alternate format must include the
full Project Gutenberg™ License as specified in paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt that
s/he does not agree to the terms of the full Project Gutenberg™
License. You must require such a user to return or destroy all
copies of the works possessed in a physical medium and
discontinue all use of and all access to other copies of Project
Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
damaged disk or other medium, a computer virus, or computer
codes that damage or cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except


for the “Right of Replacement or Refund” described in paragraph
1.F.3, the Project Gutenberg Literary Archive Foundation, the owner
of the Project Gutenberg™ trademark, and any other party
distributing a Project Gutenberg™ electronic work under this
agreement, disclaim all liability to you for damages, costs and
expenses, including legal fees. YOU AGREE THAT YOU HAVE NO
REMEDIES FOR NEGLIGENCE, STRICT LIABILITY, BREACH OF
WARRANTY OR BREACH OF CONTRACT EXCEPT THOSE
PROVIDED IN PARAGRAPH 1.F.3. YOU AGREE THAT THE
FOUNDATION, THE TRADEMARK OWNER, AND ANY
DISTRIBUTOR UNDER THIS AGREEMENT WILL NOT BE LIABLE
TO YOU FOR ACTUAL, DIRECT, INDIRECT, CONSEQUENTIAL,
PUNITIVE OR INCIDENTAL DAMAGES EVEN IF YOU GIVE
NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of receiving it,
you can receive a refund of the money (if any) you paid for it by
sending a written explanation to the person you received the work
from. If you received the work on a physical medium, you must
return the medium with your written explanation. The person or entity
that provided you with the defective work may elect to provide a
replacement copy in lieu of a refund. If you received the work
electronically, the person or entity providing it to you may choose to
give you a second opportunity to receive the work electronically in
lieu of a refund. If the second copy is also defective, you may
demand a refund in writing without further opportunities to fix the
problem.

1.F.4. Except for the limited right of replacement or refund set forth in
paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.
1.F.6. INDEMNITY - You agree to indemnify and hold the
Foundation, the trademark owner, any agent or employee of the
Foundation, anyone providing copies of Project Gutenberg™
electronic works in accordance with this agreement, and any
volunteers associated with the production, promotion and distribution
of Project Gutenberg™ electronic works, harmless from all liability,
costs and expenses, including legal fees, that arise directly or
indirectly from any of the following which you do or cause to occur:
(a) distribution of this or any Project Gutenberg™ work, (b)
alteration, modification, or additions or deletions to any Project
Gutenberg™ work, and (c) any Defect you cause.

Section 2. Information about the Mission of


Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many small
donations ($1 to $5,000) are particularly important to maintaining tax
exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About Project


Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.

Project Gutenberg™ eBooks are often created from several printed


editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like