Wim P. Krijnen November 10, 2009 - Cran.r-project

Transcription

Applied Statistics for Bioinformatics using RWim P. KrijnenNovember 10, 2009

iiPrefaceThe purpose of this book is to give an introduction into statistics in orderto solve some problems of bioinformatics. Statistics provides procedures toexplore and visualize data as well as to test biological hypotheses. The bookintends to be introductory in explaining and programming elementary statistical concepts, thereby bridging the gap between high school levels and thespecialized statistical literature. After studying this book readers have a sufficient background for Bioconductor Case Studies (Hahne et al., 2008) andBioinformatics and Computational Biology Solutions Using R and Bioconductor (Genteman et al., 2005). The theory is kept minimal and is alwaysillustrated by several examples with data from research in bioinformatics.Prerequisites to follow the stream of reasoning is limited to basic high-schoolknowledge about functions. It may, however, help to have some knowledgeof gene expressions values (Pevsner, 2003) or statistics (Bain & Engelhardt,1992; Ewens & Grant, 2005; Rosner, 2000; Samuels & Witmer, 2003), andelementary programming. To support self-study a sufficient amount of challenging exercises are given together with an appendix with answers.The programming language R is becoming increasingly important becauseit is not only very flexible in reading, manipulating, and writing data, butall its outcomes are directly available as objects for further programming.R is a rapidly growing language making basic as well as advanced statistical programming easy. From an educational point of view, R provides thepossibility to combine the learning of statistical concepts by mathematics,programming, and visualization. The plots and tables produced by R canreadily be used in typewriting systems such as Emacs, LATEX, or Word.Chapter 1 gives a brief introduction into basic functionalities of R. Chapter 2 starts with univariate data visualization and the most important descriptive statistics. Chapter 3 gives commonly used discrete and continuousdistributions to model events and the probability by which these occur. Thesedistributions are applied in Chapter 4 to statistically test hypotheses frombioinformatics. For each test the statistics involved are briefly explained andits application is illustrated by examples. In Chapter 5 linear models are explained and applied to testing for differences between groups. It gives a basicapproach. In Chapter 6 the three phases of analysis of microarray data (preprocessing, analysis, post processing) are briefly introduced and illustratedby many examples bringing ideas together with R scrips and interpretation ofresults. Chapter 7 starts with an intuitive approach into Euclidian distance

iiiand explains how it can be used in two well-known types of cluster analysis tofind groups of genes. It also explains how principal components analysis canbe used to explore a large data matrix for the direction of largest variation.Chapter 8 shows how gene expressions can be used to predict the diagnosisof patients. Three such prediction methods are illustrated and compared.Chapter 9 introduces a query language to download sequences efficiently andgives various examples of computing important quantities such as alignmentscores. Chapter 10 introduces the concept of a probability transition matrixwhich is applied to the estimation of phylogenetic trees and (Hidden) MarkovModels.R commands come after its prompt , except when commands are partof the ongoing text. Input and output of R will be given in verbatimtypewriting style. To save space sometimes not all of the original outputfrom R is printed. The end of an example is indicated by the box . Inits Portable Document Format (PDF)1 there are many links to the Index,Table of Contents, Equations, Tables, and Figures. Readers are encouragedto copy and paste scripts from the PDF into the R system in order to studyits outcome. Apart from using the book to study application of statistics inbioinformatics, it can also be useful for statistical programming.I would like to thank my colleges Joop Bouman, Sven Warris and JanPeter Nap for their useful remarks on parts of an earlier draft. Many thanksalso go to my students for asking questions that gave hints to improve clarity.Remarks to further improve the text are appreciated.Wim P. KrijnenHanze UniversityInstitute for Life Science and TechnologyZernikeplein 119747 AS GroningenThe r 2009c Thisdocument falls under the GNU Free Document Licence and may be used freelyfor educational purposes.

iv

ContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Brief Introduction into Using R1.1 Getting R Started on your PC . . . .1.2 Getting help . . . . . . . . . . . . . .1.3 Calculating with R . . . . . . . . . .1.4 Generating a sequence and a factor .1.5 Computing on a data vector . . . . .1.6 Constructing a data matrix . . . . .1.7 Computing on a data matrix . . . . .1.8 Application to the Golub (1999) data1.9 Running scripts . . . . . . . . . . . .1.10 Overview and concluding remarks . .1.11 Exercises . . . . . . . . . . . . . . . .2 Data Display and Descriptive Statistics2.1 Univariate data display . . . . . . . . . .2.1.1 Pie and Frequency table . . . . .2.1.2 Plotting data . . . . . . . . . . .2.1.3 Histogram . . . . . . . . . . . . .2.1.4 Boxplot . . . . . . . . . . . . . .2.1.5 Quantile-Quantile plot . . . . . .2.2 Descriptive statistics . . . . . . . . . . .2.2.1 Measures of central tendency . .2.2.2 Measures of spread . . . . . . . .2.3 Overview and concluding remarks . . . .2.4 Exercises . . . . . . . . . . . . . . . . . .v.iii.1134456810121314.171717181920222424252626

vi3 Important Distributions3.1 Discrete distributions . . . . . . .3.1.1 Binomial distribution . . .3.2 Continuous distributions . . . . .3.2.1 Normal distribution . . . .3.2.2 Chi-squared distribution .3.2.3 T-Distribution . . . . . . .3.2.4 F-Distribution . . . . . . .3.2.5 Plotting a density function3.3 Overview and concluding remarks3.4 Exercises . . . . . . . . . . . . . .CONTENTS.4 Estimation and Inference4.1 Statistical hypothesis testing . . . . . . . . . . . .4.1.1 The Z-test . . . . . . . . . . . . . . . . . .4.1.2 One Sample t-Test . . . . . . . . . . . . .4.1.3 Two-sample t-test with unequal variances4.1.4 Two sample t-test with equal variances . .4.1.5 F-test on equal variances . . . . . . . . . .4.1.6 Binomial test . . . . . . . . . . . . . . . .4.1.7 Chi-squared test . . . . . . . . . . . . . .4.1.8 Normality tests . . . . . . . . . . . . . . .4.1.9 Outliers test . . . . . . . . . . . . . . . . .4.1.10 Wilcoxon rank test . . . . . . . . . . . . .4.2 Application of tests to a whole set gene expression4.3 Overview and concluding remarks . . . . . . . . .4.4 Exercises . . . . . . . . . . . . . . . . . . . . . . .5 Linear Models5.1 Definition of linear models . . . .5.2 One-way analysis of variance . . .5.3 Two-way analysis of variance . .5.4 Checking assumptions . . . . . .5.5 Robust tests . . . . . . . . . . . .5.6 Overview and concluding remarks5.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .data. . . . 6869.7374778385868888

CONTENTSvii6 Micro Array Analysis6.1 Probe data . . . . . . . . . . . . . .6.2 Preprocessing methods . . . . . . . .6.3 Gene filtering . . . . . . . . . . . . .6.4 Applications of linear models . . . .6.5 Searching an annotation package . .6.6 Using annotation to search literature6.7 Searching GO numbers and evidence6.8 GO parents and children . . . . . . .6.9 Gene filtering by a biological term . .6.10 Significance per chromosome . . . . .6.11 Overview and concluding remarks . .6.12 Exercises . . . . . . . . . . . . . . . .7 Cluster Analysis and Trees7.1 Distance . . . . . . . . . . . . .7.2 Two types of Cluster Analysis . .7.2.1 Single Linkage . . . . . . .7.2.2 k-means . . . . . . . . . .7.3 The correlation coefficient . . . .7.4 Principal Components Analysis .7.5 Overview and concluding remarks7.6 Exercises . . . . . . . . . . . . . .8 Classification Methods8.1 Classification of microRNA . . . .8.2 ROC types of curves . . . . . . .8.3 Classification trees . . . . . . . .8.4 Support Vector Machine . . . . .8.5 Neural Networks . . . . . . . . .8.6 Generalized Linear Models . . . .8.7 Overview and concluding remarks8.8 Exercises . . . . . . . . . . . . . .91919497100104106107108109110112112.117. 118. 121. 121. 125. 130. 133. 141. 142.145. 146. 147. 150. 160. 162. 164. 167. 1679 Analyzing Sequences1739.1 Using a query language . . . . . . . . . . . . . . . . . . . . . . 1739.2 Getting information on downloaded sequences . . . . . . . . . 1749.3 Computations on sequences . . . . . . . . . . . . . . . . . . . 176

viiiCONTENTS9.49.59.69.7Matching patterns . . .Pairwise alignments . . .Overview and concludingExercises . . . . . . . . . . . . . . . . .remarks. . . . .18118218918910 Markov Models10.1 Random sampling . . . . . . . . . .10.2 Probability transition matrix . . . .10.3 Properties of the transition matrix10.4 Stationary distribution . . . . . . .10.5 Phylogenetic distance . . . . . . . .10.6 Hidden Markov Models . . . . . . .10.7 Appendix . . . . . . . . . . . . . .10.8 Overview and concluding remarks .10.9 Exercises . . . . . . . . . . . . . . .193193194199201203209213214214A Answers to exercises219B References257

List of Figures2.12.22.32.42.52.63.13.23.3Plot of gene expression values of CCND3 Cyclin D3. . . . . . .Stripchart of gene expression values of CCND3 Cyclin D3 forALL and AML patients. . . . . . . . . . . . . . . . . . . . . .Histogram of ALL expression values of gene CCND3 Cyclin D3.Boxplot of ALL and AML expression values of gene CCND3Cyclin D3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Q-Q plot of ALL gene expression values of CCND3 Cyclin D3.Boxplot with arrows and explaining text. . . . . . . . . . . .2020212123293.53.63.73.83.93.10Binomial probabilities with n 22 and p 0.7 . . . . . . .Binomial cumulative probabilities with n 22 and p 0.7. .Graph of normal density with mean 1.9 and standard deviation0.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Graph of normal distribution with mean 1.9 and standard deviation 0.5. . . . . . . . . . . . . . . . . . . . . . . . . . . .χ25 -density. . . . . . . . . . . . . . . . . . . . . . . . . . . . .χ25 distribution. . . . . . . . . . . . . . . . . . . . . . . . . .Density of T10 distribution. . . . . . . . . . . . . . . . . . . .Distribution function of T10 . . . . . . . . . . . . . . . . . . .Density of F26,10 . . . . . . . . . . . . . . . . . . . . . . . . .Distribution of F26,10 . . . . . . . . . . . . . . . . . . . . . . .4.14.24.3Acceptance and rejection regions of the Z-test. . . . . . . . . . 50Acceptance and rejection regions of the T5 -test. . . . . . . . . 52Rejection region of χ23 -test. . . . . . . . . . . . . . . . . . . . . 595.15.2Plot of SKI-like oncogene expressions for three patient groups. 81Plot of Ets2 expression values for three patient groups. . . . . 813.4ix. 34. 34. 36.36383839394141

