Simulations and programming in R. Why to simulate and program in R at all? ADVANTAGES –All R...

7
Simulations and programming in R

Transcript of Simulations and programming in R. Why to simulate and program in R at all? ADVANTAGES –All R...

Page 1: Simulations and programming in R. Why to simulate and program in R at all? ADVANTAGES –All R facilities can be used in the simulations Random number generators.

Simulations and programming in R

Page 2: Simulations and programming in R. Why to simulate and program in R at all? ADVANTAGES –All R facilities can be used in the simulations Random number generators.

Why to simulate and program in R at all?

• ADVANTAGES– All R facilities can be used in the simulations

• Random number generators• Easy to create own R-functions

– Simulation results are readily in R to be visualized and analyzed

• DISADVANTAGES– Loops may be slow

• Alternative solution– Compile C-code under R

Page 3: Simulations and programming in R. Why to simulate and program in R at all? ADVANTAGES –All R facilities can be used in the simulations Random number generators.

Random numbers

• Random numbers are numbers drawn from a specific probability distribution

Area of a bar approximates the probability of getting a number in that interval.

These probabilities sum up to one.

Page 4: Simulations and programming in R. Why to simulate and program in R at all? ADVANTAGES –All R facilities can be used in the simulations Random number generators.

Most common distributions

CONTINUOUS• Normal distribution: e.g. weight and length of an individual• Exponential distribution: 'waiting time', e.g. lifetime of an individual• Uniform distribution: flat distribution, i.e. values do not concentrate

around some peak but are spread randomly within an interval

DISCRETE• Poisson distribution: number count, e.g. number of fish caught• Binomial: 0 or 1, e.g. outcome of tossing coin, choice to

metamorphose or not• (Multinomial: same as binomial except more than two possible

outcomes)

Page 5: Simulations and programming in R. Why to simulate and program in R at all? ADVANTAGES –All R facilities can be used in the simulations Random number generators.

Random number generators

• Random number tools for normal distribution

– rnorm() random number generator

– dnorm() density function (probability function for discrete distributions)

– pnorm() distribution function– qnorm() quantile function

• Similarly for binomial, Poisson, exponential, multinomial, uniform distributions… (and many others), e.g. – runif(), rexp(), rpois(), rbinom()

-> DEMO 1

Page 6: Simulations and programming in R. Why to simulate and program in R at all? ADVANTAGES –All R facilities can be used in the simulations Random number generators.

Creating an R-function

“Name of the function” = function( input parameters ){…}

Procedures to be carried out

All the input stuff needed for the procedures the function will perform

DEMO 2

Page 7: Simulations and programming in R. Why to simulate and program in R at all? ADVANTAGES –All R facilities can be used in the simulations Random number generators.

Basic programming loops in R

• Much of simulations is based on three loops:

• for( ‘index’ in ‘index vector’ ) {…}– Repeats the procedure for all the indices

• while(‘a logical proposition’) {…}– Repeats the procedure until the logical proposition is FALSE

• if (‘a logical proposition’) {…} else {…}– If the condition holds, does the first procedure, otherwise the second.

DEMO 3