Announcement

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

  • table creation after foreach and selecting variables

    Hello everybody and thanks in advance.
    I'm wondering how to create tables in Stata 16 after a loop via foreach command.

    Say...

    foreach var of varlist co1 sv1 ef_av1 {
    spearman lvls_sp1 `var'
    }

    In this example I do a spearman correlation of several variables versus a single variable and I want as output a table with 3 columns: variable name, z and p values.

    I'm not sure if this post is a duplicate of other posts but I didn't find the answer to my question.
    Thanks again.

    Gianfranco

  • #2
    Here is the code, illustrated with the auto.dta

    Code:
    clear*
    sysuse auto
    
    frame create results str32 vble long N float(rho p)
    
    foreach v of varlist mpg headroom gear_ratio {
        spearman price `v'
        frame post results ("`v'") (`r(N)') (`r(rho)') (`r(p)')
    }
    
    frame results {
        gen t = rho*sqrt(N-2)/sqrt((1-rho^2))
        format t rho p %4.3f
        list, noobs clean
    }
    Note the -spearman- calculates a t-statistic, not a z-statistic. The code reflects this. Also, I think a table of values of t and p without mentioning rho is, at best, useless, so I have chosen to include rho (and N) in the output. Of course, you are free to edit those out if you really don't want them.

    Comment

    Working...
    X