xLIST OF .97.107.117.127.137.148.18.28.38.48.58.68.7Mat plot of intensity values for a probe of MLL.B. . . . . . .Density of MLL.B data. . . . . . . . . . . . . . . . . . . . . .Boxplot of the ALL1/AF4 patients. . . . . . . . . . . . . . .Boxplot of the ALL1/AF4 patients after median subtractionand MAD division. . . . . . . . . . . . . . . . . . . . . . . .Venn diagram of seleced ALL genes. . . . . . . . . . . . . . .Boxplot of the ALL1/AF4 patients after median subtractionand MAD division. . . . . . . . . . . . . . . . . . . . . . . . 93. 93. 97. 97. 100. 100Plot of five points to be clustered. . . . . . . . . . . . . . . . . 122Tree of single linkage cluster analysis. . . . . . . . . . . . . . . 122Example of three without clusters. . . . . . . . . . . . . . . . 123Three clusters with different standard deviations. . . . . . . . 123Plot of gene ”CCND3 Cyclin D3” and ”Zyxin” expressions forALL and AML patients. . . . . . . . . . . . . . . . . . . . . . 124Single linkage cluster diagram from gene ”CCND3 Cyclin D3”and ”Zyxin” expressions values. . . . . . . . . . . . . . . . . 124K-means cluster analysis. . . . . . . . . . . . . . . . . . . . . . 126Tree of single linkage cluster analysis. . . . . . . . . . . . . . . 126Plot of kmeans (stars) cluster analysis on CCND3 Cyclin D3and Zyxin discriminating between ALL (red) and AML (black)patients. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130Vectors of linear combinations. . . . . . . . . . . . . . . . . . . 135First principal component with projections of data. . . . . . . 135Scatter plot of selected genes with row labels on the first twoprincipal components. . . . . . . . . . . . . . . . . . . . . . . 138Single linkage cluster diagram of selected gene expression values.138Biplot of selected genes from the golub data. . . . . . . . . . . 144ROC plot for expression values of CCND3 Cyclin D3. . . . . .ROC plot for expression values of gene Gdf5. . . . . . . . . .Boxplot of expression values of gene a for each leukemia class.Classification tree for gene for three classes of leukemia. . . . .Boxplot of expression values of gene a for each leukemia class.Classification tree of expression values from gene A, B, and Cfor the classification of ALL1, ALL2, and AML patients. . . .Boxplot of expression values from gene CCND3 Cyclin D3 forALL and AML patients . . . . . . . . . . . . . . . . . . . . .149149151151154154156

LIST OF FIGURESClassification tree of expression values from gene CCND3 Cyclin D3 for classification of ALL and AML patients. . . . .8.9 rpart on ALL B-cel 123 data. . . . . . . . . . . . . . . . . .8.10 Variable importance plot on ALL B-cell 123 data. . . . . .8.11 Logit fit to the CCND3 Cyclin D3 expression values. . . . .xi8.89.19.29.3.156159159171G C fraction of sequence ”AF517525.CCND3” along a window of length 50 nt. . . . . . . . . . . . . . . . . . . . . . . . 178Frequency plot of amino acids from accession number AF517525.CCND3.179Frequency plot of amino acids from accession number AL160163.CCND3.17910.1 Graph of probability transition matrix . . . . . . . . . . . . . 19610.2 Evaluation of models by AIC . . . . . . . . . . . . . . . . . . . 21610.3 Tree according to GTR model. . . . . . . . . . . . . . . . . . . 217

xiiLIST OF FIGURES

List of Tables2.1A frequency table and its pie of Zyxin gene. . . . . . . . . . . 183.13.23.3Discrete density and distribution function values of S3 , withp 0.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Built-in-functions for random variables used in this chapter. . 42Density, mean, and variance of distributions used in this chapter. 437.1Data set for principal components analysis.8.18.2Frequencies empirical p-values lower than or equal to 0.01. . . 146Ordered expression values of gene CCND3 Cyclin D3, index2 indicates ALL, 1 indicates AML, cutoff points, number offalse positives, false positive rate, number of true positives,true positive rate. . . . . . . . . . . . . . . . . . . . . . . . . 1709.1BLOSUM50 matrix. . . . . . . . . . . . . . . . . . . . . . . . 186xiii. . . . . . . . . . 134

xivLIST OF TABLES

