less than 1 minute read

R Simulation and Profiling

We can simulate random distributions and sampling in R by using:

  • rnorm, pnorm, qnorm, dnorm.
  • rpois, ppois, qpois, dpois.
  • rbinom, pbinom, qbinom, dbinom
  • and so on ….

The ones with prefix r like rnorm, rpois, rbinom are used as random number generators which sample from their respective distributions. We also have a sample function which allows us to sample from our own probability distributions or vectors.

The R profiler Rprof() and system.time() are useful tools to help optimize our code since they let us see where the bottlenecks are. system.time() is useful when we actually know what is causing the bottleneck so we can look at how long its execution takes (user, elapsed times). The profiler actually looks at the function call stack and tells us how long we are spending in which calls. It has by.total and by.self. by self is better since by.total gives 100pc for all top level functions.