2nd Edition

# Introduction to Scientific Programming and Simulation Using R

**Also available as eBook on:**

*Learn How to Program Stochastic Models*

Highly recommended, the best-selling first edition of **Introduction to Scientific Programming and Simulation Using R **was lauded as an excellent, easy-to-read introduction with extensive examples and exercises. This second edition continues to introduce scientific programming and stochastic modelling in a clear, practical, and thorough way. Readers learn programming by experimenting with the provided R code and data.

The book’s four parts teach:

- Core knowledge of R and programming concepts
- How to think about mathematics from a numerical point of view, including the application of these concepts to root finding, numerical integration, and optimisation
- Essentials of probability, random variables, and expectation required to understand simulation
- Stochastic modelling and simulation, including random number generation and Monte Carlo integration

In a new chapter on systems of ordinary differential equations (ODEs), the authors cover the Euler, midpoint, and fourth-order Runge-Kutta (RK4) schemes for solving systems of first-order ODEs. They compare the numerical efficiency of the different schemes experimentally and show how to improve the RK4 scheme by using an adaptive step size.

Another new chapter focuses on both discrete- and continuous-time Markov chains. It describes transition and rate matrices, classification of states, limiting behaviour, Kolmogorov forward and backward equations, finite absorbing chains, and expected hitting times. It also presents methods for simulating discrete- and continuous-time chains as well as techniques for defining the state space, including lumping states and supplementary variables.

Building readers’ statistical intuition, **Introduction to Scientific Programming and Simulation Using R, Second Edition** shows how to turn algorithms into code. It is designed for those who want to make tools, not just use them. The code and data are available for download from CRAN.

Table of Contents

Preface

How to use this book

Programming

Setting up

Installing R

Starting R

Working directory

Writing scripts

Help

Supporting material

R as a calculating environment

Arithmetic

Variables

Functions

Vectors

Missing data: NA

Expressions and assignments

Logical expressions

Matrices

The workspace

Exercises

Basic programming

Introduction

Branching with if

Looping with for

Looping with while

Vector-based programming

Program flow

Basic debugging

Good programming habits

Exercises

**Input and output**

Text

Input from a file

Input from the keyboard

Output to a file

Plotting

Exercises

Programming with functions

Functions

Arguments

Vector-based programming using functions

Recursive programming

Debugging functions

Exercises

Sophisticated data structures

Factors

Dataframes

Lists

Exercises

Better graphics

Introduction

Graphics parameters: par

Graphical augmentation

Mathematical typesetting

Permanence

Grouped graphs: lattice

Exercises

**Pointers to further programming techniques**

Packages

Frames and environments

Debugging again

Identifying bottlenecks

Object-oriented programming: S3

Object-oriented programming: S4

Manipulation of data

Compiled code

Further reading

Exercises

**Numerical accuracy and program efficiency**

Machine representation of numbers

Significant digits

Time

Loops versus vectors

Parallel processing

Memory

Caveat

Exercises

Root-finding

Introduction

Fixed-point iteration

The Newton–Raphson method

The secant method

The bisection method

Exercises

**Numerical integration**

Trapezoidal rule

Simpson’s rule

Adaptive quadrature 210

11.4 Exercises 214

Optimisation

Newton’s method for optimisation

The golden-section method

Multivariate optimisation

Steepest ascent

Newton’s method in higher dimensions

Optimisation in R and the wider world

A curve-fitting example

Exercises

**Systems of ordinary differential equations**

Euler’s method

Midpoint method

Fourth-order Runge–Kutta

Efficiency

Adaptive step size

Exercises

**Probability**

The probability axioms

Conditional probability

Independence

The Law of Total Probability

Bayes’ theorem

Exercises

Random variables

Definition and distribution function

Discrete and continuous random variables

Empirical cdf’s and histograms

Expectation and finite approximations

Transformations

Variance and standard deviation

The Weak Law of Large Numbers

Exercises

Discrete random variables

Discrete random variables in R

Bernoulli distribution

Binomial distribution

Geometric distribution

Negative binomial distribution

Poisson distribution

Exercises

**Continuous random variables**

Continuous random variables in R

Uniform distribution

Lifetime models: exponential and Weibull

The Poisson process and the gamma distribution

Sampling distributions: normal, χ2, and t

Exercises

**Parameter estimation**

Point estimation

The Central Limit Theorem

Confidence intervals

Monte Carlo confidence intervals

Exercises

Markov chains

Introduction to discrete time chains

Basic formulae: discrete time

Classification of states

Limiting behaviour: discrete time

Finite absorbing chains

