One-sample test for proportion

Viewed 6689

I want to do "One-sample test for proportion" with Python. I found this document one sample proportion ztest example but I don't understand how to use it. For example, what are count and nobs. In the 2 examples, example1 gives single number for count and nobs, however, example2 gives 2 numbers.

For result, I'd like to know the p-value that the event happen rate is higher than 60%

Example1

>>> count = 5
>>> nobs = 83
>>> value = .05
>>> stat, pval = proportions_ztest(count, nobs, value)
>>> print('{0:0.3f}'.format(pval))
0.695

Example2

>>> import numpy as np
>>> from statsmodels.stats.proportion import proportions_ztest
>>> count = np.array([5, 12])
>>> nobs = np.array([83, 99])
>>> stat, pval = proportions_ztest(counts, nobs)
>>> print('{0:0.3f}'.format(pval))
0.159

My data looks like this

Yes No
1   0
1   0
1   0
0   1
0   1
1   0
1   0
0   1
0   1
0   1
0   1
0   1

Can you help explain how to use it and give some examples?

Thank you!

3 Answers

In case of example 1:

nobs is the total number of trials, i.e. the number of rows in your list.

count is the number of successful trials, i.e. the number of Yes events in your list.

value is the proportion to test against, i.e. 0.6 based on your question text.

The null hypothesis here is that the single sample given by these values was drawn from a distribution with proportion equal to the specified value.

In case of example 2:

There are two independent samples, the first entry of the nobs and count vectors represent the first sample, the second ones the second sample. value is then omitted and the null hypothesis will be that the two samples have equal true proportion.

The answer provided by user10605163 is correct for the question asked.

However, as the proportions_ztest from statsmodel uses classical statistical method of using normal distribution to approximate binomial distribution, the p-value you get from proportions_ztest is different from what you get if you calculate it from first principle.

In this age of computing, it seems that using normal distribution to approximate binomial is no longer necessary, especially for such small trial sizes.

You can use a bit of combinatorics to calculate the probability space, or you can simulate many sets of trials and get the p-value directly without any test statistics.

For the explanation of how to use this document is in comments. For printing out p-value in scientific notation:

from decimal import Decimal
print('{0:.2E}'.format(Decimal(pval))) 
Related