Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Tab_chi without installing package

    Dear Statalist

    I am working within a secure remote data environment, and so it is difficult to download stata packages. I would like to use tab_chi, or similar, to report the residuals within a 4x2 table. Is it possible to do this without installing tab_chi?

    Thank you for your help

    Joe

  • #2
    tab_chi is a package from SSC. It contains various commands with different names, of which tabchi sounds most relevant here.

    What tabchi offers that tabulate doesn't includes Pearson and so-called adjusted residuals. Here for the first is sample Mata-based code showing that the calculation is quite easy.

    I am the author of these commands,which (should) produce the results they were designed to produce, but they were originally written in the late 1990s for Stata 6, long before Mata was introduced.


    Code:
    . sysuse auto, clear
    (1978 automobile data)
    
    . tabchi foreign rep78, pearson adjust
    
              observed frequency
              expected frequency
              Pearson residual
              adjusted residual
    
    --------------------------------------------------
    Car       |           Repair record 1978          
    origin    |      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
              |  0.949   1.990   3.236  -2.098  -4.040
              | 
      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
              | -0.949  -1.990  -3.236   2.098   4.040
    --------------------------------------------------
    
    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
    
    . tab foreign rep78, matcell(freq)
    
               |                   Repair record 1978
    Car origin |         1          2          3          4          5 |     Total
    -----------+-------------------------------------------------------+----------
      Domestic |         2          8         27          9          2 |        48 
       Foreign |         0          0          3          9          9 |        21 
    -----------+-------------------------------------------------------+----------
         Total |         2          8         30         18         11 |        69 
    
    . mata :
    ------------------------------------------------- mata (type end to exit) -------------------------------------
    : freq = st_matrix("freq")
    
    : exp = rowsum(freq) * colsum(freq) / sum(freq)
    
    : exp
                     1             2             3             4             5
        +-----------------------------------------------------------------------+
      1 |  1.391304348   5.565217391   20.86956522   12.52173913   7.652173913  |
      2 |  .6086956522   2.434782609   9.130434783    5.47826087   3.347826087  |
        +-----------------------------------------------------------------------+
    
    : Pearson = (freq :- exp) :/ sqrt(exp)
    
    : Pearson
                      1              2              3              4              5
        +----------------------------------------------------------------------------+
      1 |   .5160468465    1.032093693    1.341944566    -.995233204   -2.043257494  |
      2 |  -.7801894976   -1.560378995   -2.028829482    1.504651174    3.089114967  |
        +----------------------------------------------------------------------------+
    
    : strofreal(Pearson, "%4.3f")
                1        2        3        4        5
        +----------------------------------------------+
      1 |   0.516    1.032    1.342   -0.995   -2.043  |
      2 |  -0.780   -1.560   -2.029    1.505    3.089  |
        +----------------------------------------------+

    Comment

    Working...
    X