Introduction to Simulations in R
About this work
This tutorial was created by Malika Ihle based on materials from Joel Pick, Hadley Wickham, and Kevin Hallgren, with contributions from James Smith.
It is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Prerequisites
- Have R and RStudio installed. If you don’t, follow these instructions.
- Know some R basics (e.g. how to select a value in a data frame, how to create a vector). If you don’t, visit the following tutorial: https://lmu-osc.github.io/introduction-to-R/.
Prior to the session: optional preparation to get familiarised with the subject
Watch this 30-minute introduction to credible research, which contextualises the importance of simulations for reliable research.
Self-paced workshop
How it works
The self-paced tutorial (pages linked below) will alternate presentation of concepts and simple exercises for you to try to apply them in R. Each time you see written YOUR TURN, switch to your local copy of the exercise script (you can choose between a file with or without the solutions depending on e.g. your level of familiarity with R), review the examples if needed, complete the exercise, and check out the proposed answer (which often contains additional tips). Come back to the online tutorial and after finishing one page, you can navigate to the next page linked at the bottom to continue. The exercise script contains code for all the exercises and code that generates the plots that appear in the online tutorial, all in order of appearance in the tutorial.
It is necessary that you work through the sections of the tutorial in order. Please read the blurbs of each section below to get an overview of this workshop. Then click on the first page ‘Download the material’ and follow along by navigating to the next page linked at the bottom of each page! You can get back to this overview at any time by clicking on the title ‘Introduction-Simulations-in-R’ at the top of each page.
Tutorial
- Download the material – Get this tutorial onto your machine.
- Definition – What are simulations?
- Purpose – What can we use simulations for?
- Basic principles – What do we need to create a simulation?
- Random number generators – How to generate random numbers in R?
- Repeat – How to repeat the generation of random numbers multiple times?
- Setting the seed – How can you generate the same random numbers?
- Sample size
n
– How many values should you generate within a simulation? - Number of repetitions
nrep
– How many repeats of a simulation should you run? - DRY rule – How to write your own functions?
- Simulate to check alpha – Write your first simulation and check the rate of false-positive findings.
- Simulate to check power – Simulate data to perform a power analysis.
- Simulate to prepare a preregistration – Simulate data to test statistical analyses before preregistering them.
- General structure – What is the general structure of a simulation?
- Limitations – What are the limitations of simulations?
- Real-life example – What are real-life examples of simulations?
- Additional resources – What resources can help you write your own simulation?