Announcement

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

  • Chi2 test

    I am running a chi2 test in Stata, however I see the degreees of freedom may not be right. The data can be put as:

    input str10 gender chocolat vainilla strawberry
    "man" 15 10 15
    "woman" 10 5 5
    end


    When running the test, as for example, in chocolat:

    . tab gender chocolat, chi2

    | chocolat
    gender | 10 15 | Total
    -----------+----------------------+----------
    man | 0 1 | 1
    woman | 1 0 | 1
    -----------+----------------------+----------
    Total | 1 1 | 2

    Pearson chi2(1) = 2.0000 Pr = 0.157


    The degress of freedom is calculated as 1. However, (rows-1)x(columns-1), is 2. What I am missing here?

  • #2
    When you run -tab gender chocolat, chi2-, the variables vainilla [sic] and strawberry are ignored. chocolat itself is a 0/1 variable. So the number of degrees of fredom in this analysis is indeed (#genders - 1) * (#chocolats - 1) = (2-1)*(2-1) = 1*1 = 1.

    What I think you have in mind, is an analysis of gender vs flavor, where flavor encompasses all three of chocolat, vainilla, and strawberry. That would indeed provide df = (#genders - 1) * (#flavors - 1) = (2-1) * (3-1) = 1*2 = 2. But your data set, as currently organized, does not have a flavor variable, so you cannot do this analysis. To get to that you need to -reshape- the data and create a flavor variable:

    Code:
    . rename (chocolat vainilla strawberry) n=
    
    . reshape long n, i(gender) j(flavor) string
    (j = chocolat strawberry vainilla)
    
    Data                               Wide   ->   Long
    -----------------------------------------------------------------------------
    Number of observations                2   ->   6           
    Number of variables                   4   ->   3           
    j variable (3 values)                     ->   flavor
    xij variables:
            nchocolat nstrawberry nvainilla   ->   n
    -----------------------------------------------------------------------------
    
    .
    . tab gender flavor, chi2
    
               |              flavor
        gender |  chocolat  strawbe..   vainilla |     Total
    -----------+---------------------------------+----------
           man |         1          1          1 |         3
         woman |         1          1          1 |         3
    -----------+---------------------------------+----------
         Total |         2          2          2 |         6
    
              Pearson chi2(2) =   0.0000   Pr = 1.000

    Comment


    • #3
      If #1 were the only prpblem, this is another way to solve it directly:

      Code:
      . tabi 15 10 15 \ 10 5 5, chi2
      
                 |               col
             row |         1          2          3 |     Total
      -----------+---------------------------------+----------
               1 |        15         10         15 |        40 
               2 |        10          5          5 |        20 
      -----------+---------------------------------+----------
           Total |        25         15         20 |        60 
      
                Pearson chi2(2) =   1.1250   Pr = 0.570
      In #2 Clyde Schechter forgot to apply the frequencies as weights.


      Code:
      . clear 
      
      . input str10 gender chocolate vanilla strawberry
      
               gender  chocolate    vanilla  strawbe~y
        1. "man" 15 10 15
        2. "woman" 10 5 5
        3. end
      
      . 
      . rename (chocolate-strawberry) (n=), string 
      
      . 
      . reshape long n, i(gender) j(flavour) string 
      (j = chocolate strawberry vanilla)
      
      Data                               Wide   ->   Long
      -----------------------------------------------------------------------------
      Number of observations                2   ->   6           
      Number of variables                   4   ->   3           
      j variable (3 values)                     ->   flavour
      xij variables:
              nchocolate nstrawberry nvanilla   ->   n
      -----------------------------------------------------------------------------
      
      . 
      . list, sepby(gender)
      
           +--------------------------+
           | gender      flavour    n |
           |--------------------------|
        1. |    man    chocolate   15 |
        2. |    man   strawberry   15 |
        3. |    man      vanilla   10 |
           |--------------------------|
        4. |  woman    chocolate   10 |
        5. |  woman   strawberry    5 |
        6. |  woman      vanilla    5 |
           +--------------------------+
      
      . 
      . tab gender flavour [fw=n], chi2 
      
                 |             flavour
          gender | chocolate  strawbe..    vanilla |     Total
      -----------+---------------------------------+----------
             man |        15         15         10 |        40 
           woman |        10          5          5 |        20 
      -----------+---------------------------------+----------
           Total |        25         20         15 |        60 
      
                Pearson chi2(2) =   1.1250   Pr = 0.570

      Comment

      Working...
      X