BOOKS FOR PROFESSIONALS BY PROFESSIONALS® Wiley Pace Beginning R RELATED Beginning R, Second Edition is a hands-on book showing how to use the R language, write and save R scripts, read in data files, and write custom statistical functions as well as use built in functions. This book shows the use of R in specific cases such as one-way ANOVA analysis, linear and logistic regression, data visualization, parallel processing, bootstrapping, and more. It takes a hands-on, example-based approach incorporating best practices with clear explanations of the statistics being done. It has been completely re-written since the first edition to make use of the latest packages and features in R version 3. R is a powerful open-source language and programming environment for statistics and has become the de facto standard for doing, teaching, and learning computational statistics. R is both an object-oriented language and a functional language that is easy to learn, easy to use, and completely free. A large community of dedicated R users and programmers provides an excellent source of R code, functions, and data sets, with a constantly evolving ecosystem of packages providing new functionality for data analysis. R has also become popular in commercial use at companies such as Microsoft, Google, and Oracle. Your investment in learning R is sure to pay off in the long term as R continues to grow into the go to language for data analysis and research. • How to acquire and install R • Hot to import and export data and scripts • How to analyze data and generate graphics • How to program in R to write custom functions • Hot to use R for interactive statistical explorations • How to conduct bootstrapping and other advanced techniques US $39.99 Shelve in: ISBN 978-1-4842-0374-3 53999 Programming Languages/General User level: Beginning–Intermediate SECOND SOURCE CODE ONLINE 9781484203743 EDITION www.apress.com www.it-ebooks.info Beginning R An Introduction to Statistical Programming Second Edition Dr. Joshua F. Wiley Larry A. Pace www.it-ebooks.info Beginning R Copyright © 2015 by Dr. Joshua F. Wiley and the estate of Larry A. Pace This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher's location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. ISBN-13 (pbk): 978-1-4842-0374-3 ISBN-13 (electronic): 978-1-4842-0373-6 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director: Welmoed Spahr Lead Editor: Steve Anglin Technical Reviewer: Sarah Stowell Editorial Board: Steve Anglin, Louise Corrigan, Jonathan Gennick, Robert Hutchinson, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing, Steve Weiss Coordinating Editor: Mark Powers Copy Editor: Lori Jacobs Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springer.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this text is available to readers at www.apress.com/9781484203743. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/. Readers can also access source code at SpringerLink in the Supplementary Material section for each chapter. www.it-ebooks.info To Family. www.it-ebooks.info Contents at a Glance About the Author �����������������������������������������������������������������������������������������������������xv In Memoriam ���������������������������������������������������������������������������������������������������������xvii About the Technical Reviewer ��������������������������������������������������������������������������������xix Acknowledgments ��������������������������������������������������������������������������������������������������xxi Introduction ����������������������������������������������������������������������������������������������������������xxiii ■ Chapter 1: Getting Star ted �������������������������������������������������������������������������������������1 ■ Chapter 2: Dealing with Dates, Strings, and Data Frames �����������������������������������15 ■ Chapter 3: Input and Output ��������������������������������������������������������������������������������27 ■ Chapter 4: Control Structures ������������������������������������������������������������������������������35 ■ Chapter 5: Functional Programming �������������������������������������������������������������������43 ■ Chapter 6: Probability Distributions ��������������������������������������������������������������������53 ■ Chapter 7: Working with Tables ���������������������������������������������������������������������������67 ■ Chapter 8: Descriptive Statistics and Exploratory Data Analysis ������������������������73 ■ Chapter 9: Working with Graphics �����������������������������������������������������������������������81 ■ Chapter 10: Traditional Statistical Methods ��������������������������������������������������������93 ■ Chapter 11: Modern Statistical Methods �����������������������������������������������������������101 ■ Chapter 12: Analysis of Variance ����������������������������������������������������������������������111 ■ Chapter 13: Correlation and Regression ������������������������������������������������������������121 ■ Chapter 14: Multiple Regression �����������������������������������������������������������������������139 ■ Chapter 15: Logistic Regression ������������������������������������������������������������������������163 v www.it-ebooks.info ■ Contents at a GlanCe ■ Chapter 16: Modern Statistical Methods II ��������������������������������������������������������193 ■ Chapter 17: Data Visualization Cookbook ���������������������������������������������������������215 ■ Chapter 18: High-Performance Computing ��������������������������������������������������������279 ■ Chapter 19: Text Mining�������������������������������������������������������������������������������������303 Index ���������������������������������������������������������������������������������������������������������������������321 vi www.it-ebooks.info Contents About the Author �����������������������������������������������������������������������������������������������������xv In Memoriam ���������������������������������������������������������������������������������������������������������xvii About the Technical Reviewer ��������������������������������������������������������������������������������xix Acknowledgments ��������������������������������������������������������������������������������������������������xxi Introduction ����������������������������������������������������������������������������������������������������������xxiii ■ Chapter 1: Getting Star ted �������������������������������������������������������������������������������������1 1.1 What is R, Anyway? ....................................................................................................1 1.2 A First R Session ........................................................................................................3 1.3 Your Second R Session ...............................................................................................6 1.3.1 Working with Indexes ....................................................................................................................6 1.3.2 Representing Missing Data in R ....................................................................................................7 1.3.3 Vectors and Vectorization in R .......................................................................................................8 1.3.4 A Brief Introduction to Matrices .....................................................................................................9 1.3.5 More on Lists ...............................................................................................................................11 1.3.6 A Quick Introduction to Data Frames ...........................................................................................12 ■ Chapter 2: Dealing with Dates, Strings, and Data Frames �����������������������������������15 2.1 Working with Dates and Times .................................................................................15 2.2 Working with Strings ................................................................................................16 2.3 Working with Data Frames in the Real World ...........................................................18 2.3.1 Finding and Subsetting Data .......................................................................................................19 2.4 Manipulating Data Structures ..................................................................................21 2.5 The Hard Work of Working with Larger Datasets ......................................................22 vii www.it-ebooks.info ■ Contents ■ Chapter 3: Input and Output ��������������������������������������������������������������������������������27 3.1 R Input ......................................................................................................................27 3.1.1 The R Editor .................................................................................................................................28 3.1.2 The R Data Editor .........................................................................................................................29 3.1.3 Other Ways to Get Data Into R .....................................................................................................30 3.1.4 Reading Data from a File .............................................................................................................31 3.1.5 Getting Data from the Web ..........................................................................................................31 3.2 R Output ...................................................................................................................33 3.2.1 Saving Output to a File ................................................................................................................33 ■ Chapter 4: Control Structures ������������������������������������������������������������������������������35 4.1 Using Logic ...............................................................................................................35 4.2 Flow Control .............................................................................................................36 4.2.1 Explicit Looping ...........................................................................................................................36 4.2.2 Implicit Looping ...........................................................................................................................38 4.3 If, If-Else, and ifelse( ) Statements ............................................................................41 ■ Chapter 5: Functional Programming �������������������������������������������������������������������43 5.1 Scoping Rules ..........................................................................................................44 5.2 Reserved Names and Syntactically Correct Names .................................................45 5.3 Functions and Arguments ........................................................................................46 5.4 Some Example Functions .........................................................................................47 5.4.1 Guess the Number .......................................................................................................................47 5.4.2 A Function with Arguments..........................................................................................................48 5.5 Classes and Methods ...............................................................................................49 5.5.1 S3 Class and Method Example ....................................................................................................49 5.5.2 S3 Methods for Existing Classes .................................................................................................50 viii www.it-ebooks.info ■ Contents ■ Chapter 6: Probability Distributions ��������������������������������������������������������������������53 6.1 Discrete Probability Distributions .............................................................................53 6.2 The Binomial Distribution .........................................................................................54 6.2.1 The Poisson Distribution ..............................................................................................................57 6.2.2 Some Other Discrete Distributions ..............................................................................................58 6.3 Continuous Probability Distributions ........................................................................58 6.3.1 The Normal Distribution ...............................................................................................................58 6.3.2 The t Distribution .........................................................................................................................61 6.3.3 The t distribution ..........................................................................................................................63 6.3.4 The Chi-Square Distribution.........................................................................................................64 References .....................................................................................................................65 ■ Chapter 7: Working with Tables ���������������������������������������������������������������������������67 7.1 Working with One-Way Tables ..................................................................................67 7.2 Working with Two-Way Tables ..................................................................................71 ■ Chapter 8: Descriptive Statistics and Exploratory Data Analysis ������������������������73 8.1 Central Tendency ......................................................................................................73 8.1.1 The Mean .....................................................................................................................................73 8.1.2 The Median ..................................................................................................................................74 8.1.3 The Mode .....................................................................................................................................75 8.2 Variability ..................................................................................................................76 8.2.1 The Range ....................................................................................................................................76 8.2.2 The Variance and Standard Deviation ..........................................................................................77 8.3 Boxplots and Stem-and-Leaf Displays .....................................................................78 8.4 Using the fBasics Package for Summary Statistics .................................................79 References .....................................................................................................................80 ix www.it-ebooks.info ■ Contents ■ Chapter 9: Working with Graphics �����������������������������������������������������������������������81 9.1 Creating Effective Graphics ......................................................................................81 9.2 Graphing Nominal and Ordinal Data .........................................................................82 9.3 Graphing Scale Data .................................................................................................84 9.3.1 Boxplots Revisited .......................................................................................................................84 9.3.2 Histograms and Dotplots .............................................................................................................86 9.3.3 Frequency Polygons and Smoothed Density Plots .......................................................................87 9.3.4 Graphing Bivariate Data ...............................................................................................................89 References .....................................................................................................................92 ■ Chapter 10: Traditional Statistical Methods ��������������������������������������������������������93 10.1 Estimation and Confidence Intervals ......................................................................93 10.1.1 Confidence Intervals for Means .................................................................................................93 10.1.2 Confidence Intervals for Proportions .........................................................................................94 10.1.3 Confidence Intervals for the Variance ........................................................................................95 10.2 Hypothesis Tests with One Sample ........................................................................96 10.3 Hypothesis Tests with Two Samples .......................................................................98 References ...................................................................................................................100 ■ Chapter 11: Modern Statistical Methods �����������������������������������������������������������101 11.1 The Need for Modern Statistical Methods ............................................................101 11.2 A Modern Alternative to the Traditional t Test .......................................................102 11.3 Bootstrapping .......................................................................................................104 11.4 Permutation Tests .................................................................................................107 References ...................................................................................................................109 x www.it-ebooks.info
Description: