Earlier, we briefly mentioned the chi square test statistic and now we want to look more at chi squared
tests.
There are many different kinds of chi square tests we can perform.
We'll look at three of them and then in particular we'll focus on one.
So one kind of chi squared test that we can do is a chi squared test for homogeneity, where we take
a sample from two groups and essentially compare their probability distributions.
So an example of a question we could answer here is whether or not gender has an effect on pet preference.
So if we were to ask men and women whether they prefer cats, dogs or some other kind of pet and then
create a table of our data, it might look something like this, We can see that what we're doing here
is sampling from two groups.
One group is men, one group is women, and we are comparing their probability distributions.
So we have a probability distribution for men across cats, dogs and other pets, and then the same
probability distribution for women.
Now, if gender has no effect at all on pet preference, then we would expect those probability distributions
to be similar.
In other words, if men prefer cats at roughly a 25% rate, dogs at 50% and other pets at 25%, then
we would expect roughly the same distribution for women, 25% for cats, 50% for dogs and 25% for other
pets.
Now, of course, we know that these two distributions are very unlikely to be identical, but if gender
doesn't affect pet preference, then we could expect them to be roughly similar.
But if gender does have an effect on pet preference, then we might see these probability distributions
being extremely different.
And the chi squared test statistic lets us look at how different the distributions are and then determine
whether the difference is big enough to be called statistically significant, such that we could reject
the null hypothesis that gender doesn't affect pet preference and therefore lend support to the alternative
hypothesis that pet preference is affected by gender.
So that's one kind of test We can run.
Another kind of test we can run with chi squared is a chi squared test for association or independence.
In this kind of test, we sample from one group and we compare characteristics for that same group.
For instance, we could compare eye color and handedness.
So whether someone is left handed or right handed for the same set of individuals, we might make a
data table that looks like this.
So we pull a sample of people all from the same population and we record whether they are left handed
or right handed in contrast with their eye color.
So out of our one group, we can see that we have 72 people who are left handed with brown eyes, 36
people who are left handed with blue eyes, 130 people who are right handed with green eyes, etc..
So in this kind of test, our null hypothesis would be that eye color and handedness are independent,
they're not associated, they have no association.
And therefore the alternative hypothesis would state that eye color and handedness are associated,
they are dependent and we would again, similar to the homogeneity test, look at the distributions
of the left handed people and the right handed people.
If handedness and eye color are not associated, then the distributions for left handed and right handed
should be similar.
But if handedness and eye color are associated, if they're dependent on one another, then maybe we'll
see different distributions for left handed and right handed people.
And if that difference is significant enough, then we may be able to reject the null and lend support
to our alternative hypothesis that handedness and eye color are associated variables.
So you can see how these two kinds of tests are similar.
The third kind of test will look at is a chi squared goodness of fit test.
And here we're comparing actual versus expected values in a contingency table.
For example, we might try to answer the question, are sales number of sales of a product affected
by month?
And maybe we would use this table here where we have the months of the year, January, February, March,
etc. and we have the number of sales that we make in each month.
So we call this a contingency table because the number of sales are contingent on the month of the year.
To answer this kind of a question, we would think about the expected number of sales in each month
based on the total number of sales for the year.
In other words, if sales aren't really impacted by month, then we would expect a steady level or a
consistent level of sales across the year.
Month wouldn't have an effect on sales, and sales should remain fairly steady.
And so in that sense, just like with these two tests, we would compare the distribution of actual
sales to the distribution of expected sales.
And if those two distributions are significantly different enough, then we might be able to conclude
that sales.
Are affected by month.
So that gives you a little bit of an idea of what we're doing with chi squared tests.
Let's dig into this goodness of fit test in a little more detail so that we can see an example and work
through a full chi squared test.
So we're starting here with an expanded version of the table that we had for the goodness of FIT test.
We're trying to figure out if sales are affected by month.
So we had the months of the year.
These were the observed number of sales in each month.
So in January we sold 60 products, in February we sold 80 products, etc. And when we total up all
those sales, we have 1020 sales for the year.
Now, if we take 1020 and we divide it by 12 months in the year, we get 85.
And so our expected value for each month of the year is 85, because if a month has no effect on number
of sales, then sales should remain steady and our expected value would be a steady, consistent 85
sales per month for a total at the end of the year of 1020 sales.
What we want to do now is use a chi squared goodness of fit test.
We're going to pick a confidence level of 95% or an alpha value of 0.05 and output value of 5%.
And we're going to use this chi squared test to make a determination about whether sales are affected
by month of the year.
So with all of these chi squared tests are null, hypothesis is that month has no effect or that the
distributions aren't different.
Basically that the two variables don't affect each other.
In this case, that observed and expected values aren't different.
So we would say here that sales.
Aren't affected by month.
That means that our null hypothesis then is that sales are affected by month or that the month of the
year does have an effect on sales.
So again, if sales aren't affected by month, then it would make sense that they would be evenly distributed
over each month and we would see something fairly consistent like this expected value of 85.
Now, keep in mind here that we can't just lend support to the alternative hypothesis that sales are
affected by month just by having different observed values then the value that we expect.
For instance, if our observed values were 84 for January, 86, for February 84, for March, 86,
for April, 84, for May, 86 for June, etc. Yes, those values are technically different than the
expected values of 85 for every single month, but the values are so similar, they're consistent enough
that the difference will not be statistically significant and we will not have enough evidence to reject
the null hypothesis and therefore lend support to the alternative.
These observed values here not only have to be different, they have to be different enough to meet
our level of statistical significance, and that's what will determine with our chi squared test statistic.
So to calculate chi squared, that's our next step.
For each of our data points, we find the difference between the observed and expected values.
So we take observed value minus expected value.
That should kind of remind us of the idea of the residual that we've been looking at as we've been working
through regression here.
So we take the observed minus expected and we get that difference.
And so we can see here that the third row in our table is observed minus expected.
Then we square that value to make all the values positive.
That's the last row of our table, all those squared values.
Once we have all those squared values, we add them all up and then divide by the expected value.
We divide each one of them by the expected value, and then we add all of those results together in
this particular chi squared test, because the expected values are all the same.
That means we can add all these squared values in the fourth row and then just divide by 85 all at once.
So the sum here of this fourth row is 6850, which means that the chi squared test statistic for this
particular test will be 6850 divided by our expected value, 85, which is approximately 80 point.
Five nine.
So this is our chi squared test statistic, just like we would calculate a PT test statistic or a Z
score, we calculate this chi squared test statistic and then, as you might have suspected, just like
the T score or the Z score, we need to look this up in a chi squared table.
Chi squared has its own distribution and so it has its own table and the chi squared table looks like
this.
It's very similar to the t table in the sense that we have a degrees of freedom value down this left
hand side.
Now when we think about degrees of freedom for a chi squared test, we always need to go back to the
table that we're starting with and we want to look at the original data that we had, the observed data,
including the total.
So in this case we would be looking at this part right here of our table, all the original observed
data plus the total.
Now the degrees of freedom is the number of values that we would have to include in the body of the
table, not including the total, in order to be able to fill in any other missing values in the table.
So here's what we mean.
If we look at the body of this table, we have all these values in the body 60, 80, 65, all the way
up to 65 here in December.
And then we have the total.
The question is, how many values can we remove from the body of the table before we would no longer
know what to put in each cell?
Well, if we remove the value here in December and we pretend now that we don't have the value for December,
the only values we have are January through November.
And the total the question is could we figure out what value to put back in for December?
And of course, the answer is yes, because we could take the total 1020 and subtract from it all of
these other values for January through November.
And what we would be left with is 65, which has to be December's value.
So we can remove the value for December and we can still know what all of the values in the table will
be.
Now the question becomes, can we remove another value from the body of the table and still know how
to fill in the rest of the table?
So let's say we remove November's value.
Now the question is, can we accurately fill in November and December?
And the answer is no, because we have the total 1020.
But if we subtract out all these values January through October, we're left with a missing 125.
But we don't know how to split 125 between November and December.
We have no way of telling how much of that 125 goes in November and how much goes in December, which
means that we can only remove one value from the table before we would no longer be able to finish filling
it in.
So if we can only remove one value, that means we have 11 values remaining in the table, which means
in this case the degrees of freedom is 11.
Degrees of freedom is the remaining number of values that we have in the table.
And since that's a little confusing, let's look at one of the tables we had from earlier in the video.
This handedness versus I color table.
Really what this comes down to is that when we look at the body of the table, we're always able to
remove the last row and or column of the table.
So in this table we could remove all of these values.
Because you can see that even if we didn't have any of these values here, we could find left handed
Hazel by subtracting 72, 36 and 20 from this 140 total on the right hand side that would allow us to
fill in this left handed Hazel here.
So then we would have this value.
And then once we have that value, we can fill in right handed and brown by subtracting 72 from 532.
So we'd have that value.
We would find right handed and blue by subtracting 36 from 251 and so on.
We'd take 150 -20 to get this cell and then 67 -12 to find this cell, and suddenly we'd have the whole
table again.
So if we're able to remove all of these values here, that means degrees of freedom for this table,
for this scenario is three, because the number of remaining values in the body of our table is three.
We have 72, 36 and 20.
And so degrees of freedom here would be three in the same way that degrees of freedom up here would
be 11.
So back to our original example, our sales affected by month.
We have 11 degrees of freedom and we're interested in this 5% significant level.
So we come here to 11 degrees of freedom and we work our way across to an upper tail probability of
0.05.
Whatever our level of significance is, we find that in the upper tail probability hetero, and then
we come here to their intersection and we see this value 19.68 from here.
The only thing left to say is whether our chi squared test statistic that we calculated is less than
or greater than the value that we find in our chi squared table.
In this case, we can see obviously that 80.59 is greater than 19.68 and whenever we find a chi squared
test statistic that is greater than this critical value that we find in the chi squared table, that
means that the test statistic we found is significant enough to allow us to reject the null hypothesis.
In our case, there's a massive difference between 80.59 and 19.68, which tells us that we have very,
very strong evidence that sales are indeed affected by month of the year.
So we can reject this null hypothesis that sales aren't affected by month and we give a lot of support
because of the massive difference here.
We give a lot of support to this alternative hypothesis, the hypothesis that sales are indeed affected
by month of the year, that the distribution of sales doesn't just hold steady and constant unaffected
as we go month to month, and that instead they fluctuate with some level of significance and the number
of sales we make is dictated by the month of the year.
It's affected by month of the year.
So in a way we could say that month of the year and sales are dependent variables.
They are associated, they are not independent of one another.
So even though we looked at different kinds of Chi square tests, they all follow the same kind of general
pattern.
We're always comparing two different distributions and we sort of start with this idea that the distributions
are the same and so we compute this expected value.
Then we look at the difference between actual value or observed value and expected value, and we use
this formula to calculate the chi squared test statistic.
Once we have the chi squared test statistic and the number of degrees of freedom, we can look for our
level of significance in the chi squared table and compare our test statistic to the value that we find
in the table and use the relationship between the test statistic and the value from the table to make
a conclusion about whether or not we can reject the null hypothesis.