Announcement

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

  • Mann-Whitney U

    Hello,
    This is my first post on this forum, and my first question: I cannot find a command that would return a value of Mann-Whitney U (for example, after . ranksum). Am I missing something?
    Thanks a lot in advance for a feedback.
    Best greetings,
    Piotr

  • #2
    The -ranksum- command does this.

    Comment


    • #3
      Dear Clyde,
      Thank you for your prompt reaction but this is not what my question was about. Sorry, I should have been more precise. What I am asking about is the value of U. ranksum returns table, z, p, variance but not the U. All other statistical packages I know (Statistica, SPSS, MedCalc) do it, but I cannot find it in Stata. It can be of course calculated mentally or with paper-and-pencil for smaller groups, but for bigger it would be reasonable to have it done by computer, I think.
      Once again, thanks in advance for your kind comments,
      Best greetings,
      Piotr

      Comment


      • #4
        Something like this?

        .ÿversionÿ14.1

        .ÿ
        .ÿclearÿ*

        .ÿsetÿmoreÿoff

        .ÿinputÿbyteÿ(grpÿval)

        ÿÿÿÿÿÿÿÿÿÿgrpÿÿÿÿÿÿÿval
        ÿÿ1.ÿ0ÿ1
        ÿÿ2.ÿ0ÿ2
        ÿÿ3.ÿ0ÿ3
        ÿÿ4.ÿ0ÿ4
        ÿÿ5.ÿ0ÿ5
        ÿÿ6.ÿ0ÿ6
        ÿÿ7.ÿ0ÿ7
        ÿÿ8.ÿ1ÿ4
        ÿÿ9.ÿ1ÿ5
        ÿ10.ÿ1ÿ6
        ÿ11.ÿ1ÿ7
        ÿ12.ÿ1ÿ8
        ÿ13.ÿ1ÿ9
        ÿ14.ÿ1ÿ10
        ÿ15.ÿend

        .ÿ
        .ÿranksumÿval,ÿby(grp)

        Two-sampleÿWilcoxonÿrank-sumÿ(Mann-Whitney)ÿtest

        ÿÿÿÿÿÿÿÿÿgrpÿ|ÿÿÿÿÿÿobsÿÿÿÿrankÿsumÿÿÿÿexpected
        -------------+---------------------------------
        ÿÿÿÿÿÿÿÿÿÿÿ0ÿ|ÿÿÿÿÿÿÿÿ7ÿÿÿÿÿÿÿÿÿÿ36ÿÿÿÿÿÿÿÿ52.5
        ÿÿÿÿÿÿÿÿÿÿÿ1ÿ|ÿÿÿÿÿÿÿÿ7ÿÿÿÿÿÿÿÿÿÿ69ÿÿÿÿÿÿÿÿ52.5
        -------------+---------------------------------
        ÿÿÿÿcombinedÿ|ÿÿÿÿÿÿÿ14ÿÿÿÿÿÿÿÿÿ105ÿÿÿÿÿÿÿÿÿ105

        unadjustedÿvarianceÿÿÿÿÿÿÿ61.25
        adjustmentÿforÿtiesÿÿÿÿÿÿÿ-0.54
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ----------
        adjustedÿvarianceÿÿÿÿÿÿÿÿÿ60.71

        Ho:ÿval(grp==0)ÿ=ÿval(grp==1)
        ÿÿÿÿÿÿÿÿÿÿÿÿÿzÿ=ÿÿ-2.118
        ÿÿÿÿProbÿ>ÿ|z|ÿ=ÿÿÿ0.0342

        .ÿ
        .ÿscalarÿdefineÿU1ÿ=ÿr(sum_obs)ÿ-ÿr(N_1)ÿ*ÿ(r(N_1)ÿ+ÿ1)ÿ/ÿ2

        .ÿscalarÿdefineÿU2ÿ=ÿr(N_1)ÿ*ÿr(N_2)ÿ-ÿU1

        .ÿdisplayÿinÿsmclÿasÿtextÿ"Mann-WhitneyÿUÿ=ÿ"ÿasÿresultÿmin(U1,ÿU2)
        Mann-WhitneyÿUÿ=ÿ8

        .ÿ
        .ÿexit

        endÿofÿdo-file


        .

        Comment


        • #5
          Piotr:
          my last experience with -ranksum- dates back to many years ago.
          I can't say if what follows is right: I've tried to follow the formulae calculi reported in the -ranksum- entry of Stata .pdf manual:
          Code:
          . ranksum mpg, by(treat)
          
          Two-sample Wilcoxon rank-sum (Mann-Whitney) test
          
                 treat |      obs    rank sum    expected
          -------------+---------------------------------
             untreated |       12         128         150
               treated |       12         172         150
          -------------+---------------------------------
              combined |       24         300         300
          
          unadjusted variance      300.00
          adjustment for ties       -4.04
                               ----------
          adjusted variance        295.96
          
          Ho: mpg(treat==untreated) = mpg(treat==treated)
                       z =  -1.279
              Prob > |z| =   0.2010
          
          . return list
          
          scalars:
                          r(N_2) =  12
                          r(N_1) =  12
                        r(Var_a) =  295.9565217391304
                            r(z) =  -1.278817949868369
                      r(sum_exp) =  150
                      r(sum_obs) =  128
                       r(group1) =  0
          
          
          
          
          . di r(sum_obs)-((r(N_1)*(r(N_1)+1))*.5)
          50
          Kind regards,
          Carlo
          (Stata 18.0 SE)

          Comment


          • #6
            Dear Carlo,
            Thank you very much for your support. I used a similar approach, but the problem with it is that the formula r(sum_obs)-((r(N_1)*(r(N_1)+1))*.5) does not take into consideration, which U is smaller and which is bigger (U1 or U2), so if you might display "wrong" U

            Dear Joseph,
            Your commands seem to do the job; many best thanks! Actually, this is strange that it is not built in as default.

            Best greetings,
            Piotr

            Comment


            • #7
              Piotr:
              Joseph's advice outperforms mine.
              Glad that you have found the solution to your problem.
              Kind regards,
              Carlo
              (Stata 18.0 SE)

              Comment


              • #8
                I found this thread while trying to work out how to compute Cliff's delta. One equation for delta uses the value of U:
                Cliff's delta = 2U/(n1n2) - 1
                In the Methods and formulas section of the help for -ranksum-, I found that p, the probability value returned by the porder option, is equal to U/(n1n2).

                Putting it all that together suggested this variation on Joseph's code in #4.

                Code:
                clear *
                input byte (grp val)
                0 1
                0 2
                0 3
                0 4
                0 5
                0 6
                0 7
                1 4
                1 5
                1 6
                1 7
                1 8
                1 9
                1 10
                end
                
                ranksum val, by(grp) porder
                
                quietly {
                local n1n2 = r(N_1)*r(N_2)
                local p1 = r(porder)
                local p2 = 1-`p1'
                local U1 = `p1'*`n1n2'
                local U2 = `p2'*`n1n2'
                local U = min(`U1',`U2')
                local cliffsd = 2*`U'/`n1n2'-1
                }
                
                display _newline ///
                as text "Mann-Whitney U = " as result `U' _newline ///
                as text "Cliff's delta = " as result `cliffsd'
                Selected output:

                Code:
                . ranksum val, by(grp) porder
                
                Two-sample Wilcoxon rank-sum (Mann-Whitney) test
                
                         grp |      obs    rank sum    expected
                -------------+---------------------------------
                           0 |        7          36        52.5
                           1 |        7          69        52.5
                -------------+---------------------------------
                    combined |       14         105         105
                
                unadjusted variance       61.25
                adjustment for ties       -0.54
                                     ----------
                adjusted variance         60.71
                
                Ho: val(grp==0) = val(grp==1)
                             z =  -2.118
                    Prob > |z| =   0.0342
                
                P{val(grp==0) > val(grp==1)} = 0.163
                
                .
                . quietly {
                
                .
                . display _newline ///
                > as text "Mann-Whitney U = " as result `U' _newline ///
                > as text "Cliff's delta = " as result `cliffsd'
                
                Mann-Whitney U = 8
                Cliff's delta = -.67346939
                Finally, I completely agree with Piotr that -ranksum- ought to report the value of U!


                --
                Bruce Weaver
                Email: [email protected]
                Web: http://sites.google.com/a/lakeheadu.ca/bweaver/
                Version: Stata/MP 18.0 (Windows)

                Comment


                • #9
                  Here's a shorter version of the code in #8.

                  Code:
                  ranksum val, by(grp) porder
                  
                  quietly {
                  local n1n2 = r(N_1)*r(N_2)
                  local p1 = r(porder)
                  local p2 = 1-`p1'
                  local U = min(`p1',`p2')*`n1n2'
                  local cliffsd = 2*min(`p1',`p2')-1
                  }
                  
                  display _newline ///
                  as text "Mann-Whitney U = " as result `U' _newline ///
                  as text "Cliff's delta = " as result `cliffsd'

                  Output:

                  Code:
                  . display _newline ///
                  > as text "Mann-Whitney U = " as result `U' _newline ///
                  > as text "Cliff's delta = " as result `cliffsd'
                  
                  Mann-Whitney U = 8
                  Cliff's delta = -.67346939
                  --
                  Bruce Weaver
                  Email: [email protected]
                  Web: http://sites.google.com/a/lakeheadu.ca/bweaver/
                  Version: Stata/MP 18.0 (Windows)

                  Comment

                  Working...
                  X