Announcement

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

  • Wilcoxon rank sum test for more than 3 groups?

    So Stata is not letting me do a rank sum test for more than 3 groups with the command: ranksum score if outcome~=1, by(outcome). Does anyone know how I can use the ranksum command for more than 3 groups in Stata?

  • #2
    If you want an omnibus test, use kwallis; otherwise, use a pair of nested loops to do pairwise testing with ranksum.

    Comment


    • #3
      I've tried to look up how to do nested loops for ranksum and have no idea. Would anyone have any resources/suggestions?

      Comment


      • #4
        So I found this on old Stata forums: *ranksum rating if inlist(outcome, 1,2), by(outcome)* so I can technically do that but I have 9 outcomes and 9Cr2 gives me 36 so does that mean I need to do 36 of these combinations? There has to be an easier way?

        Comment


        • #5
          Run the following code. It shows how to set up the loops for cases both where your outcome variable is consecutively numbered and where it isn't. You can hard-code the values in the loop for the consecutively numbered case instead of using summarize to find them for you.
          Code:
          version 16.1
          
          clear *
          
          set seed `=strreverse("1550349")'
          
          quietly set obs 90
          generate byte outcome = mod(_n, 9) + 1
          generate double score = rnormal()
          
          *
          * Begin here
          *
          tempfile tmpfil0
          tempname file_handle
          postfile `file_handle' int row str100 cf double(z p) using `tmpfil0'
          
          // Set up the pair of nested loops (consecutive indexes)
          summarize outcome, meanonly
          local max `r(max)'
          forvalues i = `r(min)'/`=`max'-1' {
              forvalues j = `=`i'+1'/`max' {
                  local cf Outcome `i' versus `j'
                  quietly ranksum score if inlist(outcome, `i', `j'), by(outcome)
                  post `file_handle' (`i'`j') ("`cf'") (r(z)) (r(p))
              }
          }
          postclose `file_handle'
          preserve
          use `tmpfil0', clear
          
          count
          quietly replace p = min(p * r(N), 1)
          
          sort row
          format z p %04.2f
          
          list cf-p, noobs separator(0)
          
          // Set up the pair of nested loops (nonconsecutive indexes)
          restore
          quietly recode outcome (1=0)
          
          postfile `file_handle' int row str100 cf double(z p) using `tmpfil0', replace
          
          quietly levelsof outcome, local(outcomes)
          local outcome_tally : word count `outcomes'
          
          forvalues i = 1/`=`outcome_tally' - 1' {
              local left : word `i' of `outcomes'
              forvalues j = `=`i'+1'/`outcome_tally' {
                  local right : word `j' of `outcomes'
                  local cf Outcome `left' versus `right'
                  quietly ranksum score if inlist(outcome, `left', `right'), by(outcome)
                  post `file_handle' (`left'`right') ("`cf'") (r(z)) (r(p))
              }
          }
          postclose `file_handle'
          use `tmpfil0', clear
          
          count
          quietly replace p = min(p * r(N), 1)
          
          sort row
          format z p %04.2f
          
          list cf-p, noobs separator(0)
          
          exit
          The code above uses postfile to store the results in a dataset. You can skip that and simplify your code if all you want is the printout of the command's results.

          Comment


          • #6
            I wonder if this might also be helpful?

            https://www.stata-journal.com/articl...article=st0381

            Comment

            Working...
            X