A First Course in Statistical Programming with R
目录
1 Getting started 1
1.1 What is statistical programming? 1
1.2 Outline of the book 2
1.3 The R package 3
1.4 Why use a command line? 3
1.5 Font conventions 4
1.6 Installation of R 4
2 Introduction to the R language 5
2.1 Starting and quitting R 5
2.1.1 Recording your work 6
2.2 Basic features of R 7
2.2.1 Calculating with R 7
2.2.2 Named storage 7
2.2.3 Functions 9
2.2.4 Exact or approximate? 9
2.2.5 R is case-sensitive 12
2.2.6 Listing the objects in the workspace 12
2.2.7 Vectors 12
2.2.8 Extracting elements from vectors 13
2.2.9 Vector arithmetic 14
2.2.10 Simple patterned vectors 15
2.2.11 Missing values and other special values 16
2.2.12 Character vectors 16
2.2.13 Factors 17
2.2.14 More on extracting elements from vectors 18
2.2.15 Matrices and arrays 18
2.2.16 Data frames 19
2.2.17 Dates and times 21
2.3 Built-in functions and online help 21
2.3.1 Built-in examples 22
2.3.2 Finding help when you don’t know
the function name 23
2.3.3 Built-in graphics functions 23
2.3.4 Additional elementary built-in functions 25
2.4 Logical vectors and relational operators 26
2.4.1 Boolean algebra 26
2.4.2 Logical operations in R 27
2.4.3 Relational operators 28
2.5 Data input and output 29
2.5.1 Changing directories 29
vi CONTENTS
2.5.2 dump() and source() 29
2.5.3 Redirecting R output 30
2.5.4 Saving and retrieving image files 31
2.5.5 Data frames and the read.table function 31
2.5.6 Lists 31
Chapter exercises 32
3 Programming statistical graphics 33
3.1 High-level plots 33
3.1.1 Bar charts and dot charts 34
3.1.2 Pie charts 35
3.1.3 Histograms 35
3.1.4 Box plots 36
3.1.5 Scatterplots 38
3.1.6 QQ plots 39
3.2 Choosing a high-level graphic 41
3.3 Low-level graphics functions 42
3.3.1 The plotting region and margins 42
3.3.2 Adding to plots 43
3.3.3 Setting graphical parameters 45
Chapter exercises 46
4 Programming with R 47
4.1 Flow control 47
4.1.1 The for() loop 47
4.1.2 The if() statement 50
4.1.3 The while() loop 54
4.1.4 Newton’s method for root finding 55
4.1.5 The repeat loop, and the break and next statements 57
4.2 Managing complexity through functions 59
4.2.1 What are functions? 59
4.2.2 Scope of variables 62
4.3 Miscellaneous programming tips 63
4.3.1 Using fix() 63
4.3.2 Documentation using # 64
4.4 Some general programming guidelines 65
4.4.1 Top-down design 67
4.5 Debugging and maintenance 72
4.5.1 Recognizing that a bug exists 72
4.5.2 Make the bug reproducible 73
4.5.3 Identify the cause of the bug 73
4.5.4 Fixing errors and testing 75
4.5.5 Look for similar errors elsewhere 75
4.5.6 The browser() and debug() functions 75
4.6 Efficient programming 77
4.6.1 Learn your tools 77
4.6.2 Use efficient algorithms 78
4.6.3 Measure the time your program takes 79
CONTENTS vii
4.6.4 Be willing to use different tools 80
4.6.5 Optimize with care 80
Chapter exercises 80
5 Simulation 82
5.1 Monte Carlo simulation 82
5.2 Generation of pseudorandom numbers 83
5.3 Simulation of other random variables 88
5.3.1 Bernoulli random variables 88
5.3.2 Binomial random variables 89
5.3.3 Poisson random variables 93
5.3.4 Exponential random numbers 97
5.3.5 Normal random variables 99
5.4 Monte Carlo integration 101
5.5 Advanced simulation methods 104
5.5.1 Rejection sampling 104
5.5.2 Importance sampling 107
Chapter exercises 109
6 Computational linear algebra 112
6.1 Vectors and matrices in R 113
6.1.1 Constructing matrix objects 113
6.1.2 Accessing matrix elements; row and column names 115
6.1.3 Matrix properties 117
6.1.4 Triangular matrices 118
6.1.5 Matrix arithmetic 118
6.2 Matrix multiplication and inversion 119
6.2.1 Matrix inversion 120
6.2.2 The LU decomposition 121
6.2.3 Matrix inversion in R 122
6.2.4 Solving linear systems 123
6.3 Eigenvalues and eigenvectors 124
6.4 Advanced topics 125
6.4.1 The singular value decomposition of a matrix 125
6.4.2 The Choleski decomposition of a positive definite matrix 126
6.4.3 The QR decomposition of a matrix 127
6.4.4 The condition number of a matrix 128
6.4.5 Outer products 129
6.4.6 Kronecker products 129
6.4.7 apply() 129
Chapter exercises 130
7 Numerical optimization 132
7.1 The golden section search method 132
7.2 Newton–Raphson 135
7.3 The Nelder–Mead simplex method 138
7.4 Built-in functions 142
viii CONTENTS
7.5 Linear programming 142
7.5.1 Solving linear programming problems in R 145
7.5.2 Maximization and other kinds of constraints 145
7.5.3 Special situations 146
7.5.4 Unrestricted variables 149
7.5.5 Integer programming 150
7.5.6 Alternatives to lp() 151
7.5.7 Quadratic programming 151
Chapter exercises 157
下载: A First Course in Statistical Programming with R.rar(833.34 KB, 下载次数: 1, 售价: 5 )