R recipes a problem solution approach

253 45 0
R recipes a problem solution approach

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info For your convenience Apress has placed some of the front matter material after the index Please use the Bookmarks and Contents at a Glance links to access them www.it-ebooks.info Contents at a Glance About the Author���������������������������������������������������������������������������������������������������������������xiii About the Technical Reviewer�������������������������������������������������������������������������������������������� xv Acknowledgments������������������������������������������������������������������������������������������������������������ xvii Introduction����������������������������������������������������������������������������������������������������������������������� xix ■■Chapter 1: Migrating to R: As Easy As 1, 2, 3��������������������������������������������������������������������1 ■■Chapter 2: Input and Output��������������������������������������������������������������������������������������������17 ■■Chapter 3: Data Structures����������������������������������������������������������������������������������������������27 ■■Chapter 4: Merging and Reshaping Datasets������������������������������������������������������������������43 ■■Chapter 5: Working with Dates and Strings��������������������������������������������������������������������57 ■■Chapter 6: Working with Tables���������������������������������������������������������������������������������������67 ■■Chapter 7: Summarizing and Describing Data����������������������������������������������������������������79 ■■Chapter 8: Graphics and Data Visualization��������������������������������������������������������������������89 ■■Chapter 9: Probability Distributions������������������������������������������������������������������������������107 ■■Chapter 10: Hypothesis Tests for Means, Ranks, or Proportions����������������������������������117 ■■Chapter 11: Relationships Between and Among Variables��������������������������������������������143 ■■Chapter 12: Contemporary Statistical Methods������������������������������������������������������������157 ■■Chapter 13: Writing Reusable Functions�����������������������������������������������������������������������167 iii www.it-ebooks.info ■ Contents at a Glance ■■Chapter 14: Working with Financial Data����������������������������������������������������������������������183 ■■Chapter 15: Dealing with Big Data��������������������������������������������������������������������������������201 ■■Chapter 16: Mining the Gold in Data and Text���������������������������������������������������������������215 Index���������������������������������������������������������������������������������������������������������������������������������237 iv www.it-ebooks.info Introduction R is an open source implementation of the programming language S, created at Bell Laboratories by John Chambers, Rick Becker, and Alan Wilks In addition to R, S is the basis of the commercially available S-PLUS system Widely recognized as the chief architect of S, Chambers in 1998 won the prestigious Software System Award from the Association for Computing Machinery, which said Chambers’ design of the S system “forever altered how people analyze, visualize, and manipulate data.” Think of R as an integrated system or environment that allows users multiple ways to access its many functions and features You can use R as an interactive command-line interpreted language, much like a calculator Type a command, press Enter, and R provides the answer in the R console R is simultaneously a functional language and an object-oriented language In addition to thousands of contributed packages, R has programming features, just as all computer programming languages do, allowing conditionals and looping, and giving the user the facility to create custom functions and specify various input and output options R is widely used as a statistical computing and software environment, but the R Core Team would rather consider R an environment “within which many classical and modern statistical techniques have been implemented.” In addition to its statistical prowess, R provides impressive and flexible graphics capabilities Many users are attracted to R primarily because of its graphical features R has basic and advanced plotting functions with many customization features Chambers and others at Bell Labs were developing S while I was in college and grad school, and of course I was completely oblivious to that fact, even though my major professor and I were consulting with another AT&T division at the time I began my own statistical software journey writing programs in Fortran I might find that a given program did not have a particular analysis I needed, such as a routine for calculating an intraclass correlation, so I would write my own program BMDP and SAS were available in batch versions for mainframe computers when I was in graduate school—one had to learn Job Control Language (JCL) in order to tell the computer which tapes to load I typed punch cards and used a card reader to read in JCL and data On a much larger and very much more sophisticated scale, this is essentially why the computer scientists at Bell Labs created S (for statistics) Fortran was and still is a general-purpose language, but it did not have many statistical capabilities The design of S began with an informal meeting in 1976 at Bell Labs to discuss the design of a high-level language with an “algorithm,” which meant a Fortran-callable subroutine Like its predecessor S, R can easily and transparently access compiled code from various other languages, including Fortran and C++ among others R can also be interfaced with a variety of other programs, such as Python and SPSS R works in batch mode, but its most popular use is as an interactive data analysis, calculation, and graphics system running in a windowing system R works on Linux, PC, and Mac systems Be forewarned that R is not a point-and-click graphical user interface (GUI) program such as SPSS or Minitab Unlike these programs, R provides terse output, but can be queried for more information should you need it In this book, you will see screen captures of R running in the Windows operating system According to my friend and colleague, computer scientist and bioinformatics expert Dr Nathan Goodman, statistical analysis essentially boils down to four empirical problems: problems involving description, problems involving differences, problems involving relationships, and problems involving classification I agree wholeheartedly with Nat All the problems and solutions presented in this book fall into one or more of those general categories The problems are manifold, but the solutions are mostly limited to these four situations xix www.it-ebooks.info ■ Introduction What this Book Covers This book is for anyone—business professional, programmer, statistician, teacher, or student—who needs to find a way to use R to solve practical problems Readers who have solved or attempted problems similar to the ones in this book using other tools will readily concur that each tool in one’s toolbox works better for some problems than for others R novices will find best practices for using R’s features effectively Intermediate-to-advanced R users and programmers will find shortcuts and applications that they may not have considered, as well as different ways to things they might want to The Structure of this Book The standardized format will make this a useful book for future reference Unlike most other books, you not have to start at the beginning and go through this book sequentially Each chapter is a stand-alone lesson that starts with a typical problem (most of which come from true-life problems that I have faced, or ones that others have described and have given me permission to share) The datasets used with this book to illustrate the solutions should be similar to the datasets readers have worked with, or would like to work with Apart from a few contrived examples in the early chapters, most of the datasets and exercises come from real-world problems and data Following a bit of background, the problem and the data are presented, and then readers learn one efficient way to solve the problem using R Similar problems will quickly come to mind, and readers will be able to adapt what they learn here to those problems Conventions Used in this Book In this book, code and script segments will be shown this way: > x px dist The following section will show you how to take R for a quick spin Okay, So I Have R What’s Next? Whether you are a programmer or a statistician, or like me, a little of both, R takes some getting used to Most statistics programs, such as SPSS, separate the data, the syntax (programming language), and the output R takes a minimalist stance on this If you are not using something, it is not visible to you If you need to use something, either you must open it, as in the R Editor for writing and saving R scripts, or R will open it for you, as in the R Graphics Device when you generate a histogram or some other graphic output So, let’s see how to get around in the R interface A quick glance shows that the R interface is not particularly fancy, but it is highly functional Examine the options available to you in the menu bar and the icon bar R opens with the welcome screen shown in Figure 1-2 You can keep that if you like (I like it), or simply press Ctrl+L or select Edit ➤ Clear Console to clear the console You will be working in the R Console most of the time, but you can open a window with a simple text editor for writing scripts and functions Do this by selecting File ➤ New script The built-in R Editor is convenient for writing longer scripts and functions, but also simply for writing R commands and editing them before you run them Many R users prefer to use the text editor of their liking For Windows users, Notepad is fine When you produce a graphic object, the R Graphics Device will open The R GUI (graphical user interface) is completely customizable as well Although we are showing R running in the R Console, you should be aware that there are several integrated development environments (IDEs) for R One of the best of these is RStudio www.it-ebooks.info ■ index mode() function, 81 Modern portfolio theory (MPT) computational finance, 198 constituent assets, 196 description, 196 global minimum variance portfolio, 199 Multiple regression academic and motivational variables, 152–153 confidence interval, interpretation, 155 lm() function, 153, 155 multiple linear regression model, 152 predictors, 154 regression coefficients, 152 MyPieLife, 61 Nominal and ordinal data, 120–123 Pearson’s product-moment correlation, 150 with Spearman’s Rho, 151 Pie chart representation argument col = FALSE, 90 copy, save and print graphics, 91 description, 90 graphical representations, 90 par() function, 90 Plastic Omnium’s environmental policy, 24 PMF See Probability mass function (PMF) Poisson probabilities characteristics, 110 description, 110 probabilities of events, 111 prettyR, 84–85 Probability mass function (PMF), 109 p values, 112 p values for F distribution, 113–115 „„         O „„         Q One-sample tests description, 117 for means, 118–120 nominal and ordinal data (see Nominal and ordinal data) One-way tests, 133–136 Oscars, 79 Quantiles problem, 87 solution, 87–88 „„         N „„         P Parallel R clusters, 206 doParallel package, 204 doSNOW package works, 207 embarrassingly parallel\perfectly parallel processing, 201 master processor, 201 message passing interface (MRI), 202 parallel computing, 202 proof of concept, 204 purposes, 206 quad-core processor, 205 snow package, 203 standard network name, 202–203 toy example, 205 vectorized operations and functions, 205 Pearson’s product-moment correlation, 145, 148, 150, 152 Permutation tests, 165–166 phi coefficient and chi-square, 150 description, 146, 150 „„         R Rattle package running under R 3.1.2, 216 Relationship between variables correlation coefficient, 146–150 multiple regression, 152–155 scale variables, correlation, 143–146 Spearman’s rank correlation, 151–152 the phi coefficient, 150 Repeated-measures designs, 138–141 Resampling techniques, traditional hypothesis testing bootstrapping method, 160 histogram, medians, 159 hypothetical scores, 157–158 sample medians, distribution, 158 Reshaping datasets longScores dataset, 51 measurement, 50 reshape() function, 50 timevar argument, 51 Reusable functions writing arguments, 167 BMI function, 176–177 BSDA package, 174 “cover all outcomes” approach, 171 description, 167 environment, 167 ‘fBasics’, 171 240 www.it-ebooks.info ■ Index histogram, 172 input and output, 167 interface script, 178 length() function, 170 median, 168 null hypothesis, 169 return() statement, 175 Run App, 179 sample means, 173 scale function, 173 server script, 177 Shiny application, 180 Shiny apps, 179, 181 source code, 168 squareX, 175 take.root, 175 two-sided hypothesis test, 170 user interface script, 177 web-based server, 177 z.test function, 169 Stem-and-leaf plots, 100–101 Stock returns analysis growth pattern, 189 investment decisions, 187 Netflix stock, 188–191 Stocks comparison CumReturns function, 194–195 Netflix stocks, 192 PerformanceAnalytics, 192–193 R packages, 192 stringr package advantages, 63 description, 63 extracting words, 66 install.packages() function, 63 padding, 65 str_c() function, 64 str_dup() function, 64 str_length() function, 64 str_sub() function, 64 trim strings, 66 wrapping, 65–66 „„         S Scale variables brain volume and intelligence, 143 correlation coefficient, 143 covariance, 144 heights and weights, measurement, 144–145 “scalelessness” property, 144 slope coefficient, 146 Scatterplot description, 99 ggplot2, 95 of Starbucks volume by date, 96 with line of best fit added, 100 Scheirer-Ray-Hare (SRH) test, 138 Side-by-side boxplots, 97 Simple frequency distributions problem, 79 solution, 79–80 Skewed, 82 Spearman’s rank correlation, 151 SPSS output, 87 Standard graphs bar charts, 93–94 histograms, 92–93 line graphs, 94–95 scatterplots (see Scatterplot) Tufte’s principles, 92 Standard normal curves, 107–109 Statistical functions problem, 81 solution, 81–87 „„         T, U, V Tables description, 67 HairEyeColor data, 67 one-and two-way tables analyzing problem, 72 solution, 72–74 working with higher-order problem, 74–75 solution, 75–77 working with one-way problem, 68 solution, 68–69 working with two-way problem, 69 solution, 70–72 t distribution, 112–113 Trimmed means, Welch t test memory-enhancing supplement, 162 memory scores, groups, 162–163 Mann-Whitney U test, 165 variance, calculation, 161 Wilcox’s WRS package, 161 Winsorizing, 160 Yuen’s robust t test, 163–164 Two-sample tests cbind() function, 124 chi-square with degree of freedom, 127 241 www.it-ebooks.info ■ index Two-sample tests (cont.) histograms, word recognition data, 125–126 hypothetical airbag data, 131 independent-samples t test, 123 McNemar test, 123, 127 paired-samples t test, 124 parametric tests, 128 pooled-variance t test, 130 z.test function, 128–129 Two-way tests, 136–138 „„         W, X Welch t test, 160–161, 164 Wilcoxon signed rank test, 123 wilcox.test function, 130 Winsorizing, 160 „„         Y, Z Yuen’s robust t test, 163–164 242 www.it-ebooks.info R Recipes A Problem-Solution Approach Larry A Pace www.it-ebooks.info R Recipes: A Problem-Solution Approach Copyright © 2014 by 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-0131-2 ISBN-13 (electronic): 978-1-4842-0130-5 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 Development Editor: Matthew Moodie Technical Reviewer: Myron Hlynka Editorial Board: Steve Anglin, Mark Beckner, Gary Cornell, Louise Corrigan, Jim DeWolf, Jonathan Gennick, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing, Matt Wade, Steve Weiss Coordinating Editor: Melissa Maldonado Copy Editor: Kimberly Burton-Weisman Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Cover Designer: Anna Ishchenko 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 orders-ny@springer-sbm.com, or visit www.springeronline.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 rights@apress.com, 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 For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/ www.it-ebooks.info Contents About the Author���������������������������������������������������������������������������������������������������������������xiii About the Technical Reviewer�������������������������������������������������������������������������������������������� xv Acknowledgments������������������������������������������������������������������������������������������������������������ xvii Introduction����������������������������������������������������������������������������������������������������������������������� xix ■■Chapter 1: Migrating to R: As Easy As 1, 2, 3��������������������������������������������������������������������1 Getting R Up and Running on Your System�����������������������������������������������������������������������������������1 Okay, So I Have R What’s Next?����������������������������������������������������������������������������������������������������4 Understanding the Data Types in R�����������������������������������������������������������������������������������������������9 Handling Missing Data in R��������������������������������������������������������������������������������������������������������������������������������� 10 Working with Vectors in R������������������������������������������������������������������������������������������������������������������������������������ 10 Working with Matrices in R��������������������������������������������������������������������������������������������������������������������������������� 12 Looking Backward and Forward��������������������������������������������������������������������������������������������������15 ■■Chapter 2: Input and Output��������������������������������������������������������������������������������������������17 Recipe 2-1 Inputting and Outputting Data����������������������������������������������������������������������������������17 Problem �������������������������������������������������������������������������������������������������������������������������������������������������������������� 17 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 17 Recipe 2-2 Cleaning Up Data�����������������������������������������������������������������������������������������������������21 Problem �������������������������������������������������������������������������������������������������������������������������������������������������������������� 21 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 21 Recipe 2-3 Dealing with Text Data���������������������������������������������������������������������������������������������24 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 24 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 24 v www.it-ebooks.info ■ Contents Recipe 2-4 Getting Data from the Internet���������������������������������������������������������������������������������25 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 25 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 25 ■■Chapter 3: Data Structures����������������������������������������������������������������������������������������������27 Recipe 3-1 How to Work with Vectors����������������������������������������������������������������������������������������27 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 27 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 27 Recipe 3-2 How to Work with Matrices��������������������������������������������������������������������������������������29 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 29 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 29 Recipe 3-3 How to Work with Lists��������������������������������������������������������������������������������������������31 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 31 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 32 Recipe 3-4 Working with Data Frames���������������������������������������������������������������������������������������36 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 36 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 36 ■■Chapter 4: Merging and Reshaping Datasets������������������������������������������������������������������43 Recipe 4-1 Merging Datasets by a Common Variable����������������������������������������������������������������43 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 43 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 43 Recipe 4-2 Adding Rows and Columns��������������������������������������������������������������������������������������48 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 48 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 48 Recipe 4-3 Reshaping a Dataset������������������������������������������������������������������������������������������������50 Problem �������������������������������������������������������������������������������������������������������������������������������������������������������������� 50 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 50 Recipe 4-4 Stacking and Unstacking Data���������������������������������������������������������������������������������52 Problem �������������������������������������������������������������������������������������������������������������������������������������������������������������� 52 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 53 vi www.it-ebooks.info ■ Contents ■■Chapter 5: Working with Dates and Strings��������������������������������������������������������������������57 Recipe 5-1 Working with Dates and Times��������������������������������������������������������������������������������57 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 57 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 57 Recipe 5-2 Working with Character Strings�������������������������������������������������������������������������������60 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 60 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 61 ■■Chapter 6: Working with Tables���������������������������������������������������������������������������������������67 Recipe 6-1 Working with One-Way Tables ��������������������������������������������������������������������������������68 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 68 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 68 Recipe 6-2 Working with Two-Way Tables ���������������������������������������������������������������������������������69 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 69 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 70 Recipe 6-3 Analyzing One- and Two-Way Tables�����������������������������������������������������������������������72 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 72 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 72 Recipe 6-4 Working with Higher-Order Tables���������������������������������������������������������������������������74 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 74 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 75 ■■Chapter 7: Summarizing and Describing Data����������������������������������������������������������������79 Recipe 7-1 Creating Simple Frequency Distributions�����������������������������������������������������������������79 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 79 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 79 Recipe 7-2 Creating Grouped Frequency Distributions��������������������������������������������������������������81 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 81 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 81 Recipe 7-3 Calculating Summary Statistics�������������������������������������������������������������������������������81 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 81 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 81 vii www.it-ebooks.info ■ Contents Recipe 7-4 Working with Quantiles��������������������������������������������������������������������������������������������87 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 87 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 87 ■■Chapter 8: Graphics and Data Visualization��������������������������������������������������������������������89 Recipe 8-1 Getting the Colors You Want�������������������������������������������������������������������������������������89 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 89 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 90 Recipe 8-2 Using the Standard Graphs��������������������������������������������������������������������������������������91 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 91 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 92 Recipe 8-3 Using Graphics for Exploratory Data Analysis����������������������������������������������������������96 Problem��������������������������������������������������������������������������������������������������������������������������������������������������������������� 96 Solution��������������������������������������������������������������������������������������������������������������������������������������������������������������� 96 Recipe 8-4 Using Graphics for Data Visualization��������������������������������������������������������������������102 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 102 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 102 ■■Chapter 9: Probability Distributions������������������������������������������������������������������������������107 Recipe 9-1 Finding Areas Under the Standard Normal Curve��������������������������������������������������107 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 107 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 107 Recipe 9-2 Working with Binomial Probabilities����������������������������������������������������������������������109 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 109 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 109 Recipe 9-3 Working with Poisson Probabilities������������������������������������������������������������������������110 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 110 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 111 Recipe 9-4 Finding p Values and Critical Values of t, F, and Chi-Square����������������������������������111 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 111 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 111 viii www.it-ebooks.info ■ Contents ■■Chapter 10: Hypothesis Tests for Means, Ranks, or Proportions����������������������������������117 Recipe 10-1 One-Sample Tests������������������������������������������������������������������������������������������������117 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 117 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 117 Recipe 10-2 Two-Sample Tests for Related Means, Ranks, and Proportions���������������������������123 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 123 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 123 Recipe 10-3 Two-Sample Tests for Independent Means, Ranks, and Proportions�������������������128 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 128 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 128 Recipe 10-4 Tests for Three or More Means����������������������������������������������������������������������������132 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 132 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 132 Recipe 10-5 Repeated-Measures Designs�������������������������������������������������������������������������������138 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 138 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 139 ■■Chapter 11: Relationships Between and Among Variables��������������������������������������������143 Recipe 11-1 Determining Whether Two Scale Variables Are Correlated�����������������������������������143 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 143 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 144 Recipe 11-2 Special Cases of the Correlation Coefficient��������������������������������������������������������146 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 146 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 146 Recipe 11-3 A Brief Introduction to Multiple Regression ��������������������������������������������������������152 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 152 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 152 ■■Chapter 12: Contemporary Statistical Methods������������������������������������������������������������157 Recipe 12-1 Resampling Techniques���������������������������������������������������������������������������������������157 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 157 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 158 ix www.it-ebooks.info ■ Contents Recipe 12-2 Making Inferences About Means from Real Data�������������������������������������������������160 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 160 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 160 Recipe 12-3 Permutation Tests������������������������������������������������������������������������������������������������165 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 165 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 166 ■■Chapter 13: Writing Reusable Functions�����������������������������������������������������������������������167 Recipe 13-1 Understanding R Functions����������������������������������������������������������������������������������167 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 167 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 167 Recipe 13-2 Writing Functions That Produce Other Functions�������������������������������������������������175 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 175 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 175 Recipe 13-3 Writing Functions That Request User Input����������������������������������������������������������176 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 176 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 176 Recipe 13-4 Taking R to the Web���������������������������������������������������������������������������������������������177 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 177 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 177 ■■Chapter 14: Working with Financial Data����������������������������������������������������������������������183 Recipe 14-1 Getting and Visualizing Financial Data�����������������������������������������������������������������183 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 183 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 183 Recipe 14-2 Analyzing Stock Returns��������������������������������������������������������������������������������������187 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 187 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 188 Recipe 14-3 Comparing Stocks������������������������������������������������������������������������������������������������192 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 192 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 192 x www.it-ebooks.info ■ Contents Recipe 14-4 A Brief Introduction to Portfolios��������������������������������������������������������������������������195 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 195 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 196 ■■Chapter 15: Dealing with Big Data��������������������������������������������������������������������������������201 Recipe 15-1 Parallel R��������������������������������������������������������������������������������������������������������������201 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 201 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 202 Recipe 15-2 Using Data Tables�������������������������������������������������������������������������������������������������207 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 207 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 207 Recipe 15-3 Compiled Code and Preallocation������������������������������������������������������������������������211 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 211 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 211 ■■Chapter 16: Mining the Gold in Data and Text���������������������������������������������������������������215 Recipe 16-1 Reducing the Dimensionality of Data�������������������������������������������������������������������217 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 217 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 217 Recipe 16-2 Finding Clusters of Individuals or Objects������������������������������������������������������������221 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 221 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 222 Recipe 16-3 Looking for Associations��������������������������������������������������������������������������������������226 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 226 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 226 Recipe 16-4 Mining Text: A Brief Introduction��������������������������������������������������������������������������232 Problem������������������������������������������������������������������������������������������������������������������������������������������������������������� 233 Solution������������������������������������������������������������������������������������������������������������������������������������������������������������� 233 Index���������������������������������������������������������������������������������������������������������������������������������237 xi www.it-ebooks.info About the Author Larry A Pace is a statistics author, educator, and consultant He lives in the upstate area of South Carolina in the town of Anderson Larry earned his PhD from the University of Georgia, majoring in psychometrics and industrial psychology He has written more than 100 publications, including books, articles, chapters, and book and test reviews He has worked in academics for many years, as well as in private industry as a personnel psychologist and organization effectiveness manager He has programmed in a variety of computer languages and scripting languages, including Fortran, Basic, C++, PHP, Python, and ASP Larry has won numerous awards for teaching, research, and service He is currently a Graduate Research Professor at Keiser University, where he teaches doctoral classes in statistics and research methods He also teaches statistics, research methods, and tests and measurements part-time at Clemson University When he is not reading or writing about statistics, he likes to tend a small vegetable and herb garden, play his guitar, and cook on the grill Larry is married to Shirley Pace, and the Paces have four grown children and two grandsons The Paces volunteer for Meals on Wheels and are avid recyclers as well as pet rescuers In a long ago time, when college students carried slide rules instead of cell phones, Larry was a member of a famous local rock band, replete with groupies, but he decided to keep music as a hobby and pursue statistics as a profession xiii www.it-ebooks.info About the Technical Reviewer Dr Myron Hlynka has a bachelor’s degree in mathematics from the University of Manitoba, and a PhD in statistics from Pennsylvania State University He holds P Stat status from the Statistical Society of Canada He is currently a professor in the Department of Mathematics and Statistics at the University of Windsor, in Ontario, Canada, where he has taught for almost 30 years He has authored over 30 refereed publications and has supervised 30 graduate students He has over 60 article and book reviews in math reviews at MathSciNet Dr Hlynka’s research specialty is queueing theory and stochastic models He maintains a popular web site in queueing theory at http://web2.uwindsor.ca/ math/hlynka/queue.html xv www.it-ebooks.info Acknowledgments I love working with Apress My editorial team consisting of Steve Anglin, Matthew Moodie, Melissa Maldonado, and Kimberly Burton-Weisman kept me on my toes and made my life easier at the same time It is my privilege to have Myron Hlynka as a technical reviewer once again His eagle eyes and great suggestions kept me from embarrassing myself in print The Apress production team is excellent, and they did a great job as usual with production of final copy and graphics My friend and fellow statistics geek Nat Goodman was an informal but much appreciated sounding board throughout this whole process I am also thankful to my dean, Sara Malmstrom, and my department chair, Sue Adragna, for their support of my writing projects Finally, I thank my wife Shirley for tolerating my obsessions with writing and statistics with good humor and grace Finally, I want to thank my many students over the years, too many to mention by name, who have taught me how to be a better statistics teacher xvii www.it-ebooks.info ... vector The general category of array-programming languages includes languages that generalize operations on scalars transparently to vectors, matrices, and higher-order arrays An operation that... lines and paragraphs for human beings to read and understand Qualitative researchers treat textual material the same way quantitative researchers treat numbers Qualitative researchers describe... have a particular analysis I needed, such as a routine for calculating an intraclass correlation, so I would write my own program BMDP and SAS were available in batch versions for mainframe computers

Ngày đăng: 13/03/2019, 10:44

Mục lục

  • R Recipes

    • Contents at a Glance

    • Contents

    • About the Author

    • About the Technical Reviewer

    • Acknowledgments

    • Introduction

    • Chapter 1: Migrating to R: As Easy As 1, 2, 3

      • Getting R Up and Running on Your System

      • Okay, So I Have R. What’s Next?

      • Understanding the Data Types in R

        • Handling Missing Data in R

        • Working with Vectors in R

        • Working with Matrices in R

        • Looking Backward and Forward

        • Chapter 2: Input and Output

          • Recipe 2-1. Inputting and Outputting Data

            • Problem

            • Solution

              • Keyboard and Monitor Access

              • Reading and Writing Data Files

                • Reading Data Files

                • Writing Data Files

                • Recipe 2-2. Cleaning Up Data

                  • Problem

                  • Solution

                  • Recipe 2-3. Dealing with Text Data

                    • Problem

                    • Solution

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan