Preface xiii
I An Overview of R 1
1 Main Concepts 3
1.1 Installing R . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Work Session . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.1 Session in Linux . . . . . . . . . . . . . . . . . . . . . 4
1.2.2 Session in Windows . . . . . . . . . . . . . . . . . . . 4
1.2.3 Session on a Mac . . . . . . . . . . . . . . . . . . . . . 5
1.3 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.1 Online Assistance . . . . . . . . . . . . . . . . . . . . . 5
1.3.2 Help on CRAN . . . . . . . . . . . . . . . . . . . . . . 6
1.4 R Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4.1 Creating, Displaying and Deleting Objects . . . . . . . 6
1.4.2 Type of Object . . . . . . . . . . . . . . . . . . . . . . 7
1.4.3 The Missing Value . . . . . . . . . . . . . . . . . . . . 8
1.4.4 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.4.4.1 Numeric Vectors . . . . . . . . . . . . . . . . 9
1.4.4.2 Character Vectors . . . . . . . . . . . . . . . 10
1.4.4.3 Logical Vectors . . . . . . . . . . . . . . . . . 11
1.4.4.4 Selecting Part of a Vector . . . . . . . . . . . 12
1.4.4.5 Selection in Practice . . . . . . . . . . . . . . 13
1.4.5 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4.5.1 Selecting Elements or Part of a Matrix . . . 14
1.4.5.2 Calculating with the Matrices . . . . . . . . 16
1.4.5.3 Row and Column Operations . . . . . . . . . 17
1.4.6 Factors . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.4.7 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.4.7.1 Creating a List . . . . . . . . . . . . . . . . . 20
1.4.7.2 Extraction . . . . . . . . . . . . . . . . . . . 20
1.4.7.3 A Few List Functions . . . . . . . . . . . . . 21
1.4.7.4 Dimnames List . . . . . . . . . . . . . . . . . 21
1.4.8 Data-Frames . . . . . . . . . . . . . . . . . . . . . . . 22
1.5 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.5.1 Arguments of a Function . . . . . . . . . . . . . . . . 23
v
R for Statistics
1.5.2 Output . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.6 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.6.1 What Is a Package ? . . . . . . . . . . . . . . . . . . . 24
1.6.2 Installing a Package . . . . . . . . . . . . . . . . . . . 24
1.6.3 Updating Packages . . . . . . . . . . . . . . . . . . . . 25
1.6.4 Using Packages . . . . . . . . . . . . . . . . . . . . . . 25
1.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2 Preparing Data 29
2.1 Reading Data from File . . . . . . . . . . . . . . . . . . . . . 29
2.2 Exporting Results . . . . . . . . . . . . . . . . . . . . . . . . 33
2.3 Manipulating Variables . . . . . . . . . . . . . . . . . . . . . 34
2.3.1 Changing Type . . . . . . . . . . . . . . . . . . . . . . 34
2.3.2 Dividing into Classes . . . . . . . . . . . . . . . . . . . 35
2.3.3 Working on Factor Levels . . . . . . . . . . . . . . . . 36
2.4 Manipulating Individuals . . . . . . . . . . . . . . . . . . . . 39
2.4.1 Identifying Missing Data . . . . . . . . . . . . . . . . . 39
2.4.2 Finding Outliers . . . . . . . . . . . . . . . . . . . . . 42
2.5 Concatenating Data Tables . . . . . . . . . . . . . . . . . . . 43
2.6 Cross-Tabulation . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3 R Graphics 51
3.1 Conventional Graphical Functions . . . . . . . . . . . . . . . 51
3.1.1 The Plot Function . . . . . . . . . . . . . . . . . . . . 52
3.1.2 Representing a Distribution . . . . . . . . . . . . . . . 58
3.1.3 Adding to Graphs . . . . . . . . . . . . . . . . . . . . 60
3.1.4 Graphs with Multiple Dimensions . . . . . . . . . . . 64
3.1.5 Exporting Graphs . . . . . . . . . . . . . . . . . . . . 66
3.1.6 Multiple Graphs . . . . . . . . . . . . . . . . . . . . . 67
3.1.7 Multiple Windows . . . . . . . . . . . . . . . . . . . . 69
3.1.8 Improving and Personalising Graphs . . . . . . . . . . 70
3.2 Graphical Functions with lattice . . . . . . . . . . . . . . . . 73
3.2.1 Characteristics of a \Lattice" Graph . . . . . . . . . . 75
3.2.2 Formulae and Groups . . . . . . . . . . . . . . . . . . 76
3.2.3 Customising Graphs . . . . . . . . . . . . . . . . . . . 79
3.2.3.1 Panel Function . . . . . . . . . . . . . . . . . 79
3.2.3.2 Controlling Size . . . . . . . . . . . . . . . . 80
3.2.3.3 Panel Layout . . . . . . . . . . . . . . . . . . 81
3.2.4 Exportation . . . . . . . . . . . . . . . . . . . . . . . . 81
3.2.5 Other Packages . . . . . . . . . . . . . . . . . . . . . . 82
3.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
4 Making Programs with R 87
4.1 Control Flows . . . . . . . . . . . . . . . . . . . . . . . . . . 87
4.1.1 Grouped Expressions . . . . . . . . . . . . . . . . . . . 87
4.1.2 Loops (for or while) . . . . . . . . . . . . . . . . . . . 87
4.1.3 Conditions (if, else) . . . . . . . . . . . . . . . . . . . 89
4.2 Predened Functions . . . . . . . . . . . . . . . . . . . . . . 90
4.3 Creating a Function . . . . . . . . . . . . . . . . . . . . . . . 97
4.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
II Statistical Methods 101
Introduction to the Statistical Methods Part 103
5 A Quick Start with R 105
5.1 Installing R . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
5.2 Opening and Closing R . . . . . . . . . . . . . . . . . . . . . 105
5.3 The Command Prompt . . . . . . . . . . . . . . . . . . . . . 105
5.4 Attribution, Objects, and Function . . . . . . . . . . . . . . 106
5.5 Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
5.6 Other . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
5.7 Rcmdr Package . . . . . . . . . . . . . . . . . . . . . . . . . . 107
5.8 Importing (or Inputting) Data . . . . . . . . . . . . . . . . . 107
5.9 Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
5.10 Statistical Analysis . . . . . . . . . . . . . . . . . . . . . . . 108
6 Hypothesis Test 109
6.1 Condence Intervals for a Mean . . . . . . . . . . . . . . . . 109
6.1.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . 109
6.1.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . 109
6.1.3 Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
6.1.4 Processing the Example . . . . . . . . . . . . . . . . . 110
6.1.5 Rcmdr Corner . . . . . . . . . . . . . . . . . . . . . . . 112
6.1.6 Taking Things Further . . . . . . . . . . . . . . . . . . 112
6.2 Chi-Square Test of Independence . . . . . . . . . . . . . . . . 113
6.2.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . 113
6.2.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . 113
6.2.3 Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
6.2.4 Processing the Example . . . . . . . . . . . . . . . . . 114
6.2.5 Rcmdr Corner . . . . . . . . . . . . . . . . . . . . . . . 116
6.2.6 Taking Things Further . . . . . . . . . . . . . . . . . . 117
6.3 Comparison of Two Means . . . . . . . . . . . . . . . . . . . 118
6.3.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . 118
6.3.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . 118
6.3.3 Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
6.3.4 Processing the Example . . . . . . . . . . . . . . . . . 119
6.3.5 Rcmdr Corner . . . . . . . . . . . . . . . . . . . . . . . 123
6.3.6 Taking Things Further . . . . . . . . . . . . . . . . . . 124
6.4 Testing Conformity of a Proportion . . . . . . . . . . . . . . 125
6.4.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . 125
6.4.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . 125
6.4.3 Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
6.4.4 Processing the Example . . . . . . . . . . . . . . . . . 125
6.4.5 Rcmdr Corner . . . . . . . . . . . . . . . . . . . . . . . 126
6.4.6 Taking Things Further . . . . . . . . . . . . . . . . . . 126
6.5 Comparing Several Proportions . . . . . . . . . . . . . . . . 127
6.5.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . 127
6.5.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . 127
6.5.3 Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
6.5.4 Processing the Example . . . . . . . . . . . . . . . . . 127
6.5.5 Rcmdr Corner . . . . . . . . . . . . . . . . . . . . . . . 128
6.5.6 Taking Things Further . . . . . . . . . . . . . . . . . . 128
6.6 The Power of a Test . . . . . . . . . . . . . . . . . . . . . . . 129
6.6.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . 129
6.6.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . 129
6.6.3 Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
6.6.4 Processing the Example . . . . . . . . . . . . . . . . . 130
6.6.5 Rcmdr Corner . . . . . . . . . . . . . . . . . . . . . . . 130
6.6.6 Taking Things Further . . . . . . . . . . . . . . . . . . 131
7 Regression 133
7.1 Simple Linear Regression . . . . . . . . . . . . . . . . . . . . 133
7.1.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . 133
7.1.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . 134
7.1.3 S
Bibliography 295
Index of the Functions 297
Index 301