Announcement

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

  • Generate mean and median test results in Stata

    Dear Stata Users:
    I am trying to generate mean and median test results in Stata. Thanks to Kain Chen
    HTML Code:
     http://kaichen.work/?p=1218
    I have a sample code. However it is not working in stata 14.
    Could you please help.

    Code:
    local vars  var1 var2 var3 var4
    local group class
    
    foreach v in `vars' {
      di "`v'"
      ttest `v', by(`group')
      local mean_`v'_mean_0=round(r(mu_1),.001)
      local mean_`v'_mean_1=round(r(mu_2),.001)
      local mean_`v'_diff=`mean_`v'_mean_1'-`mean_`v'_mean_0'
      local mean_`v'_p=r(p)
    }
     
    foreach v in `vars' {
      sum `v' if `group'==0, detail
      local p50_`v'_p50_0=round(r(p50),.001)
      sum `v' if `group'==1, detail
      local p50_`v'_p50_1=round(r(p50),.001)
      ranksum `v', by(`group')
      local p50_`v'_n_0=r(N_1)
      local p50_`v'_n_1=r(N_2)
      local p50_`v'_diff=`p50_`v'_p50_1'-`p50_`v'_p50_0'
      local p50_`v'_p=2*normprob(-abs(r(z)))
    }
     
    qui {
      noi di _newline
      noi di "{hline 115}"
      noi di _col(15) "{c |} `group' = 1" ///
             _col(45) "{c |} `group' = 0" ///
             _col(75) "{c |} Diff"
      noi di _col(16) "{hline 100}"
      noi di _col(15) "{c |} Mean" ///
             _col(25) "{c |} Median" ///
             _col(35) "{c |} N" ///
             _col(45) "{c |} Mean" ///
             _col(55) "{c |} Median" ///
             _col(65) "{c |} N" ///
             _col(75) "{c |} Mean" ///
             _col(85) "{c |} P" ///
             _col(95) "{c |} Median" ///
             _col(105) "{c |} P"
      noi di "{hline 115}"
      foreach v in `vars' {
        noi di %12s abbrev("`v'",12) ///
               _col(15) "{c |}" %8.3f `mean_`v'_mean_1' ///
               _col(25) "{c |}" %8.3f `p50_`v'_p50_1' ///
               _col(35) "{c |}" %8.0f `p50_`v'_n_1' ///
               _col(45) "{c |}" %8.3f `mean_`v'_mean_0' ///
               _col(55) "{c |}" %8.3f `p50_`v'_p50_0' ///
               _col(65) "{c |}" %8.0f `p50_`v'_n_0' ///
               _col(75) "{c |}" %8.3f `mean_`v'_diff' ///
               _col(85) "{c |}" %8.3f `mean_`v'_p' ///
               _col(95) "{c |}" %8.3f `p50_`v'_diff' ///
               _col(105) "{c |}" %8.3f `p50_`v'_p' 
      }
      noi di "{hline 115}"
    }
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(var1 var2 var3 var4 class)
    2.1709347229330476  .02042863528691251     .03228491681068315  6.344270516206654 1
     5.487542064826069  .12843769095325996     .04757251167276653  6.154184831332533 1
     2.403442868218309  .14004722153361354    .056244318978395644  6.982910060182892 1
    1.1505027133742167  .11100197329982031    .004023767790014441  6.811365516638235 1
    1.8804746970472777   .5373991437510292    .007095957516877986  9.874810261649259 1
    1.9808765550239236   .3227327984439582    .021833211767566252  9.931394505789136 1
     2.676107983414312   .2086538813493268     .07895762414439786  6.142981046239942 1
    5.0250382942155865   .5440525877207101    .025750244090855447  5.636962241280868 0
     4.258091546463471 .012181876701455542     .04470620519153116  6.660379227959387 1
     2.197711032881432                   0     .03437963510075126 4.4299473733834605 0
    2.2245278845356267   .4394672805861094    .009428517082833212  5.723343242641416 1
    1.1452441380506346 .005852152919964551   .0037893836139718242 11.088995933771097 1
     .9484033697297157   .3734964247700097    .002009329861113992  7.095376932085215 1
    1.1523751483206472   .2830885583049549    .014212063384959249  5.654406639014709 1
    1.7533311612460127  .19266922499285918     .06038927853563682   7.63872652475961 1
     .8706634694165418  .27225441581137516    .013483301895410065 7.3137409182571105 1
     1.079994695584802  .24342634444177425     .00556080453072516  8.201428740938756 1
     .9901501157807883    .200563603986151    .008008362894517725  6.203767551444203 1
    1.3157547495682214 .013003901170351105     .11967674566421503   8.84246002419529 1
    1.6125047223917022  .27092330708216894  .00022581646766590453 10.475257746929474 1
       1.6695885509839   .7825286415711947  -.0016366612111292963  8.494743062578646 0
     .8480628518530803   .3766737329713641  -.0002713747199372005  7.579527636994385 1
     1.060821685855263   .2603442745643109   -.002459104030792259  9.143559338285831 1
    1.6878696906442758  .11737663668433701   .0026116483332574844  7.910486424877584 1
     3.968033586295519  .09784727102453503     .04043281225865062    7.8916183004611 1
      .899243711630933  .12953298510293923    .002730703847420142  8.836453142906317 1
     1.876879945068383   .2996552112194602    .006820006046659643   8.30061876606964 0
     .9541869853059908   .3654661990765307    .017404474266007573   7.69546963611513 1
    1.9644481201108408   .4032161241201265    .005893921591571983 11.974259250751283 0
    2.5286603845167903   .3319836814821631    .012760344774700163 10.392128915166236 1
    2.5399946760599135  .28070659154350697    .014606202798280617  6.371492181952594 0
    1.9045555011585498   .2964226277064348     .03337520020575096  8.209402284791736 1
     5.716134885512472   .5292907790381521    .008094833449250688   8.67395024313201 0
    1.3615053404450799  .14013686402856293      .0557818383280838 11.487299917331256 1
    2.4918920664014497                   0    .022622035593839667 5.8137662863912745 1
     4.115151363312894  .41287287656029065   .0038089010156910734  7.650706871093464 0
     2.355544245111873  .18922108333108298   .0066966541804420205   7.86064405705401 0
    1.1459177647714638   .3806578806414909    .011546484840845521  7.993001640282866 1
    1.4456867284194304   .2235793293343836      .0307410848329395  6.388823556055131 1
     2.433206439905795  .05182226940660904    .005467731741770416  8.736676452032238 1
     .6718319519007932  .24684993964632423  -.0034781785701610136   8.62690498651182 0
     1.610265383568393 .003758584550491892   .0015988813762135522  8.218744836646748 0
    1.5904666612063105  .08420437504195837    .004750224395124635  8.339667396550764 1
     .4844194771165337  .19147015156146804    .008096030121903125  8.126672457793893 1
    1.1255137911908935 .013977875086425444   -.022882576630560036  5.852547247921313 0
    1.0980541357873876  .23833167825223436   .0021414722790804503  9.602645938552133 1
     1.459342862276669   .2960699771294345    .026362620968550576  8.654420620275989 1
    3.7872993641364756  .16344200892632452     .03434962682508268  6.424757186209322 1
    1.3330632839020906  .14922273950851017    .028375058135984177 5.3653356230988205 1
    1.7050250470489474  .09482475476419729    .044931304649582844  8.317028590089064 1
    1.4095675648788928  .45225421663566984    .012633874174308985  9.654641503869545 1
     .9817325091888597  .19951911856495427     .01903986539661863  6.826789228492126 0
     4.749956883277682 .004365141127600385     .03350505532830546   8.57455905040462 1
    2.6315519937451133  .24769642069198225     .04563033716614323  8.256789004740584 1
    1.2951633934280147  .40049463644969263    .008107996872041815   6.28326437993576 1
    1.6885666182075358  .12606567517413872    .007133636339187263  6.390119854398666 1
    2.0852919852933036  .22075020161884867     .03615227896612162   8.04582799565937 1
     .6189007671401604  .25361495023248226    .010725521502892112  7.872453625672574 0
    1.5426325594770125   .2503213115847204    .038414029355626496  6.398403568472147 1
     5.431799918831169  .48254364089775564     .03834164588528678  6.465522446681758 1
     .6169221507832088  .09853527536057495  -.0068559833290489135  5.904451351934881 1
    1.3892754575052342 .054018197650424475     .02138990983282448  7.747180085196543 1
     3.208024624600127   .2938049183043822      -.018247490676978 7.8478625324739415 1
    2.0495010093948625  .07491891129600903    .028160696657735153 5.4289595895385325 1
     1.914610189433304  .12734084304731733     .03212300106711626  7.359841852124484 0
     .4680346209512774  .07488053707720517   .0033831925393684814  8.738226678109067 1
    1.5331495506412196   .2399151993404393     .02296684529768565  9.739909238559308 1
    2.4155976127480647   .1974669905582754     .04859411600264234   8.57590766064048 1
    1.3535190448409071   .5582551101016601   -.005989655296997654  9.011132073356997 1
    1.8093710795578795   .3263061983574228     .02702264160363464  6.177338121345474 1
     2.413543812058275  .13404023535930262    .007393056878498134  9.294058900151573 1
    1.7598221676427495  .35767105564133356    .011749799851318124    6.3289790974743 1
    1.9380582068072627  .24758035817844862     .03609937184292087  7.932295030766125 0
    2.0306460746550394  .24806585552336896    .021115980979310157 6.4785541880673865 1
    1.8385283854866579  .19986105409416743    .020652414185113842 10.363061986291324 1
    1.9736462512097805  .06855061601161756    .024047875579706748  6.750961108209461 0
    1.9769853417901335  .33724378397180416    .017118646198720125  7.777863842906347 1
    1.8545938219946896  .30055710669517777    .018918646200314113  7.636383979077606 1
     2.886666087988723  .15277429315835184     .03533725755090225 10.855454260862754 1
    1.3807206988594998    .616143277023171    .003723889966898756  5.639606554437637 1
    2.2361776526107477  .26794909161566116    .012921402895171477  7.630267068171986 1
      9.28029906542056  .27524229074889867     .04070484581497797  8.644002038279933 1
     .8217743575613246  .30930619871638637    .007741141789399162  5.845432488574447 1
    1.3653890875928012  .22438623490829931     .01337300005543486   7.03656089770649 0
     2.800319755600815   .3067567966466924 -.00008855565723056941 10.430521115146327 1
     3.927241192562631                   0     .05982923895641445  8.717433576126178 1
     .5205014326016568  .17731842326756325   .0007743689109328184    8.6229545667191 0
     .7988620988725066  .18910471323051906    .002006304018415759  5.249142878535657 0
    1.1528772598589017  .27211150188875866    .013282133925851286  8.443672585190909 1
    1.0824540711537936  .31280606062952193     .01893637035804013  7.857182870229327 1
    1.1188457318289626   .2452208335452753    .010463449736842892 6.5064146534896405 1
     4.414392777315159  .30744329614941296    .009213276808564675 6.2400224017506565 1
     1.863419219993175   .2169520322068658    .018969571690629208  8.835908851864653 1
    1.2937478285552086  .05945033965279936   .0030191359943169205 11.814059157345177 1
    1.2836064650560342  .19992189011901249    .003265378320655072  7.041395040734514 0
     .9734652518048188  .41056808441462483    .011505039458516832  4.915093087375768 1
    2.3414603025370666  .04547157992309307    .007938720680141068  8.953379403377928 0
     5.007207397093378                   .     .04567133374239973  9.570989408736887 1
    1.2348910387473195   .6933934494285644    .015182351673371145  6.540061276160971 1
     13.08921134631084   .3304803372220662     .03931358971740739  7.134521062086644 1
    end

  • #2
    You don't say what "not working" means exactly. See FAQ Advice #12 on how you should show an error message from Stata or otherwise explain why your results are surprising or disappointing to you.

    Meanwhile, never do this:

    Code:
    local mean_`v'_mean_0=round(r(mu_1),.001)  
    local mean_`v'_mean_1=round(r(mu_2),.001)  
    local mean_`v'_diff=`mean_`v'_mean_1'-`mean_`v'_mean_0'
    That is poor practice.

    1. Never round before calculating a difference.

    2. If you want a formatted result, use a display format, not round() with a second argument less than 1. Stata works in binary and the results of rounding to the nearest 0.001 will sometimes bite you. Most multiples of 0.001 cannot be held exactly in binary.

    Here is better practice:

    Code:
    local mean_`v'_diff1 = r(mu_1) - r(mu_2)
    local mean_`v'_diff2 : display %4.3f `mean_`v'_diff1'
    Last edited by Nick Cox; 29 Sep 2019, 03:41.

    Comment


    • #3
      In addition to Nick's good advice, I think that -ranksum- doesn't really give you "median test results". You might want to take another look at the article whose URL you provide in your other thread.

      Comment


      • #4
        Hi Nick I get an empty output like this.
        Click image for larger version

Name:	Capture.GIF
Views:	1
Size:	4.2 KB
ID:	1518373

        Comment


        • #5
          This works for me. When I look at all the code, I see that the formatting is not needed, as you apply display formats at the end.


          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input double(var1 var2 var3 var4 class)
          2.1709347229330476  .02042863528691251     .03228491681068315  6.344270516206654 1
           5.487542064826069  .12843769095325996     .04757251167276653  6.154184831332533 1
           2.403442868218309  .14004722153361354    .056244318978395644  6.982910060182892 1
          1.1505027133742167  .11100197329982031    .004023767790014441  6.811365516638235 1
          1.8804746970472777   .5373991437510292    .007095957516877986  9.874810261649259 1
          1.9808765550239236   .3227327984439582    .021833211767566252  9.931394505789136 1
           2.676107983414312   .2086538813493268     .07895762414439786  6.142981046239942 1
          5.0250382942155865   .5440525877207101    .025750244090855447  5.636962241280868 0
           4.258091546463471 .012181876701455542     .04470620519153116  6.660379227959387 1
           2.197711032881432                   0     .03437963510075126 4.4299473733834605 0
          2.2245278845356267   .4394672805861094    .009428517082833212  5.723343242641416 1
          1.1452441380506346 .005852152919964551   .0037893836139718242 11.088995933771097 1
           .9484033697297157   .3734964247700097    .002009329861113992  7.095376932085215 1
          1.1523751483206472   .2830885583049549    .014212063384959249  5.654406639014709 1
          1.7533311612460127  .19266922499285918     .06038927853563682   7.63872652475961 1
           .8706634694165418  .27225441581137516    .013483301895410065 7.3137409182571105 1
           1.079994695584802  .24342634444177425     .00556080453072516  8.201428740938756 1
           .9901501157807883    .200563603986151    .008008362894517725  6.203767551444203 1
          1.3157547495682214 .013003901170351105     .11967674566421503   8.84246002419529 1
          1.6125047223917022  .27092330708216894  .00022581646766590453 10.475257746929474 1
             1.6695885509839   .7825286415711947  -.0016366612111292963  8.494743062578646 0
           .8480628518530803   .3766737329713641  -.0002713747199372005  7.579527636994385 1
           1.060821685855263   .2603442745643109   -.002459104030792259  9.143559338285831 1
          1.6878696906442758  .11737663668433701   .0026116483332574844  7.910486424877584 1
           3.968033586295519  .09784727102453503     .04043281225865062    7.8916183004611 1
            .899243711630933  .12953298510293923    .002730703847420142  8.836453142906317 1
           1.876879945068383   .2996552112194602    .006820006046659643   8.30061876606964 0
           .9541869853059908   .3654661990765307    .017404474266007573   7.69546963611513 1
          1.9644481201108408   .4032161241201265    .005893921591571983 11.974259250751283 0
          2.5286603845167903   .3319836814821631    .012760344774700163 10.392128915166236 1
          2.5399946760599135  .28070659154350697    .014606202798280617  6.371492181952594 0
          1.9045555011585498   .2964226277064348     .03337520020575096  8.209402284791736 1
           5.716134885512472   .5292907790381521    .008094833449250688   8.67395024313201 0
          1.3615053404450799  .14013686402856293      .0557818383280838 11.487299917331256 1
          2.4918920664014497                   0    .022622035593839667 5.8137662863912745 1
           4.115151363312894  .41287287656029065   .0038089010156910734  7.650706871093464 0
           2.355544245111873  .18922108333108298   .0066966541804420205   7.86064405705401 0
          1.1459177647714638   .3806578806414909    .011546484840845521  7.993001640282866 1
          1.4456867284194304   .2235793293343836      .0307410848329395  6.388823556055131 1
           2.433206439905795  .05182226940660904    .005467731741770416  8.736676452032238 1
           .6718319519007932  .24684993964632423  -.0034781785701610136   8.62690498651182 0
           1.610265383568393 .003758584550491892   .0015988813762135522  8.218744836646748 0
          1.5904666612063105  .08420437504195837    .004750224395124635  8.339667396550764 1
           .4844194771165337  .19147015156146804    .008096030121903125  8.126672457793893 1
          1.1255137911908935 .013977875086425444   -.022882576630560036  5.852547247921313 0
          1.0980541357873876  .23833167825223436   .0021414722790804503  9.602645938552133 1
           1.459342862276669   .2960699771294345    .026362620968550576  8.654420620275989 1
          3.7872993641364756  .16344200892632452     .03434962682508268  6.424757186209322 1
          1.3330632839020906  .14922273950851017    .028375058135984177 5.3653356230988205 1
          1.7050250470489474  .09482475476419729    .044931304649582844  8.317028590089064 1
          1.4095675648788928  .45225421663566984    .012633874174308985  9.654641503869545 1
           .9817325091888597  .19951911856495427     .01903986539661863  6.826789228492126 0
           4.749956883277682 .004365141127600385     .03350505532830546   8.57455905040462 1
          2.6315519937451133  .24769642069198225     .04563033716614323  8.256789004740584 1
          1.2951633934280147  .40049463644969263    .008107996872041815   6.28326437993576 1
          1.6885666182075358  .12606567517413872    .007133636339187263  6.390119854398666 1
          2.0852919852933036  .22075020161884867     .03615227896612162   8.04582799565937 1
           .6189007671401604  .25361495023248226    .010725521502892112  7.872453625672574 0
          1.5426325594770125   .2503213115847204    .038414029355626496  6.398403568472147 1
           5.431799918831169  .48254364089775564     .03834164588528678  6.465522446681758 1
           .6169221507832088  .09853527536057495  -.0068559833290489135  5.904451351934881 1
          1.3892754575052342 .054018197650424475     .02138990983282448  7.747180085196543 1
           3.208024624600127   .2938049183043822      -.018247490676978 7.8478625324739415 1
          2.0495010093948625  .07491891129600903    .028160696657735153 5.4289595895385325 1
           1.914610189433304  .12734084304731733     .03212300106711626  7.359841852124484 0
           .4680346209512774  .07488053707720517   .0033831925393684814  8.738226678109067 1
          1.5331495506412196   .2399151993404393     .02296684529768565  9.739909238559308 1
          2.4155976127480647   .1974669905582754     .04859411600264234   8.57590766064048 1
          1.3535190448409071   .5582551101016601   -.005989655296997654  9.011132073356997 1
          1.8093710795578795   .3263061983574228     .02702264160363464  6.177338121345474 1
           2.413543812058275  .13404023535930262    .007393056878498134  9.294058900151573 1
          1.7598221676427495  .35767105564133356    .011749799851318124    6.3289790974743 1
          1.9380582068072627  .24758035817844862     .03609937184292087  7.932295030766125 0
          2.0306460746550394  .24806585552336896    .021115980979310157 6.4785541880673865 1
          1.8385283854866579  .19986105409416743    .020652414185113842 10.363061986291324 1
          1.9736462512097805  .06855061601161756    .024047875579706748  6.750961108209461 0
          1.9769853417901335  .33724378397180416    .017118646198720125  7.777863842906347 1
          1.8545938219946896  .30055710669517777    .018918646200314113  7.636383979077606 1
           2.886666087988723  .15277429315835184     .03533725755090225 10.855454260862754 1
          1.3807206988594998    .616143277023171    .003723889966898756  5.639606554437637 1
          2.2361776526107477  .26794909161566116    .012921402895171477  7.630267068171986 1
            9.28029906542056  .27524229074889867     .04070484581497797  8.644002038279933 1
           .8217743575613246  .30930619871638637    .007741141789399162  5.845432488574447 1
          1.3653890875928012  .22438623490829931     .01337300005543486   7.03656089770649 0
           2.800319755600815   .3067567966466924 -.00008855565723056941 10.430521115146327 1
           3.927241192562631                   0     .05982923895641445  8.717433576126178 1
           .5205014326016568  .17731842326756325   .0007743689109328184    8.6229545667191 0
           .7988620988725066  .18910471323051906    .002006304018415759  5.249142878535657 0
          1.1528772598589017  .27211150188875866    .013282133925851286  8.443672585190909 1
          1.0824540711537936  .31280606062952193     .01893637035804013  7.857182870229327 1
          1.1188457318289626   .2452208335452753    .010463449736842892 6.5064146534896405 1
           4.414392777315159  .30744329614941296    .009213276808564675 6.2400224017506565 1
           1.863419219993175   .2169520322068658    .018969571690629208  8.835908851864653 1
          1.2937478285552086  .05945033965279936   .0030191359943169205 11.814059157345177 1
          1.2836064650560342  .19992189011901249    .003265378320655072  7.041395040734514 0
           .9734652518048188  .41056808441462483    .011505039458516832  4.915093087375768 1
          2.3414603025370666  .04547157992309307    .007938720680141068  8.953379403377928 0
           5.007207397093378                   .     .04567133374239973  9.570989408736887 1
          1.2348910387473195   .6933934494285644    .015182351673371145  6.540061276160971 1
           13.08921134631084   .3304803372220662     .03931358971740739  7.134521062086644 1
          end 
           
           
          local vars  var1 var2 var3 var4
          local group class
          
          foreach v in `vars' {
            di "`v'"
            ttest `v', by(`group')
            local mean_`v'_mean_0 = r(mu_1)
            local mean_`v'_mean_1 = r(mu_2)
            local mean_`v'_diff = r(mu_1) - r(mu_2) 
            local mean_`v'_p=r(p)
           
            sum `v' if `group'==0, detail
            local p50_`v'_p50_0 = r(p50) 
            sum `v' if `group'==1, detail
            local p50_`v'_p50_1 = r(p50) 
            
            ranksum `v', by(`group')
            local p50_`v'_n_0=r(N_1)
            local p50_`v'_n_1=r(N_2)
            local p50_`v'_diff=`p50_`v'_p50_1'-`p50_`v'_p50_0'
            local p50_`v'_p=2*normprob(-abs(r(z)))
          }
           
          di _newline
          di "{hline 115}"
          di _col(15) "{c |} `group' = 1" ///
             _col(45) "{c |} `group' = 0" ///
             _col(75) "{c |} Diff"
          di _col(16) "{hline 100}"
          di _col(15) "{c |} Mean" ///
             _col(25) "{c |} Median" ///
             _col(35) "{c |} N" ///
             _col(45) "{c |} Mean" ///
             _col(55) "{c |} Median" ///
             _col(65) "{c |} N" ///
             _col(75) "{c |} Mean" ///
             _col(85) "{c |} P" ///
             _col(95) "{c |} Median" ///
             _col(105) "{c |} P"
          di "{hline 115}"
          
          foreach v in `vars' {
              di %12s abbrev("`v'",12) ///
                     _col(15) "{c |}" %8.3f `mean_`v'_mean_1' ///
                     _col(25) "{c |}" %8.3f `p50_`v'_p50_1' ///
                     _col(35) "{c |}" %8.0f `p50_`v'_n_1' ///
                     _col(45) "{c |}" %8.3f `mean_`v'_mean_0' ///
                     _col(55) "{c |}" %8.3f `p50_`v'_p50_0' ///
                     _col(65) "{c |}" %8.0f `p50_`v'_n_0' ///
                     _col(75) "{c |}" %8.3f `mean_`v'_diff' ///
                     _col(85) "{c |}" %8.3f `mean_`v'_p' ///
                     _col(95) "{c |}" %8.3f `p50_`v'_diff' ///
                     _col(105) "{c |}" %8.3f `p50_`v'_p' 
            }
            
          di "{hline 115}"

          Comment


          • #6
            Thanks Nick, how can I export this table into Excel

            Comment


            • #7
              That is not something I ever want to do myself.

              Comment


              • #8
                Originally posted by Nick Cox View Post
                This works for me. When I look at all the code, I see that the formatting is not needed, as you apply display formats at the end.


                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input double(var1 var2 var3 var4 class)
                2.1709347229330476 .02042863528691251 .03228491681068315 6.344270516206654 1
                5.487542064826069 .12843769095325996 .04757251167276653 6.154184831332533 1
                2.403442868218309 .14004722153361354 .056244318978395644 6.982910060182892 1
                1.1505027133742167 .11100197329982031 .004023767790014441 6.811365516638235 1
                1.8804746970472777 .5373991437510292 .007095957516877986 9.874810261649259 1
                1.9808765550239236 .3227327984439582 .021833211767566252 9.931394505789136 1
                2.676107983414312 .2086538813493268 .07895762414439786 6.142981046239942 1
                5.0250382942155865 .5440525877207101 .025750244090855447 5.636962241280868 0
                4.258091546463471 .012181876701455542 .04470620519153116 6.660379227959387 1
                2.197711032881432 0 .03437963510075126 4.4299473733834605 0
                2.2245278845356267 .4394672805861094 .009428517082833212 5.723343242641416 1
                1.1452441380506346 .005852152919964551 .0037893836139718242 11.088995933771097 1
                .9484033697297157 .3734964247700097 .002009329861113992 7.095376932085215 1
                1.1523751483206472 .2830885583049549 .014212063384959249 5.654406639014709 1
                1.7533311612460127 .19266922499285918 .06038927853563682 7.63872652475961 1
                .8706634694165418 .27225441581137516 .013483301895410065 7.3137409182571105 1
                1.079994695584802 .24342634444177425 .00556080453072516 8.201428740938756 1
                .9901501157807883 .200563603986151 .008008362894517725 6.203767551444203 1
                1.3157547495682214 .013003901170351105 .11967674566421503 8.84246002419529 1
                1.6125047223917022 .27092330708216894 .00022581646766590453 10.475257746929474 1
                1.6695885509839 .7825286415711947 -.0016366612111292963 8.494743062578646 0
                .8480628518530803 .3766737329713641 -.0002713747199372005 7.579527636994385 1
                1.060821685855263 .2603442745643109 -.002459104030792259 9.143559338285831 1
                1.6878696906442758 .11737663668433701 .0026116483332574844 7.910486424877584 1
                3.968033586295519 .09784727102453503 .04043281225865062 7.8916183004611 1
                .899243711630933 .12953298510293923 .002730703847420142 8.836453142906317 1
                1.876879945068383 .2996552112194602 .006820006046659643 8.30061876606964 0
                .9541869853059908 .3654661990765307 .017404474266007573 7.69546963611513 1
                1.9644481201108408 .4032161241201265 .005893921591571983 11.974259250751283 0
                2.5286603845167903 .3319836814821631 .012760344774700163 10.392128915166236 1
                2.5399946760599135 .28070659154350697 .014606202798280617 6.371492181952594 0
                1.9045555011585498 .2964226277064348 .03337520020575096 8.209402284791736 1
                5.716134885512472 .5292907790381521 .008094833449250688 8.67395024313201 0
                1.3615053404450799 .14013686402856293 .0557818383280838 11.487299917331256 1
                2.4918920664014497 0 .022622035593839667 5.8137662863912745 1
                4.115151363312894 .41287287656029065 .0038089010156910734 7.650706871093464 0
                2.355544245111873 .18922108333108298 .0066966541804420205 7.86064405705401 0
                1.1459177647714638 .3806578806414909 .011546484840845521 7.993001640282866 1
                1.4456867284194304 .2235793293343836 .0307410848329395 6.388823556055131 1
                2.433206439905795 .05182226940660904 .005467731741770416 8.736676452032238 1
                .6718319519007932 .24684993964632423 -.0034781785701610136 8.62690498651182 0
                1.610265383568393 .003758584550491892 .0015988813762135522 8.218744836646748 0
                1.5904666612063105 .08420437504195837 .004750224395124635 8.339667396550764 1
                .4844194771165337 .19147015156146804 .008096030121903125 8.126672457793893 1
                1.1255137911908935 .013977875086425444 -.022882576630560036 5.852547247921313 0
                1.0980541357873876 .23833167825223436 .0021414722790804503 9.602645938552133 1
                1.459342862276669 .2960699771294345 .026362620968550576 8.654420620275989 1
                3.7872993641364756 .16344200892632452 .03434962682508268 6.424757186209322 1
                1.3330632839020906 .14922273950851017 .028375058135984177 5.3653356230988205 1
                1.7050250470489474 .09482475476419729 .044931304649582844 8.317028590089064 1
                1.4095675648788928 .45225421663566984 .012633874174308985 9.654641503869545 1
                .9817325091888597 .19951911856495427 .01903986539661863 6.826789228492126 0
                4.749956883277682 .004365141127600385 .03350505532830546 8.57455905040462 1
                2.6315519937451133 .24769642069198225 .04563033716614323 8.256789004740584 1
                1.2951633934280147 .40049463644969263 .008107996872041815 6.28326437993576 1
                1.6885666182075358 .12606567517413872 .007133636339187263 6.390119854398666 1
                2.0852919852933036 .22075020161884867 .03615227896612162 8.04582799565937 1
                .6189007671401604 .25361495023248226 .010725521502892112 7.872453625672574 0
                1.5426325594770125 .2503213115847204 .038414029355626496 6.398403568472147 1
                5.431799918831169 .48254364089775564 .03834164588528678 6.465522446681758 1
                .6169221507832088 .09853527536057495 -.0068559833290489135 5.904451351934881 1
                1.3892754575052342 .054018197650424475 .02138990983282448 7.747180085196543 1
                3.208024624600127 .2938049183043822 -.018247490676978 7.8478625324739415 1
                2.0495010093948625 .07491891129600903 .028160696657735153 5.4289595895385325 1
                1.914610189433304 .12734084304731733 .03212300106711626 7.359841852124484 0
                .4680346209512774 .07488053707720517 .0033831925393684814 8.738226678109067 1
                1.5331495506412196 .2399151993404393 .02296684529768565 9.739909238559308 1
                2.4155976127480647 .1974669905582754 .04859411600264234 8.57590766064048 1
                1.3535190448409071 .5582551101016601 -.005989655296997654 9.011132073356997 1
                1.8093710795578795 .3263061983574228 .02702264160363464 6.177338121345474 1
                2.413543812058275 .13404023535930262 .007393056878498134 9.294058900151573 1
                1.7598221676427495 .35767105564133356 .011749799851318124 6.3289790974743 1
                1.9380582068072627 .24758035817844862 .03609937184292087 7.932295030766125 0
                2.0306460746550394 .24806585552336896 .021115980979310157 6.4785541880673865 1
                1.8385283854866579 .19986105409416743 .020652414185113842 10.363061986291324 1
                1.9736462512097805 .06855061601161756 .024047875579706748 6.750961108209461 0
                1.9769853417901335 .33724378397180416 .017118646198720125 7.777863842906347 1
                1.8545938219946896 .30055710669517777 .018918646200314113 7.636383979077606 1
                2.886666087988723 .15277429315835184 .03533725755090225 10.855454260862754 1
                1.3807206988594998 .616143277023171 .003723889966898756 5.639606554437637 1
                2.2361776526107477 .26794909161566116 .012921402895171477 7.630267068171986 1
                9.28029906542056 .27524229074889867 .04070484581497797 8.644002038279933 1
                .8217743575613246 .30930619871638637 .007741141789399162 5.845432488574447 1
                1.3653890875928012 .22438623490829931 .01337300005543486 7.03656089770649 0
                2.800319755600815 .3067567966466924 -.00008855565723056941 10.430521115146327 1
                3.927241192562631 0 .05982923895641445 8.717433576126178 1
                .5205014326016568 .17731842326756325 .0007743689109328184 8.6229545667191 0
                .7988620988725066 .18910471323051906 .002006304018415759 5.249142878535657 0
                1.1528772598589017 .27211150188875866 .013282133925851286 8.443672585190909 1
                1.0824540711537936 .31280606062952193 .01893637035804013 7.857182870229327 1
                1.1188457318289626 .2452208335452753 .010463449736842892 6.5064146534896405 1
                4.414392777315159 .30744329614941296 .009213276808564675 6.2400224017506565 1
                1.863419219993175 .2169520322068658 .018969571690629208 8.835908851864653 1
                1.2937478285552086 .05945033965279936 .0030191359943169205 11.814059157345177 1
                1.2836064650560342 .19992189011901249 .003265378320655072 7.041395040734514 0
                .9734652518048188 .41056808441462483 .011505039458516832 4.915093087375768 1
                2.3414603025370666 .04547157992309307 .007938720680141068 8.953379403377928 0
                5.007207397093378 . .04567133374239973 9.570989408736887 1
                1.2348910387473195 .6933934494285644 .015182351673371145 6.540061276160971 1
                13.08921134631084 .3304803372220662 .03931358971740739 7.134521062086644 1
                end
                
                
                local vars var1 var2 var3 var4
                local group class
                
                foreach v in `vars' {
                di "`v'"
                ttest `v', by(`group')
                local mean_`v'_mean_0 = r(mu_1)
                local mean_`v'_mean_1 = r(mu_2)
                local mean_`v'_diff = r(mu_1) - r(mu_2)
                local mean_`v'_p=r(p)
                
                sum `v' if `group'==0, detail
                local p50_`v'_p50_0 = r(p50)
                sum `v' if `group'==1, detail
                local p50_`v'_p50_1 = r(p50)
                
                ranksum `v', by(`group')
                local p50_`v'_n_0=r(N_1)
                local p50_`v'_n_1=r(N_2)
                local p50_`v'_diff=`p50_`v'_p50_1'-`p50_`v'_p50_0'
                local p50_`v'_p=2*normprob(-abs(r(z)))
                }
                
                di _newline
                di "{hline 115}"
                di _col(15) "{c |} `group' = 1" ///
                _col(45) "{c |} `group' = 0" ///
                _col(75) "{c |} Diff"
                di _col(16) "{hline 100}"
                di _col(15) "{c |} Mean" ///
                _col(25) "{c |} Median" ///
                _col(35) "{c |} N" ///
                _col(45) "{c |} Mean" ///
                _col(55) "{c |} Median" ///
                _col(65) "{c |} N" ///
                _col(75) "{c |} Mean" ///
                _col(85) "{c |} P" ///
                _col(95) "{c |} Median" ///
                _col(105) "{c |} P"
                di "{hline 115}"
                
                foreach v in `vars' {
                di %12s abbrev("`v'",12) ///
                _col(15) "{c |}" %8.3f `mean_`v'_mean_1' ///
                _col(25) "{c |}" %8.3f `p50_`v'_p50_1' ///
                _col(35) "{c |}" %8.0f `p50_`v'_n_1' ///
                _col(45) "{c |}" %8.3f `mean_`v'_mean_0' ///
                _col(55) "{c |}" %8.3f `p50_`v'_p50_0' ///
                _col(65) "{c |}" %8.0f `p50_`v'_n_0' ///
                _col(75) "{c |}" %8.3f `mean_`v'_diff' ///
                _col(85) "{c |}" %8.3f `mean_`v'_p' ///
                _col(95) "{c |}" %8.3f `p50_`v'_diff' ///
                _col(105) "{c |}" %8.3f `p50_`v'_p'
                }
                
                di "{hline 115}"
                Hi Dear Cox,
                Dear Cox I want to do the same mean and median test,
                but the above code is not working, Kindly guide me in this regard
                Thanks in Advance

                Comment


                • #9
                  "not working" is not a problem I can diagnose. Please back up and follow https://www.statalist.org/forums/help#stata

                  Comment


                  • #10
                    Originally posted by Nick Cox View Post
                    "not working" is not a problem I can diagnose. Please back up and follow https://www.statalist.org/forums/help#stata
                    Following are the data set for my analysis
                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input byte(familyceo familycfo ceoexp ceoduality firmauditedbybig4) int firmage float(ROA leverage FSize GROWTH)
                    1 0 . 0 0 32   1.459615  .6678604  7.668056         .
                    1 0 . 0 0 33  -7.754289  .6067408  7.261759  3.054217
                    1 0 . 0 0 34          .         .         .         .
                    1 0 . 0 0 35   -8.80937  .4181014  7.164674         .
                    1 0 . 0 0 36  .25011736 .45187625  7.217708  .6899911
                    1 0 . 0 0 37   3.831065  .5182462   7.38139  .8478552
                    0 0 0 0 1 63   19.89492 .21250607  9.529928         .
                    0 0 1 0 1 63   19.89492 .21463886  9.710406  .9301814
                    0 0 1 0 1 63   19.89492 .21250607  9.778219  .9051874
                    0 0 1 0 1 63   19.89492 .25370887  9.864554  .8964928
                    0 0 1 0 1 63   19.89492  .3474259  9.917455  .8778219
                    0 0 1 0 1 63  12.098378  .3556304  9.940432   .985522
                    0 0 . 0 0 30   3.164827  .7846099  8.184522         .
                    0 0 . 0 0 31   -8.80937   .910777   8.06464  .5994081
                    0 0 . 0 0 32   -8.80937  .9236114  8.022999 4.0967345
                    0 0 . 0 0 33  -6.127638  .9236114  7.958175 1.4421222
                    0 0 . 0 0 34   -8.80937  .9236114  7.938602  2.514825
                    0 0 . 0 0 35  -6.362999  .8580823  8.310107  .7753903
                    1 0 0 1 0 22 -1.7192656  .6652378   8.14824         .
                    0 0 . 0 0 23 -3.2373226 .57061285  7.844554  1.294626
                    0 0 . 0 0 24   7.366319  .4672839  7.735307  .7518582
                    0 0 . 0 0 25 -.14124398  .7162448 8.3098955  1.762861
                    0 0 . 0 0 26 -.07025166  .6959535  8.216352  .4916485
                    0 0 . 0 0 27  -4.469071    .47768  8.778835  1.625662
                    1 0 . 0 0 28   3.908297  .6691024  6.680208         .
                    1 0 . 0 0 29   1.857098  .5940771  6.490783 1.2480315
                    1 0 . 0 0 30  1.9604052 .49459785  6.373218  .9451178
                    1 0 . 0 0 31  -3.821005  .5233763  6.373218 1.4368476
                    1 0 . 0 0 32   -8.80937  .6072503  6.373218 3.9577906
                    1 0 . 0 0 33   -8.80937  .8332708  6.373218 2.8359556
                    0 0 1 0 1 33  13.014237 .21250607  8.039464         .
                    0 0 1 0 1 34   19.89492 .21250607  8.173347  .6416053
                    0 0 1 0 1 35  18.970385 .21250607  8.232238  .8549759
                    0 0 1 0 1 36  19.466335 .21250607  8.333967 1.0178238
                    0 0 1 0 1 37  17.775503 .21250607  8.377935  .9254964
                    0 0 1 0 1 38  17.249828 .21250607  8.448696  .8582649
                    0 0 0 0 1 55   -8.80937  .8003521 10.759789         .
                    0 0 1 0 1 56  -7.516587  .8591401 10.759789  .7888702
                    0 0 . 0 1 57 -3.6039126  .9080115 10.759789  .4299975
                    0 0 . . 1 58  -8.231997  .9236114 10.759789  2.319735
                    0 0 . 0 1 59  -7.876342  .9236114 10.740938  .7834263
                    0 0 1 0 0 60  -1.404496  .8447106 10.759789   .372364
                    0 0 0 0 0 25  2.2452927  .4854042  8.198164         .
                    0 0 0 0 0 26  -1.921782  .4668746  8.207247  1.224217
                    0 0 0 0 0 27  .23675595 .47850555  8.209219  .9401861
                    0 0 0 0 0 28   .2322288  .5353287   8.32279  .8519874
                    0 0 0 0 0 29  1.5040905 .55433726  8.369829  .7831377
                    0 0 0 0 0 30   2.717768 .59114134  8.467881  .7119889
                    0 0 1 0 1 15  -2.511726  .8443624  9.697705         .
                    0 0 1 0 1 15   -8.80937  .8167302  9.638017  .9754818
                    0 0 1 0 1 15  -1.249461  .8270376  9.639021   .985204
                    0 0 1 0 1 15   4.851413  .6315141  9.808291  .6844545
                    0 0 1 0 1 15   9.378237  .5842944  9.924554  .7446014
                    0 0 1 0 1 15   -1.26006  .7327657 10.396075  .9344124
                    0 0 1 0 1 15  17.075975  .3470866  8.287368         .
                    0 0 0 0 1 15          .         .         .  .9948369
                    0 0 1 0 1 15  14.846066  .3408561 8.6568165  .8658588
                    0 0 1 0 1 15  10.669405  .3241286  8.650183  .9789823
                    0 0 1 0 1 15   9.325613 .21644107 9.0378475 1.0358547
                    0 0 1 0 1 15          .         .         .         .
                    0 0 0 0 0 23   8.826463 .49371645  8.388728         .
                    0 0 . 0 0 24  15.698702  .3681829  8.168033  .9409341
                    0 0 . 0 0 25   15.83402 .29515263  8.105588 1.0126767
                    0 0 . 0 0 26  4.0019803  .5149329  8.550848 1.0875539
                    0 0 . 0 0 27   19.89492  .3589711  8.631696  .7108834
                    0 0 . 0 0 28   18.73228  .4967346  8.766024 1.0459486
                    1 0 0 0 0 46   -8.80937  .9236114  8.559438         .
                    1 0 0 0 0 47   -8.80937  .9236114  8.462826 1.9834975
                    1 0 0 0 0 48   -8.80937  .9236114  8.377414         .
                    1 0 0 0 0 49  -8.137987  .9236114  8.293559         .
                    1 0 0 0 0 50 -1.3095858  .9236114  7.815776         .
                    1 0 0 0 0 51   -5.59072  .9236114  7.717165         .
                    0 0 0 0 1 31   19.89492 .21250607 9.2092085         .
                    0 0 0 0 1 32   19.89492 .21250607  8.764783  .9112272
                    0 0 0 0 1 33   19.89492 .27664977    8.4434  .7964498
                    0 0 0 0 1 34   19.89492  .6385494  8.679538  .6411182
                    0 0 0 0 1 35   19.89492  .7981861  8.822537  .9741348
                    0 0 0 0 1 36   19.89492  .8349503  8.608017 1.3845043
                    1 0 0 0 1 45   1.160986  .6246384  8.789524         .
                    1 0 0 0 0 46   3.092305  .6332455  8.874778 1.1051954
                    1 0 0 0 0 47  3.2294085   .609208   8.85407  .8597001
                    1 0 0 0 0 48  -1.332969  .7230148  9.160687 1.1750988
                    1 0 0 0 0 49  -2.577426    .68208   9.15288   .759365
                    1 0 0 0 0 50   4.937258   .661628  9.132781  .9722548
                    . . . . 1 15   4.791622  .5250776  7.512246         .
                    0 0 1 1 1 15   3.826842  .3260834  8.383708  .8903771
                    0 0 1 1 1 15   6.872365 .23133583  8.512298  .7228909
                    0 0 1 0 1 15  -.1420071  .3481553   8.66932     1.078
                    0 0 1 0 1 15  -.2942775  .3985448  8.901327 1.1904831
                    0 0 1 0 1 15  3.0737975  .3928256  8.932312 1.2827805
                    1 0 1 0 1 56   3.493249  .2982024  7.010952         .
                    0 0 1 0 0 57  -4.987038  .3384683  7.036983   1.23194
                    0 0 1 0 0 58  -2.645522 .37328205  7.206051  1.051406
                    0 0 1 0 0 59  -2.538368  .3618834  7.143128  .8510298
                    0 0 1 0 0 60   2.648612  .3908854  7.214931   .906648
                    0 0 1 0 0 61  -.4464564  .4778426  7.642673 1.0372677
                    0 0 1 0 1 19   19.89492 .26621428   8.65854         .
                    0 0 1 0 1 20  2.2178755 .21250607  8.571435   1.22401
                    0 0 1 0 1 21  18.719685  .2179374  8.674351  .8938115
                    0 0 1 0 1 22   19.89492 .26749927  8.778236  .9885384
                    end
                    label values ceoduality CEOD
                    label def CEOD 0 "Chairperson & CEO are not the Same Person", modify
                    label def CEOD 1 "Chairperson & CEO are the Same Person", modify
                    label values firmauditedbybig4 BIG4
                    label def BIG4 0 "Not Audited by BIG4", modify
                    label def BIG4 1 "Audited by BIG4", modify

                    Dear Cox when I change the first two lines with the above-mentioned code with my variables the then it did not work, I want the mean median difference test for Family CEOs and Non-Family CEOs.
                    Last edited by Sattar Khan; 04 Jun 2021, 04:25.

                    Comment


                    • #11
                      Sorry, but #10 is just -- as far as I can follow it -- equivalent to "I changed the code and it no longer works". Sad to hear that, but it's the same story: you need to give the code you used and explain what did not work" means.

                      Comment

                      Working...
                      X