Data Analysis for the Life Sciences

Share Embed Donate


Short Description

Data Analysis for the Life Sciences...

Description

Data Analysis for the Life Sciences Rafael A Irizarry and Michael I Love This book is for sale at http://leanpub.com/dataanalysisforthelifesciences This version was published on 2015-09-23

This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. ©2015 Rafael A Irizarry and Michael I Love

Contents Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

i

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What Does This Book Cover? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How Is This Book Different? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1 1 2

Getting Started . . . . . . . . . Installing R . . . . . . . . . Installing RStudio . . . . . Learn R Basics . . . . . . . Installing Packages . . . . . Importing Data into R . . . Brief Introduction to dplyr Mathematical Notation . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

4 4 4 5 6 6 10 13

Inference . . . . . . . . . . . . . . . . . . . . . . Introduction . . . . . . . . . . . . . . . . . . Random Variables . . . . . . . . . . . . . . . The Null Hypothesis . . . . . . . . . . . . . . Distributions . . . . . . . . . . . . . . . . . . Probability Distribution . . . . . . . . . . . . Normal Distribution . . . . . . . . . . . . . . Populations, Samples and Estimates . . . . . Central Limit Theorem and t-distribution . . Central Limit Theorem in Practice . . . . . . t-tests in Practice . . . . . . . . . . . . . . . . The t-distribution in Practice . . . . . . . . . Confidence Intervals . . . . . . . . . . . . . . Power Calculations . . . . . . . . . . . . . . Monte Carlo Simulation . . . . . . . . . . . . Parametric Simulations for the Observations . Permutation Tests . . . . . . . . . . . . . . . Association Tests . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

18 18 20 21 22 25 28 31 34 41 48 50 53 62 74 79 82 86

CONTENTS

Exploratory Data Analysis . . . . . . . . . . Quantile Quantile Plots . . . . . . . . . . Boxplots . . . . . . . . . . . . . . . . . . Scatterplots And Correlation . . . . . . . Stratification . . . . . . . . . . . . . . . . Bi-variate Normal Distribution . . . . . . Plots To Avoid . . . . . . . . . . . . . . . Misunderstanding Correlation (Advanced) Robust Summaries . . . . . . . . . . . . . Wilcoxon Rank Sum Test . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

92 92 96 98 99 100 104 123 126 132

Matrix Algebra . . . . . . . . . . Motivating Examples . . . . Matrix Notation . . . . . . . Solving System of Equations Vectors, Matrices and Scalars Matrix Operations . . . . . . Examples . . . . . . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

138 138 147 147 148 151 157

Linear Models . . . . . . . . . . . . The Design Matrix . . . . . . . . The Mathematics Behind lm() . . Standard Errors . . . . . . . . . Interactions and Contrasts . . . . Linear Model with Interactions . Analysis of variance . . . . . . . Co-linearity . . . . . . . . . . . Rank . . . . . . . . . . . . . . . Removing Confounding . . . . . The QR Factorization (Advanced) Going Further . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

166 168 180 184 194 210 216 226 229 229 232 237

Inference For High Dimensional Data . . . . . . . . Introduction . . . . . . . . . . . . . . . . . . . . . Inference in Practice . . . . . . . . . . . . . . . . . Procedures . . . . . . . . . . . . . . . . . . . . . . Error Rates . . . . . . . . . . . . . . . . . . . . . . The Bonferroni Correction . . . . . . . . . . . . . False Discovery Rate . . . . . . . . . . . . . . . . . Direct Approach to FDR and q-values (Advanced) . Basic Exploratory Data Analysis . . . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

240 240 244 249 250 253 256 265 271

Statistical Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 The Binomial Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

CONTENTS

The Poisson Distribution . . . . . . . . . . . Maximum Likelihood Estimation . . . . . . . Distributions for Positive Continuous Values . Bayesian Statistics . . . . . . . . . . . . . . . Hierarchical Models . . . . . . . . . . . . . . Distance and Dimension Reduction Introduction . . . . . . . . . . . Euclidean Distance . . . . . . . Distance in High Dimensions . . Dimension Reduction Motivation Singular Value Decomposition . Projections . . . . . . . . . . . . Rotations . . . . . . . . . . . . . Multi-Dimensional Scaling Plots Principal Component Analysis .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

282 287 290 301 309

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

317 317 319 319 323 330 340 346 349 358

Basic Machine Learning . . . . . . . . . . . . Clustering . . . . . . . . . . . . . . . . . . Conditional Probabilities and Expectations . Smoothing . . . . . . . . . . . . . . . . . . Bin Smoothing . . . . . . . . . . . . . . . . Loess . . . . . . . . . . . . . . . . . . . . . Class Prediction . . . . . . . . . . . . . . . Cross-validation . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

365 365 376 380 382 385 390 399

Batch Effects . . . . . . . . . . . . . . . . . . . . . Confounding . . . . . . . . . . . . . . . . . . . Confounding: High-throughput Example . . . . Discovering Batch Effects with EDA . . . . . . Gene Expression Data . . . . . . . . . . . . . . Motivation for Statistical Approaches . . . . . Adjusting for Batch Effects with Linear Models Factor Analysis . . . . . . . . . . . . . . . . . . Modeling Batch Effects with Factor Analysis .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

409 412 417 420 421 433 436 443 449

