Announcement

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

  • Table of statistical test results

    Hello everybody,

    I have a problem creating a summary table of statistical tests by gender, since I have several variables and I want put them all in a one table, for that, I'm using a global variable:

    global variables v1 v2 v3 v4 ... vn

    and:
    foreach var of global variables {
    table (command) (result), ///
    command(Male = r(mu_1) N1 = r(N_1) Female = r(mu_2) N2 = r(N_2) Diff = (r(mu_1)-r(mu_2)) p-value = r(p): ttest `var', by(sex)) ///
    nformat(%6.4f Male Female Diff p-value)
    collect label list command, all
    }

    So, when i run the code, the result is a table for each variable in "variables", but want all the variables in a single table. Manually, the code comes out the way I want, but I want to work skillfully.

    Thanks.
    Last edited by Cinthia Salinas; 30 Jun 2023, 09:30. Reason: foreach, global, loop, test, table

  • #2
    Instead of running the table command in the loop, you can use the loop to construct a list of the command() options to pass to a single call to table.

    Here is an example using the auto data:
    Code:
    sysuse auto
    
    global variables mpg turn trunk displ head gear
                    
    * results of interest
    local results   Male=r(mu_1) ///
                    N1=r(N_1) ///
                    Female=r(mu_2) ///
                    N2=r(N_2) ///
                    Diff=(r(mu_1)-r(mu_2)) ///
                    r(p)
    
    * list of command() options
    foreach var of global variables {
            local cmdopts `cmdopts' ///
                    command(`results' : ttest `var', by(foreign))
    }      
    
    table (command) (result), ///
            `cmdopts' ///
            nformat(%6.4f Male Female Diff p)
    Here is the resulting table
    Code:
    ----------------------------------------------------------------------------------------
                             |      Male   N1     Female   N2       Diff   Two-sided p-value
    -------------------------+--------------------------------------------------------------
    ttest mpg, by(foreign)   |   19.8269   52    24.7727   22    -4.9458              0.0005
    ttest turn, by(foreign)  |   41.4423   52    35.4091   22     6.0332              0.0000
    ttest trunk, by(foreign) |   14.7500   52    11.4091   22     3.3409              0.0017
    ttest displ, by(foreign) |  233.7115   52   111.2273   22   122.4843              0.0000
    ttest head, by(foreign)  |    3.1538   52     2.6136   22     0.5402              0.0110
    ttest gear, by(foreign)  |    2.8065   52     3.5073   22    -0.7007              0.0000
    ----------------------------------------------------------------------------------------
    Last edited by Jeff Pitblado (StataCorp); 30 Jun 2023, 13:10.

    Comment


    • #3
      Thanks for the quick reply!

      Comment

      Working...
      X