Simulations and programming in R PowerPoint PPT Presentation

presentation player overlay
1 / 7
About This Presentation
Transcript and Presenter's Notes

Title: Simulations and programming in R


1
Simulations and programming in R
2
Why to simulate and program in R at all?
  • ADVANTAGES
  • All R facilities can be used in the simulations
  • Random number generators
  • Handy way of creating own R-functions
  • Simulation results are readily in R to be
    visualized and analyzed
  • DISADVANTAGES
  • Loops may be slow
  • Writing output may be slower than e.g. in C
    language
  • "Black-horse" solution
  • Compile C-code under R

3
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.
4
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
  • DISCRETE
  • Poisson distribution number count, e.g. number
    of fish caught
  • Binomial outcome of tossing coin, choice to
    metamorphose or not
  • (Multinomial same as binomial except more than
    two possible outcomes)

5
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()
  • -gt DEMO 1

6
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
7
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 condition )
  • Repeats the procedure until the logical
    conditions is FALSE
  • if ( a logical condition ) else
  • If the condition holds, does the first procedure,
    otherwise the second.

DEMO 3
Write a Comment
User Comments (0)
About PowerShow.com