TT HH EE BB OO OO KK OO FF RR • 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 S T A T I S T I C S A N D • • T I L M A N M . D A V I E S • • THE BOOK OF R T H E B O O K O F R A First Course in Programming and Statistics by Tilman M. Davies San Francisco THE BOOK OF R. Copyright © 2016 by Tilman M. Davies. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Printed in USA First printing 20 19 18 17 16 1 2 3 4 5 6 7 8 9 ISBN-10: 1-59327-651-6 ISBN-13: 978-1-59327-651-5 Publisher: William Pollock Production Editor: Riley Hoffman Cover Illustration: Josh Ellingson Interior Design: Octopod Studios Developmental Editor: Liz Chadwick Technical Reviewer: Debbie Leader Copyeditor: Kim Wimpsett Compositor: Riley Hoffman Proofreader: Paula Fleming Indexer: BIM Creatives, LLC For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 415.863.9900; [email protected] www.nostarch.com Library of Congress Cataloging-in-Publication Data Names: Davies, Tilman M., author. Title: The book of R : a first course in programming and statistics / by Tilman M. Davies. Description: San Francisco : No Starch Press, [2016] | Includes bibliographical references and index. Identifiers: LCCN 2015035305| ISBN 9781593276515 | ISBN 1593276516 Subjects: LCSH: R (Computer program language) | Computer programming. | Statistics--Data processing. Classification: LCC QA76.73.R3 D38 2016 | DDC 519.50285--dc23 LC record available at http://lccn.loc.gov/2015035305 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it. Prof. Dr. Dr. JochenWeniger,1925–2015 EinWissenschaftler. MeinOpa. BRIEF CONTENTS Preface............................................................................. xxiii Acknowledgments................................................................... xxv Introduction.........................................................................xxvii PARTI:THELANGUAGE Chapter1:GettingStarted ........................................................... 3 Chapter2:Numerics,Arithmetic,Assignment,andVectors.............................. 17 Chapter3:MatricesandArrays ...................................................... 39 Chapter4:Non-numericValues....................................................... 59 Chapter5:ListsandDataFrames ..................................................... 89 Chapter6:SpecialValues,Classes,andCoercion......................................103 Chapter7:BasicPlotting.............................................................127 Chapter8:ReadingandWritingFiles.................................................147 PARTII:PROGRAMMING Chapter9:CallingFunctions .........................................................165 Chapter10:ConditionsandLoops....................................................179 Chapter11:WritingFunctions........................................................215 Chapter12:Exceptions,Timings,andVisibility.........................................241 PARTIII:STATISTICSANDPROBABILITY Chapter13:ElementaryStatistics .....................................................261 Chapter14:BasicDataVisualization .................................................289 Chapter15:Probability..............................................................309 Chapter16:CommonProbabilityDistributions .........................................331 PARTIV:STATISTICALTESTINGANDMODELING Chapter17:SamplingDistributionsandConfidence....................................367 Chapter18:HypothesisTesting.......................................................385 Chapter19:AnalysisofVariance.....................................................435 Chapter20:SimpleLinearRegression.................................................451 Chapter21:MultipleLinearRegression................................................485 Chapter22:LinearModelSelectionandDiagnostics ...................................527 PARTV:ADVANCEDGRAPHICS Chapter23:AdvancedPlotCustomization.............................................575 Chapter24:GoingFurtherwiththeGrammarofGraphics ..............................609 Chapter25:DefiningColorsandPlottinginHigherDimensions..........................631 Chapter26:Interactive3DPlots ......................................................691 AppendixA:Installing R andContributedPackages....................................737 AppendixB:WorkingwithRStudio ...................................................751 ReferenceList .......................................................................761 Index...............................................................................767 viii BriefContents