We're going to wrap up our discussion on working with categorical variables with more than two levels with the chi-square independence test. In this case we're dealing with two categorical variables at least one of which has more than two levels and we're evaluating the relationship between them. Take a look at this example on obesity and marital status. A study reported in the medical journal Obesity in 2009 analyzed data from the National Longitudinal Study of Adolescent Health. Obesity was defined as having a body mass index of 30 or more. More. The research subjects were followed from adolescence to adulthood, and all the people in the sample were categorized in terms of whether they were obese and whether they were dating, cohabitating, or married. We can view the study results in a contingency table. One of the variables is the obesity status, and that only has two levels, obese or not obese, and the other variable is the relationship status, and that has three levels, dating, co-habiting, and married. Using these results, we want to answer the question, does there appear to be a relationship between weight and relationship status? As usual, we set two hypotheses. The first one is our null hypothesis that says that there's nothing going on. In this case that means that weight and relationship status are independent and obesity rates do not vary by relationship status. The alternative hypothesis, as usual, says that there's something going on, and in this case, that means weight and relationship status are dependent and obesity rates do vary by relationship status. To evaluate these hypotheses just like with the chi square test of goodness of fit, we quantify how different the observed counts are from the expected counts. And we want to keep in mind that large deviations from what would be expected based on sampling variation or chance alone provide strong evidence for the alternative hypothesis. This is called an independence test since we're evaluating the relationship between two categorical variables. The mechanics of the chi square test of independence is very similar to the chi square goodness of fit test, in fact we calculate the chi square test statistic in an exactly the same way. For each cell we look at the observed minus the expected square, divide by the expected counts and we add this over for each of the cells. What is different however, is how we calculate the degrees of freedom. Remember with the chi square goodness of fit test, the degrees of freedom was simply k- 1, k being the number of cells. In this case because we have a two-way table, we need to consider the number of levels for both of the categorical variables. So the degrees of freedom is calculated as the number of rows minus one times number of columns minus one. We're denoting that as r minus one times c minus one here. The conditions are exactly the same between the chi-square test of independence and the chi-square goodness of fit test. The first condition is independence, where we think about sampled observations having to be independent of each other. And remember that we can ensure this by random sampling or assignment, depending on the type of study we're working with. And if sampling is happening without replacement, we want to make sure that our sample size is less than 10% of our population. And we also want to make sure that each case only contributes to one cell in the table. The second condition, as usual, is about sample size. We want to make sure that each particular scenario or cell has at least five expected counts. So we established that the chi squared statistic is calculated similarly for the chi square goodness of fit test and the chi square independence test but before we can get to that calculation we need to define how to calculate the expected counts for a two way table. It's slightly different than when we only work with a one way table and only one categorical variable for the chi-square goodness of fit test. This is what our data looked like. The first question we want to answer is what is the overall obesity rate in the sample? And to calculate that, we simply need to take everyone who's obese in the sample and divide by the overall sample size. So that's 331 divided by 1293. That gives us an overall obesity rate of 25.6%. In the second question, we ask if in fact weight and relationship status are independent, in other words, if in fact the null hypothesis is true, how many of the dating people would we expect to be obese? Likewise, how many of the cohabiting and married people would we expect to be obese? If we're assuming the the null hypothesis is true. That means that we're assuming that the rate of obesity does not vary by relationship status. So the overall rate of obesity that we calculated, the 25.6% should apply to each one of the relationship statuses. In this case, to calculate the number of people who are dating and expected to be obese, we simply take that overall number of people who are dating, 440, and multiply it by the overall obesity rate, 0.256, and that yields roughly 113. We can see that the observed number of people who are dating and obese, 81, is actually much lower than what's expected under the assumption that the null hypothesis is true. Similarly, for cohabiting people, we take the overall number of people who are cohabiting, 429, and multiply that by the overall obesity rate, 0.256, which yields 110 as the expected count and for married people that's 424 x 0.256 = 108. The expected count this time is much lower than what's actually observed. To make sure that our calculations are correct, we can always add up these three expected counts and we should hit the total number of people who are obese. So 113 + 110 + 108 does in fact give us 331. This quick check is always useful to make sure that you're doing your rounding properly, and you're doing your calculations overall properly as well. So in short, to find the expected counts in a two way table, we first consider the overall rate of success and then we apply that overall rate of success to each one of our groups. In short, we could actually come up with a formula for an expected count for a given cell. We multiply the row total times the column total and divide it by the table total. Remember that to find the overall rate of obesity, we had first calculated row total divided by table total, and then for each one of our cells, we had simply multiplied the relevent column total. So this formula simply puts those two steps we did separately in the previous slide, together. Here's our data table once again with all the expected counts filled in. Some of which we've calculated together, some of which we haven't. We want to use these data to test the hypothesis that relationship status and obesity are associated at the 5% significance level. To do so, we need a chi-square statistic. And remember that for each cell, we take the observed minus the expected squared divided by the expected. Similarly, for cohabiting people, 103- 110 squared divided 110 for married people and obese, that's a 147- 108 squared divided by 108. We can go through this same calculations for each one of the cells in our table. You can see the value of computation here as the table gets larger, the calculations by hand become more and more tedious, and more and more tedious always means more error-prone. The chi-square statistic here comes out to be 31.68. We also needed degrees of freedom to calculate the p-value associated with this hypothesis test. And remember that in a chi-square test of independence, the degrees of freedom is number of rows minus one times number of columns minus one. So that's (2 -1) x (3- 1), that's a degrees of freedom of two. We can calculate the p value using r and the function we're going to use is p chi square, and remember that takes the inputs of the observed chi square statistic, the degrees of freedom, and we also usually specify whether we want the tail area below the observed or above the observed. And for chi-square test we always want the tail area above the observed chi-square statistic. So that comes out to be p chi-square of 31.68, two further degrees of freedom and we don't want the lower tail. And that's a pretty small p value we have there. With a small p value, we reject the null hypothesis in favor of the alternative. Which means that these data provide convincing evidence that relationship status and obesity are associated. So based on the significance p value, can we conclude from these data that living with someone is making some people obese, and marrying someone is making people even more obese. The answer is no, we definitely cannot. Remember that this is an observational study, so what we're, we could be seeing the effect of here could also be age. People tend to date when they're younger, then they start to live together, and then at some point they get married. It is possible that there is a causal relationship between obesity status and relationship status but the type of analysis that we conducted here is simply not sufficient to deduce a causal relationship. And we always want to consider in these cases the effect of possible confounders like age or other life factors that one might think about that change along with the different life periods where people tend to be dating co-habitating and married with each other. To recap, we saw two types of chi-square tests. Chi-square test of independence and chi-square test of goodness of fit. In a chi-square test of goodness of fit, we compared the distribution of one categorical variable with more than two levels to a hypothesized distribution. In a chi-square test of independence we evaluate the relationship between two categorical variables one of which at least has more than two levels, so with a chi-square goodness of fit test we only have data from one variable so you can think of that as one column of observed data that we compare to a hypothesized distribution. In a chi-square test of independence, we have data from two variables. So that's two columns of data, and we evaluate the relationship between these two variables to determine if they're independent or dependent.