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 Optional arguments and default values
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 Compiled code
8.8 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 Memory
9.6 Caveat
9.7 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

### Part 3: III Probability and statistics

13 Probability
13.1 The probability axioms
13.2 Conditional probability
13.3 Independence
13.4 The Law of Total Probability
13.5 Bayes’ theorem
13.6 Exercises

14 Random variables
14.1 Definition and distribution function
14.2 Discrete and continuous random variables
14.3 Empirical cdf’s and histograms
14.4 Expectation and finite approximations
14.5 Transformations
14.6 Variance and standard deviation
14.7 The Weak Law of Large Numbers
14.8 Exercises

15 Discrete random variables
15.1 Discrete random variables in R
15.2 Bernoulli distribution
15.3 Binomial distribution
15.4 Geometric distribution
15.5 Negative binomial distribution
15.6 Poisson distribution
15.7 Exercises

16 Continuous random variables
16.1 Continuous random variables in R
16.2 Uniform distribution
16.3 Lifetime models: exponential and Weibull
16.4 The Poisson process and the gamma distribution
16.5 Sampling distributions: normal, χ2, and t
16.6 Exercises

17 Parameter Estimation
17.1 Point Estimation
17.2 The Central Limit Theorem
17.3 Confidence intervals
17.4 Monte-Carlo confidence intervals
17.5 Exercises

### Part 4: IV Simulation

18 Simulation
18.1 Simulating iid uniform samples
18.2 Simulating discrete random variables
18.3 Inversion method for continuous rv
18.4 Rejection method for continuous rv
18.5 Simulating normals
18.6 Exercises

19 Monte-Carlo integration
19.1 Hit-and-miss method
19.2 (Improved) Monte-Carlo integration
19.3 Exercises

20 Variance reduction
20.1 Antithetic sampling
20.2 Importance sampling
20.3 Control variates
20.4 Exercises

21 Case studies
21.1 Introduction
21.2 Epidemics
21.3 Inventory
21.4 Seed dispersal

22 Student projects
22.1 The level of a dam
22.2 Roulette
22.3 Buffon’s needle and cross
22.4 Insurance risk
22.5 Squash
22.6 Stock prices

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