ebook img

Beginning R 4: From Beginner to Pro PDF

481 Pages·2020·9.618 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 Beginning R 4: From Beginner to Pro

Beginning R 4 From Beginner to Pro — Matt Wiley Joshua F. Wiley Beginning R 4 From Beginner to Pro Matt Wiley Joshua F. Wiley Beginning R 4: From Beginner to Pro Matt Wiley Joshua F. Wiley Victoria College, Turner Institute for Brain and Mental Health, Victoria, TX, USA Monash University, Melbourne, Australia ISBN-13 (pbk): 978-1-4842-6052-4 ISBN-13 (electronic): 978-1-4842-6053-1 https://doi.org/10.1007/978-1-4842-6053-1 Copyright © 2020 by Matt Wiley, Joshua F. Wiley 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. 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, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Steve Anglin Development Editor: Matthew Moodie Coordinating Editor: Mark Powers Cover designed by eStudioCalamar Cover image by Shutterstock Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], 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 [email protected]; for reprint, paperback, or audio rights, please e-mail [email protected]. Apress titles 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 Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/9781484260524. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper Table of Contents About the Authors ��������������������������������������������������������������������������������������������������xiii About the Technical Reviewer ���������������������������������������������������������������������������������xv Acknowledgments �������������������������������������������������������������������������������������������������xvii Foreword ����������������������������������������������������������������������������������������������������������������xix Chapter 1: Installing R ����������������������������������������������������������������������������������������������1 1.1 Your Tech Stack .......................................................................................................................2 1.2 Updating Your Operating System ............................................................................................2 Windows ..................................................................................................................................2 MacOS .....................................................................................................................................3 1.3 Downloading and Installing R from CRAN ...............................................................................3 Windows ..................................................................................................................................3 MacOS .....................................................................................................................................4 1.4 Downloading and Installing RStudio .......................................................................................5 Windows ..................................................................................................................................6 MacOS .....................................................................................................................................6 1.5 Using RStudio ..........................................................................................................................6 New Projects .........................................................................................................................11 1.6 My First R Script ...................................................................................................................13 1.7 Summary...............................................................................................................................17 1.8 Practice for Mastery ..............................................................................................................18 Comprehension Checks .........................................................................................................18 Exercises ...............................................................................................................................19 iii Table of ConTenTs Chapter 2: Installing Packages and Using Libraries �����������������������������������������������21 2.1 Installing Packages ...............................................................................................................23 haven .....................................................................................................................................24 readxl .....................................................................................................................................25 writexl ....................................................................................................................................25 data.table ..............................................................................................................................26 extraoperators .......................................................................................................................26 JWileymisc ............................................................................................................................27 ggplot2 ..................................................................................................................................27 visreg .....................................................................................................................................28 emmeans ...............................................................................................................................28 ez ...........................................................................................................................................29 palmerpenguins .....................................................................................................................29 2.2 Using Packages .....................................................................................................................29 2.3 Summary...............................................................................................................................30 2.4 Practice for Mastery ..............................................................................................................31 Comprehension Checks .........................................................................................................31 Exercises ...............................................................................................................................31 Chapter 3: Data Input and Output ���������������������������������������������������������������������������33 3.1 Setup .....................................................................................................................................33 3.2 Input ......................................................................................................................................34 Manual Entry .........................................................................................................................35 CSV: .csv ................................................................................................................................36 Excel: .xlsx or .xls ..................................................................................................................38 RDS: .rds ................................................................................................................................40 Other Proprietary Formats .....................................................................................................41 3.3 Output ...................................................................................................................................43 CSV ........................................................................................................................................43 Excel ......................................................................................................................................44 RDS ........................................................................................................................................44 iv Table of ConTenTs 3.4 Summary...............................................................................................................................44 3.5 Practice for Mastery ..............................................................................................................45 Comprehension Checks .........................................................................................................45 Exercises ...............................................................................................................................46 Chapter 4: Working with Data ��������������������������������������������������������������������������������47 4.1 Setup .....................................................................................................................................47 4.2 What Do Our Data Look Like?................................................................................................49 4.3 How Does data.table Work? .............................................................................................52 How Do Row Operations Work? .............................................................................................53 How Do Column Operations Work? ........................................................................................65 How Do by Operations Work? ................................................................................................72 4.4 Examples...............................................................................................................................74 Example 1: Metropolitan Area Counts ....................................................................................74 Example 2: Metropolitan Statistical Areas (MSAs) .................................................................76 Example 3 ..............................................................................................................................77 4.5 Summary...............................................................................................................................77 4.6 Practice for Mastery ..............................................................................................................79 Comprehension Checks .........................................................................................................79 Exercises ...............................................................................................................................80 Chapter 5: Data and Samples ���������������������������������������������������������������������������������81 5.1 R Setup .................................................................................................................................82 5.2 Populations and Samples......................................................................................................82 5.3 Variables and Data ................................................................................................................83 Example .................................................................................................................................85 Example .................................................................................................................................88 Example .................................................................................................................................88 Thoughts on Variables and Data ............................................................................................89 5.4 Thinking Statistically .............................................................................................................90 v Table of ConTenTs 5.5 Evaluating Studies ................................................................................................................90 5.6 Evaluating Samples...............................................................................................................92 Convenience Samples ...........................................................................................................93 Kth Samples ..........................................................................................................................95 Cluster Samples ..................................................................................................................100 Stratified Samples ...............................................................................................................103 Random Samples ................................................................................................................107 Sample Recap .....................................................................................................................110 5.7 Frequency Tables ................................................................................................................112 Example ...............................................................................................................................112 5.8 Summary.............................................................................................................................120 5.9 Practice for Mastery ............................................................................................................122 Comprehension Checks .......................................................................................................122 Exercises .............................................................................................................................123 Chapter 6: Descriptive Statistics ��������������������������������������������������������������������������125 6.1 R Setup ...............................................................................................................................125 6.2 Visualization ........................................................................................................................126 Histograms ..........................................................................................................................127 Dot Plots/Charts ...................................................................................................................133 ggplot2 ................................................................................................................................135 6.3 Central Tendency .................................................................................................................144 Arithmetic Mean ..................................................................................................................145 Median .................................................................................................................................149 6.4 Position ...............................................................................................................................153 Example ...............................................................................................................................154 Example ...............................................................................................................................157 Example ...............................................................................................................................160 vi Table of ConTenTs 6.5 Turbulence...........................................................................................................................161 Example ...............................................................................................................................166 Example ...............................................................................................................................168 6.6 Summary.............................................................................................................................170 6.7 Practice for Mastery ............................................................................................................171 Comprehension Checks .......................................................................................................172 Exercises .............................................................................................................................172 Chapter 7: Understanding Probability and Distributions ��������������������������������������175 7.1 R Setup ...............................................................................................................................176 7.2 Probability ...........................................................................................................................177 Example: Independent .........................................................................................................179 Example: Complement .........................................................................................................181 Probability Final Thoughts ...................................................................................................182 7.3 Normal Distribution .............................................................................................................183 Example ...............................................................................................................................186 Example ...............................................................................................................................188 Example ...............................................................................................................................190 Example ...............................................................................................................................192 7.4 Distribution Probability........................................................................................................197 Example ...............................................................................................................................201 Example ...............................................................................................................................203 7.5 Central Limit Theorem .........................................................................................................204 Example ...............................................................................................................................207 Example ...............................................................................................................................212 Example ...............................................................................................................................217 7.6 Summary.............................................................................................................................220 7.7 Practice for Mastery ............................................................................................................222 Comprehension Checks .......................................................................................................222 Exercises .............................................................................................................................222 vii Table of ConTenTs Chapter 8: Correlation and Regression �����������������������������������������������������������������225 8.1 R Setup ...............................................................................................................................226 8.2 Correlations .........................................................................................................................227 Parametric ...........................................................................................................................230 Non-parametric: Spearman .................................................................................................233 Non-parametric: Kendall......................................................................................................234 Correlation Choices .............................................................................................................237 8.3 Simple Linear Regression ...................................................................................................238 Introduction .........................................................................................................................238 Assumptions ........................................................................................................................243 R 2: Variance Explained .........................................................................................................250 Linear Regression in R .........................................................................................................250 8.4 Summary.............................................................................................................................262 8.5 Practice for Mastery ............................................................................................................263 Comprehension Checks .......................................................................................................263 Exercises .............................................................................................................................264 Chapter 9: Confidence Intervals ���������������������������������������������������������������������������265 9.1 R Setup ...............................................................................................................................266 9.2 Visualizing Confidence Intervals .........................................................................................268 Example: Sigma Known .......................................................................................................272 Example: Sigma Unknown ...................................................................................................279 Example ...............................................................................................................................282 Example ...............................................................................................................................284 9.3 Understanding Similar vs. Dissimilar Data ..........................................................................287 Example ...............................................................................................................................287 Example ...............................................................................................................................288 9.4 Summary.............................................................................................................................290 9.5 Practice for Mastery ............................................................................................................291 Comprehension Checks .......................................................................................................291 Exercises .............................................................................................................................292 viii Table of ConTenTs Chapter 10: Hypothesis Testing ����������������������������������������������������������������������������293 10.1 R Setup .............................................................................................................................294 10.2 H0 vs. H1 ...........................................................................................................................295 Example ...............................................................................................................................296 Example ...............................................................................................................................296 10.3 Type I/II Errors ...................................................................................................................297 Example ...............................................................................................................................298 Example ...............................................................................................................................299 Example ...............................................................................................................................300 10.4 Alpha and Beta ..................................................................................................................300 10.5 Assumptions .....................................................................................................................304 10.6 Null Hypothesis Significance Testing (NHST) ....................................................................304 Example ...............................................................................................................................306 Example ...............................................................................................................................309 Example ...............................................................................................................................312 10.7 Summary...........................................................................................................................314 10.8 Practice for Mastery ..........................................................................................................315 Comprehension Checks .......................................................................................................315 Exercises .............................................................................................................................316 Chapter 11: Multiple Regression ��������������������������������������������������������������������������317 11.1 R Setup .............................................................................................................................318 11.2 Linear Regression Redux ..................................................................................................320 Example ...............................................................................................................................322 11.3 Multiple Regression ..........................................................................................................329 Implications of Multiple Predictors ......................................................................................330 Multiple Regression in R ......................................................................................................332 Effect Sizes and Formatting ................................................................................................343 Assumption and Cleaning ....................................................................................................359 ix

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.