Hypothesis Testing with t-test and Chi-square Test in MATLAB
📖 Scenario: You are a data analyst working with a small dataset. You want to check if the average height of a group is different from a known value, and also if two categorical variables are related.
🎯 Goal: Build a MATLAB script that performs a one-sample t-test and a chi-square test on given data.
📋 What You'll Learn
Create a vector of heights with exact values
Create a contingency table with exact counts
Use MATLAB's ttest function to test the mean height against a known value
Use MATLAB's chi2gof or chi2cont equivalent to test independence
Print the test results clearly
💡 Why This Matters
🌍 Real World
Hypothesis testing is used in many fields like medicine, marketing, and social sciences to make decisions based on data.
💼 Career
Data analysts and scientists use t-tests and chi-square tests to validate assumptions and find relationships in data.
Progress0 / 4 steps
1
Create the height data vector
Create a vector called heights with these exact values: [170, 172, 168, 165, 174, 169, 171].
MATLAB
Hint
Use square brackets to create a vector in MATLAB.
2
Create the contingency table
Create a 2x2 matrix called contingencyTable with these exact counts: [30, 10; 20, 40].
MATLAB
Hint
Use square brackets and semicolon to create a matrix in MATLAB.
3
Perform the one-sample t-test
Use MATLAB's ttest function to test if the mean of heights is different from 170. Store the test result in h_ttest and the p-value in p_ttest.
MATLAB
Hint
Use [h, p] = ttest(data, mean_value) to perform a one-sample t-test.
4
Perform the chi-square test and display results
Use MATLAB's chi2gof function with the observed counts from contingencyTable to test independence. Store the test result in h_chi2 and the p-value in p_chi2. Then print the values of h_ttest, p_ttest, h_chi2, and p_chi2 with descriptive messages.
MATLAB
Hint
Use chi2gof with observed and expected frequencies. Use fprintf to print results.
Practice
(1/5)
1. What does the MATLAB function ttest primarily test?
easy
A. Whether the data follows a normal distribution
B. Whether two samples have the same variance
C. Whether the mean of a sample differs from a specified value
D. Whether the counts in categories match expected counts
Solution
Step 1: Understand the purpose of ttest
The ttest function in MATLAB tests if the mean of a sample is equal to a given value.
Step 2: Compare with other tests
chi2gof tests counts, not means; variance tests are different; normality tests are separate.
Final Answer:
Whether the mean of a sample differs from a specified value -> Option C
B. The vectors observed and expected must be the same length
C. chi2gof cannot be used with expected counts
D. The observed data should be a vector of counts, but chi2gof expects raw data samples
Solution
Step 1: Understand chi2gof input requirements
chi2gof expects raw sample data, not observed counts directly.
Step 2: Check usage of 'ExpectedCounts'
The 'ExpectedCounts' parameter is valid but only used with raw data; providing counts directly leads to incorrect results.
Final Answer:
The observed data should be a vector of counts, but chi2gof expects raw data samples -> Option D
Quick Check:
chi2gof needs raw data, not counts [OK]
Hint: chi2gof needs raw data, not counts vector [OK]
Common Mistakes:
Passing counts instead of raw data
Misspelling parameter names
Assuming chi2gof accepts counts directly
5. You have observed counts obs = [40, 35, 25] and expected proportions expProp = [0.3, 0.4, 0.3]. How do you correctly perform a chi-square goodness-of-fit test in MATLAB to check if observed counts fit expected proportions?
hard
A. Convert expected proportions to expected counts, then use chi2gof with raw data generated from counts
B. Directly use chi2gof(obs, 'Expected', expProp)
C. Use ttest(obs, expProp) to compare counts and proportions
D. Use chi2gof with observed counts and expected counts vectors
Solution
Step 1: Calculate expected counts from proportions
Multiply total observed count by expected proportions: total = 40+35+25=100; expected counts = 100 * expProp = [30, 40, 30].
Step 2: Generate raw data samples for chi2gof
Since chi2gof needs raw data, create a vector with repeated category labels matching observed counts.
Step 3: Perform chi-square test
Use chi2gof with raw data and specify expected counts via 'ExpectedCounts' parameter.
Final Answer:
Convert expected proportions to expected counts, then use chi2gof with raw data generated from counts -> Option A
Quick Check:
chi2gof needs raw data and expected counts [OK]
Hint: Convert proportions to counts, create raw data for chi2gof [OK]
Common Mistakes:
Passing proportions directly as expected counts
Using ttest for count data
Not converting counts to raw data samples
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.