Chapter 1Brief Introduction into Using RTo get started a gentle introduction to the statistical programming languageR will be given (R Development Core Team, 2009), specific for our purposes.This will solve the practical issues to follow the stream of reasoning. Inparticular, it is briefly explained how to install R and Bioconductor, how toobtain help, and how to perform simple calculations.Since many computations are essentially performed on data vectors, several basic illustrations of this are given. With respect to gene expressions thedata vectors are placed one beneath the other to form a data matrix withthe genes as rows and the patients as columns. The idea of a data matrix isextensively explained and illustrated by several examples. A larger exampleconsists of the classical Golub et al. (1999) data, which will be analyzedfrequently to illustrate statistical procedures.1.1Getting R Started on your PCYou can downloaded R freely from http://cran.r-project.org. Click onyour favorite operating system (Windows, Linux or MacOS) and simply followthe instructions. After a little patience you should be able to start R (Ihaka& Gentleman, 1996) after which a screen is opened with the prompt . Theinput and output of R will be displayed in verbatim typewriting style.All useful functions of R are contained in libraries which are called ”packages”. The standard installation of R makes basic packages available suchas base and stats. From the button Packages at cran.r-project.org itcan be seen that R has a huge number of packages available for a wide scale1

2CHAPTER 1. BRIEF INTRODUCTION INTO USING Rof statistical procedures. To download a specific package you can use thefollowing. install.packages(c("TeachingDemos"),repo "http://cran.r-project.org", dep TRUE)This installs the package TeachingDemos developed by Greg Snow from therepository http://cran.r-project.org. By setting the option dep to TRUEthe packages on which the TeachingDemos depend are also installed. This isstrongly recommended! Alternatively, in the Windows application of R youcan simply click on the Packages button at the top of your screen and followthe instructions. After installing you have to load the package in order to useits functions. For instance, to produce a nice plot of the outcome of throwingtwelve times with a die, you can use the following. library(TeachingDemos) plot(dice(12,1))In the sequel we shall often use packages from Bioconductor, a very usefulopen source software project for the analysis and comprehension of genomicdata. To follow the book it is essential to install Bioconductor on your PCor network. Bioconductor is primarily based on R and can be installed, asfollows. source("http://www.bioconductor.org/biocLite.R") biocLite()Then to download the ALL package from a repository to your system, to loadit, and to make the ALL data (Chiaretti, et. al, 2004) available for usage, youcan use the following. biocLite("ALL") library(ALL) data(ALL)These data will be analyzed extensively later-on in Chapter 5 and 6. Generalhelp on loaded Bioconductor packages becomes available by openVignette().For further information the reader is referred to www.bioconductor.org orto several other URL’s1 .1http://mccammon.ucsd.edu/ bgrant/bio3d/user guide/user cs.conductor

1.2. GETTING HELP3In this and the following chapters we will illustrate many statistical ideasby the Golub et al. (1999) data, see also Section 1.8. The golub data becomeavailable by the following.2 library(multtest) data(golub)R is object-oriented in the sense that everything consists of objects belongingto certain classes. Type class(golub) to obtain the class of the object goluband str(golub) to obtain its structure or content. Type objects() or ls()to view the currently loaded objects, a list probably growing soon to be large.To prevent conflicting definitions, it is wise to remove them all at the end ofa session by rm(list ls()). To quit a session, type q(), or simply click onthe cross in the upper right corner of your screen.1.2Getting helpAll functionalities of R are well-organized in so-called packages. Use the function library() to see which packages are currently installed on your operating system. The packages stats and base are automatically installed, because these contain many basic functionalities. To obtain an overview of thecontent of a package use ls(package:stats) or library(help "stats").Help on the purpose of specific functions can be obtained from the (package)manual by typing a question mark in front of a function. For instance, ?sumgives details on summation. In case you are seeking help on a function whichuses if, simply type apropos("if"). When you are starting with a new concept such as ”boxplot”, it is convenient to have an example showing output(a plot) and programming code. Such is given by example(boxplot). Thefunction history can be useful for collecting previously given commands.Type help.start() to launch an HTML page linking to several wellwritten R manuals such as: ”An Introduction to R”, ”The R Language Definition”, ”R Installation and Administration”, and ”R Data Import/Export”.Further help can be obtained from http://cran.r-project.org. Its ”contributed” page contains well-written freely available on-line books3 and useful reference charts4 . At http://www.r-project.org you can use R site2Functions to read data into R are read.table or read.csv, see also the ”The R DataImport/Export manual”.3”R for Beginners” by Emmanuel Paradis or the ”The R Guide” by Jason Owen4”R reference card” by Tom Short or by Jonathan Baron

