The Book of R: A First Course in Programming and Statistics 1st Edition Tilman M. Davies all chapter instant download
The Book of R: A First Course in Programming and Statistics 1st Edition Tilman M. Davies all chapter instant download
com
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
https://textbookfull.com/product/a-first-course-in-statistics-for-
signal-analysis-wojbor-a-woyczynski/
textboxfull.com
https://textbookfull.com/product/statistics-for-technology-a-course-
in-applied-statistics-third-edition-chatfield/
textboxfull.com
https://textbookfull.com/product/mathematical-statistics-with-
applications-in-r-third-edition-kandethody-m-ramachandran/
textboxfull.com
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
https://textbookfull.com/product/a-course-in-categorical-data-
analysis-first-edition-leonard/
textboxfull.com
https://textbookfull.com/product/a-first-course-in-sobolev-spaces-
giovanni-leoni/
textboxfull.com
https://textbookfull.com/product/a-first-course-in-functional-
analysis-1st-edition-orr-moshe-shalit/
textboxfull.com
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
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
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
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
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
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
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
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
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
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
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
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
INDEX 767
Tilman M. Davies
Dunedin, New Zealand
xxiv Preface
ACKNOWLEDGMENTS
Cheers.
xxvi Acknowledgments
INTRODUCTION
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.
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
Updated editions will replace the previous one—the old editions will
be renamed.
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.
• 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 comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
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.
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.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
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.
textbookfull.com