Running simulations and power analysis in R

Tejaswini Sharma

18/02/2026

Licence


Creative Commons Attribution 4.0

This work was originally created by Malika Ihle based on materials from Joel Pick, Hadley Wickham, and Kevin Hallgren, with contributions from James Smith. This current work by Tejaswini Sharma, Sarah von Grebmer zu Wolfsthurn and Malika Ihle is licensed under a CC-BY-SA 4.0 Creative Commons Attribution 4.0 International SA License. It permits unrestricted re-use, distribution, and reproduction in any medium, provided the original work is properly cited. If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.

Contribution statement


Creator: Sharma, Tejaswini (orcid logo0009-0000-0305-9751)

Reviewer: Von Grebmer zu Wolfsthurn, Sarah (orcid logo0000-0002-6413-3895)

Consultant: Ihle, Malika (orcid logo0000-0002-3242-5981)

Prerequisites

Important

Before completing this submodule, please carefully read about the prerequisites.

Prerequisite Description Link/Where to find it
R and RStudio installed Latest R version: ‘4.4.0+’ and RStudio version: ‘2026.01.0+392’ Installation Link
R basics e.g., how to select a value in a data frame, how to create a vector Tutorial Link
Familiarity with basic statistical concepts e.g., hypothesis testing, descriptive statistics, data analysis Cheatsheet Link

Before we start: Survey time!


Let us find out where are you at!

How confident are you in defining what a “simulation” is in the context of scientific research?

  1. Not confident.

  2. Somewhat confident.

  3. Confident.

  4. Very confident

Simulations are often used in research to… (Select all you think apply)

  1. Generate artificial data

  2. Visualize complex statistical concepts

  3. Replace all statistical analyses

  4. Test how models perform under different scenarios

  5. No clue

How familiar are you with the concept of “power” in statistics?

  1. I have never heard of it

  2. I heard the term before, but do not know what it means

  3. I understand the basics of “power”

  4. I am comfortable with calculating power

Power analysis is important for research because… (Select all you think apply)

  1. It helps determine how many samples you need

  2. It ensures studies always find significant results

  3. It helps you design more reliable studies

  4. It explains all statistical results

  5. No clue

How comfortable are you with performing simulations in R?

  1. Very uncomfortable

  2. Somewhat uncomfortable

  3. Neutral

  4. Somewhat comfortable

  5. Very comfortable

Discussion of survey results


What do we see in the results?

Learning goals

  • To understand the concept of simulations
  • To explain the purpose of simulations in research
  • To summarize how simulations support hypothesis testing and experimentation
  • To perform the simulation process in the context of a fictional experiment
  • To understand the concept of power analysis
  • To explain the purpose of power analysis in research
  • To summarize how power analysis supports hypothesis testing and experimentation
  • To successfully perform a simulation in R

Key terms and definitions

  • Simulations: Simulations are computer experiments that generate artificial data by pseudo-random sampling, allowing researchers to evaluate statistical methods under controlled, known conditions (Morris et al., 2019).
  • Power analysis: Power analysis is a statistical method used to determine the probability that a study will detect a true effect (if it exists) or, equivalently, the probability of correctly rejecting a false null hypothesis (Steidl et al., 1997).
  • Effect size: Effect size quantifies the magnitude and direction of a difference or relationship between groups or variables (Lorah, 2018).

What are simulations?

Let’s flip a coin

  • Imagine you want to flip a coin 100 times, and see the results.

  • Actually flipping the coin -> Pretend to flip in mind…

  • And write down the results each time.

  • This pretending is called a simulation.

What are simulations?

  • In a simulation, you make up data that acts like what you expect in the real world.
  • You do this again and again and see what results you get.

What are simulations?

  • In a simulation, you make up data that acts like what you expect in the real world.
  • You do this again and again and see what results you get.

Your turn!
Get in a pair, first imagine flipping a coin 100 times and note how many heads and tails you expect. Then, discuss why doing this pretend experiment first is useful for planning a real study.

Why are simulations used in research?



What ideas have you come up with during the previous activity?

Why are simulations used in research?

  1. To build “good feeling” about data (intuition)

  2. To understand chances (probability)

  3. To check if an experiment is strong enough (power)

  4. To practice before the real experiment (planning)

  5. To contribute to open research (transparency)

Basic simulation process

Your turn!

Time to apply the simulation process to your coin-flip experiment. Get in your pair, and discuss each step according to your experiment.

For example, if you flip a coin a 10 times (n = 10), your data could be H, T, H, H, T, T, H, T, H, H (random data).

Learning goals: Check-in

  • To understand the concept of simulations ✅
  • To explain the purpose of simulations in research ✅
  • To summarize how simulations support hypothesis testing and experimentation ✅
  • To perform the simulation process in the context of a fictional experiment ✅

What is power analysis?

