Programming 1
1 Setting up 3
1.1 Installing R 3
1.2 Starting R 3
1.3 Working directory 4
1.4 Writing scripts 5
1.5 Help 5
1.6 Supporting material 5
2 R as a calculating environment 11
2.1 Arithmetic 11
2.2 Variables 12
2.3 Functions 13
2.4 Vectors 15
2.5 Missing data 18
2.6 Expressions and assignments 19
2.7 Logical expressions 20
2.8 Matrices 23
2.9 The workspace 25
2.10 Exercises 25
3 Basic programming 29
3.1 Introduction 29
3.2 Branching with if 31
3.3 Looping with for 33
3.4 Looping with while 36
3.5 Vector-based programming 38
3.6 Program flow 39
3.7 Basic debugging 41
3.8 Good programming habits 42
3.9 Exercises 43
4 I/O: Input and Output 49
4.1 Text 49
4.1 Text 49
4.2 Input from a file 51
4.3 Input from the keyboard 53
4.4 Output to a file 55
4.5 Plotting 56
4.6 Exercises 58
5 Programming with functions 63
5.1 Functions 63
5.2 Scope and its consequences 68
5.3 Optional arguments and default values 70
5.4 Vector-based programming using functions 70
5.5 Recursive programming 74
5.6 Debugging functions 76
5.7 Exercises 78
6 Sophisticated data structures 85
6.1 Factors 85
6.2 Dataframes 88
6.3 Lists 94
6.4 The apply family 98
6.5 Exercises 105
7 Better graphics 109
7.1 Introduction 109
7.2 Graphics parameters: par 111
7.3 Graphical augmentation 113
7.4 Mathematical typesetting 114
7.5 Permanence 118
7.6 Grouped graphs: lattice 119
7.7 3D-plots 123
7.8 Exercises 124
8 Pointers to further programming techniques 127
8.1 Packages 127
8.2 Frames and environments 132
8.3 Debugging again 134
8.4 Object-oriented programming: S3 137
8.5 Object-oriented programming: S4 141
8.6 Compiled code 144
8.7 Further reading 146
8.8 Exercises 146
9 Numerical accuracy and program efficiency 151
9.1 Machine representation of numbers 151
9.2 Significant digits 154
9.3 Time 156
9.4 Loops versus vectors 158
9.5 Memory 160
10 Root-finding 167
10.1 Introduction 167
10.2 Fixed-point iteration 168
10.3 The Newton-Raphson method 173
10.4 The secant method 176
10.5 The bisection method 178
10.6 Exercises 181
11 Numerical integration 187
11.1 Trapezoidal rule 187
11.2 Simpson’s rule 189
11.3 Adaptive quadrature 194
11.4 Exercises 198
12 Optimisation 201
12.1 Newton’s method for optimisation 202
12.2 The golden-section method 204
12.3 Multivariate optimisation 207
12.4 Steepest ascent 209
12.5 Newton’s method in higher dimensions 213
12.6 Optimisation in R and the wider world 218
12.7 A curve fitting example 219
12.8 Exercises 220
13 Probability 227
13.1 The probability axioms 227
13.2 Conditional probability 230
13.3 Independence 232
13.4 The Law of Total Probability 233
13.5 Bayes’ theorem 234
13.6 Exercises 235
14 Random variables 241
14.1 Definition and distribution function 241
14.2 Discrete and continuous random variables 242
14.3 Empirical cdf’s and histograms 245
14.4 Expectation and finite approximations 246
14.5 Transformations 251
14.6 Variance and standard deviation 256
14.7 The Weak Law of Large Numbers 257
14.8 Exercises 261
15 Discrete random variables 267
15.1 Discrete random variables in R 267
15.2 Bernoulli distribution 268
15.3 Binomial distribution 268
15.4 Geometric distribution 270
15.5 Negative binomial distribution 273
15.6 Poisson distribution 274
15.7 Exercises 277
16 Continuous random variables 281
16.1 Continuous random variables in R 281
16.2 Uniform distribution 282
16.3 Lifetime models: exponential and Weibull 282
16.4 The Poisson process and the gamma distribution 287
16.5 Sampling distributions: normal, χ2, and t 292
16.6 Exercises 297
17 Parameter Estimation 303
17.1 Point Estimation 303
17.2 The Central Limit Theorem 309
17.3 Confidence intervals 314
17.4 Monte-Carlo confidence intervals 321
17.5 Exercises 322
18 Simulation 331
18.1 Simulating iid uniform samples 331
18.2 Simulating discrete random variables 333
18.3 Inversion method for continuous rv 338
18.4 Rejection method for continuous rv 339
18.5 Simulating normals 345
18.6 Exercises 348
19 Monte-Carlo integration 355
19.1 Hit-and-miss method 355
19.2 (Improved) Monte-Carlo integration 358
19.3 Exercises 360
20 Variance reduction 363
20.1 Antithetic sampling 363
20.2 Importance sampling 367
20.3 Control variates 372
20.4 Exercises 374
21 Case studies 377
21.1 Introduction 377
21.2 Epidemics 378
21.3 Inventory 390
21.4 Seed dispersal 405
22 Student projects 421
22.1 The level of a dam 421
22.2 Roulette 425
22.3 Buffon’s needle and cross 428
22.4 Insurance risk 430
22.5 Squash 433
22.6 Stock prices 438
Glossary of R commands 441
Programs and functions developed in the text 447 |