如何在双向表

时间:2017-08-07 17:16:19

标签: stata chi-squared

我使用命令cchi2在Stata的双向表格中显示每个单元格对Pearson卡方的贡献。每个单元格的p值显示为1十进制,例如,p值为0.0,但我希望看到更多的数字,例如0.052或.050。

有没有办法设置p值十进制的数字?

enter image description here

3 个答案:

答案 0 :(得分:1)

这缺乏一个可重复的示例,我们可以理解数据调用,实际上不提供任何代码。它在Stata和统计数据方面似乎也很混乱。

当与两个变量一起使用时,cchi2命令有一个选项tabulatecchi2 是一个单独的命令;它会产生对卡方的贡献,并且在与chi2选项结合使用时最有意义,例如

. sysuse auto, clear
(1978 Automobile Data)

. tab foreign rep78, chi2 cchi2

+-------------------+
| Key               |
|-------------------|
|     frequency     |
| chi2 contribution |
+-------------------+

           |                   Repair Record 1978
  Car type |         1          2          3          4          5 |     Total
-----------+-------------------------------------------------------+----------
  Domestic |         2          8         27          9          2 |        48 
           |       0.3        1.1        1.8        1.0        4.2 |       8.3 
-----------+-------------------------------------------------------+----------
   Foreign |         0          0          3          9          9 |        21 
           |       0.6        2.4        4.1        2.3        9.5 |      19.0 
-----------+-------------------------------------------------------+----------
     Total |         2          8         30         18         11 |        69 
           |       0.9        3.5        5.9        3.3       13.7 |      27.3 

          Pearson chi2(4) =  27.2640   Pr = 0.000

在这里我们可以看到对卡方的贡献;在这种情况下,27.264卡方统计量的9.5来自右下角的细胞。这些不是单独的P值;对于整个表格,只有一个P值,这里给出0.000到3 d.p.

获取更多详细信息的一种方法是使用tabchi下载ssc install tab_chi命令。这里pearson选项给出了更有用的Pearson残差,(观察到的 - 预期的)/预期的平方根,这是对卡方的贡献的有符号平方根。

. tabchi foreign rep78, pearson

          observed frequency
          expected frequency
          Pearson residual

--------------------------------------------------
          |           Repair Record 1978          
 Car type |      1       2       3       4       5
----------+---------------------------------------
 Domestic |      2       8      27       9       2
          |  1.391   5.565  20.870  12.522   7.652
          |  0.516   1.032   1.342  -0.995  -2.043
          | 
  Foreign |      0       0       3       9       9
          |  0.609   2.435   9.130   5.478   3.348
          | -0.780  -1.560  -2.029   1.505   3.089
--------------------------------------------------

4 cells with expected frequency < 5
1 cell with expected frequency < 1

          Pearson chi2(4) =  27.2640   Pr = 0.000
 likelihood-ratio chi2(4) =  29.9121   Pr = 0.000

在该命令后键入return list会为P值提供更多小数位:

. ret li

scalars:
                  r(N) =  69
                  r(r) =  2
                  r(c) =  5
               r(chi2) =  27.26396103896104
                  r(p) =  .0000175796084266

在您的情况下,我们可以使用您显示的频率从同一包中的命令tabchii获取Pearson残差。

. tabchii 3 10 2 \ 11 54 10, pearson

          observed frequency
          expected frequency
          Pearson residual

----------------------------------
          |          col          
      row |      1       2       3
----------+-----------------------
        1 |      3      10       2
          |  2.333  10.667   2.000
          |  0.436  -0.204   0.000
          | 
        2 |     11      54      10
          | 11.667  53.333  10.000
          | -0.195   0.091   0.000
----------------------------------

2 cells with expected frequency < 5

         Pearson chi2(2) =   0.2786   Pr = 0.870
likelihood-ratio chi2(2) =   0.2643   Pr = 0.876

在你的情况下,由于总卡方统计量恰好小于1,那么所有贡献,(观察到的 - 预期的)/预期的平方也都小于1,但是(a)不是通常为真(b)它们不是P值(在每个单元中没有进行单独的测试)。

tabchitabchii的{​​{1}}选项也相当于cont的{​​{1}}选项。您还可以使用cchi2选项调整显示的小数位数,如文档所述。

答案 1 :(得分:-1)

有一种简单的方法可以在不使用浏览器以外的任何软件的情况下完成此操作。 此在线统计计算器可以提供最多12个小数点的p值:https://www.icalcu.com/stat/chisqtest.html

对于第一个问题,只需粘贴以下数字:

3 10 2
11 54 10

你得到的p值为0.869979427395

对于第二个数据集,只需粘贴以下数字:

2       8      27       9       2
0       0       3       9       9

,你的p值为0.000017579608。

Chi-square test calculator examples

答案 2 :(得分:-1)

我认为将p值显示为大于3 dp的最快方法是在执行卡方后显示存储的结果:

. tab var1 var2, col chi

. display `r(p)'