Okay, In this video, we're going to demonstrate how to conduct a power analysis using simulations. And in particular, we're going to simulate a power analysis for an experiments that would be analyzed with a two-sample t-test. We're going to talk very briefly about why we'd want to use simulations. And second, we'll go through an example. So first, why would we want to use simulations for power analysis, especially when there's already existing software, including some functions in R that can perform power analysis already. Okay? We're going to be three good reasons. The first is that this approach of using simulations is widely applicable beyond t-tests. Existing software can only perform power analysis for certain types of data analysis. And it may very well be that the type of analysis that you would like to use is not available for a power analysis in these other packages. And so what we're going to do through a series of videos is build up our understanding of how to perform power analysis via simulations. So that if you find yourself in that position where you want to perform a power analysis for a technique that is not kind of your bog standard power analysis thing, you'll be in a position to do that. So we're going to start that road with t-tests, OK. Second, the approach that I'm going to introduce you to, is it really flexible? So we can use a number of different criteria to assess their results. So in other videos, we've talked about why using a threshold p-value of 0.05 to designate statistical significance is not always a wise, sorry. I was about to say it's not always a wise thing to do in the videos, I suggest that's always a bad thing to do, okay? However, most commonly power analyses are performed under that framework of comparing the p-value to 0.05. What I'm going to show you in these videos in is how to perform power analysis, both from the p-value less than 0.05 perspective, which is what I can do in this video. And then also from other perspectives, okay, so there's greater flexibility in, in how we, in our perspectives that we can apply when performing power analyses. And finally, this is kind of a fault just on teaching and understanding what we're doing. When you are in a position to simulate your own data and be able to predict what the analysis should provide as output, then that's a really nice place to be. Because when you're in that place, that means you really understand the nature of the data that goes into analysis and you understand how the analysis works. So to be honest, I have sometimes used simulations just to check my own intuition of how some types of models work. So this learning to simulate data like this is a very cool. Approach generally. Okay, so ends, one of our previous videos, we talked about how we can think of a power analysis from the perspective of simulations. And we had a slide that looked exactly like this, but it had a black background. And we imagined a world where we had two groups, tubes of fish when they had a mean of five grams and another had a mean of six grams. And we imagined that the standard deviation of each of these groups was 0.5, okay? And we wanted to know if this view the world is correct. Will we be able to detect differences between these groups if you only have five fish and each treatment. And we said how we can use simulations to answer this question. So we said we would generate five control fish by drawing five random numbers from a normal distribution with a mean of five and a standard deviation of 0.5. And then we can do the same thing for another group that has a mean of six. And then run a t-test and ask if we can detect the difference. And that is exactly what we're going to do. Okay? So at this point we're going to go into R. So I have an R project or RStudio project open. This is a nice thing to do at the beginning, which where this command will allow us to basically clear anything that we already have stored in R's memory. Okay? Ls is a function that just lists all of the information that's stored in R's memory, for example, previous DataFrames. And if we use this, the RM command causes us to remove files. Okay? So if we use this whole command, this will basically remove anything that's already in our memory, allowing us to get a clean start. There we go. Okay, Sorry about that. It's not a good Siamese start out with a typo, okay? But this is what you would do. This is what you would use to just get a clean start. Now, we wants to simulate our data. And we said we have two different means and we have a standard deviation of 0.5. So let's start just by creating some variables that will store those mean values. So let's say I mean one is equal to 5 and mean two is equal to 6. And we'll say sample size is equal to five. And standard deviation of both of our variables is 0.5. Okay, this is a situation that he wants to consider. So what we're gonna do is we're going to create two datasets. Okay, we're going to say group one. We're going to create our datasets by drawing five random numbers from a normal distribution. To draw random numbers from a normal distribution, we use the rnorm function. And you can see here that R is telling us that we need to first specify the sample size we want to obtain. So that's going to equal this variable here that we put in sample size. Then we specify the mean of that group and the standard deviation of that group. So we can fill those in, whoops, just by filling in these variables. So we want the number of samples to be drawn to be equal to the value that's stored in. Oops, I've no idea why I said that. Sample size. Our mean is mean one, and our standard deviation as sd both. Okay? So now if we run all this code, we will see that sample size is equal to 5. And group one will include these five data points. Okay? Now, what we'd like to do is we would like to repeat this. But for our second group, we want everything to be the same except we're just going to change the mean. And we're going to store these data in group two. Okay, so this code here allows us to generate our two data sets. Now I want to perform a t-test. We're going to store our data and which is what I'm calling an object called T out. And we're going to perform our t-test just using a t-test function. And to perform this t-test, we can just list the two groups who we want to compare. Oops, I'm going to say group one and group two. Group two. And we're going to specify that specifically want to do a two-sample t-test just to keep things bog standard. And so in this case, you want to specify that the variance is going to equal between our groups. Excuse me. Okay? So now if we run this code, we can now look at what our output would be. Okay? You can see that we get a p-value here of 0.2 to row three. We want to extract that p-value. And we can do that by adding this dollar sign and allowing us to extract just the p-value. Okay? So what we can do now is we can say, we can ask. To assess the p-value. Sorry, Let's take a step back. Because we've done so far. We've generated two data sets, just like we specify in our slides. We've run our test and stored the results of that test. And we've obtained our p-value. What we'd like to R to do now is to count the number of times that our p-value is less than 0.05. And by keeping track of the number of times that our p-value is less than 0.05. That allows us to count the number of times that we've been able to detect a difference between these groups. I'm sorry, between these groups. According to this old way of thinking where we say we found a difference if our p-value is less than 0.05. Okay? So that's what we're gonna do. So we can do this, we can count our PVA or count the number of times that we have a p-value is less than 0.05 by including an if statement. But we're going to say if the p-value is less than 0.05, but some curly brackets, then what we're going to do is we're going to say counter is counter plus one. Okay? Now we haven't specified by counter is before. So we're going to go up here and we're going to say counter is equal to 0. Okay? So what this is going to do is each time that we have, what, what, what's going to happen here is that each time we have a p-value that's less than 0.05. We will essentially increase this counter by one. We'll take the original value of the counter, which right now is 0. If we get a p-value of less than 0.05, then we'll take their original count, add one to it, and create our new value. And so this will now be equal to one. If you get another instance where P-values less than 0.05, then this will count up to two. Okay? I've kind of gotten ahead of myself a little bit here in that I've been implying that we would count the number of times that we get a p-value less than 0.05. What that would imply is that we're going to go through this process many times. And indeed, that's what we need to do. And so to do that, I'm going to create a for loop where I'm going to say, we're going to run our power analysis for n Sims times. We haven't specified and sims. Let's run it 1000 times. Okay, might be better to run it 10 thousand times to get more precision. But for the moment, let's just, you know what, what the hell? Let's make it 10 thousand times. Okay. Now we're gonna do is I'm just going to copy our code and paste it into this for loop. Okay? And finally, so what this is going to do now is each time we're going to run this, run this code here 10 thousand times. So 10 thousand times are going to generate datasets according to these values. We are going to run a t-test. And if our p-value, if that t-test is less than 0.05, were to count that as one tiny we've detected something. Okay. Now we want to do is we want to know the fraction of times that we got a success or that we got a p-value of less than 0.05. And so to get that, we just say counter divided by n Sims. And we're just going to save that, copy all this and run it. And there we go. There's our output. Our, US are simulated value. Alright, sorry, are what we get from this is that 79% of the time, 79 put 18 percent of the time, we were able to detect a difference between these groups. And if you remember back to our previous video, this matches very closely to the results that were presented in the previous video. Okay? And that is how you can simulate or how you can perform a power analysis for a t-test using simulations. Let's go back to some slides just to wrap this up. Okay? So what I really want to demonstrate here is on top of, you know, how to, how to perform a power analysis for a t-test with simulations. I'd also like to highlight this approach is very flexible. We assumed at the sample sizes were equal. We don't have to do that. We assumed that the variance or the standard deviation was equal. We don't have to do that, okay? You can make these changes, but just by specifying variables differently from how we did in our code. Okay? And finally, are we using a for loop like this? Can be useful. There can be other approaches for implementing this. We might want to package the approach that we've done in a different way. For example, we might want to package our approach using a function. I'm not going to show you how to do that in this video, but I am going to make available some supplementary materials and a PDF file that will show you how to do that. And on that note, I hope this video has been helpful and I'll say, thank you very much.