Announcement

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

  • Code for performing Chi Squared test or Fisher's Exact test for a 2X2 table appropriately

    Hi everyone,

    I am working on a project where there is a need to develop a program (codes) to evaluate whether Chi Squared test or Fisher's Exact test is suitable for the tabulated variables (2x2 table).

    For example, there are to categorical variable A and B. Both are binary.

    I am thinking to first -tab a b, mis col expected- ; second if any expected value for any cell is <5, then do -tab a b, mis col exact-, otherwise do -tab a b, mis col chi-. Although having the thought in mind, I have a hard time to realize it in Stata code and could not find any relevant information on google.

    Please accept my sincere appreciation on any thoughts/insights you would like to share with me.

    Kindly regards,
    Mengmeng

  • #2
    It can be done in several ways. It depends on the way your data is coded. A general approach would be to assume a variable called event (1= event, 0 = non-event) and a variable called group (1= exposed/case/treatment,0 =non-exposed/control).



    Code:
    
    preserve
    gene  new_event = event
    collapse (count) new_event, by(event  group)
    list
    sum new_event
    local min = r(min)
    local k = r(N)
    restore
    
    if `min'<5|`k'<4 {
    tab event group, exact
    }
    else {
    tab event group, all
    }
    Even though I find Fisher's exact test one the most counterintuitive and strange test ever invented, I would suggest to use it regardless of your sample size.

    Comment


    • #3
      Originally posted by Tiago Pereira View Post
      It can be done in several ways. It depends on the way your data is coded. A general approach would be to assume a variable called event (1= event, 0 = non-event) and a variable called group (1= exposed/case/treatment,0 =non-exposed/control).



      Code:
      
      preserve
      gene new_event = event
      collapse (count) new_event, by(event group)
      list
      sum new_event
      local min = r(min)
      local k = r(N)
      restore
      
      if `min'<5|`k'<4 {
      tab event group, exact
      }
      else {
      tab event group, all
      }
      Even though I find Fisher's exact test one the most counterintuitive and strange test ever invented, I would suggest to use it regardless of your sample size.
      Hi Tiago,

      Thank you for sharing your ideas. I tried your codes on my dataset, which seemed working well. I want to also have a clear understanding with your codes. For example, `min' is the actual minimal number of "event = 1" cases among four eventXgroup types, but not the minimum of the expected values. Do you know how to capture the minimal expected value instead?

      Much appreciated for your help.

      Mengmeng

      Comment


      • #4
        Hi, Mengmeng

        After the program -collapse-, you can add lines to calculate a new variable with the expected number for each cell. Then, you summarize that new variable and continue from there.

        Tiago

        Comment

        Working...
        X