Python Tutorial: Testing proportion and correlation

Learn three statistical tests in Python: chi-square for comparing a sample to expected proportions, Fisher exact test for two categorical groups, and Pearson correlation for continuous variables like athlete height and weight. Each test produces a p-value to determine if results are statistically significant.
Chapters
Lets move on to some more tests. Recall, the 2 sample T test is a way for us to look for differences between the means of two samples. This means a t test looks at a continuous variable in relation to a categorical one. In this lesson we'll look at three new types of hypothesis tests. The chi square test, the related Fisher exact test and the Pearson test of correlation. The first two of these are for examining proportions of samples in discrete categories, while the last examines if two variables are correlated. Consider a discrete variable like a coin toss where the outcome can be either heads or tails. Let's say we want to test whether a coin gives bias results. A chi square test allows us to distinguish between the null hypothesis that the observed outcomes fit with the unbiased expected distribution and the alternative hypothesis that they are biased and don't fit the expected distribution of outcomes. If we flip a coin 30 times and get 34 heads, is that significantly different from our expectation of 15 heads? Lets run a chi square test using our coin flipping example. We have a data frame where each row has a value of heads or tails. For flip, we call the valuecounts function on on flip which gives us the summary totals. We then pass this to the chi square function which when given a single argument, will compute the p value or the likelihood of observing this distribution of values. When we expect an equal ability of both outcomes. A related test is the Fisher exact test, also known as the chi square 2x2 contingency table. A Fisher exact test resembles a chi square in the same way that a 2 sample T test resembles a 1 sample T test. This test distinguishes between the null hypothesis that the distribution of outcome is the same in two samples and the alternative hypothesis that the outcomes differ. Here we can test whether the differences between the numbers of heads versus tails observed after 30 flips is significantly different for these two samples, allowing us to relate one discrete variable to another. To implement this test, we first need to create a crosstabulation which summarizes the occurrence of each possible combination of our variables. To do so, we give the crosstab function two variables, coin and flip, yielding a 2x2 table with the numbers of heads and tails for each coin. We then give this table to the fisherexact function which will output our p value at index 1. The last two tests dealt with discrete variables. What if we want to see how one continuous variable relates to another? How do we examine the possibility that two variables are correlated? This graph shows the weight and height of Olympic athletes and it looks like they might be correlated to test this we use Pearson's or, which measures the degree of linear correlation between two variables. To implement the test, we need to provide the Pearson OR function with two series, one for weight and one for height. We can extract these from a data frame with a dot. The output of this function contains two numbers. At position 0 is the correlation coefficient, a value between 0 and 1, measuring the correlation between the two variables. The P value is at position 1, and again tells us how likely we would be to see this pattern if no correlation were present. Let's practice these three news to.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







