Excel统计:如何计算2x2列联表的p值?

问题描述 投票:0回答:1

给出数据,例如:

        A         B           C
1               Group 1     Group 2
2   Property 1     56         651
3   Property 2     97       1,380

如何直接计算p值(即卡方分布的“右尾”概率),而无需为表的期望值设置单独的计算?

p值是在Excel中通过函数ChiSq.dist.RT计算的如果您知道表的卡方值或通过ChiSq.Test 如果知道的表的“期望值”表表格。卡方值是使用期望值计算的,并且期望值是通过一些复杂的公式从原始表中计算得出的,因此,无论哪种方式,Excel都要求我们自己计算期望值以获得p值,这似乎有点傻。因此,如何在Excel中获得p值而无需单独计算期望值?

编辑:该问题最初以标题“如何使用2属性数组计算Pearson相关系数?”发布。并询问为什么函数pearson给出错误的答案。好吧,答案是我将p值与Pearson相关系数混淆了,这是不同的东西。因此,我对问题进行了重新表述,以问我真正需要知道的内容,并发布了答案。我会等一会儿再接受自己的答案,以防其他人有更好的答案。

excel-formula p-value chi-squared
1个回答
0
投票

在我看来,这需要VBA。我编写了以下VBA函数来计算卡方或p值,以及用于2x2列联表的其他两个关联度量:

Public Function nStatAssoc_2x2(sType As String, nGrp1PropCounts As Range, nGrp2PropCounts As Range) As Single

' Return one of several measures of statistical association of a 2×2 contingency table:
'                   Property 1      Property 2
'       Group 1     nCount(1, 1)    nCount(1, 2)
'       Group 2     nCount(2, 1)    nCount(2, 2)

' sType is:     to calculate:
'   "OR"        Odds ratio
'   "phi"       Phi coefficient
'   "chi-sq"    Chi-squared
'   "p"         p-value, i.e., right-tailed probability of the chi-squared distribution

' nGrp<n>PropCounts is a range of two cells containing the number of members of group n that have each of two properties.
' These arguments are 1-D arrays in order to allow the data to appear is non-adjacent ranges in the spreadsheet.

' References:
    ' Contingency table:        https://en.wikipedia.org/wiki/Contingency_table
    ' Measure of association:   www.britannica.com/topic/measure-of-association
    ' Odds ratio:               https://en.wikipedia.org/wiki/Odds_ratio
    '                           https://en.wikipedia.org/wiki/Effect_size#Odds_ratio
    ' Phi coefficient:          https://en.wikipedia.org/wiki/Phi_coefficient
    ' Chi-sq:                   https://en.wikipedia.org/wiki/Pearson's_chi-squared_test#Calculating_the_test-statistic
    '                           www.mathsisfun.com/data/chi-square-test.html
    '                               Shows calculation of expectation values.
    ' p-value:                  https://docs.microsoft.com/en-us/office/vba/api/excel.worksheetfunction.ChiSq_Dist_RT

Dim nCount(1 To 2, 1 To 2) As Integer
Dim nSumGrp(1 To 2) As Integer, nSumProp(1 To 2) As Integer, nSumAll As Integer
Dim nExpect(1 To 2, 1 To 2) As Single
Dim nIndex1 As Byte, nIndex2 As Byte
Dim nRetVal As Single

' Combine input arguments into contingency table:
For nIndex1 = 1 To 2
    nCount(1, nIndex1) = nGrp1PropCounts(nIndex1)
    nCount(2, nIndex1) = nGrp2PropCounts(nIndex1)
  Next nIndex1

' Calculate totals of group counts, property counts, and all counts (used for phi and chi-sq):
For nIndex1 = 1 To 2
    For nIndex2 = 1 To 2
        nSumGrp(nIndex1) = nSumGrp(nIndex1) + nCount(nIndex1, nIndex2)
        nSumProp(nIndex2) = nSumProp(nIndex2) + nCount(nIndex1, nIndex2)
      Next nIndex2
  Next nIndex1
nSumAll = nSumGrp(1) + nSumGrp(2)

If nSumAll <> nSumProp(1) + nSumProp(2) Then
    nRetVal = -2           ' Error: Sums differ.
    GoTo Finished
  End If

Select Case sType

    ' Odds ratio
    Case "OR":
        nRetVal = (nCount(1, 1) / nCount(1, 2)) / (nCount(2, 1) / nCount(2, 2))
        If nRetVal <> (nCount(1, 1) / nCount(2, 1)) / (nCount(1, 2) / nCount(2, 2)) Then
            nRetVal = -3            ' Error: OR calculation results differ.
            GoTo Finished
          End If

    ' Phi coefficient
    Case "phi":
        nRetVal = ((CLng(nCount(1, 1)) * nCount(2, 2)) - (CLng(nCount(1, 2)) * nCount(2, 1))) / _
                    (CSng(nSumGrp(1)) * nSumGrp(2) * nSumProp(1) * nSumProp(2)) ^ 0.5

    ' Chi-squared
    Case "chi-sq", "p":
        ' Calculate table of expectation values:
            ' In this loop, the division is done first to prevent integer overflow,
            '   which can happen if the multiplication is done first.
        For nIndex1 = 1 To 2
            For nIndex2 = 1 To 2
                nExpect(nIndex1, nIndex2) = nSumGrp(nIndex1) / nSumAll * nSumProp(nIndex2)
                If nExpect(nIndex1, nIndex2) < 5 Then
                    ' https://en.wikipedia.org/wiki/Pearson's_chi-squared_test#Assumptions
                    nRetVal = -4        ' Error: Expected value too small.
                    GoTo Finished
                  Else
                    nRetVal = nRetVal + _
                        (nCount(nIndex1, nIndex2) - nExpect(nIndex1, nIndex2)) ^ 2 / nExpect(nIndex1, nIndex2)
                  End If
              Next nIndex2
          Next nIndex1

    Case Else:
        nRetVal = -1           ' Error: Invalid measure type.
        GoTo Finished
  End Select

Select Case sType
    Case "OR", "phi", "chi-sq":

    Case "p": nRetVal = WorksheetFunction.ChiSq_Dist_RT(nRetVal, 1)
  End Select

Finished: nStatAssoc_2x2 = nRetVal

End Function        ' nStatAssoc_2x2()

该函数在Excel 2019中进行了测试,并为几个测试表的所有四个度量生成正确的值。欢迎批评或提出改进建议。

如果我错了,并且这不需要VBA或出于任何其他原因,有更好的方法可以执行此操作,请为此发布其他答案。正如我在问题的编辑笔记中所说的那样,我将等待一会儿再接受我的答案,看看是否有人有更好的答案。

© www.soinside.com 2019 - 2024. All rights reserved.