Welcome everybody you're watching mr fugu data science. Today, we got a lot to go through.
This is anova analysis of variants explained. As always thank you to the recent subscribers
and viewers i greatly appreciate it. Feel free to hit me up right here on twitter
and instagram. If you'd like to help support the channel feel free to do that right here.
If you want to follow along feel free. You're going to have to pause the video extensively.
The code will be linked in the description below for my Github. Here's what we need to import. So
far analysis of variance is a technique when all your explanatory variables are categorical. The
explanatory variables are called factors. The idea of anova compares two or more means. Whereas if
you did a t-test it would compare two the way that this is achieved is by calculating the variance.
We're trying to study if our measurements are similar for different treatments which are
called levels. This is a hypothesis test. The null hypothesis, states that all the means are equal or
they do not have any significant difference. Otherwise the alternate hypothesis says there
is at least one mean that is different. Why not just use the t-test? Well if you're going above
two means for comparisons, then you have to do a combination set up. which will cause you
issues. Think about this if we had an alpha equals 0.05, our confidence interval would be 0.95.
If we had three samples we have to multiply this times each one of those confidence intervals.
Then we would do one minus that, which would give us a 14.3 percent error rate which is not good.
So it starts compounding. The between group variability is interesting; pay attention,
to this because there's a distinction. We're looking at between all sample means. i'll show
a plot for this next. We're trying to find out do the sample means come from a common population.
Feel free to pause this if you want the code. i said variation between all sample means.
That's what's going on here. Assuming that we're comparing each one of these means to each other.
They do not need to be exact. And you're making a comparison for all these means relative to some
population. Think about it as the next thing we're going to show. This is going to be the same plot
but, explain more deeply. We're comparing how close we are to the population mean all right.
And remember this is between means. Get this code, if you want or scroll a little slowly. it's a lot.
So this is the same thing just expanded. Assume that this blue one was our population mean.
Notice the vertical lines blue, red and green. The distance from the red mean to the blue mean.
The distance from the green mean to the blue mean are sample one and sample two. And we're
trying to relate this to the population. The amount of overlap that we have for these samples
is going to contribute towards our population mean. The next thing we need to look at is within
groups which is next. Within group variability is different. This is the variation for each
sample above. We're looking at all the samples combined and see how they make up or relate to the
population. This is individual, it's the spread of each sample. Also, no interactions between samples
are considered. Anything that's overlapping is part of the population. This is the plot. Feel
free to pause this as usual. Variation within each sample, look at the bottom right here.
This portion right here, is highlighting only one of the samples. We would do the same thing
and check the variation between the green one as well. We're now doing a comparison here like we
did for the last one relating it to the blue one. Which was in the previous example. the variation;
the more spread out these are from each other, the less likely they came from the same population.
The more closer they are to each other and this overlap the more likely it could occur. And what
would happen is this right here. Would start dropping down and spreading further to the left
and the right giving you more variation. This information we're dealing with, the differences
in comparisons from between and within the total variance is the variance between plus the variance
within. This is going to lead us to what's called the f-score or f-stat. We do this called the
f-test. it's measuring, if the means of different samples are significantly different;
the lower the ratio of the f-test the more similar the sample means are. And therefore we will fail
to reject the null hypothesis. Here's what that ratio looks like. And we need to expand on this.
Here's a table of what it looks like for you to calculate the between groups the within groups,
or your residuals or error. You have your degrees of freedom for between as k minus 1. Within is n
minus k. That's the distinction. Then we have to take our errors and divided by our degrees
of freedom and then take the ratio of between, divided by within. Which gives us our f statistic.
First to betweens, that's the numerator. Let's think about this as the numerator grows,
we will have the likelihood of our data coming from a different population . Meaning that your
overall statistic will become greater than one. Because, it's in the numerator and it's growing.
That means that the means are going away from each other so you're having less of that overlap.
And they're kind of pushing each other away. Here's what it looks like for the sum of squares
between. Normally we don't calculate this by hand unless you're in an introductory course
or your professor just wants you to drill this through your head. But, this is how you set it
up. This would be the number n equals the number of samples that we're going to have. For instance,
for our example, we're going to have five samples for each a group. And we'll have to
find the total mean, which is or grand mean which this is usually called. I put the population. But
this is actually called the grand mean, or total mean. You'll see. And then we'll divide it by the
degrees of freedom, to get our mean squared error between. We'll do the same thing with within. But
it's a little bit different. Notice that we're summing the difference between our actual mean
and then you're you're going to have your column mean. And then you'll have your degrees of freedom
here. Or your sample size minus number of samples you divide that through. And now we
got the denominator portion for f statistic. Next we have to combine everything to give the s step
as the numerator grows like i said before. The sample means grow apart, meaning that they come
from different populations . Important right here. i'm going to show two ways you can do a comparison
for these tests so you're aware of it. If the f statistic we calculate is more than the f critical
point at a given alpha, then we will reject the null hypothesis. You're going to see this in a
plot so it's going to make more sense. Your alpha is going to be your cut off region. If you had .05
anything left to the right of that is going to be the rejection zone, and the f critical is going
to be the point at which it crosses the x-axis to give you your division. This means that any
value to the right of it is a rejection. On the other hand, i'm just reiterating here so you get
an idea. And you'll say well what's going on here? Well this can occur if we have one or more means
that are an outlier. And then we can say that this creates a large variance discrepancy for us which
would cause this issue. Here's our example we're going to work with. Assume that we have three
diet programs and a control group. And this is measuring the amount of pounds that these people
lost during a 10-week study. This is made up data, all right. We're assuming there were 20 patients
chosen randomly to go into these four groups. Meaning that we have five people per group.
Here's the data. The first thing you want to do is establish a null hypothesis. So that's what
we're doing here, we're saying that the means are equal to each other. The alternate at least
one mean is different. What statistic will we use? The f statistic. What is our decision criteria? i
decided 0.05, you could do 0.01, whatever you want. Then you have to find your degrees of freedom.
For us we're going to have two degrees of freedom in this test, as you're going to see. And then we
need to compute the f statistic. We take in our data, this is what it looks like. Its vector
form. We combine it into a single vector. You can throw this into a data frame if you wanted to. Set
it up different. Then i do this repetition. This repetition is allowing me to create. Let me show
you. It's giving you four groups of five, which we're going to repeat okay. And this is going to
basically match up with our data. Here's our data. This low fat corresponds to this point. This low
fat corresponds to this point and so on, through the different groups. We could do our mapping,
then i throw this into a data frame. All of my data is considered. My y and my group,
which is getting changed into a factor is this right here. Which is our mapping portion. We
run a linear model, then we throw in our anova and you're gonna notice something. This anova,
there's more than one type. There's aov, anova and then there's another test. I force this test
to do the f, so you can understand explicitly what we're trying to do. This isn't exactly necessary
but, i wanted you to know f test anova. Bam here's what it looks like as a printout. There's the f
value, that we have corresponding to this. Here's our first degree of freedom, it would be
n minus 1. This one is n minus k. i'm going to show you how we can retrieve all of these
working backwards. We need to do a comparison with this f value to this critical value. Notice,
that this f value is less than the f critical. Meaning it lies to the left of this region.
Therefore we are going to not reject the null hypothesis. You can do a comparison,
also by the probability. The probability here is 0.09, yet we're doing a 0.05 alpha
which means that this is bigger than the alpha. Therefore we do not reject the null hypothesis.
Remember if you saw in my prior videos, if you run the quantile function for the f distribution,
anything with the q returns a value. So we're chucking in our 0.05 the degrees of freedom, one
degrees of freedom two and we want the right side not the left. That's why we're doing lower tail
equals false. And i rounded this and that's how we get our actual value back. The x axis which is our
critical value you can do a one-way test. This is a little different, its doing the same thing but,
there's assumptions that you should look for. I think this is the Welch test. I can't remember.
Look that one up, don't quote me on it. But, if you notice we got the same things going on here.
You can do also the aov method. I don't remember the distinction between anova and aov. One of them
is used if you want to, i think it's do multiple models like when you start having more variables.
I can't remember . i'm a little rusty at this. But i did the same thing three ways. Let's look
at a plot. Feel free to pause this, real quick. But here's our cutoff limit, anything from this
critical value to the right is a rejection zone. Anything to the left is what we're keeping. And
that's where we ended up for our f statistic with these data. So what is the interpretation
from our exercise? Well here's the takeaway the f stat was less than the f critical. We
keep the null hypothesis, otherwise do not reject. We're saying that we observe these data from a
similar population or we say that the means are not significantly different. You can also do the
comparison, there's two ways doing the comparison. This is the second. The p-value is greater than
the threshold for our alpha. Therefore we reject to disregard the null hypothesis. We would
therefore, say that we do not have a significant difference in mean weight loss with these diets.
Let's work backwards. Let's keep this real quick so we can have an idea of what's going on with
some of our values. Right here is a print off of each column mean, variance, the sum of the values,
and how many values were in that row we're going to take these means right here to do
our calculations to work backwards. So we're taking our table values here. We're taking all of
these means, that you're going to see right here. Dividing by the number of different variables,
we have which is 4. That's going to give us our big mean that i'm calling it. Then to get the
sum of squares between remember that we had to do that multiplication by n so i have five instances
of each sample or each group. And then i'm taking the individual mean for that column as well as the
sum of those means divided by four. And i'm running that through each one of those columns.
Then i'm printing that out. From there, we're taking the sum of the y with respect to each group
subtracting the individual column mean and then summing it together. And doing that for each one
of the four different groups. Then to get the mean squared error divided by the degrees of freedom
for within, and between. And that's how we get our answers, right here. I know that's kind of fast,
and that's how we worked backwards to get our answers. Now, i left something for you
right here when you're working with this stuff you need to think about some assumptions that
need to be checked off. Normality equality of variance independence. I left some tests,
right here that i suggest you pause the video and look into. i'm doing those down here so we
can see these in order to correctly identify and understand your data for interpretation.
These tests are useful, so you don't get spurious results or inconclusive interpretation. First
thing, we're doing is normality tests. You could do a qq plot, histogram or the Shapiro-Wilk test.
i'm going to show you all three run through the anova. Remember your linear model here, and then
throw it into a histogram. But, remember you're doing this aov first, in order to do a histogram
of your residuals. All of these tests right here are with respect to the residuals. That's why we
have to do the anova first. These samples don't look like they're normally distributed. Consider
doing transformations to your data. I tried that log transform, exponential, square root.
Considering the fact we only have 20 entries, it's really hard to get a nice looking plot.
Therefore to get a numerical estimation to show you a little more clearly. We're going to do that
after the qq plot. q q plot, here's our confidence intervals here. Only one of the points right here,
falls without it. Falls outside of it. So we're doing pretty good here, in terms of normality.
I decided to do a density plot so we could get an idea it looks like it's slightly bimodal. But
you could do some finessing here with probably, a transform to make this look a little better.
So it doesn't look doesn't look horrible and doesn't look highly skewed or anything.
The Shapiro test is a hypothesis test saying that the null hypothesis data came from a
normal distribution. With a probability of 0.365, here meaning that is greater than our 0.05 value.
Therefore, we keep the null hypothesis. Basically here's where it is . Remember with respect to the
residuals. Here's the comparison with the f-test as well as the probability. I just printed this
off so you could see it's not going to give you the same results. But i want you to just reinforce
what's going on. Test for equality of variance, you can use a one-way test the Levine test,
and box plot. I will show all of those are the variances equal along the samples. For instance
the various diets the one issue here. You're going to see is group two. Therefore, i decided to do
the Levine test for a quantitative approach. So we can get a good depiction that is dealing with
a hypothesis testing, as well and the Levine test is less sensitive to fluctuations for normality.
It says that the hypothesis equals variance, equal variance we'll end up with a pretty good
probability value from the Levine test. Meaning that we don't reject the null hypothesis.
This is the second group right here. You can see this bottom line isn't protruding
so we got kind of a little bit of issue. But otherwise the variances don't look too bad
here's. A Levine test, you have to import the car library to use it. You notice that we have a
pretty good p value, much higher than the alpha evel . Therefore, we keep or don't reject the
null hypothesis. And here's the one way test. Also, just look at this variance equals true.
Assuming it's equal variance, you could also have this as false. It does another test,
i can't remember the name. We have independence this is usually taken care of. If you're doing
a good experimental design, that's going to be left for you in the end. And i'll leave it up
so you can kind of read it since, we didn't do this for our experiment. We didn't set up
a good experimental design to avoid any kind of collinearity and things like this to affect our
independence. I'm going to use the Durbin Watson test, which is saying with the null hypothesis:
the coefficients equals zero correlation. But you first have to do a linear regression model
to apply this test. Apply the linear regression model then, apply the Durbin-Watson test. Here
we go. We have a pretty good p-value, greater than the 0.05. We fail to reject the null hypothesis,
more reinforcement. i show the anova once more, and this is for anyone who ever comes to the
end of this video. You got an extra treat because, as always please like share and subscribe and if
you subscribe turn on that notification bell and please feel free to help support this channel. Go
on buy me a coffee there's my handle mr fugu data sci. Now here's our extra tidbit. These are the
assumptions that you should be using when you're trying to make any kind of experimental design,
supposed to be used with anova. i'm not going to explain it. i'm going to scroll through it slowly
throw on some music and hopefully somebody one day will get this far in the video to watch
this
have a good one. i'll see you in the next video. bye