Let’s flip the coin again, but how many times?

  • Power analysis is like figuring out how many times you need to flip a coin -> to be pretty sure you can tell if the coin is fair or not.

  • Power = your chance of catching the coin’s true nature if it really is biased.

  • If power = 80% -> you have an 80% chance of spotting a bias if it’s really there.

What is power analysis?

Power depends on:

  • The effect size (= how biased the coin might be)

  • The sample size (= how many times you flip the coin)

  • The significance level (= how sure you want to be)

Your turn!

Get together in your pair, and discuss why conducting power analysis for your experiment could be useful.

Why is power analysis used in research?



What ideas have you come up with during the previous activity?

Why is power analysis used in research?

  1. Prevents underpowered studies: By helping you determine the minimum sample size, it makes sure you do not do an experiment that is too weak to detect an effect that is there.

  2. Minimizes wasting resources: Helps you figure out the most efficient experimental design, which in terms minimizes wasting time, money or human resources.

  3. Required for transparency and publication: Represents evidence that your experimental plan is thought through, e.g., the sample size, and others can replicate your methods (i.e., in e spirit of Open Research)

Performing a power analysis

Two ways:

  1. Power analysis through a formula = method that uses a mathematical formula to estimate your sample size or effect size (see G*Power). For example, the formula could be used to quickly estimate your required sample size for your experiment. Works best for simple study designs (e.g., one-sample t-test, simple linear regression).

  2. Power analysis through simulations = pretending to do the experiment many times on a computer before actually doing it. You input what you expect to happen (e.g., study design, means, etc.). The experiment “runs” using these parameters and you see how often the simulated experiment correctly detects an effect. Works well for complex study designs (e.g., longitudinal data) and unusual data. (This is today’s focus).

Basic simulation process

In-class Activity

Time to apply the power analysis process to your coin-flip experiment. Get in your pair, and discuss when and how you would integrate a power analysis.

Your turn!

In this next part, you will familiarize yourself with simulations through hands-on exercices and activities:

Tip

Since this is a self-paced tutorial, take your time on navigating it; and it can be finished at home as well. We will have a check-in moment at the end of this session.

Assignment


If you have not completed the tutorial yet:

Complete the rest in your own time.

Learning goals: Check-in

  • To understand the concept of simulations ✅
  • To explain the purpose of simulations in research ✅
  • To summarize how simulations support hypothesis testing and experimentation ✅
  • To perform the simulation process in the context of a fictional experiment ✅
  • To understand the concept of power analysis ✅
  • To explain the purpose of power analysis in research ✅
  • To summarize how power analysis supports hypothesis testing and experimentation ✅
  • To successfully perform a simulation in R ✅

Relevance to you?


Can you imagine using simulations for your work/projects/studies?

Take-home messages

  • Simulations = safe playground: You try your study many times with pretend data, so you learn how your experiment might behave before touching real data.

  • Power analysis = trial period: You find the “just right” study size, not too small to miss real effects, not too big to waste resources.

  • Together they’re your research rehearsal: first you imagine the experiment (simulate), then you check if the plan is strong enough (power), and only then you go on stage with real data.

To conclude: Survey time!

How confident are you in defining what a “simulation” is in the context of scientific research?

  1. Not confident.

  2. Somewhat confident.

  3. Confident.

  4. Very confident

Simulations are often used in research to… (Select all you think apply)

  1. Generate artificial data

  2. Visualize complex statistical concepts

  3. Replace all statistical analysis

  4. Test how models perform under different scenarios

How familiar are you with the concept of “power” in statistics?

  1. I’ve never heard of it

  2. I know the term

  3. I understand the basics

  4. I’m comfortable with calculating power

Power analysis is important for research because… (Select all you think apply)

  1. It helps determine how many samples you need

  2. It ensures studies always find significant results

  3. It helps you design more reliable studies

  4. It explains all statistical results

  5. No clue

How comfortable are you with running basic R code and performing simulations in R?

  1. Very uncomfortable

  2. Somewhat uncomfortable

  3. Neutral

  4. Somewhat comfortable

  5. Very comfortable

Discussion of survey results


What do we see in the results?

References

Lorah, J. (2018). Effect size measures for multilevel models: Definition, interpretation, and TIMSS example. Large-Scale Assessments in Education, 6(1), 8. https://doi.org/10.1186/s40536-018-0061-2
Morris, T. P., White, I. R., & Crowther, M. J. (2019). Using simulation studies to evaluate statistical methods. Statistics in Medicine, 38(11), 2074–2102. https://doi.org/10.1002/sim.8086
Steidl, R. J., Hayes, J. P., & Schauber, E. (1997). Statistical Power Analysis in Wildlife Research. The Journal of Wildlife Management, 61(2), 270. https://doi.org/10.2307/3802582

Additional Resources:

Thanks!

See you next class :)

Contribution Statement - UNINISHED