好书
Introduction to the R Project for Statistical Computing for use at ITC
Contents
1 What is R? 1
2 Why R for ITC? 2
2.1 Advantages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Disadvantages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.3 Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3.1 S-PLUS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3.2 Statistical packages . . . . . . . . . . . . . . . . . . . . . . 5
2.3.3 Special-purpose statistical programs . . . . . . . . . . . 5
2.3.4 Spreadsheets . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3.5 Applied mathematics programs . . . . . . . . . . . . . . 6
3 Using R for Windows 6
3.1 R on the ITC network . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2 Starting R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.3 Stopping R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.4 Setting up a workspace . . . . . . . . . . . . . . . . . . . . . . . . 7
3.5 The command prompt . . . . . . . . . . . . . . . . . . . . . . . . 8
3.6 On-line help in R . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.7 Internet help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.8 Saving your analysis steps . . . . . . . . . . . . . . . . . . . . . . 10
3.9 Saving your graphs . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.10Writing and running scripts . . . . . . . . . . . . . . . . . . . . . 11
3.11 The Tinn-R code editor . . . . . . . . . . . . . . . . . . . . . . . 12
3.12 Using the Rcmdr GUI . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.13 Loading optional packages . . . . . . . . . . . . . . . . . . . . . . 13
3.14 Sample datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
The S language 16
4.1 Command-line calculator and mathematical operators . . . . 16
4.2 Creating new objects: the assignment operator . . . . . . . . . 17
4.3 Methods and their arguments . . . . . . . . . . . . . . . . . . . . 18
4.4 Vectorized operations and re-cycling . . . . . . . . . . . . . . . 19
4.5 Vector and list data structures . . . . . . . . . . . . . . . . . . . 21
4.6 Arrays and matrices . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.7 Data frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4.8 Factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.9 Selecting subsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4.9.1 Simultaneous operations on subsets . . . . . . . . . . . 35
4.10 Rearranging data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
4.11 Random numbers and simulation . . . . . . . . . . . . . . . . . 37
4.12 Character strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
4.13 Objects and classes . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4.13.1 The S3 and S4 class systems . . . . . . . . . . . . . . . . 41
4.14 Descriptive statistics . . . . . . . . . . . . . . . . . . . . . . . . . 44
4.15 Classification tables . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.16 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.17 Statistical models in S . . . . . . . . . . . . . . . . . . . . . . . . . 47
4.17.1 Models with categorical predictors . . . . . . . . . . . . 51
4.17.2 Analysis of Variance (ANOVA) . . . . . . . . . . . . . . . 52
4.18Model output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.18.1 Model diagnostics . . . . . . . . . . . . . . . . . . . . . . . 54
4.18.2 Model-based prediction . . . . . . . . . . . . . . . . . . . 56
4.19 Advanced statistical modelling . . . . . . . . . . . . . . . . . . . 57
4.20Missing values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
4.21 Control structures and looping . . . . . . . . . . . . . . . . . . . 59
4.22 User-defined functions . . . . . . . . . . . . . . . . . . . . . . . . 60
4.23 Computing on the language . . . . . . . . . . . . . . . . . . . . . 62
5 R graphics 64
5.1 Base graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.1.1 Types of base graphics plots . . . . . . . . . . . . . . . . 68
5.1.2 Interacting with base graphics plots . . . . . . . . . . . . 69
5.2 Trellis graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
5.2.1 Types of Trellis graphics plots . . . . . . . . . . . . . . . 76
5.2.2 Adjusting Trellis graphics parameters . . . . . . . . . . 76
5.3 Multiple graphics windows . . . . . . . . . . . . . . . . . . . . . . 78
5.4 Multiple graphs in the same window . . . . . . . . . . . . . . . 78
5.5 Colours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
6 Preparing your own data for R 84
6.1 Preparing data directly in R . . . . . . . . . . . . . . . . . . . . . 84
6.2 A GUI data editor . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
6.3 Importing data from a CSV file . . . . . . . . . . . . . . . . . . . 86
6.4 Importing images . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
7 Exporting from R 90
8 Miscellaneous R tricks 92
8.1 Setting up a regular grid . . . . . . . . . . . . . . . . . . . . . . . 92
8.2 Setting up a random sampling scheme . . . . . . . . . . . . . . 92
9 Learning R 94
9.1 R tutorials and introductions . . . . . . . . . . . . . . . . . . . . 94
9.2 Textbooks using R . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
9.3 Technical notes using R . . . . . . . . . . . . . . . . . . . . . . . 96
9.4 Web Pages to learn R . . . . . . . . . . . . . . . . . . . . . . . . . 96
9.5 Keeping up with developments in R . . . . . . . . . . . . . . . . 97
10 Frequently-asked questions 100
10.1 Help! I got an error, what did I do wrong? . . . . . . . . . . . . 100
10.2Why didn’t my command(s) do what I expected? . . . . . . . . 102
10.3 How do I find the method to do what I want? . . . . . . . . . . 103
10.4Memory problems . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
10.5What version of R am I running? . . . . . . . . . . . . . . . . . . 106
10.6What statistical procedure should I use? . . . . . . . . . . . . . 107
A Obtaining your own copy of R 108
A.1 Installing new packages . . . . . . . . . . . . . . . . . . . . . . . 110
A.2 Customizing your installation . . . . . . . . . . . . . . . . . . . . 110
A.3 R in different human languages . . . . . . . . . . . . . . . . . . . 111
B An example script 112
C An example function 115


雷达卡



京公网安备 11010802022788号







