Simple tests for theses II: Chi-square test of independence¶

In Battalio, Kagel, and Jiranyakul, 1990, participants had the choice between the risky gamble (payoffs in \$)

$A: \begin{cases} 20 & \text{ with probability } 0.7\\ 0 & \text{ with probability } 0.3\\ \end{cases}$

and \$14 for sure

$B: \begin{cases} 14& \text{ with probability } 1.0\\ \end{cases}$.

There were two samples. Respondents in the first sample answered a hypothetical question. 16 out of 29 participants chose $B$. The respondents' choices in the second sample had real consequences. Each respondent played the preferred lottery. 24 out of 31 participants chose $B$.

If we assume that the proportion of people choosing B is the same for real and hypothetical questions, how likely are the observed frequencies? Battalio, Kagel, and Jiranyakul (1990) reported a test statistic of 3.34 and a p-value of 0.07 for this item.

Let's look at the data in the form of a contingency table:

B A
Real 24 7
Hypothetical 16 13
In [1]:
import scipy
import matplotlib
import numpy as np
from matplotlib import pyplot as plt
from scipy import stats
print('numpy version: ' + np.__version__)
print('scipy version: ' + scipy.__version__)
print('matplotlib version: ' + matplotlib.__version__)
numpy version: 1.19.2
scipy version: 1.6.2
matplotlib version: 3.5.0

As a numpy array:

In [2]:
obs = np.array([[24, 7], [16, 13]])
obs
Out[2]:
array([[24,  7],
       [16, 13]])

We can extend the data with the row and column sums (calculating "marginal sums").

B A Sum
Real 24 7 31
Hypothetical 16 13 29
Sum 40 20 60

If our null hypothesis of independence is true, our best estimator of the fraction of people choosing $B$ is $40/60=2/3$ (and $1/3$ for $A$). Then, the expected frequencies given the sizes of the two groups are:

B A Sum
Real 20.67 10.33 31
Hypothetical 19.33 9.67 29
Sum 40 20 60

scipy agrees.

In [3]:
stats.contingency.expected_freq(obs)
Out[3]:
array([[20.66666667, 10.33333333],
       [19.33333333,  9.66666667]])

Similar to the one-way chi square test, the test statistic is defined as the sum of the differences between observed and expected frequencies, $\sum \frac{(observed - expected)^2}{expected}$, sometimes written as $\chi^2 = \sum_i \frac{(E_i-O_i)^2}{E_i}$. The more the observed frequencies deviate from the expected ones, the higher the test statistic. In our example:

In [4]:
(24-20.67) ** 2 / 20.67 + (16-19.33) ** 2 / 19.33 + (7-10.33) ** 2 / 10.33 + (13-9.67) ** 2 / 9.67
Out[4]:
3.3303336453568035

This is the test statistic reported in Battalio, Kagel, and Jiranyakul (1990). (There is small approximation error.) scipy's documentation suggests to call

In [5]:
chi2, p, dof, ex = stats.chi2_contingency(obs, correction=False)

to obtain the test statistic

In [6]:
chi2
Out[6]:
3.337041156840934

the p-value

In [7]:
p
Out[7]:
0.0677363129657488

and the expected frequencies under the null hypothesis

In [8]:
ex
Out[8]:
array([[20.66666667, 10.33333333],
       [19.33333333,  9.66666667]])

Note: In R, this test is one line of code: "prop.test(x=c(24,16), n=c(31,29), correct=F)". Try it online.

Bottom Line: Using R, you can add a statistical test to your thesis just by running a single line of code.

Optional: You may set up a small simulation to estimate the p-value, see Simple Tests for Theses I: One-Way Chi-Square Test.

Literature:

  • Battalio, R., Kagel, J. and Jiranyakul, K., 1990, Testing between alternative models of choice under uncertainty: Some initial results, Journal of Risk and Uncertainty 3, 25-50.