Announcement

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

  • Custom table after regress COMMAND

    Hi,

    I am looking to create a custom table that exports my results from a linear regression automatically to prevent manual work since I have a lot of variables. I am using the FOREACH COMMAND to run all the regressions at once but I don't know how to get them onto a table like the one below. Any help is appreciated.

    sysuse auto.dta

    foreach var of varlist price mpg rep78 headroom trunk weight length turn displacement gear_ratio{
    regress `var' i.foreign
    }
    Variable Coefficient 95% CI P-value
    Mpg 4.95 2.23 – 7.66 0.001
    Rep78 1.26 0.84 – 1.68 <0.001

  • #2
    Code:
    clear*
    sysuse auto
    
    frame create results str32 variable float(coef ll ul pvalue)
    
    foreach var of varlist price mpg rep78 headroom trunk weight length turn displacement gear_ratio{
        regress `var' i.foreign
        matrix M = r(table)
        frame post results ("`var'") (M["b", "1.foreign"]) (M["ll", "1.foreign"]) ///
            (M["ul", "1.foreign"]) (M["pvalue", "1.foreign"])
    }
    
    frame change results
    format coef %3.2f
    egen ci = concat(ll ul), format(%3.2f) punct(", ")
    replace ci = "[" + ci + "]"
    tostring pvalue, gen(P_value) format(%05.3f) force
    replace P_value = "<0.001" if pvalue < 0.001
    list variable coef ci P_value, noobs clean
    Note: It isn't a good idea to use a dash to separate the two numbers in a confidence interval, as it gets confusing if one of the numbers is negative. Here I used a comma instead.

    Comment


    • #3
      Thank you Clyde, this worked perfectly!

      Comment

      Working...
      X