To analyze a multi-way contingency table, you use log-linear models. In truth, log-linear models are a special case of the Poisson generalized linear model, so you could do that, but log-linear models are more user-friendly. In Python, you may need to use the Poisson GLM, as I gather log-linear models may not be implemented. I will demonstrate the log-linear model using your data with R.
library(MASS)
tab = array(c(95, 31, 20, 70, 29, 18, 21, 69, 98, 54, 35, 11), dim=c(3,2,2))
tab = as.table(tab)
names(dimnames(tab)) = c("outcomes", "actions", "observations")
dimnames(tab)[[1]] = c("0", "1", "2")
dimnames(tab)[[2]] = c("0", "1")
dimnames(tab)[[3]] = c("1", "2")
tab
# , , observations = 1
# actions
# outcomes 0 1
# 0 95 70
# 1 31 29
# 2 20 18
#
# , , observations = 2
# actions
# outcomes 0 1
# 0 21 54
# 1 69 35
# 2 98 11
Log-linear models are simply a series of goodness of fit tests. We can start with a (trivial) null model that assumes all cells have the same expected value:
summary(tab)
# Number of cases in table: 551
# Number of factors: 3
# Test for independence of all factors:
# Chisq = 159.18, df = 7, p-value = 4.772e-31
The null is rejected. Next, we can fit a saturated model:
m.sat = loglm(~observations*actions*outcomes, tab)
m.sat
# Call:
# loglm(formula = ~observations * actions * outcomes, data = tab)
#
# Statistics:
# X^2 df P(> X^2)
# Likelihood Ratio 0 0 1
# Pearson 0 0 1
Naturally, this fits perfectly. At this point, we could build up from the null model seeing if additional terms improve the fit, or drop terms from the saturated model to see if the fit gets significantly worse. The latter is more convenient and is conventional. To see if the distribution of outcomes
by actions
differs as a function of the observation
, we need to drop the interactions between the observations
and the actions * outcomes
. If we also drop the marginal effect of observations
, we are testing if the mean count differs between the two levels of observations
. That may or may not be of interest to you, I don't know.
m1 = loglm(~observations + actions*outcomes, tab)
sum(tab[,,1]) # 263
sum(tab[,,2]) # 288
m2 = loglm(~actions*outcomes, tab)
anova(m2, m1)
# LR tests for hierarchical log-linear models
#
# Model 1:
# ~actions * outcomes
# Model 2:
# ~observations + actions * outcomes
#
# Deviance df Delta(Dev) Delta(df) P(> Delta(Dev))
# Model 1 126.4172 6
# Model 2 125.2825 5 1.134691 1 0.28678
# Saturated 0.0000 0 125.282534 5 0.00000
Model 1
has dropped a single degree of freedom from Model 2
(note that, confusingly, Model 1
$\rightarrow$ m2
, and Model 2
$\rightarrow$ m1
), but the decrease in model fit is very small. It is not significant. There is not enough evidence to suggest that the mean counts differ by observation
. On the other hand, when Model 2
is compared to the Saturated
model, the decrease in fit is highly significant. The data are inconsistent with the idea that the distribution of counts is the same in both levels of observation
.