Announcement

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

  • post and postfile help after regression

    I am running a simple regression repeated for ~1,000 variables in a loop. I would like to capture the beta coefficients, se, and p values for my exposure and 3 adjustment variables and output them to a csv file. I have used the following code for mixed models and cannot figure out how to alter it to use with this simple model. I get the following error code: equation xylose not found
    post: above message corresponds to expression 2, variable ratio

    I am using v16.1. Below are my sample data and code.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int subjectid byte(timepoint sex age) float(aibwkg ratio xylose xylitol valine)
    20001 3 1 39 74.58166 .09547353   -.7932054 -1.0131693   4.88012
    20001 2 1 39 74.58166 .09547353   .57306993  -.8202816 4.3152866
    20001 1 1 39 74.58166 .09547353  -.50978696 -1.1714463  4.004311
    20002 3 1 35 74.30232 .06258648    1.187921 -1.0057454 4.0414968
    20002 2 1 35 74.30232 .06258648  -.57098293  -.9478416   3.50136
    20002 1 1 35 74.30232 .06258648  -.54130113  -.7070154  4.680255
    20003 3 0 42 71.09388   .028846     -.87649  .57306993   5.20874
    20003 2 0 42 71.09388   .028846      .29774     .27476   4.66839
    20003 1 0 42 71.09388   .028846     -.39757    -.36489  4.004348
    20004 3 0 36  59.1862 .04826844   -.4515804 -.06558012  5.631982
    20004 2 0 36  59.1862 .04826844  -.50320864  -.7149013  4.967944
    20004 1 0 36  59.1862 .04826844    -.588939   -.788257  5.110322
    20005 3 1 51 58.46099 .04752519   -.9756348 -1.1163021  5.163349
    20005 2 1 51 58.46099 .04752519   -.6698709  -.8362016  4.571539
    20005 1 1 51 58.46099 .04752519   -.8011949 -1.2734073  5.349926
    20006 3 1 19 76.56038 .05422749   -.1484685 -1.1551343  3.649079
    20006 2 1 19 76.56038 .05422749   -.7596757  -1.080953  5.256241
    20006 1 1 19 76.56038 .05422749 -.002501656 -1.0889778  3.596983
    end
    Code:
    capture postutil clear
    tempfile metabolitebresults
    postfile handle str32 Metabolite float ratio se_ratio ///
        age se_age sex se_sex aibwkg se_aibwkg using `metabolitebresults'
    
    foreach var of varlist xylose - valine {
    
        reg `var' ratio age sex aibwkg if timepoint==1
        
        local topost ("`var'")
        foreach x in ratio age sex aibwkg  {
            local topost `topost' (_b[`var':`x']) (_se[`var':`x'])
        }
        
        // POST IT
        post handle `topost'
    }
    
    postclose handle
    
    use `metaboliteresults', clear
    
    foreach v of varlist ratio age sex aibwkg {
       gen t_`v' = `v'/se_`v'
       gen p_`v' = 2*normal(-abs(t_`v'))
       order t_`v' p_`v', after(se_`v')
    }
    export delimited using my_metaboliteresults, replace

  • #2
    -regress- is a single equation command, so reference to its results does not use an equation name separated from a variable name by a colon, just a variable name.

    Code:
    capture postutil clear
    tempfile metabolitebresults
    postfile handle str32 Metabolite float ratio se_ratio ///
        age se_age sex se_sex aibwkg se_aibwkg using `metabolitebresults'
    
    foreach var of varlist xylose - valine {
    
        reg `var' ratio age sex aibwkg if timepoint==1
        
        local topost ("`var'")
        foreach x in ratio age sex aibwkg  {
            local topost `topost' (_b[`x']) (_se[`x'])
        }
        
        // POST IT
        post handle `topost'
    }
    
    postclose handle
    will run just fine.

    More generally, to find the way to refer to the results of an estimation command in Stata, replay the command along with the -coefl- option. Here's an illustration:

    Code:
    . sysuse auto, clear
    (1978 automobile data)
    
    . regress price mpg i.foreign
    
          Source |       SS           df       MS      Number of obs   =        74
    -------------+----------------------------------   F(2, 71)        =     14.07
           Model |   180261702         2  90130850.8   Prob > F        =    0.0000
        Residual |   454803695        71  6405685.84   R-squared       =    0.2838
    -------------+----------------------------------   Adj R-squared   =    0.2637
           Total |   635065396        73  8699525.97   Root MSE        =    2530.9
    
    ------------------------------------------------------------------------------
           price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
             mpg |  -294.1955   55.69172    -5.28   0.000    -405.2417   -183.1494
                 |
         foreign |
        Foreign  |   1767.292    700.158     2.52   0.014     371.2169    3163.368
           _cons |   11905.42   1158.634    10.28   0.000     9595.164    14215.67
    ------------------------------------------------------------------------------
    
    . regress, coefl
    
          Source |       SS           df       MS      Number of obs   =        74
    -------------+----------------------------------   F(2, 71)        =     14.07
           Model |   180261702         2  90130850.8   Prob > F        =    0.0000
        Residual |   454803695        71  6405685.84   R-squared       =    0.2838
    -------------+----------------------------------   Adj R-squared   =    0.2637
           Total |   635065396        73  8699525.97   Root MSE        =    2530.9
    
    ------------------------------------------------------------------------------
           price | Coefficient  Legend
    -------------+----------------------------------------------------------------
             mpg |  -294.1955  _b[mpg]
                 |
         foreign |
        Foreign  |   1767.292  _b[1.foreign]
           _cons |   11905.42  _b[_cons]
    ------------------------------------------------------------------------------
    
    . mvreg price weight = mpg i.foreign
    
    Equation             Obs   Parms        RMSE    "R-sq"          F      P>F
    --------------------------------------------------------------------------
    price                 74       3    2530.946    0.2838   14.07045   0.0000
    weight                74       3    400.9136    0.7412    101.667   0.0000
    
    ------------------------------------------------------------------------------
                 | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
    price        |
             mpg |  -294.1955   55.69172    -5.28   0.000    -405.2417   -183.1494
                 |
         foreign |
        Foreign  |   1767.292    700.158     2.52   0.014     371.2169    3163.368
           _cons |   11905.42   1158.634    10.28   0.000     9595.164    14215.67
    -------------+----------------------------------------------------------------
    weight       |
             mpg |  -91.21962   8.821828   -10.34   0.000    -108.8098   -73.62939
                 |
         foreign |
        Foreign  |  -550.0519   110.9083    -4.96   0.000    -771.1968   -328.9071
           _cons |    5125.72   183.5331    27.93   0.000     4759.765    5491.674
    ------------------------------------------------------------------------------
    
    . mvreg, coefl
    
    Equation             Obs   Parms        RMSE    "R-sq"          F      P>F
    --------------------------------------------------------------------------
    price                 74       3    2530.946    0.2838   14.07045   0.0000
    weight                74       3    400.9136    0.7412    101.667   0.0000
    
    ------------------------------------------------------------------------------
                 | Coefficient  Legend
    -------------+----------------------------------------------------------------
    price        |
             mpg |  -294.1955  _b[price:mpg]
                 |
         foreign |
        Foreign  |   1767.292  _b[price:1.foreign]
           _cons |   11905.42  _b[price:_cons]
    -------------+----------------------------------------------------------------
    weight       |
             mpg |  -91.21962  _b[weight:mpg]
                 |
         foreign |
        Foreign  |  -550.0519  _b[weight:1.foreign]
           _cons |    5125.72  _b[weight:_cons]
    ------------------------------------------------------------------------------
    As an aside, your tempfile is declared under the name metabolitebresults, and the -postfile- command uses that name as well. Later, however, you attempt to -use `metaboliteresults'-. Had your code gotten that far, Stata would have told you that no such file can be found. Make the name consistent everywhere.

    Comment


    • #3
      Thanks, Clyde, that ran just fine. I really appreciate the explanation and suggestion for using the coefl command. I struggle with some of the nuances for the post/postfile commands.
      -Sandi

      Comment

      Working...
      X