Introduction to Scientific Programming and Simulation Using R

Owen Jones, Robert Maillardet, and Andrew Robinson

### Part I: Programming

1 Setting up
1.1 Installing R
1.2 Starting R
1.3 Working directory
1.4 Writing scripts
1.5 Help
1.6 Supporting material

2 R as a calculating environment
2.1 Arithmetic
2.2 Variables
2.3 Functions
2.4 Vectors
2.5 Missing data
2.6 Expressions and assignments
2.7 Logical expressions
2.8 Matrices
2.9 The workspace
2.10 Exercises

3 Basic programming
3.1 Introduction
3.2 Branching with if
3.3 Looping with for
3.4 Looping with while
3.5 Vector-based programming
3.6 Program flow
3.7 Basic debugging
3.8 Good programming habits
3.9 Exercises

4 I/O: Input and Output
4.1 Text
4.2 Input from a file
4.3 Input from the keyboard
4.4 Output to a file
4.5 Plotting
4.6 Exercises

5 Programming with functions
5.1 Functions
5.2 Scope and its consequences
5.3 Arguments
5.4 Vector-based programming using functions
5.5 Recursive programming
5.6 Debugging functions
5.7 Exercises

6 Sophisticated data structures
6.1 Factors
6.2 Dataframes
6.3 Lists
6.4 The apply family
6.5 Exercises

7 Better graphics
7.1 Introduction
7.2 Graphics parameters: par
7.3 Graphical augmentation
7.4 Mathematical typesetting
7.5 Permanence
7.6 Grouped graphs: lattice
7.7 3D plots
7.8 Exercises

8 Pointers to further programming techniques
8.1 Packages
8.2 Frames and environments
8.3 Debugging again
8.4 Object-oriented programming: S3
8.5 Object-oriented programming: S4
8.6 Manipulation of data
8.7 Compiled code
8.9 Exercises

### Part 2: II Numerical techniques

9 Numerical accuracy and program efficiency
9.1 Machine representation of numbers
9.2 Significant digits
9.3 Time
9.4 Loops versus vectors
9.5 Parallel processing
9.6 Memory
9.7 Caveat
9.8 Exercises

10 Root-finding
10.1 Introduction
10.2 Fixed-point iteration
10.3 The Newton-Raphson method
10.4 The secant method
10.5 The bisection method
10.6 Exercises

11 Numerical integration
11.1 Trapezoidal rule
11.2 Simpson’s rule
11.4 Exercises

12 Optimisation
12.1 Newton’s method for optimisation
12.2 The golden-section method
12.3 Multivariate optimisation
12.4 Steepest ascent
12.5 Newton’s method in higher dimensions
12.6 Optimisation in R and the wider world
12.7 A curve fitting example
12.8 Exercises
13 Systems of ordinary differential equations
13.1 Euler's method
13.2 Midpoint method
13.3 Fourth-order Range-Kutta
13.4 Efficiency
13.6 Exercises

### Part 3: III Probability and statistics

14 Probability
14.1 The probability axioms
14.2 Conditional probability
14.3 Independence
14.4 The Law of Total Probability
14.5 Bayes’ theorem
14.6 Exercises

15 Random variables
15.1 Definition and distribution function
15.2 Discrete and continuous random variables
15.3 Empirical cdf’s and histograms
15.4 Expectation and finite approximations
15.5 Transformations
15.6 Variance and standard deviation
15.7 The Weak Law of Large Numbers
15.8 Exercises

16 Discrete random variables
16.1 Discrete random variables in R
16.2 Bernoulli distribution
16.3 Binomial distribution
16.4 Geometric distribution
16.5 Negative binomial distribution
16.6 Poisson distribution
16.7 Exercises

17 Continuous random variables
17.1 Continuous random variables in R
17.2 Uniform distribution
17.3 Lifetime models: exponential and Weibull
17.4 The Poisson process and the gamma distribution
17.5 Sampling distributions: normal, χ2, and t
17.6 Exercises

18 Parameter Estimation
18.1 Point Estimation
18.2 The Central Limit Theorem
18.3 Confidence intervals
18.4 Monte-Carlo confidence intervals
18.5 Exercises
19 Markov chains
19.1 Introduction to discrete time chains
19.2 Basic formulae: discrete time
19.3 Classification of states
19.4 Limiting behaviour: discrete time
19.5 Finite absorbing chains
19.6 Introduction to continuous time chains
19.7 Rate matrix and associated equations
19.8 Limiting behaviour: continuous time
19.9 Defining the state space
19.10 Simulation
19.11 Estimation
19.12 Estimating the mean of the limiting distribution
19.13 Exercises

### Part 4: IV Simulation

20 Simulation
20.1 Simulating iid uniform samples
20.2 Simulating discrete random variables
20.3 Inversion method for continuous rv
20.4 Rejection method for continuous rv
20.5 Simulating normals
20.6 Exercises

21 Monte-Carlo integration
21.1 Hit-and-miss method
21.2 (Improved) Monte-Carlo integration
21.3 Exercises

22 Variance reduction
22.1 Antithetic sampling
22.2 Importance sampling
22.3 Control variates
22.4 Exercises

23 Case studies
23.1 Introduction
23.2 Epidemics
23.3 Inventory
23.4 Seed dispersal

24 Student projects
22.1 The level of a dam
22.2 Runoff down a slope
22.3 Roulette
22.4 Buffon’s needle and cross
22.5 The pipe spiders of Brunswick
22.6 Insurance risk
22.7 Squash
22.8 Stock prices
22.9 Conserving water

Glossary of R commands

Programs and functions developed in the text

Index

Home page - Introduction to Scientific Programming and Simulation Using R -- page last modified: 27 July, 2009 -- Maintained by rjmail at unimelb.edu.au