Okay, and a previous video, I mentioned something called Balanced data. In the context of a multi-factor general linear model. Balanced data referred to a situation where you have the same sample size for all combinations have your treatments. So for example, if you had a two factor general linear model, where each factor has two levels. So imagine with my fingers hears level one, level two, and then we had for another factor, level one, level two, that would give us four different treatment combinations. And balanced data occur when all of those different treatment combinations have the same sample size. It should seem obvious then that unbalanced data occur when the sample size is unequal between our different treatment combinations. It turns out that the way in which we calculate p-values can be different depending on whether or not we have balanced data or unbalanced data. And the point of this video is to show you how to calculate P-values when you have unbalanced data. I'm going to start by showing you that when you have balance data, the different approaches that I'm going to demonstrate don't make any difference who end up getting the same answer. And so really what? At the end of the video we're going to be highlighting the fact that when you have unbalanced data, the approach used to analyze your data do matter or does matter. Pardon me? Just as a point of warning, I'm going to be bringing up a number of issues that are larger topics in and of themselves. And those topics are going to be dealt with in other videos. So they're going to be some points in this video where I'm gonna have to say just trust me, just do the things that I'm suggesting. Actually now that I say that that sounds awful because you should never just trust someone. But I'm going to be giving you the best advice that I can at this stage. And in later videos, I'm going to be giving you more information so that you can make further judgements on these issues yourself. Okay, so let's start with just importing a very simple dataset, which I've created my simulated these data. To be honest, the actual nature of these data do not really matter for this example, because really the points is going to be the process, not actually what the data are, where the conclusions are. So these data really follow on from an example we considered earlier, where we imagined that we measured some value y from a number of individuals. We have two different factors. One being sex, where you either have females. Oops, pardon me, or males. And then something called treatment, which involves giving females or males either control treatment or a drug treatment. Okay. Now, when we look at these data, when we explore these data in this video, we're not concerned with all the usual steps you would take to make sure we get reliable results. Because that's not the point of this video. The point of this video is simply to show how you can get different answers from taking different approaches. And so we're going to jump straight to conducting a linear model. We're not even going to bother plotting the data at this point. So let's create a linear model will just save our output in something we call EEG dot lm. And then we'll just say b for remembering than we have balanced data. I didn't mention that before. I should say that these data here involve ten data points for each combination of sex and treatment. And since we have four combinations, that's why we have 40 data points overall in this dataset. So this first situation that we're looking at involves balanced data. So we're gonna use our linear model. Our dependent variable is called y. And we're going to model our two independent variables, sex and treatment. And then we'll include interaction. And that our dataset, data equals a dataset has just called EEG. So let's run this. And we're not going to worry about checking the residuals or anything like that because again, that's that's not the point of this video. Instead, we're just going to jump straight to getting the P-values. And when you have balance data, it is perfectly fine to use this function anova in order to obtain the ANOVA table for, for an x for an analysis like this. So we can just say EEG ANOVA of EEG dot lm dot B. And you can see here the p-values that we get. There is another way of calculating these p-values, which involves using a function in the library car. And this approach involves calculating what's called type three sums of squares, which I'll say a little bit more about in a moment. First, I just want to show you, Whoops. I pressed paste instead of, instead of copy. I'm just going to modify this output to taken to allow us to conduct this analysis using this alternative method. And I'll say more about exactly what we're doing in a moment. First, what I want you to see is I'm going to be adding an additional option to our LM function, where I'm saying contrasts equals and then a list. And what I'm going to be doing is I'm going to be taking each factor, woops. I need to be more careful. I'm gonna take each factor. So sex and treatment. And I'm going to tell the functions that we're using exactly what kinds of contrasts we want to be making for each of those factors. So we're going to say sex equals contrast some. And treatment equals contrast some. Now, exactly what our contrasts. That is one of the topics, that's a larger topic then we're going to address in this video. And exactly what does contrasts some mean? Well, this is a particular form of contrasts which we're going to be using for our treatments, sex at, for our factors, sex and treatment. But exactly how does this form contrast work? Again, that's a topic that's got to wait for a later video. So now that we've specified these contrasts for each of our different factors, we can say R1. And now instead of using the ANOVA function with a small a, we can use an ANOVA function with a large just to make our lives a little bit more confusing. And this function anova with a large a, this is a functions available in the car like car library. If you don't have this library installed for r, then you can install it just by using the Install Packages function. So I'm just going to say ANOVA. Now for this and now I'm specifically going to say type equals three. What does this mean? This means that I'm telling, are, are, are telling the ANOVA function that I want to calculate P-values for the output contained within this object using what's called type three sums of squares. There are several ways of calculating p values based on different types of comparisons. And they involve type one, type two, and type three sums of squares. The differences between those three things, again, is something that will be addressed in another video. For now, I'm just going to tell you that I've decided to show you how to obtain p-values using type three sums of squares. Simply because that is the default output for many other statistical packages. Does that mean that type three sums of squares are the best? Actually, no. There are many people that believe in certain circumstances. It makes much more sense to use what's called type two sums of squares. And I'll actually say that I think that type two sums of squares often make more sense than Type three sums of squares. So it feels a bit strange to me to be making a statement like that right now because I'm recommend because what that means is that I'm potentially recommending something that might not always be best. So I'm going to leave this topic of what type of sums of squares to calculate. I'm going to say, we're going to deal with that another video. And I'm going to say that that really just reiterate the reason why I'm giving you this type of, why I'm teaching you this types of sums of squares is simply to allow you to get results that are the default fro or many other statistical programs. Now let's say R1. And let's just compare these p-values. Let's just run that first output again, just to get everything a bit closer. So what I want you to see here is the p-values that we obtain using these three different methods are identical, 0.00706, 0.00706 for the sex treatment. And then for the fact called treatment 0.01. 3.03.45 for the interaction. 0.45 for the interaction. Okay? So when we have balanced data, we end up getting exactly the same results for our two different approaches. Let's now change our datasets. So we have an unbalanced data set. So we'll just create a new dataset which call EEG dot UB for unbalanced. And we're just going to remove a number of data points from our original data set. So we'll say jim dataset was called EEG. Wi's are square brackets to go into the guts of our data set. And we're just going to specify which rows you want to remove. So we'll say minus to say we're removing some rows. I'm a create a vector of, let's say, rose 36 to 40. So we're going to remove the last five lines of our dataset, which means that for our last treatment combination, we're going to change our sample size from ten down to five. So let's run that. And just to confirm that we've done what we wanted. We're just going to say n rho of e g dot UB. So we're just going to get the number of rows in the DataFrame. It should be 35 now, and we can see it, yes, we now have 35. Instead of going back to our original DataFrame instead of 40. Okay? Now, what we can do is we can just repeat the analysis that we ran earlier. I'm just going to save myself some typing. This is a pretty bad habit for what I'm about to do. But we're now going to say we're just can change DataFrame. So changing the dataframe that we're using. So now using unbalanced data. And we've changed the name of our object that we're saving the output in. And we're making sure that we're getting are P-values for the correct object. Okay, so for the correct output. So let's just say run and never get those p-values. And let's do the same thing for our other approach. So we've changed which which data set we want to use. And we've changed the name of the output that we're, of the object where we're saving our output. And now let's see what we get. Okay? Now you can see that the p-values are no longer similar. They are similar for the interaction for reasons that we're not going to go into in this video again, that's waiting for a future video. But you can see that the p-values that we get for our other two main factors, sex and treatment, are no longer the same between these two different approaches. When you have. So. Here's the first punchline for this video. This video is that when you have unbalanced data and if you want to obtain p-values that you would expect to get with other statistical packages. Then you can follow this approach that I have highlighted here. So you can calculate Type three sums of squares for your output. To do that, that involves specifying the contrasts for each of your, for each of your factors in your multi-factor general linear model. And the type of contrast that we're using here is contrast sum for each of these. And again, the meaning of that is something we'll deal with in a future video. Okay, so I hope that is helpful. There's one other thing that I want to highlight, which is that when we use these contrasts, they actually change the meaning of some of our other output. So let's, In other videos, we've wanted to look at the coefficients of our analyses. And so let's look at the coefficients for our analysis that use the lm function where we did not specify the contrasts. Okay, so in other words, when we've analyzed our data in a way that we'd normally be comfortable when we had balanced data. If we look at this output, then I'll say that these estimates have the standard meaning that we would normally ascribe to them. However, if we look at the output for if you look at the summary of the output where we did specify the contrasts, you'll notice that these estimates are not the same as we have in the previous analysis, sorry, and the previous output. So here we have sex male being my 7.9. And we don't find that here as well. The, the, the notation here is change somewhat. But really the main points that I want to draw your attention is that these estimates, when we specify the contrasts, no longer have the type of meaning that we had discussed in other videos. So the second main point of this video is that when you specify the contrasts as we've done, as we've done, come on. As we've done here. Okay, when you specify those contrasts, the meaning of your coefficients will change in your summary output, okay? And we will discuss their meaning again in a future video. So please be cautious just to wrap up two main points here from this video. Number one is that if you have unbalanced data, you need to calculate your p values using the approach. Or you need to calculate your p-values. And a different way from simply using the ANOVA function with the small a. There are a number of different approaches to calculating your p-values. And this video I've shown you one way which involves specifying the contrasts and using this ANOVA function with a big a. There are other ways to calculate p-values. I'm highlighting one of them in this video. The second is that if you use this approach where you're specifying your contrasts, then be cautious about when you interpret your coefficients because they will no longer have the meaning that we have discussed. In previous videos. I hope this video has been helpful. And I will end there and say, thank you very much.