One Sample Tests of Proportion - Python#

Binomial Test#

  • Samples: 1

  • Response Categories: 2

  • Exact?: Yes, use with N≤200

  • Reporting: “Out of 60 outcomes, 19 were ‘x’ and 41 were ‘y’. A two-sided exact binomial test indicated that these proportions were statistically significantly different from chance (p < .05)”

import pandas as pd

# Example data
# df is a long-format data table w/columns for subject (S) and 2-category outcome (Y)
df = pd.read_csv("data/0F0LBs_binomial.csv")
df.head(20)
S Y
0 1 y
1 2 y
2 3 x
3 4 y
4 5 y
5 6 x
6 7 y
7 8 x
8 9 y
9 10 y
10 11 x
11 12 y
12 13 y
13 14 y
14 15 y
15 16 x
16 17 x
17 18 y
18 19 y
19 20 x
from scipy.stats import binom_test

counts = df.Y.value_counts()
binom_test(counts["y"], counts.sum())
0.0062176026593266705

Multinomial Test#

  • Samples: 1

  • Response Categories: ≥2

  • Exact?: Yes, use with N≤200

  • Reporting: “Out of 60 outcomes, 17 were ‘x’ and 8 were ‘y’, and 35 were ‘z’. An exact multinomial test indicated that these proportions were statistically significantly different from chance (p < .0001)”

import pandas as pd

df = pd.read_csv("data/0F0LBs_multinomial.csv")
df.head(20)
S Y
0 1 z
1 2 z
2 3 z
3 4 x
4 5 z
5 6 z
6 7 z
7 8 z
8 9 z
9 10 z
10 11 z
11 12 x
12 13 z
13 14 y
14 15 z
15 16 y
16 17 x
17 18 z
18 19 z
19 20 z
from scipy.stats import multinomial
import numpy as np

counts = df["Y"].value_counts()
n_cat = df["Y"].nunique()
multinomial.pmf(xt.values, len(df), np.full(n_cat, 1/n_cat))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[4], line 6
      4 counts = df["Y"].value_counts()
      5 n_cat = df["Y"].nunique()
----> 6 multinomial.pmf(xt.values, len(df), np.full(n_cat, 1/n_cat))

NameError: name 'xt' is not defined

One-Sample Pearson Chi-Squared Test#

  • Samples: 1

  • Response Categories: ≥2

  • Exact?: No, use with N>200

  • Reporting: “Out of 60 outcomes, 17 were ‘x’, 8 were ‘y’, and 35 were ‘z’. A one-sample Pearson Chi-Squared test indicated that these proportions were statistically significantly different from chance (χ2 (2, N=60) = 18.90, p < .0001).”

import pandas as pd

df = pd.read_csv("data/0F0LBs_multinomial.csv")
df.head(20)
S Y
0 1 z
1 2 z
2 3 z
3 4 x
4 5 z
5 6 z
6 7 z
7 8 z
8 9 z
9 10 z
10 11 z
11 12 x
12 13 z
13 14 y
14 15 z
15 16 y
16 17 x
17 18 z
18 19 z
19 20 z
from scipy.stats import chisquare

chisquare(df["Y"].value_counts())
Power_divergenceResult(statistic=18.9, pvalue=7.868956527179467e-05)