Saturday, April 3, 2010

Using the Python code for testing proportions. More examples.

Click on Test of Proportions to view the Python code.

How to use the testforproportions.py program? The Python code includes examples from Walpole's text.
We illustrate again the use of the code for testing proportions.

1. Case 1. Single population. Exact test for small n (n < 30).

It is desired to test the null hypothesis that a bad population of manufactured goods has
proportion of defectives approaching 10 %. Test the hypothesis 4 defective items turned up in
a small sample of size 12. Use a two-sided test and a significance level of 5 percent.

The null hypothesis is p_0 = 0.10. and the sample proportion obtained is 4/12 = 1/3.
Here is the call from testofproportions.py, assuming you have done
from testofproportions import *
.

print exactproptest(0.10, 4,12, 0.05, 0)

The output is (0.19384765625, 4, (3, 9)) where the first value is the p-value, the next is the test statistic and (3,9) are the
upper and critical values. Since 4 is inside the (95 percent confidence interval), and the pvalue is greater than five percent,
we accept the Null hypothesis that the population
proportion is 10 percent. The user of our library is provided with much information!

2. Case 2. Single population. Normal approximation for large samples.

Now consider the case of handling a large sample size. It is assumed that the prevalence rate of occasional smoking
among girls who graduated from exclusive religious schools is 2 percent. A survey of 1000 respondents from different
public colleges yielded 31 respondents who answered affirmatively to "did occasional smoking". Does this survey
support the hypothesis?

This time we call on normproptest() with the same type of arguments.

print normproptest(0.02, 31, 1000, alpha = 0.01, side=1)
(0.0064840026163685005, 2.4846467329894408, 2.3263478740408408)

No the output has a p-value of 0.6 percent much less than 1 percent significance level. So we REJECT the null hypothesis.

3. Case 3. Testing whether two populations have the same proportion.

The function twosampleproptest expects the following from the two samples:
x1, n1, x2, n2 (x1, and x2 are the number of cases with the characteristic to study;n1, n2 are sample sizes)
and the specified significance level and the kind of test to perform.

Example from Berenson,"Basic Business Statistics", p.371:
330 boys and 330 girls were surveyed and asked "Do you worry about having enough money?". 201 of the boys and 178 of the
girls answere Yes. At the 5 percent level of significance, is the proportion of the boys who worry about having enough money
greater than the proportion of girls?

Calling the function twosampleproptest resulted in

>>> twosampleproptest(201, 330, 178,330, alpha = 0.05, side=1)
p1hat, p2hat 0.609090909091 0.539393939394
(0.035099935662108783, 1.8106188559981824, 1.6448536269514722)

Since the p-value is less than 5 percent, we reject the Null hypothesis. We also reject it on the ground that the statistical test statistic 1.81 is greater than upper critical limit 1.64.