Hypothesis testing
Rapaio library aims to contain an extensive set of alternatives for hypothesis testing. Right now there are available just some of them. Hypothesis testing an invaluable tool to answer questions when we are dealing with uncertainty.
We deal with presenting hypothesis testing by following some examples.
Z tests
Any hypothesis test which uses normal distribution for the computed statistic is named a z test. We should note that z tests needs to know standard deviations for the involved populations. It is accustomed that when the sample is large enough the value of the population standard deviation can be estimated from data. This is not implemented in library. For the case when one does not know the involved standard deviations one can use t tests.
Note than because of this requirement, the z tests are rarely used. This is so because we rarely know the population parameters.
Example 1: One sample z-test
Sue is in charge of Quality Control at a bottling facility1. She is checking the operation of a machine that should deliver 355 mL of liquid into an aluminum can. If the machine delivers too little, then the local Regulatory Agency may fine the company. If the machine delivers too much, then the company may lose money. For these reasons, Sue is looking for any evidence that the amount delivered by the machine is different from 355 mL.
During her investigation, Sue obtains a random sample of cans. She measures the following volumes:
355.02 355.47 353.01 355.93 356.66 355.98 353.74 354.96 353.81 355.79
The machine's specifications claim that the amount of liquid delivered varies according to a normal distribution, with mean = 355 mL and standard deviation = 0.05 mL.
Do the data suggest that the machine is operating correctly?
The null hypothesis is that the machine is operating according to its specifications; thus
( is the mean volume delivered by the machine)
Sue is looking for evidence of any difference; thus, the alternate hypothesis is
Since the hypothesis concerns a single population mean and the population follows a normal distribution with known standard deviation, a z-test is appropriate.
What we can do is to use HTTools facility which offers shortcut methods to all implemented hypothesis tests. One of them is one sample z test which enables one to test if the sample mean is far from the expected sample mean.
// build the sample
Var cans = NumericVar.copy(355.02, 355.47, 353.01, 355.93, 356.66, 355.98, 353.74, 354.96, 353.81, 355.79);
// run the test and print results
ZTestOneSample.test(cans, 355, 0.05).printSummary();
> HTTools.zTestOneSample
One Sample z-test
mean: 355
sd: 0.05
significance level: 0.05
alternative hypothesis: two tails P > |z|
sample size: 10
sample mean: 355.037
z score: 2.3400855
p-value: 0.019279327322640594
conf int: [355.0060102,355.0679898]
The interpretation of the results is the following:
- the z-score is , which means that the computed sample mean is greater with more than 2 standard deviations
- for critical level being and p-value , we reject the null hypothesis that the mean volume delivered by the machine is equal with
Note: even if we know that the sample mean is greater than the proposed mean, we cannot propose this conclusion. The proper conclusion would be that is different than .
What if we ask if the machine produces more than standard specification?
We deal with this question by changing the null hypothesis. Our hypotheses become:
Our code looks like:
ZTestOneSample.test(cans,
355, \\ mean
0.05, \\ sd
0.05, \\ significance level
HTest.Alternative.GREATER_THAN \\ alternative
).printSummary();
> HTTools.zTestOneSample
One Sample z-test
mean: 355
sd: 0.05
significance level: 0.05
alternative hypothesis: one tail P > z
sample size: 10
sample mean: 355.037
z score: 2.3400855
p-value: 0.009639663661320297
conf int: [355.0060102,355.0679898]
As expected the statistical power of this test is increased. As a consequence the p value was smaller and we still reject the null hypothesis. In this case we had an obvious case, when testing one side gave the same result as testing with two sides. I gave example just to help the user to pay attention to those kind of details.
1: This example is adapted from here.
Example 2: One sample z-test
A herd of steer was fed a special high‐protein grain for a month. A random sample of were weighed and had gained an average of pounds. If the standard deviation of weight gain for the entire herd is , test the hypothesis that the average weight gain per steer for the month was more than pounds.2
We have the following null and alternative hypothesis:
ZTestOneSample ztest = ZTestOneSample.test(
6.7, // sample mean
29, // sample size
5, // tested mean
7.1, // population standard deviation
0.05, // significance level
HTest.Alternative.GREATER_THAN // alternative
);
ztest.printSummary();
> HTTools.zTestOneSample
One Sample z-test
mean: 5
sd: 7.1
significance level: 0.05
alternative hypothesis: one tail P > z
sample size: 29
sample mean: 6.7
z score: 1.2894057
p-value: 0.0986285477062051
conf int: [4.1159112,9.2840888]
P-value is greater than significance level which means that we cannot reject the null hypothesis. We don't have enough evidence.
2: This example is taken from here.
Example 3: Two samples z test
The amount of a certain trace element in blood is known to vary with a standard deviation of ppm (parts per million) for male blood donors and ppm for female donors. Random samples of male and female donors yield concentration means of and ppm, respectively. What is the likelihood that the population means of concentrations of the element are the same for men and women?3
According with central limit theorem we can assume that the distribution of the sample mean is a normal distribution. More than that, since we have random samples, than the sample mean difference has a normal distribution. And because we know the standard deviation for each population, we can use a two sample z test for testing the difference of the sample means.
ZTestTwoSamples.test(
28, 75, // male sample mean and size
33, 50, // female sample mean and size
0, // difference of means
14.1, 9.5, // standard deviations
).printSummary();
> HTTools.zTestTwoSamples
Two Samples z-test
x sample mean: 28
x sample size: 75
y sample mean: 33
y sample size: 50
mean: 0
x sd: 14.1
y sd: 9.5
significance level: 0.05
alternative hypothesis: two tails P > |z|
sample mean: -5
z score: -2.3686842
p-value: 0.017851489594360337
conf int: [-9.1372421,-0.8627579]
The test run with significance level (because it was a default value). The alternative is two tails since we test for difference in means not equal with zero. The resulted p-value is lower than the significance value which means that we reject the hypothesis that the two populations have the same mean. We can see that also from confidence interval, since it does not include .
Note: If we would considered a significance level of than we would not be able to reject the null hypothesis.
3: This example is taken from here.
T tests
T tests are similar with z tests. Both tests uses a statistic which has a normal distribution. A z test is used when one knows standard deviations. A t test is used when the standard deviation is estimated from data. Note that when the sample size is large enough, both tests gives virtually identical results, since the t distributions converges to a normal distribution when the sample increases.
Example 3:
A baseball team coach wants to know if his team is significantly different than other teams in the league in scoring runs. Nationally, the average number of score runs is 5.7. He peek randomly five games which happen to have scores: 8, 9, 4, 10 and 8.
The coach is looking to see if the average number of scores for his team is different than the national average, which is given. We can assume the sample is normally distributed. A z test would be fine under those conditions, but we do not know the national standard deviations. We have to estimate that from sample. Thus a t test is proper in this case, especially because the sample is so small. We fix the significance level at and choose the two tail version of the test since we are interested in testing the equality.
Var x = NumericVar.copy(8, 9, 4, 10, 8).withName("x");
TTestOneSample.test(x, 5.7).printSummary();
> HTTools.tTestOneSample
One Sample t-test
mean: 5.7
significance level: 0.05
alternative hypothesis: two tails P > |z|
sample size: 5
sample mean: 7.8
sample sd: 2.2803509
df: 4
t: 2.0592194
p-value: 0.10854860356776591
conf int: [4.9685704,10.6314296]
The p-value is greater that significance level, so we fail to reject the test.
What if we would estimate the sample standard deviation directly from the sample and we would than use a z test? Let's see.
Var x = NumericVar.copy(8, 9, 4, 10, 8).withName("x");
ZTestOneSample.test(x, 5.7, CoreTools.var(x).sdValue()).printSummary();
> HTTools.zTestOneSample
One Sample z-test
mean: 5.7
sd: 2.2803509
significance level: 0.05
alternative hypothesis: two tails P > |z|
sample size: 5
sample mean: 7.8
z score: 2.0592194
p-value: 0.03947322372463613
conf int: [5.8012211,9.7987789]
According with this test the mean score of the team is different than the national average. This is an illustration failed application of central limit theorem fails with small number of observations. The t test was built to incorporate the unknown in estimating the variance. Thus we increase the need to provide stronger evidence to reject the null hypothesis.