ebook img

R companion to elementary applied statistics PDF

377 Pages·2019·5.24 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview R companion to elementary applied statistics

R Companion to Elementary Applied Statistics R Companion to Elementary Applied Statistics Christopher Hay-Jahans CRCPress Taylor&FrancisGroup 6000BrokenSoundParkwayNW,Suite300 BocaRaton,FL33487-2742 (cid:2)c 2019byTaylor&FrancisGroup,LLC CRCPressisanimprintofTaylor&FrancisGroup,anInformabusiness NoclaimtooriginalU.S.Governmentworks Printedonacid-freepaper InternationalStandardBookNumber-13:978-1-138-32916-4(Paperback) 978-1-138-32925-6(Hardback) Thisbookcontainsinformationobtainedfromauthenticandhighlyregardedsources.Rea- sonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the conse- quences of their use. The authors and publishers have attempted to trace the copyright holdersofallmaterialreproducedinthispublicationandapologizetocopyrightholdersif permissiontopublishinthisformhasnotbeenobtained.Ifanycopyrightmaterialhasnot beenacknowledgedpleasewriteandletusknowsowemayrectifyinanyfuturereprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means,nowknownorhereafterinvented,includingphotocopying,microfilming,andrecord- ing,orinanyinformationstorageorretrievalsystem,withoutwrittenpermissionfromthe publishers. For permission to photocopy or use material electronically from this work, please access www.copyright.com(http://www.copyright.com/)orcontacttheCopyrightClearanceCen- ter, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not- for-profit organization that provides licenses and registration for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate system ofpaymenthasbeenarranged. Trademark Notice:Productorcorporatenamesmaybetrademarksorregisteredtrade- marks,andareusedonlyforidentificationandexplanationwithoutintenttoinfringe. Library of Congress Cataloging-in-Publication Data Names:Hay-Jahans,Christopher,author. Title: R companion to elementary applied statistics / Christopher Hay- Jahans. Othertitles:Companiontoelementaryappliedstatistics. Description:BocaRaton:CRCPress,Taylor&FrancisGroup,2018.| Includesbibliographicalreferencesandindex. Identifiers:LCCN2018030262|ISBN9781138329164(pbk.). Subjects: LCSH: Statistics–Data processing. | R (Computer program lan- guage). Classification:LCCQA276.45.R3H392018|DDC519.50285/5133--dc23 LCrecordavailableathttps://lccn.loc.gov/2018030262 Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com To my family for their support and patience, and to my students who inspire me to learn new ideas through their curiosity, enthusiasm, and willingness to learn. Contents Preface xv 1 Preliminaries 1 1.1 First Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Running Code in R . . . . . . . . . . . . . . . . . . . . . . . 1 1.3 Some Terminology . . . . . . . . . . . . . . . . . . . . . . . . 5 1.4 Hierarchy of Data Classes . . . . . . . . . . . . . . . . . . . . 7 1.5 Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.6 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.7 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.8 R Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.9 Probability Distributions . . . . . . . . . . . . . . . . . . . . 15 1.10 Coding Conventions . . . . . . . . . . . . . . . . . . . . . . . 15 1.11 Some Bookkeeping and Other Tips . . . . . . . . . . . . . . 18 1.12 Getting Quick Coding Help . . . . . . . . . . . . . . . . . . . 20 2 Bringing Data Into and Out of R 21 2.1 Entering Data Through Coding . . . . . . . . . . . . . . . . 21 2.2 Number and Sample Generating Tricks . . . . . . . . . . . . 25 2.3 The R Data Editor . . . . . . . . . . . . . . . . . . . . . . . 28 2.4 Reading Text Files . . . . . . . . . . . . . . . . . . . . . . . . 29 2.5 Reading Data from Other File Formats . . . . . . . . . . . . 33 2.6 Reading Data from the Keyboard . . . . . . . . . . . . . . . 35 2.7 Saving and Exporting Data . . . . . . . . . . . . . . . . . . . 36 3 Accessing Contents of Data Structures 39 3.1 Extracting Data from Vectors . . . . . . . . . . . . . . . . . 39 3.2 Conducting Data Searches in Vectors . . . . . . . . . . . . . 41 3.3 Working with Factors . . . . . . . . . . . . . . . . . . . . . . 44 3.4 Navigating Data Frames . . . . . . . . . . . . . . . . . . . . 45 3.5 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3.6 Choosing an Access/Extraction Method . . . . . . . . . . . . 49 3.7 Additional Notes . . . . . . . . . . . . . . . . . . . . . . . . . 49 3.7.1 More About the attach Function. . . . . . . . . . . . 49 3.7.2 About Functions and Their Arguments . . . . . . . . 50 3.7.3 Alternative Argument Assignments in Function Calls. 52 vii viii Contents 4 Altering and Manipulating Data 53 4.1 Altering Entries in Vectors . . . . . . . . . . . . . . . . . . . 53 4.2 Transformations . . . . . . . . . . . . . . . . . . . . . . . . . 55 4.3 Manipulating Character Strings . . . . . . . . . . . . . . . . 56 4.4 Sorting Vectors and Factors . . . . . . . . . . . . . . . . . . 58 4.5 Altering Data Frames . . . . . . . . . . . . . . . . . . . . . . 60 4.6 Sorting Data Frames . . . . . . . . . . . . . . . . . . . . . . 63 4.7 Moving Between Lists and Data Frames . . . . . . . . . . . . 65 4.8 Additional Notes on the merge Function . . . . . . . . . . . 67 5 Summaries and Statistics 69 5.1 Univariate Frequency Distributions . . . . . . . . . . . . . . 69 5.2 Bivariate Frequency Distributions . . . . . . . . . . . . . . . 72 5.3 Statistics for Univariate Samples . . . . . . . . . . . . . . . . 73 5.3.1 Measures of Central Tendency . . . . . . . . . . . . . 73 5.3.2 Measures of Spread. . . . . . . . . . . . . . . . . . . . 76 5.3.3 Measures of Position . . . . . . . . . . . . . . . . . . . 77 5.3.4 Measures of Shape . . . . . . . . . . . . . . . . . . . . 81 5.4 Five-Number Summaries and Outliers . . . . . . . . . . . . . 85 5.4.1 Elementary Five-Number Summary . . . . . . . . . . 86 5.4.2 Tukey’s Five-Number Summary . . . . . . . . . . . . . 87 5.4.3 The boxplot.stats Function . . . . . . . . . . . . . . 88 6 More on Computing with R 89 6.1 Computing with Numeric Vectors . . . . . . . . . . . . . . . 89 6.2 Working with Lists, Data Frames, and Arrays . . . . . . . . 91 6.2.1 The sapply Function . . . . . . . . . . . . . . . . . . 91 6.2.2 The tapply Function . . . . . . . . . . . . . . . . . . 93 6.2.3 The by Function . . . . . . . . . . . . . . . . . . . . . 95 6.2.4 The aggregate Function . . . . . . . . . . . . . . . . 96 6.2.5 The apply Function . . . . . . . . . . . . . . . . . . . 98 6.2.6 The sweep Function . . . . . . . . . . . . . . . . . . . 99 6.3 For-Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 6.4 Conditional Statements and the switch Function . . . . . . 103 6.4.1 The if-then Statement . . . . . . . . . . . . . . . . . . 104 6.4.2 The if-then-else Statement . . . . . . . . . . . . . . . . 106 6.4.3 The switch Function . . . . . . . . . . . . . . . . . . 107 6.5 Preparing Your Own Functions . . . . . . . . . . . . . . . . . 108 7 Basic Charts for Categorical Data 111 7.1 Preliminary Comments . . . . . . . . . . . . . . . . . . . . . 111 7.2 Bar Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 7.3 Dot Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 7.4 Pie Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 7.5 Exporting Graphics Images . . . . . . . . . . . . . . . . . . . 123 Contents ix 7.6 Additional Notes . . . . . . . . . . . . . . . . . . . . . . . . . 125 7.6.1 Customizing Plotting Windows . . . . . . . . . . . . . 125 7.6.2 The plot.new and plot.window Functions . . . . . . 126 7.6.3 More on the paste Function . . . . . . . . . . . . . . 127 7.6.4 The title Function . . . . . . . . . . . . . . . . . . . 127 7.6.5 More on the legend Function . . . . . . . . . . . . . . 128 7.6.6 More on the mtext Function . . . . . . . . . . . . . . 128 7.6.7 The text Function . . . . . . . . . . . . . . . . . . . . 129 8 Basic Plots for Numeric Data 131 8.1 Histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 8.2 Boxplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 8.3 Stripcharts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 8.4 QQ-Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 8.4.1 Normal Probability QQ-Plots . . . . . . . . . . . . . . 147 8.4.2 Interpreting Normal Probability QQ-Plots . . . . . . . 149 8.4.3 More on Reference Lines for QQ-Plots . . . . . . . . . 151 8.4.4 QQ-Plots for Other Distributions . . . . . . . . . . . . 152 8.5 Additional Notes . . . . . . . . . . . . . . . . . . . . . . . . . 153 8.5.1 More on the ifelse Function . . . . . . . . . . . . . . 153 8.5.2 Revisiting the axis Function . . . . . . . . . . . . . . 154 8.5.3 Frequency Polygons and Ogives . . . . . . . . . . . . . 157 9 Scatterplots, Lines, and Curves 159 9.1 Scatterplots . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 9.1.1 Basic Plots . . . . . . . . . . . . . . . . . . . . . . . . 160 9.1.2 Manipulating Plotting Characters . . . . . . . . . . . 163 9.1.3 Plotting Transformed Data . . . . . . . . . . . . . . . 163 9.1.4 Matrix Scatterplots . . . . . . . . . . . . . . . . . . . 165 9.1.5 The matplot Function . . . . . . . . . . . . . . . . . . 166 9.2 Graphs of Lines . . . . . . . . . . . . . . . . . . . . . . . . . 169 9.3 Graphs of Curves . . . . . . . . . . . . . . . . . . . . . . . . 170 9.4 Superimposing Multiple Lines and/or Curves . . . . . . . . . 174 9.5 Time-Series Plots . . . . . . . . . . . . . . . . . . . . . . . . 177 10 More Graphics Tools 179 10.1 Partitioning Graphics Windows . . . . . . . . . . . . . . . . 179 10.1.1 The layout Function . . . . . . . . . . . . . . . . . . 179 10.1.2 The split.screen Function . . . . . . . . . . . . . . 181 10.2 Customizing Plotted Text and Symbols . . . . . . . . . . . . 185 10.3 Inserting Mathematical Annotation in Plots . . . . . . . . . 187 10.4 More Low-Level Graphics Functions . . . . . . . . . . . . . . 188 10.4.1 The points and symbols Functions . . . . . . . . . . 189 10.4.2 The grid, segments, andarrows Functions . . . . . . 189 10.4.3 Boxes, Rectangles, and Polygons . . . . . . . . . . . . 191

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.