Hello and welcome to a video on Chi Square analysis. Chi Square is used to test whether there is a significant
association between two categorical variables. In simpler terms, a Chi Square test helps you to
figure out if the distribution of one variable differs from what you'd expect based on the distribution of
another variable. So let's start by loading our packages, loading in some data, and producing some categorical variables.
All right, as usual, we have our packages that we're loading in here. Today we'll be in loading in statistics and also the
chi Square contingency function from sci-py.stats.
Next we're mounting our drive, setting our file path, reading in our data. This should look
very familiar by now. So we've got our variables here to play with. Let's go ahead and collapse a few of them.
Now there is an entire video on collapsing variables. If you need a refresher on that, I would suggest revisiting it. I'm
not going to sit here and explain all of the ins and outs on how to do this. But just know
that we have set our mean for two different variables here for I.V.1 and R.D.V. And
then we're going to use NP.where to determine if we are above or below
the mean and use zeros and ones to denote that. So if we're below the mean, there will be a zero there
and if we're above the mean, there will be a one there. Okay, let's go ahead and run that code.
You can see now we've got two new variables. We've got D_R_IV1, and this is actually a column of ones
and zeros. It's just that for the first five, there are ones. And then D_R_DV, which
is now a column of zeros and ones. So now that we
have two categorical variables and we know that they're categorical because we have two categories,
zero or one that our variable could fall into. We could have more categories than this.
There are ways to split your data differently where you may decide, oh, I need three or four categories for my data.
Perhaps you have data for employment and you have unemployed, employed,
part-time employed, retired, that kind of thing. So you've got four or five, maybe even six categories.
And in that case, you would use the different method to break that down into different
categories. But for today, we're just going to keep it simple and use two.
So now that we have two categorical variables, we can make a contingency table, contingency tables are special.
This one is a two-by-two contingency table and what it does is set up your data that you can see the
relationship better between two variables based on whether they are yes or
no, based on what category they're in, essentially. So we're going to use PD.cross
tab, which makes a cross-table of whatever variables you put into it.
So here we've used our new D_R_IV1 and D_R_DV.
And we're going to save that in a variable called contingency table.
Once we've made contingency table we'll use these methods called columns and index to set the
labels for the contingency table. Okay, let's go ahead and run that.
Okay, so now we have RdV below
the mean RdV above the mean RIV1 below and RIV1 above and they're set
up like that. Now that we have our contingency table, it's finally time to do some statistics.
So we're going to be using the chi-square contingency function from sci-pi.stats and in
it we're going to put the contingency table that we just made. Now you may notice we're returning
four different variables here. And we're going to save them in the chi-square, p,
dof for degrees of freedom, and expected for expected values. There we go.
Now we have our expected frequency stable. Our chi-square statistic,
the p value, which is actually a very small number, we can see this e minus 0, 6. This means our
decimal place is actually 6 places to the left. So we have a very small value of 0 and
one degree of freedom. Okay,
so how are we able to declare four variables in one line? The answer is that chi-square contingency
function returns four values in a set order. It always does. We can also unpack lists of variables from
arrays or tuples. How do we know that the chi-square contingency returns four variables?
That for starters, we can check the official documentation for a function or package. Usually, easily found
through Google. We can also use the built-in help function in Python, like so. So I've
got help. And then in the parentheses, you put the function that you're looking for help
with. Then you click run. And if we scroll all the way to the
top, you can see it gives us some information about what a chi-square test is. Then
it tells us the parameters. So this is how you know what you need to put in the function. So if you're ever confused about what
goes in the parentheses of your function and the tooltips aren't helping you, you can go to help and you can see that oh,
This one has two optional arguments, but the
only requirement is that it has a contingency table. Next you can
see that there are returns here. This means what the function returns.
So this one returns statistic, which is the chi-square statistic, p-value, which is our
p-value of the test, d-o-f, which is the degrees of freedom and the expected frequency,
which is the expected frequencies based on the marginal totals. All right,
hopefully this is enough to help you do your homework. If you're having trouble with this, I recommend
going back again to the collapsing variables video and reviewing that a bit,
and also just taking some time to make sure that your contingency table is set up correctly.
All right, have a wonderful day and have fun coding!
have fun coding.