4CHAPTER 1. BRIEF INTRODUCTION INTO USING Rsearch, Rseek, or other useful search engines. There are a number of usefulURL’s with information on R.51.3Calculating with RR can be used as a simple calculator. For instance, to add 2 and 3 we simplyinsert the following. 2 3[1] 5In many calculations the natural base e 2.718282 of exponential functionsis used. Such type of functions can be called as follows. exp(1)[1] 2.718282To compute e2 e · e we use exp(2).6 So, indeed, we have ex exp(x), forany value of x.The sum 1 2 3 4 5 can be computed by sum(1:5)[1] 15and the product 5! 5 · 4 · 3 · 2 · 1 by prod(1:5)[1] 1201.4Generating a sequence and a factorIn order to compute so-called quantiles of distributions (see e.g. Section2.1.4) or plots of functions, we need to generate sequences of numbers. Theeasiest way to construct a sequence of numbers is by 1:5[1] 1 2 3 4 55We mention in particular:http://faculty.ucr.edu/ tgirke/Documents/R BioCond/R BioCondManual.html6The argument of functions is always placed between parenthesis ().

1.5. COMPUTING ON A DATA VECTOR5This sequence can also be produced by the function seq, which allows forvarious sizes of steps to be chosen. For instance, in order to compute percentiles of a distribution we may want to generate numbers between zero andone with step size equal to 0.1. seq(0,1,0.1)[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0For plotting and testing of hypotheses we need to generate yet anothertype of sequence, called a “factor”. It is designed to indicate an experimental condition of a measurement or the group to which a patient belongs.7When, for instance, for each of three experimental conditions there are measurements from five patients, the corresponding factor can be generated asfollows. factor - gl(3,5) factor[1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3Levels: 1 2 3The three conditions are often called “levels” of a factor. Each of theselevels has five repeats corresponding to the number of observations (patients)within each level (type of disease). We shall further illustrate the idea of afactor soon because it is very useful for purposes of visualization.1.5Computing on a data vectorA data vector is simply a collection of numbers obtained as outcomes frommeasurements. This can be illustrated by a simple example on expressionvalues of a gene. Suppose that gene expression values 1, 1.5, and 1.25 fromthe persons ”Eric”, ”Peter”, and ”Anna” are available. To store these in avector we use the concatenate command c(), as follows. gene1 - c(1.00,1.50,1.25) gene1[1] 1.00 1.50 1.257See e.g. Samuals & Witmer (2003, Chap. 8) for a full explanation of experimentsand statistical principles of design.

6CHAPTER 1. BRIEF INTRODUCTION INTO USING RNow we have created the object gene1 containing three gene expression values. To compute the sum, mean, and standard deviation of the gene expression values we use the corresponding built-in-functions. sum(gene1)[1] 3.75 mean(gene1)[1] 1.25 sum(gene1)/3[1] 1.25 sd(gene1)[1] 0.25 sqrt(sum((gene1-mean(gene1)) 2)/2)[1] 0.25By defining x1 P1.00, x2 1.50, and x3 1.25, the sum of the weightsPcanbe expressed as ni 1 xi 3.75. The mathematical summationisP symbolin R language simply sum. The mean is denoted by x 3i 1 xi /3 1.25and the sample standard deviation asvu 3uXs t (x x)2 /(3 1) 0.25.ii 11.6Constructing a data matrixIn various types of spreadsheets it is custom to store data values in theform of a matrix consisting of rows and columns. In bioinformatics geneexpression values (from several groups of patients) are stored as rows suchthat each row contains the expressions values of the patients correspondingto a particular gene and each column contains all gene expression values fora particular person. To illustrate this by a small example suppose that wehave the following expression values on three genes from Eric, Peter, andAnna.8 gene2 - c(1.35,1.55,1.00) gene3 - c(-1.10,-1.50,-1.25) gene4 - c(-1.20,-1.30,-1.00)8By the function data.entry you can open and edit a screen with the values of amatrix.

1.6. CONSTRUCTING A DATA MATRIX7Before constructing the matrix it is convenient to add the names of the rowsand the columns. To do so we construct the following list. rowcolnames - list(c("gene1","gene2","gene3","gene4"), c("Eric","Peter","Anna"))After the last comma in the first line we give a carriage return for R to comeup with a new line starting with in order to complete a command. Now wecan construct a matrix containing the expression values from our four genes,as follows. gendat - matrix(c(gene1,gene2,gene3,gene4), nrow 4, ncol 3, byrow TRUE, dimnames rowcolnames)Here, nrow indicates the number of rows and ncol the number of columns.The gene vectors are placed in the matrix as rows. The names of the rowsand columns are attached by the dimnames parameter. To see the content ofthe just created object gendat, we print it to the screen. gendatgene1gene2gene3gene4Eric Peter Anna1.00 1.50 1.251.35 1.55 1.30-1.10 -1.50 -1.25-1.20 -1.30 -1.00A matrix such as gendat has two indices [i,j], the first of which refers torows and the second to columns9 . Thus, if you want to print the secondelement of the first row to the screen, then type gendat[1,2]. If you wantto print the first row, then use gendat[1,]. For the second column, usegendat[,2].It may be desirable to write the data to a file for using these in a laterstage or to send these to a college of yours. Consider the following script. write.table(gendat,file "D:/data/gendat.Rdata") gendatread - read.table("D:/data/gendat.Rdata") gendatreadEric Peter Annagene1 1.00 1.50 1.259Indices referring to rows, columns, or elements are always between square brackets [].

8CHAPTER 1. BRIEF INTRODUCTION INTO USING Rgene2 1.35 1.55 1.30gene3 -1.10 -1.50 -1.25gene4 -1.20 -1.30 -1.00An alternative is to use write.csv.101.7Computing on a data matrixMeans or standard deviations of rows or columns are often important fordrawing biologically relevant conclusions. Such type of computations on adata matrix can be accomplished by “for loops”. However, it is much moreconvenient to use the apply functionality on a matrix. To do so we specifythe name of the matrix, indicate rows or columns (1 for rows and 2 forcolumns), and the name of the function. To illustrate this we compute themean of each person (column). apply(gendat,2,mean)Eric PeterAnna0.0125 0.0625 0.0750Similarly, the mean of each gene (row) can be computed. apply(gendat,1,mean)gene1gene2gene3gene41.250000 1.400000 -1.283333 -1.166667It frequently happens that we want to re-order the rows of a matrix accordingto a certain criterion, or, more specifically, the values in a certain columnvector. For instance, to re-order the matrix gendat according to the rowmeans, it is convenient to store these in a vector and to use the functionorder. meanexprsval - apply(gendat,1,mean) o - order(meanexprsval,decreasing TRUE) o[1] 2 1 4 310For more see the ”R Data import/Export” manual, Chapter 3 of the book ”R forBeginners”, or search the internet by the key ”r wiki matrix”.

1.7. COMPUTING ON A DATA MATRIX9Thus gene2 appears first because it has the largest mean 1.4, then gene1with 1.25, followed by gene4 with -1.16 and, finally, gene3 with -1.28. Nowthat we have collected the order numbers in the vector o, we can re-orderthe whole matrix by specifying o as the row index.11 gendat[o,]Eric Peter Annagene2 1.35 1.55 1.30gene1 1.00 1.50 1.25gene4 -1.20 -1.30 -1.00gene3 -1.10 -1.50 -1.25Another frequently occurring problem is that of selecting genes with a certainproperty. We illustrate th

Applied Statistics for Bioinformatics using R Wim P. Krijnen November 10, 2009. ii . all its outcomes are directly available as objects for further programming. R is a rapidly growing language making basic as well as advanced statisti- . to copy and paste scripts from the