Introduction to continuous time chains

Rate matrix and associated equations

Limiting behaviour: continuous time

Defining the state space

Simulation

Estimation

Estimating the mean of the limiting distribution

Exercises

**Simulation**

Simulating iid uniform samples

Simulating discrete random variables

Inversion method for continuous rv

Rejection method for continuous rv

Simulating normals

Exercises

**Monte Carlo integration**

Hit-and-miss method

(Improved) Monte Carlo integration

Exercises

**Variance reduction**

Antithetic sampling

Importance sampling

Control variates

Exercises

**Case studies**

Introduction

Epidemics

Inventory

Seed dispersal

**Student projects**

The level of a dam

Runoff down a slope

Roulette

Buffon’s needle and cross

The pipe spiders of Brunswick

Insurance risk

Squash

Stock prices

Conserving water

Glossary of R commands

Programs and functions developed in the text

Index

### Biography

Owen Jones, Robert Maillardet, Andrew Robinson

"The Introduction to Scientific Programming and Simulation Using R(2nd Edition) is a useful and well organized book. The writing is orderly, logical, consistent, intriguing, and engaging. We have read many programming and simulation oriented books that vary in context, scope, and difficulty level. This one turned out to be one of our favorites. It stands out in the sense that a decent dose of theory is given in addition to the programming related aspects. It covers an immense amount of material, yet manages to do so both thoroughly and clearly."

~Hakan Demirtas, Rachel Nordgren,University of Illinois at Chicago"Computation has become so central to the field of statistics that any practicing statistician must have a basic understanding of scientific programming and stochastic modeling.

Introduction to Scientific Programming and Simulation Using Rprovides an excellent entry-level text on the subject. This is a well written and well-designed book that will appeal to a wide readership and prove useful for several different types of courses. It provides a very good introduction to programming using the R language that has become widely used in statistical education and practice. It also introduces the fundamental tools needed for stochastic modeling: numerical analysis, probability, and simulation.

~Christopher H. Schmid, Journal of the American Statistical Association

Praise for the First Edition:"Overall, the authors have produced a highly readable text. As prerequisites do not go beyond first-year calculus, the book should appeal to a wide audience; it should also be eminently suitable for self-study. On a somewhat larger scale, it may help to further establish R as a kind of Swiss Army knife for computational science. I strongly recommend it."

~C. Kleiber, Universität Basel, Basel, Switzerland, inStatistical Papers, March 2012"This book is a good resource for someone who wants to learn R and use R for statistical computing and graphics. It will also serve well as a textbook or a reference book for students in a course related to computational statistics."

~Hon Keung Tony Ng,Technometrics, May 2011"… a very coherent and useful account of its chosen subject matter. … The programming section … is more comprehensive than Braun & Murdoch (2007), but more accessible than Venables & Ripley (2000). … The book deserves a place on university library shelves … One very useful feature of the book is that nearly every chapter has a set of exercises. There are also plenty of well-chosen examples throughout the book that are used to explain the material. I also appreciated the clear and attractive programming style of the R code presented in the book. I found very little in the way of typos or solecisms. … I can strongly recommend the book for its intended audience. If I ever again have to teach our stochastic modelling course, I will undoubtedly use some of the exercises and examples from

Scientific Programming and Simulation Using R."

~David Scott,Australian & New Zealand Journal of Statistics, 2011"It is not often that I think that a statistics text is one that most scientifc statisticians should have in their personal libraries.

Introduction to Scientific Programming and Simulation Using Ris such a text. … This text provides scientific researchers with a working knowledge of R for both reviewing and for engaging in the statistical evaluation of scientific data. …It is particularly useful for understanding and developing modeling and simulation software. I highly recommend the text, finding it to be one of the most useful books I have read on the subject."

—Journal of Statistical Software, September 2010, Volume 36"The authors have written an excellent introduction to scientific programming with R. Their clear prose, logical structure, well-documented code and realistic examples made the book a pleasure to read. One particularly useful feature is the chapter of cases studies at the end, which not only demonstrates complete analyses but also acts as a pedagogical tool to review and integrate material introduced throughout the book. … I would strongly recommend this book for readers interested in using R for simulations, particularly for those new to scientific programming or R. It is also very student-friendly and would be suitable either as a course textbook or for self-study."

—Significance, September 2009"I think that the techniques of scientific programming presented will soon enable the novice to apply statistical models to real-world problems. The writing style is easy to read and the book is suitable for private study. If you have never read a book on scientific programming and simulation, then I recommend that you start with this one."

—International Statistical Review, 2009