The Book of R, Second Edition A First Course in Programming and Statistics (Tilman M. Davies)(Z-Library)
programming
No Description
2
Views
0
Downloads
0.00
Total Donations
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
T I L M A N M . D A V I E S ® T H E B O O K O F R A F I R S T C O U R S E I N P R O G R A M M I N G A N D S T A T I S T I C S S E C O N D E D I T I O N
Page
2
(This page has no text content)
Page
3
THE BOOK OF R
Page
4
(This page has no text content)
Page
5
THE BOOK OF R 2ND EDITION A First Course in Programming andStatistics by Tilman M. Davies ® San Francisco
Page
6
THE BOOK OF R, 2ND EDITION. Copyright © 2026 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. First printing 29 28 27 26 25 1 2 3 4 5 ISBN13: 9781718503687 (print) ISBN13: 9781718503694 (ebook) ® Published by No Starch Press®, Inc. 245 8th Street, San Francisco, CA 94103 phone: +1.415.863.9900 www.nostarch.com; info@nostarch.com Publisher: William Pollock Managing Editor: Jill Franklin Production Manager: Sabrina PlomitalloGonzález Production Editor: Sydney Cromwell Cover Illustration: Josh Ellingson Developmental Editor: Eva Morrow Technical Reviewer: Debbie Leader Proofreader: Scout Festa Indexer: BIM Creatives, LLC The Library of Congress has catalogued the first edition as follows: 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 For customer service inquiries, please contact info@nostarch.com. For information on distribution, bulk sales, corporate sales, or translations: sales@nostarch.com. For permission to translate this work: rights@nostarch.com. To report counterfeit copies or piracy: counterfeit@nostarch.com. The authorized representative in the EU for product safety and compliance is EU Compliance Partner, Pärnu mnt. 139b14, 11317 Tallinn, Estonia, hello@eucompliancepartner.com, +3375690241. No Starch Press and the No Starch Press iron logo are registered trademarks of No Starch Press, Inc. Other product and company namesmentioned hereinmay 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. [E]
Page
7
Prof. Dr. Dr. Jochen Weniger, 1925–2015 Ein Wissenschaftler. Mein Opa.
Page
8
(This page has no text content)
Page
9
About the Author Tilman M. Davies is an academic at the Department of Mathematics and Statistics at the University of Otago, New Zealand, where he lectures on statistics and R at all university levels. His research, which lies primarily in spatial statistics, is regularly published in leading scholarly journals and has to date been awarded several prestigious Marsden Fund grants from the Royal Society of New Zealand. He is the lead handling editor of the Statisti cal Computing section of the Australian and New Zealand Journal of Statistics, and The Book of R, 2nd edition, is a reflection of his ongoing enthusiasm for the language as the most popular tool for statistical modeling and data science worldwide. About the Technical Reviewer Dr. Debbie Leader is a senior tutor in statistics at Massey University in New Zealand. She is passionate about teaching the fundamentals of statistics, including guiding students to develop experience with R so that they can appreciate it as a valuable component in their statistical toolbox. Debbie has been using and teaching R for over 15 years.
Page
10
(This page has no text content)
Page
11
BRIEF CONTENTS Preface to the Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Preface to the First Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi PART I: THE LANGUAGE Chapter 1: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 2: Numerics, Arithmetic, Assignment, and Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Chapter 3: Matrices and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Chapter 4: Non-Numeric Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Chapter 5: Lists and Data Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Chapter 6: Special Values, Classes, and Coercion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Chapter 7: Basic Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Chapter 8: Reading and Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 PART II: PROGRAMMING Chapter 9: Calling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Chapter 10: Conditions and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Chapter 11: Writing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Chapter 12: Exceptions, Timings, and Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 PART III: STATISTICS AND PROBABILITY Chapter 13: Elementary Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Chapter 14: Basic Data Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Chapter 15: Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 Chapter 16: Common Probability Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Page
12
PART IV: STATISTICAL TESTING AND MODELING Chapter 17: Sampling Distributions and Confidence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435 Chapter 18: Hypothesis Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 Chapter 19: Analysis of Variance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503 Chapter 20: Simple Linear Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 Chapter 21: Multiple Linear Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555 Chapter 22: Linear Model Selection and Diagnostics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599 PART V: ADVANCED GRAPHICS Chapter 23: Advanced Plot Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647 Chapter 24: Going Further with the Grammar of Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685 Chapter 25: Defining Colors and Plotting in Higher Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . 709 Chapter 26: Interactive 3D Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773 Appendix A: Installing R and Contributed Packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827 Appendix B: Working with RStudio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861 x Brief Contents
Page
13
CONTENTS IN DETA IL PREFACE TO THE SECOND EDITION xxv PREFACE TO THE FIRST EDITION xxvii ACKNOWLEDGMENTS xxix INTRODUCTION xxxi A Brief History of R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxii What’s New in the Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv Conventions and Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvi Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvi Math and Equation References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvii Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxviii For Students . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxviii For Instructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxix Web Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xl 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2.1 Base R GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.2.2 RStudio and Other Third-Party Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.3 Workflow Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.3.1 Console and Editor Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.3.2 Code Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.3.3 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.3.4 Working Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.3.5 R Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.3.6 Help Files and Function Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Page
14
1.4 Saving Work and Exiting R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.4.1 Workspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.4.2 Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2 NUMERICS, ARITHMETIC, ASSIGNMENT, AND VECTORS 15 2.1 R for Basic Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.1.1 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.1.2 Logarithms and Exponentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.1.3 E-Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.2 Assigning Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.3 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.3.1 Vector Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.3.2 Sequences, Repetition, Sorting, and Lengths . . . . . . . . . . . . . . . . . . . . . . 22 2.3.3 Subsets and Element Extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.3.4 Vector-Oriented Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3 MATRICES AND ARRAYS 37 3.1 Two-Dimensional Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 3.1.1 Matrix Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 3.1.2 Subsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 3.1.3 Matrix Operations and Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3.2 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.2.1 Array Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 3.2.2 Subsets, Extractions, and Replacements . . . . . . . . . . . . . . . . . . . . . . . . . . 53 4 NON-NUMERIC VALUES 59 4.1 Logical Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4.1.1 TRUE or FALSE? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 4.1.2 Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 4.1.3 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 4.1.4 Logicals Are Numbers! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 4.1.5 Logical Subsetting and Extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 4.2 Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 4.2.1 String Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 4.2.2 Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 4.2.3 Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 4.2.4 Substrings and Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 4.3 Factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 4.3.1 Identifying Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 4.3.2 Defining and Ordering Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 4.3.3 Dropping Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 4.3.4 Working with the Numeric Representation of Factors . . . . . . . . . . . . . . 86 4.3.5 Collapsing Factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 xii Contents in Detail
Page
15
4.3.6 Combining Factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 4.3.7 Creating Factors from Continuous Values . . . . . . . . . . . . . . . . . . . . . . . . . 90 4.4 Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 4.4.1 Creating and Formatting Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 4.4.2 Performing Simple Operations with Dates . . . . . . . . . . . . . . . . . . . . . . . . 95 4.4.3 Incorporating Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 5 LISTS AND DATA FRAMES 103 5.1 Lists of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 5.1.1 Creating Lists and Accessing Components . . . . . . . . . . . . . . . . . . . . . . . . 104 5.1.2 Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 5.1.3 Nesting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 5.2 Data Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 5.2.1 Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 5.2.2 Basic Subsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 5.2.3 Column Insertions and Data Frame Combinations . . . . . . . . . . . . . . . . . 113 5.2.4 Logical Record Subsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 5.2.5 The Explicit subset Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 6 SPECIAL VALUES, CLASSES, AND COERCION 119 6.1 Special Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 6.1.1 Infinity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 6.1.2 NaN: Not a Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 6.1.3 NA: Not Available . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 6.1.4 NULL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 6.2 Types, Classes, and Coercion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 6.2.1 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 6.2.2 Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 6.2.3 Is-Dot Object-Checking Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 6.2.4 As-Dot Coercion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 7 BASIC PLOTTING 145 7.1 Using plot with Coordinate Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 7.2 Visual Controls and Graphical Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 7.2.1 Automatic Plot Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 7.2.2 Title and Axis Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 7.2.3 Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 7.2.4 Line and Point Appearances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 7.2.5 Custom Region Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 7.2.6 Aspect Ratio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 7.3 Adding Points, Lines, and Text to an Existing Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Contents in Detail xiii
Page
16
7.4 The ggplot2 Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 7.4.1 Plotting a Toy Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 7.4.2 Setting Appearance Constants with Geoms . . . . . . . . . . . . . . . . . . . . . . . 162 7.4.3 Aesthetic Mapping with Geoms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 8 READING AND WRITING FILES 169 8.1 R-Ready Datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 8.1.1 Built-In Datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 8.1.2 Contributed Datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 8.2 Reading in External Data Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 8.2.1 The Table Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 8.2.2 Spreadsheet Workbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 8.2.3 Web-Based Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 8.2.4 Other File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 8.3 Writing Out Data Files and Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 8.3.1 Datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 8.3.2 Plots and Graphics Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 8.4 Ad Hoc Object Read/Write Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 8.4.1 save and load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 8.4.2 saveRDS and readRDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 8.4.3 dput and dget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 8.4.4 Suitability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 PART II PROGRAMMING 9 CALLING FUNCTIONS 193 9.1 Scoping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 9.1.1 Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 9.1.2 Search Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 9.1.3 Reserved and Protected Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 9.2 Argument Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 9.2.1 Exact . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 9.2.2 Partial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 9.2.3 Positional . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 9.2.4 Mixed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 9.2.5 Ellipses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 9.3 Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 9.3.1 Syntax and Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 9.3.2 Should You Pipe? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 xiv Contents in Detail
Page
17
10 CONDITIONS AND LOOPS 209 10.1 if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 10.1.1 Stand-Alone Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 10.1.2 else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 10.1.3 Element-Wise Checks with ifelse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 10.1.4 Nested and Stacked Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 10.1.5 The switch Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 10.2 for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 10.2.1 Definition and Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 10.2.2 Via Index or Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 10.2.3 Nests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 10.3 while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 10.3.1 Definition and Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 10.3.2 Condition Object Modification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 10.4 Implicit Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 10.4.1 The apply Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 10.4.2 Some apply Variants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 10.4.3 When to Use Implicit Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 10.5 Other Control Flow Mechanisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 10.5.1 break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 10.5.2 next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 10.5.3 repeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 11 WRITING FUNCTIONS 251 11.1 The function Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 11.1.1 Function Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 11.1.2 The return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 11.2 Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 11.2.1 Considering Lazy Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 11.2.2 Setting Defaults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 11.2.3 Checking for Missing Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 11.2.4 Dealing with Ellipses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 11.3 Specialized Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 11.3.1 Helper Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 11.3.2 Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 11.3.3 Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 12 EXCEPTIONS, TIMINGS, AND VISIBILITY 277 12.1 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 12.1.1 Errors and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 12.1.2 try Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 Contents in Detail xv
Page
18
12.2 Progress and Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 12.2.1 Tracking Progress: Are We There Yet? . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 12.2.2 Measuring Completion Time: How Long Did It Take? . . . . . . . . . . . . . . 287 12.2.3 Profiling Your Code: Where’s the Bottleneck? . . . . . . . . . . . . . . . . . . . . . 288 12.3 Masking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 12.3.1 Function and Object Distinction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 12.3.2 Data Frame Variable Distinction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 PART III STATISTICS AND PROBABILITY 13 ELEMENTARY STATISTICS 303 13.1 Describing Raw Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 13.1.1 Numeric Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 13.1.2 Categorical Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 13.1.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 13.1.4 Univariate and Multivariate Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 13.1.5 Parameter or Statistic? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 13.2 Summary Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 13.2.1 Measures of Centrality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 13.2.2 Counts, Percentages, and Proportions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 13.2.3 Rounding, Floors, Ceilings, and Absolute Values . . . . . . . . . . . . . . . . . . 315 13.2.4 Quantiles, Percentiles, and the “Five-Number Summary” . . . . . . . . . . . 318 13.2.5 Measures of Spread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 13.2.6 Relationships Between Numeric Variables . . . . . . . . . . . . . . . . . . . . . . . . 324 13.2.7 Outliers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 14 BASIC DATA VISUALIZATION 333 14.1 Bar Plots and Pie Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 14.1.1 Bar Plot Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 14.1.2 In ggplot2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 14.1.3 A Quick Pie Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 14.2 Histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 14.2.1 A Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 14.2.2 Bin Width and Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 14.2.3 In ggplot2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 14.3 Density Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 14.3.1 Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 14.3.2 Bandwidths and Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 14.3.3 When Multiple Densities Overlap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 14.3.4 In ggplot2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 xvi Contents in Detail
Page
19
14.4 Box Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 14.4.1 Stand-Alone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 14.4.2 Side-by-Side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 14.4.3 In ggplot2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 14.5 Violin Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 14.5.1 Design and Interpretation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 14.5.2 Side-by-Side Violins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 14.5.3 In ggplot2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 14.6 Scatterplots and Coordinate-Based Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 14.6.1 Single Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 14.6.2 Matrix of Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 14.6.3 In ggplot2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 14.6.4 Plots of Spatial Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 15 PROBABILITY 371 15.1 What Is a Probability? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 15.1.1 Events and Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372 15.1.2 Conditional Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 15.1.3 Intersection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 15.1.4 Union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 15.1.5 Complement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 15.2 Random Variables and Probability Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376 15.2.1 Realizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376 15.2.2 Discrete Random Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 15.2.3 Continuous Random Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380 15.2.4 Shape, Skew, and Modality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388 16 COMMON PROBABILITY DISTRIBUTIONS 393 16.1 Common Probability Mass Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394 16.1.1 Bernoulli Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394 16.1.2 Binomial Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395 16.1.3 Poisson Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 16.1.4 Other Mass Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404 16.2 Common Probability Density Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404 16.2.1 Uniform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405 16.2.2 Normal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410 16.2.3 Student’s t-Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 16.2.4 Exponential . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422 16.2.5 Chi-Squared . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426 16.2.6 F-Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428 16.2.7 Other Density Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431 Contents in Detail xvii
Page
20
PART IV STATISTICAL TESTING AND MODELING 17 SAMPLING DISTRIBUTIONS AND CONFIDENCE 435 17.1 Sampling Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435 17.1.1 For a Sample Mean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436 17.1.2 For a Sample Proportion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 17.1.3 For Other Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 17.2 Confidence Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446 17.2.1 An Interval for a Mean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446 17.2.2 An Interval for a Proportion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 17.2.3 Other Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450 17.2.4 Comments on the Interpretation of a CI . . . . . . . . . . . . . . . . . . . . . . . . . . . 451 18 HYPOTHESIS TESTING 455 18.1 Components of a Hypothesis Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 18.1.1 Hypotheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456 18.1.2 Test Statistic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 18.1.3 p-Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 18.1.4 Significance Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 18.1.5 Criticisms of Hypothesis Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 18.2 Testing Means . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 18.2.1 Single Mean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459 18.2.2 Two Means . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462 18.3 Testing Proportions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 18.3.1 Single Proportion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 18.3.2 Two Proportions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476 18.4 Testing Categorical Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 18.4.1 Single Categorical Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481 18.4.2 Two Categorical Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484 18.5 Errors and Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489 18.5.1 Hypothesis Test Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489 18.5.2 Type I Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490 18.5.3 Type II Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 18.5.4 Statistical Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497 19 ANALYSIS OF VARIANCE 503 19.1 One-Way ANOVA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503 19.1.1 Hypotheses and Diagnostic Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504 19.1.2 Table Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506 19.1.3 ANOVA Table Construction with the aov Function . . . . . . . . . . . . . . . . . 508 xviii Contents in Detail
The above is a preview of the first 20 pages. Register to read the complete e-book.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
【One-Line Pitch】
A comprehensive, beginner-friendly introduction to R that takes you from your first line of code to statistical modeling and advanced data visualization, ideal for students, researchers, and self-learners with little or no programming background.
【Book Arc】
- **Opening (~0%–9%)**: Covers the book's front matter, including the preface, introduction, and table of contents, establishing the book's scope as a full course in R programming and statistics, with a brief history of R and what's new in the second edition.
- **Early (~18%–32%)**: Dives into the fundamentals of the R language—installation, workflow basics, arithmetic, vectors, matrices, arrays, and non-numeric values like logicals and dates—building the core data structures you'll use throughout.
- **Middle (~41%–50%)**: Expands into more complex data structures (lists, data frames), special values (Inf, NaN, NA, NULL), classes and coercion, and introduces basic plotting with both base R and the ggplot2 package.
- **Middle (~55%)**: Covers file I/O—reading and writing datasets, spreadsheets, web-based files, and ad hoc object serialization—plus the start of the programming section, focusing on function calls, scoping, and argument matching.
- **Late (~55%–end, inferred from TOC)**: Continues with programming constructs (control flow, functions), then transitions into statistical methods (hypothesis testing, ANOVA, regression) and advanced graphics (customization, grammar of graphics, 3D plots), ending with appendices on installation and RStudio.
【Key Takeaways】
- **R is a language built for statistics** (Early): Unlike general-purpose languages, R's core data structures—vectors, matrices, and data frames—are designed to make statistical analysis and data manipulation intuitive from the start.
- **Mastering data structures is the gateway to R** (Early): Vectors, matrices, arrays, lists, and data frames each have their own creation, subsetting, and operation rules; understanding these is essential before any meaningful analysis.
- **Special values are first-class citizens** (Middle): R handles missing data (NA), undefined results (NaN), infinity (Inf), and empty objects (NULL) explicitly, and knowing how to manage them prevents silent errors in analysis.
- **Coercion and classes control behavior** (Middle): R's type system uses classes and attributes to determine how functions behave; the `is.*` and `as.*` functions let you check and convert between types, a key skill for debugging.
- **Plotting is built-in and extensible** (Middle): Base R graphics provide quick, customizable plots, while ggplot2 offers a powerful grammar-of-graphics approach for more polished, layered visualizations.
- **File I/O is practical and varied** (Middle): R can read and write table formats, spreadsheets, web-based files, and native R objects, making it easy to bring external data in and share results out.
- **Programming fundamentals underpin everything** (Middle): Scoping, environments, and argument matching are not just theoretical—they determine how your functions behave and how R resolves names, which is critical for writing robust code.
【Reading Tips】
- **Skim the front matter** (~0%–9%): The preface and introduction are useful for context but not essential; jump straight to Chapter 1 once you're ready to install R and start coding.
- **Deep-read the data structure chapters** (~18%–32%): Chapters 2–5 are the foundation; work through every example and exercise, as later material assumes fluency with vectors, matrices, and data frames.
- **Treat Chapter 6 as a reference** (Middle): Special values and coercion are concepts you'll encounter repeatedly; skim initially, then return when you hit errors or unexpected behavior.
- **Practice plotting early** (Middle): Chapter 7 introduces both base R and ggplot2; don't skip either—base R is essential for quick checks, while ggplot2 is the industry standard for publication-quality figures.
- **Use the appendices as needed**: Appendix A (installing R and packages) and Appendix B (RStudio) are practical references; consult them when setting up your environment rather than reading them cover-to-cover.
【Coverage Limits】
This guide synthesizes the book's structure and early-to-middle content based on the provided excerpts; detailed coverage of later chapters (programming constructs, statistical tests, regression, and advanced graphics) is inferred from the table of contents and not directly excerpted.
Passage locations
Page 6
ustomer service inquiries, please contact info@nostarch.com. For information on distribution, bulk sales, corporate sales, or translations: sales@nostarch.co...
View in text
Page 11
thesis Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 Chapter 19: Analysis of Vari...
View in text
Page 11
. . . . . . . . . . 19 2.3 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....
View in text
Page 15
Not a Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 6.1.3 NA: Not Available . . . . . . . . . . . . . . ....
View in text
Recommended for You
{{#thumbnailUrl}}
{{/thumbnailUrl}}
{{^thumbnailUrl}}
{{/thumbnailUrl}}
Loading recommended books...
Failed to load, please try again later
Tip the Site
Scan the WeChat Pay or Alipay code to tip. No login required.
WeChat Pay
Alipay