an introduction to r: monte carlo simulation mwera 2012 emily a. price, ms marsha lewis, mpa dr....

Download An Introduction to R: Monte Carlo Simulation MWERA 2012 Emily A. Price, MS Marsha Lewis, MPA Dr. Gordon P. Brooks

Post on 16-Dec-2015




1 download

Embed Size (px)


  • Slide 1
  • An Introduction to R: Monte Carlo Simulation MWERA 2012 Emily A. Price, MS Marsha Lewis, MPA Dr. Gordon P. Brooks
  • Slide 2
  • Objectives and/or Goals Three main parts Data generation in R Basic Monte Carlo programming (e.g. loops) Running simulations (e.g., investigating Type I errors)
  • Slide 3
  • Why Use Monte Carlo Methods? According to Mooney (1997) Monte Carlo simulations are useful to Make inferences when weak statistical theory exists for an estimator Test null hypotheses under a variety of plausible conditions Assess the quality of an inference method Assess the robustness of parametric inference to assumption violations Compare estimators properties
  • Slide 4
  • What are Monte Carlo Methods? Experiments composed of random numbers to evaluate mathematical expressions (Gentle, 2003) Empirically determine the sampling distribution of a test statistic Computer-based methods for approximating values and properties of random variables (Braun & Murdoch, 2007)
  • Slide 5
  • Logic of Monte Carlo Mooney (1997) presents five steps 1.Specify the pseudo-population in symbolic terms in such a way that it can be used to generate samples. That is, writing code to generate data in a specific manner. 2.Sample from the pseudo-population in ways that reflect the topic of interest 3.Calculate in a pseudo-sample and store it in a vector 4.Repeat steps 2 and 3 t times where t is the number of trials 5.Construct a relative frequency distribution of resulting values which is a Monte Carlo estimate of the sampling distribution of under the conditions specified by the pseudo-population and the sampling procedures
  • Slide 6
  • Practical Issues/ Considerations What software to use? How much time to run the simulation? Reproducibility of results Adequacy of random number generator
  • Slide 7
  • Why use R? Its FREE It is a flexible language that can be controlled by the user It uses a vector based approach Depending on the package, there are built in commands which the user can access and minimize the amount of programming required for MC simulation Make sure to load the require packages at the beginning of the session R community has a plethora of information: help websites, listservs, textbooks, blogs Manuals for R available at
  • Slide 8
  • Part 1: Data Generation RNG and setting seed Purpose of the seed is to recovery results Initialize all parameters of interest Loops Print results Access output
  • Slide 9
  • Generating a Single Random Variable R has four parts: CDF, PDF, Quantile function and simulation procedure dnorm, pnorm, qnorm, rnorm respectively rnorm(x,mean=0,sd=1) runif(20,min=2,max=5) Distributions: normal, uniform, poisson, beta, gamma, chisquare, weibull, exponential
  • Slide 10
  • Try it, youll like it! rnorm(x,mean=0,sd=1) Generate a normal distribution of 50 values with a mean of 50 and sd of 10 x