在统计中,Cramér's V (又称为Cramér's phi,表示为φc) 是一个衡量两个
分类变量之间关联的度量,它是一个介于0和+1(包括)之间的值, 0表示两个变量无关,1表示完全相关。它是基于Pearson's chi-squared statistic(皮尔森的卡方统计),由Harald Cramér于1946年发表的。
所以在介绍Cramér's V 相关系数之前,我们先来了解一下皮尔森的卡方统计。
2.1 皮尔森的卡方检验(Pearson's chi-squared test)
是最有名卡方检验之一,最早由卡尔·皮尔逊在1900年发表。
在不多谈理论的情况下,让我们用一个例子来说明什么是皮尔逊的卡方检验。
[En]
Without saying much about the theory, let's use an example to illustrate what Pearson's chi-square test is.
以下数据来自一个研究关于自闭症和疫苗的关联性的数据。
–表格1–
有自闭症无自闭症总共打疫苗621440,034440,655不打疫苗11796,53196,648总共738536,565537,303
在打疫苗的440,655人中,有自闭症的人有621人。在不打疫苗的96,648中,有自闭症的有117人。那么从这一组数据中,我们能否看出疫苗和自闭症是否有关联呢?
第一步
使用皮尔森的卡方检验,我们首先假设:
H0: 疫苗和自闭症并不关联
H1: 疫苗和自闭症并有关联
如果 H0假设成立,那么在打疫苗和不打疫苗的人群中,有自闭症的人数比例应该是一致的。
我们由第三行数据得到,537,303人中有738人患有自闭症,得到患有自闭症的总统比例是738 / 537 , 303 = 0.001374 738/537,303=0.001374 7 3 8 /5 3 7 ,3 0 3 =0 .0 0 1 3 7 4。大约是千分之一多一点。
第二部
如果不考虑样本偏差,那么在接种和未接种人群中的比例是相同的,我们计算了理想接种和未接种组中自闭症患者的数量。
[En]
If the sample deviation is not taken into account, then the proportion is the same in the vaccinated and unvaccinated population, and we calculate the number of autistic patients in the ideal vaccinated and unvaccinated groups.
–表格2–
期望值有自闭症无自闭症总共打疫苗440,655(738/537,303) =602.8440,655(536,565/537,303)=440,049.7440,655不打疫苗96,648(738/537,303)=132.796,648(536,565/537,303)=96,515.396,648总共738536,565537,303
第三步
我们将上面的的计算结果和实际实验数据进行比较,对每个单元格计算出以下统计量( 实 际 值 − 期 望 值 ) 2 期 望 值 \frac{(实际值-期望值)^2}{期望值}期望值(实际值−期望值)2 ,然后对它们求和。
T E S T S T A T = χ S T A T 2 = ∑ 所 有 单 元 格 ( O − E ) 2 E TEST_{STAT} = \chi ^2_{STAT} = \sum_{所有单元格}{\frac{(O-E)^2}{E}}T E S T S T A T =χS T A T 2 =所有单元格∑E (O −E )2
= ( 621 − 602.8 ) 602.8 + . . . + ( 96 , 531 − 96 , 515.3 ) 96 , 515.3 = 2.28 =\frac{(621-602.8)}{602.8}+...+\frac{(96,531-96,515.3)}{96,515.3}=2.28 =6 0 2 .8 (6 2 1 −6 0 2 .8 )+...+9 6 ,5 1 5 .3 (9 6 ,5 3 1 −9 6 ,5 1 5 .3 )=2 .2 8
这个结果服从于χ 2 \chi ^2 χ2分布, 我们可以通过查表得知
P − V a l u e = P ( χ S T A T 2 > = 2.28 ) ) = 0.1309 P-Value=P(\chi^2_{STAT}>=2.28))=0.1309 P −V a l u e =P (χS T A T 2 >=2 .2 8 ))=0 .1 3 0 9
根据经验值,一般拒绝域取0.05,0.1309>0.05, H0为真。
因此,我们认为没有明显证据证明疫苗和自闭症有联系。
3.1 自由度
Cramér's V 相关系数是χ 2 \chi^2 χ2统计量除以卡方检验中的自由度。
所谓自由度与卡方检验中的行数和列数有关。如果我们仔细观察上面的表2,我们会发现,如果已经确定了四个单元中的一个的值,那么其他三个单元的值也确定了,我们称之为自由度1。
[En]
The so-called degree of freedom is related to the number of rows and columns in the chi-square test. if we look closely at Table 2 above, we will find that if the value of one of the four cells has been determined, then the values of the other three cells are determined, we call the degree of freedom 1.
对于卡方检验中的n n n行m m m列的表格其自由度为:
自 由 度 = ( n − 1 ) ( m − 1 ) 自由度=(n-1)(m-1)自由度=(n −1 )(m −1 )
3.2 计算Cramér's V 相关系数
在算得卡方统计量的基础上再进行计算的。
C r a m e ˊ r ′ s V = ∑ 所 有 单 元 格 ( O − E ) 2 E 统 计 样 本 量 ∗ ( m i n ( n , m ) − 1 ) Cramér's V = \frac{\sum_{所有单元格}{\frac{(O-E)^2}{E}}}{统计样本量(min(n,m)-1)}C r a m e ˊr ′s V =统计样本量∗(m i n (n ,m )−1 )∑所有单元格E (O −E )2
说明*
- 统计样本量是指总体样本条数,以上面卡方统计的例子为例,这个统计样本量是537,303。
- 卡方统计的n行m列数中较小一个是指卡方统计中使用的这个表格的行列数,在上面例子中,行数是2,列数也是2,那么min(2,2)=2,那min(2,2)-1=1
所以,以上面例子为例,则计算中的分子是537,303*1。
因此,打疫苗和自闭症之间的相关系数是2.28 / 537 , 303 = 0.0000042 2.28/537,303=0.0000042 2 .2 8 /5 3 7 ,3 0 3 =0 .0 0 0 0 0 4 2。
直接奉上Python代码。
def calculate_caremers_v(df, column_a, column_b):
"""
calculate carmer v for the 2 input columns in dataframe
:param df: Pandas dataframe object
:param column_a: 1st column to study
:param column_b: 2nd column to study
:return: Pandas dataframe object with the duplicated recorders removed.
"""
if column_a not in df.columns:
print("the input columne %s doesn't exit in the dataframe."%column_a)
return None
elif column_b not in df.columns:
print("the input columne %s doesn't exit in the dataframe."%column_b)
return None
else:
cross_tb = pd.crosstab(index = df[column_a], columns = df[column_b])
np_tb = cross_tb.to_numpy()
min_row_column = min(np_tb.shape[0],np_tb.shape[1])
colume_sum = np_tb.sum(axis = 0)
row_sum = np_tb.sum(axis = 1)
total_sum = np_tb.sum()
np_mid = np.matmul(row_sum.reshape(len(row_sum),1),
colume_sum.reshape(1,len(colume_sum)))/total_sum
new_tb = np.divide(np.power((np_tb-np_mid),np.array([2])),
np_mid)
return new_tb.sum()/(total_sum*(min_row_column-1))
Original: https://blog.csdn.net/deecheanW/article/details/120474864
Author: 编程小白的逆袭日记
Title: [数学基础知识] Cramér‘s V 相关系数和Python算法实现
![[Python]-opencv-python模块(cv2)-图片读取和格式转换](https://www.itcode1024.com/wp-content/themes/begin/prune.php?src=https://www.itcode1024.com/wp-content/themes/begin/img/loading.png&w=280&h=210&a=&zc=1)
[Python]-opencv-python模块(cv2)-图片读取和格式转换

K-means、手肘法与K-means优化(K-means++、elkan-means、mini batch k-means)

使用Azero远程语音控制机械臂

OpenCV-Python实战(16)——人脸追踪详解

跟着李沐老师做BERT论文逐段精读(笔记)

【AI目标检测】MMROTATE踩坑记录

理解self-attention的Q, K, V的含义

【OpenCV 例程200篇】223. 特征提取之多边形拟合(cv.approxPolyDP)

关于基于SPFCN库位检测算法的解读与源码分析

Paper Reading||DoReFa-Net: Training Low Bitwidth CNNs With Low Bitwidth Gradients

TensorFlow 真的要被 PyTorch 比下去了吗?

KT142A语音芯片驱动程序以及使用注意事项

目标检测FCOS的初步理解

巧用工具!轻轻松松将声音转换成文字
