EME 210
Data Analytics for Energy Systems

Chi-Square Test: One Line Test

PrintPrint

Chi-Square Test: One Line Test

Read It: Chi-Square One Line Test

Similar to the one-line hypothesis tests discussed in Lesson 6, there is also a one-line test for chi-square tests. This test uses the stats.chisquare command in the scipy.stats library, the documentation for the command can be found here. In order for this test to work, you need to provide f_obs (the observed count) and f_exp (the expected count). Below we demonstrate how to implement this code.

 Watch It: Video -  Chi-Square One-Line Test (2:46 minutes)

Click here for a transcript.

We're going to go over the chi-squared test one-line command. And so, this command does what our randomization procedure does sort of under the hood. So we never need to see what's going on, but the basis of both methods is the exact same. So I've gone ahead and printed our table here, to remind us of what the table was. And so, if we want to do a one-liner, we're going to use this command up here, stats dot chi-square. we need f_obs which is the observed count, and we need f expect which is the expected count.

And so, just to note that unless specifically stated, you can do either method to conduct the chi-square test as you see fit. But if we specify that we want the randomization procedure, then you need to do that loop that we did in an earlier video. And so to conduct this one-liner, I'm going to call the variable stats, or results. And we just say stats dot chi-square. We give it our observed variable, which is table count. We give it the expected variable, which is counts_exp, or count_exp, whatever we called it up here just to make sure. Yes, count_exp. And then we can print value as just results dot p-value.

And so, from here, we could then make illusion and say that the p-value is greater than our significance level so we fail to reject or accept the null hypothesis. Similar to what we did up here, so we have the same conclusion for both styles of test.

Credit: © Penn State is licensed under CC BY-NC-SA 4.0

Try It: GOOGLE COLAB

  1. Click the Google Colab file used in the video here.
  2. Go to the Colab file and click "File" then "Save a copy in Drive", this will create a new Colab file that you can edit in your own Google Drive account.
  3. Once you have it saved in your Drive, try to edit the following code to implement the one-line test using the card data you collected earlier in the lesson:

Note: You must be logged into your PSU Google Workspace in order to access the file.

# Copy your data from the previosu DataCamp exercise
table = pd.DataFrame({'Suit' : ['Diamonds', 'Hearts', 'Clubs', 'Spades'],
                      'Count' : [0,0,0,0]})

# find expected counts
n = ...
p = ...
count_exp = ...

# implement one-line code
results = stats.chisquare(f_obs = ..., f_exp = ...)
print('p-value: ', ...)

Once you have implemented this code on your own, come back to this page to test your knowledge.


 Assess It: Check Your Knowledge

Knowledge Check