Search code examples
statastata-macros

Displaying results from ranksum


I'm trying to modify the code posted by @Nick Cox in my previous question but I have some issues.

I have set my varlist and my group variable. I have also changed the col option to fit my varname. I would like to add the number of observations for each group r(N 1) / r(N 2) and put over the results list some "titles".

I am trying to study the display command but I cannot figure out a solution.

My code is the following:

foreach v of var BVCAlogMAR AvgSSI L1DensityWholeImage { 
    quietly ranksum `v', by(G6PDcarente) porder 
    scalar pval = 2*normprob(-abs(r(z)))
    di "`v'{col 34}" %05.3f pval " " %6.4e pval  "   " %05.3f r(porder) ///
    "   " %05.3f r(N 1)  "   " %05.3f r(N 2)
} 

I am not able to put in the results list the values of r(N 1) and r(N 2) . Moreover, I have no idea how to show a column header with the following title:

P-Value, PValue2, Porder- Observ. group 1 - Observ. group 2

Can you please help me?


Solution

  • You are incorrectly referring to r(N_1) and r(N_2) as r(N 1) and r(N 2) respectively.

    In the toy example provided in your previous post, i have corrected this mistake and i inserted six additional lines in bold, which achieve what you want:

    sysuse auto, clear 
    
    local i = 0
    
    foreach v of var mpg price weight length displacement { 
        local ++i
        quietly ranksum `v', by(foreign) porder 
        scalar pval = 2*normprob(-abs(r(z)))
        
        if `i' == 1 {
          display %20s "P-Value", %5s "PValue2", %5s "Porder1", %5s "group 1", %5s "group 2"
          display ""
        }
    
        display "`v'{col 14}" %05.3f pval " " %6.4e pval  "   " %05.3f r(porder)  ///
        "   " %05.3f r(N_1)  "   " %05.3f r(N_2)
    } 
    

    The first display command acts as a header, but you will have to play with the values to get the desired spacing. The second merely adds a blank line.

    The counter macro i serves to display the header and blank lines only in the first step of the for loop instead of them repeating for each variable.

    The results are illustrated below:

                 P-Value PValue2 Porder1 group 1 group 2
    
    mpg          0.002  1.9e-03   0.271   52.000   22.000
    price        0.298  3.0e-01   0.423   52.000   22.000
    weight       0.000  3.8e-07   0.875   52.000   22.000
    length       0.000  9.4e-07   0.862   52.000   22.000
    displacement 0.000  1.1e-08   0.921   52.000   22.000
    

    For more information about output formatting using the display command, type help format in Stata's command prompt.