...

/

Writing a Simple Function

Writing a Simple Function

Learn to write a simple function.

An example using simulations

There are many times when we may want to write our own functions. As scientists, one of the most valuable things we can do is simulate the data we get to understand the effects of sample size and effect size on our statistical power. For example, we can use a pilot study to estimate our treatments’ effect size and then simulate data to determine how big a sample size we’ll need to observe statistical significance.

Maybe this means we have pilot data on two drugs we’re testing out, two behavioral treatments we’re administering, or two temperatures we’re raising plants or worms in. As long as we have some idea of what the effect size might be—that is, the difference in the means of our treatments—the examples we’ll go through here will be helpful.

Imagine we’re conducting predation trials with two different predators. We want to know how many replicates we need to run to know if there’s a significant difference between them. Let’s write a function to simulate the effects of the two predators. It’s essential to recognize that everything we do here can be done for any variable.

Starting simple

We start off very simple. The rbinom() function allows us to calculate a binomial probability (like a coin toss) for given sample size and probability. The rbinom() function takes three arguments:

  • n is
...