Navigation
Synopsis T-test on sample data.
Function
  1. num tTest(list[num] sample1, list[num] sample2)
  2. bool tTest(list[num] sample1, list[num] sample2, num alpha)
  3. bool tTest(num mu, list[num] sample, num alpha)
Usage import analysis::statistics::Inference;
Description Perform student's t-test. The test is provided in three variants:
  1. Returns the observed significance level, or p-value, associated with a two-sample, two-tailed t-test comparing the means of the input samples. The number returned is the smallest significance level at which one can reject the null hypothesis that the two means are equal in favor of the two-sided alternative that they are different. For a one-sided test, divide the returned value by 2.

    The t-statistic used is t = (m1 - m2) / sqrt(var1/n1 + var2/n2) where
    • n1 is the size of the first sample
    • n2 is the size of the second sample;
    • m1 is the mean of the first sample;
    • m2 is the mean of the second sample;
    • var1 is the variance of the first sample;
    • var2 is the variance of the second sample.
  2. Performs a two-sided t-test evaluating the null hypothesis that sample1 and sample2 are drawn from populations with the same mean, with significance level alpha. This test does not assume that the subpopulation variances are equal. Returns true iff the null hypothesis that the means are equal can be rejected with confidence 1 - alpha. To perform a 1-sided test, use alpha / 2.
  3. Performs a two-sided t-test evaluating the null hypothesis that the mean of the population from which sample is drawn equals mu. Returns true iff the null hypothesis can be rejected with confidence 1 - alpha. To perform a 1-sided test, use alpha * 2.
Examples We use the data from the following example to illustrate the t-test. First, we compute the t-statistic using the formula given above.
rascal>import util::Math;
ok
rascal>import analysis::statistics::Descriptive;
ok
rascal>import List;
ok
rascal>s1 = [5,7,5,3,5,3,3,9];
list[int]: [5,7,5,3,5,3,3,9]
rascal>s2 = [8,1,4,6,6,4,1,2];
list[int]: [8,1,4,6,6,4,1,2]
rascal>(mean(s1) - mean(s2))/sqrt(variance(s1)/size(s1) + variance(s2)/size(s2));
real: 0.847318545739313442237695301445819365361600242897486018389686
This is the same result as obtained in the cited example. We can also compute it directly using the tTest functions:
rascal>import analysis::statistics::Inference;
ok
rascal>tTest(s1, s2);
num: 0.4115203997374087
Observe that this is a smaller value than comes out directly of the formula. Recall that: The number returned is the smallest significance level at which one can reject the null hypothesis that the two means are equal in favor of the two-sided alternative that they are different. Finally, we perform the test around the significance level we just obtained:
rascal>tTest(s1,s2,0.40);
bool: false
rascal>tTest(s1,s2,0.50);
bool: true
Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.