Bird
Raised Fist0
MATLABdata~10 mins

Hypothesis testing (ttest, chi2test) in MATLAB - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to perform a t-test on sample data.

MATLAB
data = [5.1, 4.9, 5.0, 5.2, 5.3];
[h, p] = [1](data, 5);
Drag options to blanks, or click blank then click option'
Achi2test
Bttest
Canova1
Dttest2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'chi2test' which is for categorical data, not means.
Using 'ttest2' which is for two-sample tests.
2fill in blank
medium

Complete the code to perform a chi-square test on observed and expected counts.

MATLAB
observed = [20, 30, 50];
expected = [25, 25, 50];
[h, p] = chi2gof(1:length(observed), 'Ctrs', 1:length(observed), 'Frequency', [1], 'Expected', expected);
Drag options to blanks, or click blank then click option'
Aobserved
Bexpected
Ch
Dp
Attempts:
3 left
💡 Hint
Common Mistakes
Passing expected counts as 'Frequency' instead of observed counts.
Confusing the parameters and their order.
3fill in blank
hard

Fix the error in the code to perform a two-sample t-test between two groups.

MATLAB
group1 = [5.1, 5.3, 5.2];
group2 = [4.9, 5.0, 5.1];
[h, p] = [1](group1, group2);
Drag options to blanks, or click blank then click option'
Attest
Banova1
Cchi2test
Dttest2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ttest' which is for one sample or paired tests.
Using 'chi2test' which is not for means.
4fill in blank
hard

Fill both blanks to create a structure with counts and perform a chi-square test.

MATLAB
counts = [10, 15, 25];
expected = [15, 15, 20];
[h, p] = chi2gof([1], 'Frequency', [2], 'Expected', expected);
Drag options to blanks, or click blank then click option'
A1:length(counts)
Bcounts
Cexpected
Dcounts + expected
Attempts:
3 left
💡 Hint
Common Mistakes
Passing counts as the first argument instead of categories.
Passing expected counts as 'Frequency'.
5fill in blank
hard

Fill all three blanks to perform a paired t-test and display the p-value.

MATLAB
before = [5.1, 5.3, 5.2, 5.4];
after = [5.0, 5.1, 5.3, 5.5];
[h, p] = [1]([2], [3]);
disp(p);
Drag options to blanks, or click blank then click option'
Attest
Bbefore
Cafter
Dttest2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ttest2' which is for independent samples, not paired.
Swapping the order of 'before' and 'after'.

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

  1. 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.
  2. Step 2: Compare with other tests

    chi2gof tests counts, not means; variance tests are different; normality tests are separate.
  3. Final Answer:

    Whether the mean of a sample differs from a specified value -> Option C
  4. Quick Check:

    ttest tests mean difference [OK]
Hint: Remember: ttest checks means, chi2 tests counts [OK]
Common Mistakes:
  • Confusing ttest with chi-square test
  • Thinking ttest checks variance
  • Assuming ttest checks distribution shape
2. Which of the following is the correct syntax to perform a one-sample t-test in MATLAB on data vector X against mean 0?
easy
A. chi2gof(X, 0)
B. ttest(X, 0)
C. ttest(0, X)
D. ttest(X)

Solution

  1. Step 1: Recall ttest syntax

    The syntax ttest(X, mu) tests if mean of X equals mu.
  2. Step 2: Check options

    chi2gof(X, 0) uses wrong function; ttest(0, X) reverses arguments; ttest(X) misses mean value.
  3. Final Answer:

    ttest(X, 0) -> Option B
  4. Quick Check:

    Correct syntax is ttest(data, mean) [OK]
Hint: Syntax: ttest(data, mean) for one-sample test [OK]
Common Mistakes:
  • Swapping arguments order
  • Using chi2gof for mean testing
  • Omitting mean parameter
3. What is the output of the following MATLAB code?
X = [5.1, 5.3, 5.0, 5.2, 5.4];
[h, p] = ttest(X, 5.0);
disp(h); disp(p);
medium
A. h = 1, p < 0.05 (reject mean = 5.0)
B. h = 1, p > 0.05 (fail to reject mean = 5.0)
C. h = 0, p < 0.05 (reject mean = 5.0)
D. h = 0, p > 0.05 (fail to reject mean = 5.0)

Solution

  1. Step 1: Compute sample statistics

    X mean = (5.1 + 5.3 + 5.0 + 5.2 + 5.4)/5 = 5.2; std ≈ 0.158.
  2. Step 2: Interpret ttest output

    t ≈ (5.2 - 5.0) / (0.158 / √5) ≈ 2.83, df=4, p ≈ 0.047 < 0.05, so h=1 (reject null).
  3. Final Answer:

    h = 1, p < 0.05 (reject mean = 5.0) -> Option A
  4. Quick Check:

    mean=5.2, t≈2.83 > 2.776, p<0.05 [OK]
Hint: Sample mean 5.2 sufficiently differs from 5.0 (t>crit) -> h=1, p<0.05 [OK]
Common Mistakes:
  • Assuming data too close to 5.0 for significance (h=0)
  • Miscalculating p-value as >0.05
  • Confusing h=1 (reject) meaning
4. Identify the error in this MATLAB code snippet for a chi-square goodness-of-fit test:
observed = [20, 30, 50];
expected = [25, 25, 50];
h = chi2gof(observed, 'ExpectedCounts', expected);
medium
A. The 'Expected' parameter is misspelled
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

  1. Step 1: Understand chi2gof input requirements

    chi2gof expects raw sample data, not observed counts directly.
  2. Step 2: Check usage of 'ExpectedCounts'

    The 'ExpectedCounts' parameter is valid but only used with raw data; providing counts directly leads to incorrect results.
  3. Final Answer:

    The observed data should be a vector of counts, but chi2gof expects raw data samples -> Option D
  4. 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

  1. 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].
  2. Step 2: Generate raw data samples for chi2gof

    Since chi2gof needs raw data, create a vector with repeated category labels matching observed counts.
  3. Step 3: Perform chi-square test

    Use chi2gof with raw data and specify expected counts via 'ExpectedCounts' parameter.
  4. Final Answer:

    Convert expected proportions to expected counts, then use chi2gof with raw data generated from counts -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard