Challenge - 5 Problems
Chi-squared Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Chi-squared test output interpretation
What is the output of this Python code performing a chi-squared test on a contingency table?
Data Analysis Python
import numpy as np from scipy.stats import chi2_contingency # Contingency table: rows = Gender (Male, Female), columns = Preference (A, B) data = np.array([[30, 10], [20, 40]]) chi2, p, dof, expected = chi2_contingency(data) print(round(p, 3))
Attempts:
2 left
💡 Hint
Look at the p-value returned by chi2_contingency and round it to three decimals.
✗ Incorrect
The chi-squared test compares observed and expected counts. The p-value here is very small (0.001), indicating a significant association between gender and preference.
❓ data_output
intermediate2:00remaining
Expected frequencies from chi-squared test
Given this contingency table, what is the expected frequency for the cell in row 1, column 2 after running chi2_contingency?
Data Analysis Python
import numpy as np from scipy.stats import chi2_contingency data = np.array([[10, 20], [30, 40]]) chi2, p, dof, expected = chi2_contingency(data) print(round(expected[0,1], 1))
Attempts:
2 left
💡 Hint
Expected frequency = (row total * column total) / grand total.
✗ Incorrect
The expected frequency for cell (0,1) is calculated as (row 0 total * column 1 total) / grand total = (30 * 60) / 100 = 18.0.
🔧 Debug
advanced2:00remaining
Identify the error in chi-squared test code
What error does this code raise when trying to perform a chi-squared test?
Data Analysis Python
from scipy.stats import chi2_contingency data = [[10, 20, 30], [5, 15]] chi2, p, dof, expected = chi2_contingency(data)
Attempts:
2 left
💡 Hint
Check the shape of the input data for chi2_contingency.
✗ Incorrect
The input data must be a rectangular array. Here, the second row has fewer elements, causing a ValueError.
🧠 Conceptual
advanced1:30remaining
Understanding degrees of freedom in chi-squared test
For a contingency table with 4 rows and 3 columns, what is the degrees of freedom used in the chi-squared test?
Attempts:
2 left
💡 Hint
Degrees of freedom = (number of rows - 1) * (number of columns - 1).
✗ Incorrect
Degrees of freedom for chi-squared test on a contingency table is (4-1)*(3-1) = 3*2 = 6.
🚀 Application
expert3:00remaining
Chi-squared test on survey data
You have survey data on smoking status (Smoker, Non-Smoker) and lung disease (Yes, No). Which option shows the correct Python code to perform a chi-squared test and print the p-value?
Attempts:
2 left
💡 Hint
Use pivot_table with aggfunc='sum' to create the contingency table correctly.
✗ Incorrect
Option D correctly creates the contingency table summing counts by Smoking and Disease, then runs chi2_contingency and prints rounded p-value.