an introduction to r: monte carlo simulation
DESCRIPTION
An Introduction to R: Monte Carlo Simulation. MWERA 2012 Emily A. Price, MS Marsha Lewis, MPA Dr . Gordon P. Brooks. 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). - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/1.jpg)
An Introduction to R: Monte Carlo Simulation
MWERA 2012Emily A. Price, MS
Marsha Lewis, MPA Dr. Gordon P. Brooks
![Page 2: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/2.jpg)
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)
![Page 3: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/3.jpg)
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 estimator’s properties
![Page 4: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/4.jpg)
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)
![Page 5: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/5.jpg)
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 vector4. Repeat steps 2 and 3 t times where t is the number of trials5. 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
![Page 6: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/6.jpg)
Practical Issues/ Considerations
• What software to use?• How much time to run the simulation?• Reproducibility of results • Adequacy of random number generator
![Page 7: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/7.jpg)
Why use R?• It’s 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 http://cran.r-project.org/manuals.html
![Page 8: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/8.jpg)
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
![Page 9: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/9.jpg)
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
![Page 10: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/10.jpg)
Try it, you’ll 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 <- sample(1:2,20,TRUE,prob=c(1/2,1/2))Generate data that mimics rolling a die
![Page 11: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/11.jpg)
Generating Correlated Data• X~Normal (20, 5), Y~Normal (40, 10),
corr(X,Y) =0.6– 4 inputs
• Sample size, mean, variance-covariance matrix, and method
– 3 methods of data generation• Eigenvalue (default), Singular Value, and
Cholesky
![Page 12: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/12.jpg)
Try it, you’ll like it!• rmvnorm(n, mean, sigma, method)
Generate data for 3 variables such that X --Normal (20, 5), Y-- Normal (40, 10), Z -- Normal (60,15) and Corr(X,Y) =0.6, Corr(X,Z) = 0.7, Corr(Y,Z)=0.8
![Page 13: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/13.jpg)
Part 2: Basic MC Programming
• Four steps (Braun & Murdoch, 2007)1. Understand the problem2. Work out a general idea how to solve it
• Flow charts 3. Translate your general idea into a
detailed implementation• Turn the flowchart into code
4. Check: Does it work?
![Page 14: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/14.jpg)
Programming Commands*• Loops
– for, if, ifelse, while • Statements
– repeat, break, next* We can’t cover all programming aspects but wanted to mention other commands
![Page 15: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/15.jpg)
Functions• They are “self-contained units with a well-defined
purpose” (Braun & Murdoch, 2007, p. 59)– Take an input, do some calculations, and produce an output
• In R, functions are objects and can be manipulated like other more common objects such as vectors, matrices, and lists.– R provides source code for its own functions
• R allows you to write your own functions
![Page 16: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/16.jpg)
Part 3: Running Simulations
• Trimmed mean sampling distribution
• Replicating a published Monte Carlo study in R. – Zimmerman, D. W. (2004). A note on preliminary tests of
equality of variances. British Journal of Mathematical and Statistical Psychology 57, 173–181.
![Page 17: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/17.jpg)
Questions
• Thank you for your time
![Page 18: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/18.jpg)
References • Braun, W. J., & Murdoch, D. J. (2007). A first course in statistical
programming with R. New York: Cambridge University. • Gentle, J. E. (2003). Random number generation and Monte
Carlo methods (2nd ed.). New York: Springer-Verlag. • Mooney, C. Z. (1997). Monte Carlo simulation (Sage University
Paper series on Quantitative Applications in the Social Sciences, series no. 07-116). Thousand Oaks, CA: Sage.
• Zimmerman, D. W. (2004). A note on preliminary tests of equality of variances. British Journal of Mathematical and Statistical Psychology 57, 173–181.
![Page 19: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/19.jpg)
• Our code
![Page 20: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/20.jpg)
![Page 21: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/21.jpg)
![Page 22: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/22.jpg)
![Page 23: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/23.jpg)
![Page 24: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/24.jpg)
![Page 25: An Introduction to R: Monte Carlo Simulation](https://reader036.vdocuments.us/reader036/viewer/2022081420/56816136550346895dd0919b/html5/thumbnails/25.jpg)