Announcement

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

  • Temporarily flip order of binary variable coding in 2x2 table

    Hello,

    I have what I hope is a simple query but I couldn't find the answer I was looking for online or in prior forum posts. I'm using Stata 18.

    I want to generate a 2x2 table of the variable fin against arrest, but with the coding as it is, I will not have 1,1 in the top left cell as I would like.

    Is there an easy, temporary way to switch the code for tabulate without having to generate different variables or change the coding of the current variables?

    Thanks very much!


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(week arrest fin age race wexp mar paro prio)
    20 1 0 27 1 0 0 1  3
    17 1 0 18 1 0 0 1  8
    25 1 0 19 0 1 0 1 13
    52 0 1 23 1 1 1 1  1
    52 0 0 19 0 1 0 1  3
    52 0 0 24 1 1 0 0  2
    23 1 0 25 1 1 1 1  0
    52 0 1 21 1 1 0 1  4
    52 0 0 22 1 0 0 0  6
    52 0 0 20 1 1 0 0  0
    52 0 1 26 1 0 0 1  3
    52 0 0 40 1 1 0 0  2
    37 1 0 17 1 1 0 1  5
    52 0 0 37 1 1 0 0  2
    25 1 0 20 1 0 0 1  3
    46 1 1 22 1 1 0 1  2
    28 1 0 19 1 0 0 0  7
    52 0 0 20 1 0 0 0  2
    52 0 0 25 1 0 0 1 12
    52 0 0 24 0 1 0 1  1
    52 0 0 23 1 0 0 1  4
    52 0 1 44 1 1 1 1  0
    24 1 1 29 1 1 0 1  2
    52 0 1 28 0 1 0 1  1
    52 0 1 21 1 1 0 0  0
    52 0 1 19 1 1 0 1  2
    52 0 0 33 1 1 0 1  1
    52 0 0 19 1 0 0 0  2
    52 0 1 19 1 0 0 1  3
    52 0 1 23 1 1 1 1  9
    end
    label values arrest yes_no
    label values fin yes_no
    label values race yes_no
    label values wexp yes_no
    label values mar yes_no
    label values paro yes_no
    label def yes_no 0 "No", modify
    label def yes_no 1 "Yes", modify
    ------------------ copy up to and including the previous line ------------------

  • #2
    Thanks for the dataset.

    Instead of using tabulate, try table followed up with some collect commands to change order of the tabulated variables.
    Code:
    table fin arrest
    
    collect levels fin
    collect style autolevels fin 1 `s(levels)', clear
    collect levels arrest
    collect style autolevels arrest 1 `s(levels)', clear
    
    collect preview
    Here is the resulting table.
    Code:
    ---------------------------
            |       arrest
            |  Yes   No   Total
    --------+------------------
    fin     |
      Yes   |    2    9      11
      No    |    7   12      19
      Total |    9   21      30
    ---------------------------
    You can now use collect export to publish this table to any of the supported document types; such as, MS Word, MS Excel, LaTex, ...

    Comment


    • #3
      Thank you! As a follow-up question, following this approach, would I then be able to apply a chi2 test to the data?

      Comment


      • #4
        You could still use tabulate to perform/compute the Pearson chi-square test, then add its results as a note to your table.

        Here is what that would look like in code.
        Code:
        tab fin arrest, chi2
        collect notes 1: "Pearson chi2(1) = `: di %5.4f r(chi2)' Pr = `: di %5.2f r(p)'"
        collect preview
        Here is the result table, with the note.
        Code:
        ---------------------------
                |       arrest
                |  Yes   No   Total
        --------+------------------
        fin     |
          Yes   |    2    9      11
          No    |    7   12      19
          Total |    9   21      30
        ---------------------------
        Pearson chi2(1) = 1.1552 Pr = 0.28

        Comment


        • #5
          Thanks very much!

          Comment

          Working...
          X