Acknowledgements The authors would like to thank Alex Nones for proofreading the manuscript during its various stages. Also, thanks to Karl Broman for contributing the “Plots to Avoid” section and to Stephanie Hicks for designing some of the exercises. This book was conceived during the teaching of several HarvardX courses, coordinated by Heather Sternshein. We are also grateful to all the students whose questions and comments helped us improve the book. The courses were partially funded by NIH grant R25GM114818. We are very grateful to the National Institute of Health for its support. A special thanks goes to all those that edited the book via GitHub pull requests: vjcitn, yeredh, stefan, molx, kern3020, josemrecio, hcorrada, neerajt, massie, jmgore75, molecules, lzamparo, eronisko, and obicke. Cover image credit: this photograph is La Mina Falls, El Yunque National Forest, Puerto Rico, taken by Ron Kroetz https://www.flickr.com/photos/ronkroetz/14779273923 Attribution-NoDerivs 2.0 Generic (CC BY-ND 2.0)

Introduction The unprecedented advance in digital technology during the second half of the 20th century has produced a measurement revolution that is transforming science. In the life sciences, data analysis is now part of practically every research project. Genomics, in particular, is being driven by new measurement technologies that permit us to observe certain molecular entities for the first time. These observations are leading to discoveries analogous to identifying microorganisms and other breakthroughs permitted by the invention of the microscope. Choice examples of these technologies are microarrays and next generation sequencing. Scientific fields that have traditionally relied upon simple data analysis techniques have been turned on their heads by these technologies. In the past, for example, researchers would measure the transcription levels of a single gene of interest. Today, it is possible to measure all 20,000+ human genes at once. Advances such as these have brought about a shift from hypothesis to discoverydriven research. However, interpreting information extracted from these massive and complex datasets requires sophisticated statistical skills as one can easily be fooled by patterns arising by chance. This has greatly elevated the importance of statistics and data analysis in the life sciences

What Does This Book Cover? This book will cover several of the statistical concepts and data analytic skills needed to succeed in data-driven life science research. We go from relatively basic concepts related to computing p-values to advanced topics related to analyzing high-throughput data. We start with one of the most important topics in statistics and in the life sciences: statistical inference. Inference is the use of probability to learn population characteristic from data. A typical example is deciphering if two groups (for example, cases versus controls) are different on average. Specific topics covered include the t-test, confidence intervals, association tests, Monte Carlo methods, permutation tests and statistical power. We make use of approximations made possible by mathematical theory, such as the Central Limit Theorem, as well as techniques made possible by modern computing. We will learn how to compute p-values and confidence intervals and implement basic data analyses. Throughout the book we will describe visualization techniques in the statistical computer language R that are useful for exploring new data sets. For example, we will use these to learn when to apply robust statistical techniques. We will then move on to an introduction to linear models and matrix algebra. We will explain why it is beneficial to use linear models to analyze differences across groups, and why matrices are useful to represent and implement linear models. We continue with a review of matrix algebra, including matrix notation and how to multiply matrices (both on paper and in R). We will then apply what we covered on matrix algebra to linear models. We will learn how to fit linear models in R, how

Introduction

2

to test the significance of differences, and how the standard errors for differences are estimated. Furthermore, we will review some practical issues with fitting linear models, including collinearity and confounding. Finally, we will learn how to fit complex models, including interaction terms, how to contrast multiple terms in R, and the powerful technique which the functions in R actually use to stably fit linear models: the QR decomposition. In the third part of the book we cover topics related to high-dimensional data. Specifically, we describe multiple testing, error rate controlling procedures, exploratory data analysis for highthroughput data, p-value corrections and the false discovery rate. From here we move on to covering statistical modeling. In particular, we will discuss parametric distributions, including binomial and gamma distributions. Next, we will cover maximum likelihood estimation. Finally, we will discuss hierarchical models and empirical Bayes techniques and how they are applied in genomics. We then cover the concepts of distance and dimension reduction. We will introduce the mathematical definition of distance and use this to motivate the singular value decomposition (SVD) for dimension reduction and multi-dimensional scaling. Once we learn this, we will be ready to cover hierarchical and k-means clustering. We will follow this with a basic introduction to machine learning. We end by learning about batch effects and how component and factor analysis are used to deal with this challenge. In particular, we will examine confounding, show examples of batch effects, make the connection to factor analysis, and describe surrogate variable analysis.

How Is This Book Different? While statistics textbooks focus on mathematics, this book focuses on using a computer to perform data analysis. This book follows the approach of Stat Labs¹, by Deborah Nolan and Terry Speed. Instead of explaining the mathematics and theory, and then showing examples, we start by stating a practical data-related challenge. This book also includes the computer code that provides a solution to the problem and helps illustrate the concepts behind the solution. By running the code yourself, and seeing data generation and analysis happen live, you will get a better intuition for the concepts, the mathematics, and the theory. We focus on the practical challenges faced by data analysts in the life sciences and introduce mathematics as a tool that can help us achieve scientific goals. Furthermore, throughout the book we show the R code that performs this analysis and connect the lines of code to the statistical and mathematical concepts we explain. All sections of this book are reproducible as they were made using R markdown documents that include R code used to produce the figures, tables and results shown in the book. In order to distinguish it, the code is shown in the following font:

¹https://www.stat.berkeley.edu/~statlabs/

3

Introduction

x
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF