如果有其他人感兴趣,这是我想出来的方法
为了使用论文中伯克利入学的例子,首先需要使用迭代比例拟合将值标准化(使边距相等)。
def ContTableIPFP(x1ContTable):
''' poor man IPFP
compute iterative proportional fitting for
a 2 X 2 contingency table
Input :
a 2x2 contingency table as numpy array
Output :
numpy array with values standarized to equate margins
'''
import numpy as np
xSumRows = np.sum(x1ContTable, axis = 0).tolist()
xSumCols = np.sum(x1ContTable, axis = 1).tolist()
xq0 = x1ContTable/x1ContTable
xq1 = np.array([
(xq0[0] * xSumCols[0]).astype(float) / np.sum(xq0, axis = 0).tolist()[0],
(xq0[1] * xSumCols[1]).astype(float) / np.sum(xq0, axis = 0).tolist()[1],
]
)
xq2 = np.array([
(xq1[:,0] * xSumRows[0]).astype(float) / np.sum(xq1, axis = 0).tolist()[0],
(xq1[:,1] * xSumRows[1]).astype(float) / np.sum(xq1, axis = 0).tolist()[1],
]
)
return xq2.T
然后绘制图表
def FourfoldDisplay(radii):
''' radii = [10, 15, 20, 25]
'''
import numpy as np
import matplotlib.pyplot as plt
width = np.pi/ 2
theta = np.radians([0,90,180,270])
ax = plt.subplot(111, polar=True)
bars = ax.bar(theta, radii, width=width, alpha=0.5)
ax.set_xticklabels([])
ax.set_yticks([])
plt.show()
使用
import numpy as np
x1 = np.array([
[1198, 1493],
[557, 1278]
])
x2 = ContTableIPFP(x1).flatten()
FourfoldDisplay(x2)