Announcement

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

  • Store regression results of a loop in a new dataset

    Hello everybody,

    I am new in this forum and I hope that I will get some advice from you.

    I would like to run 255 regression models having one dependent variable and 9 independent variables. I would like to model every possible combination of the independent variables by including max 4 independent variables in one model specification.

    The following code works fine, but how do I store the results in a new dataset?

    Code:
     tuples x1 x2 x3 x4 x5 x6 x7 x8 x9, max(4) dis
    forval i = 1/255 { reg y `tuple`i'' }

    I would like to generate a new dataset in with 255 observations (each model specifications is one observation) and the variables are: beta_x1; se_x1; tvalue_x1; beta_x2; se_x3; tvalue_x3 up to beta_x9; se_x9; tvalue_x9 and r2 and const for each obversvation. If the variable is not part of the model secification a missing value should occur.

    I have tried this code but the result is a new dataset with 255 observations where only the first coefficient of the given model specification is displayed.

    Code:
    postfile test N beta beta_se const const_se str20 id using "results.dta", replace
    tuples x1 x2 x3 x4 x5 x6 x7 x8 x9, max(4) dis
    forval i = 1/255 { reg y `tuple`i'' mat results = r(table) local N = e(N) local beta = results[1,1] local beta_se = results[2,1] local const = results[1,2] local const_se = results[2,2] post test (`N') (`beta') (`beta_se') (`const') (`const_se') ("id`i'") }
    postclose test
    preserve
    use results.dta, clear

    Any thoughts or suggestions are greatly appreciated! Thank you
    Last edited by Agnes Tarnowski; 24 Jan 2020, 02:46.

  • #2
    Or is there an alternative way to save the results of the 255 regressions in an Excel document?
    My goal is to compare the coefficients of the 255 regression models.

    As shown below or reversed - so that the model specifications are in the columns (m1-m255) and the variables are in the rows (x1-x9)
    beta_x1 beta_x2 beta_x3 beta_x4 . . . . . . beta_x9
    model1
    model2
    model3
    model4
    .
    .
    .
    .
    model255
    I would appreciate your support here




    Last edited by Agnes Tarnowski; 24 Jan 2020, 05:56.

    Comment


    • #3
      I have no solution, but in assistance to other members, below I post a version of your code from post #1 that has the necessary line breaks.
      Code:
      tuples x1 x2 x3 x4 x5 x6 x7 x8 x9, max(4) dis
      
      postfile test N beta beta_se const const_se str20 id using "results.dta", replace
      forval i = 1/255 { 
          reg y `tuple`i'' 
          mat results = r(table) 
          local N = e(N) 
          local beta = results[1,1] 
          local beta_se = results[2,1] 
          local const = results[1,2] 
          local const_se = results[2,2] 
          post test (`N') (`beta') (`beta_se') (`const') (`const_se') ("id`i'") 
      }
      postclose test
      preserve
      use results.dta, clear

      Comment


      • #4
        This example may start you in a useful direction. If you really want an Excel worksheet, see the output of help export excel .
        Code:
        sysuse auto, clear
        rename (mpg weight length) (y x1 x2)
        tuples x1 x2
        
        postfile test model str40 id N k str16 var beta beta_se ///
            using "~/Downloads/results.dta", replace
        
        forvalues i=1/3 {
            quietly regress y `tuple`i''
            matrix results = r(table)
            local id `tuple`i''
            local N = e(N)
            local k = colsof(results)
            forvalues j=1/`k' {
                local var : word `j' of `tuple`i'' _cons
                local beta    = results[1,`j'] 
                local beta_se = results[2,`j'] 
                post test (`i') ("`id'") (`N') (`k') ("`var'") (`beta') (`beta_se')
            }
        }
        
        postclose test
        use "~/Downloads/results", clear
        list, noobs sepby(model)
        
        rename (beta beta_se) (beta_ se_)
        reshape wide beta_ se_, i(model) j(var) string
        order model id N k
        list, clean noobs abbreviate(12)
        Code:
        . use "~/Downloads/results", clear
        
        . list, noobs sepby(model)
        
          +-------------------------------------------------------+
          | model      id    N   k     var        beta    beta_se |
          |-------------------------------------------------------|
          |     1      x2   74   2      x2   -.2067688    .018544 |
          |     1      x2   74   2   _cons    60.15586   3.509057 |
          |-------------------------------------------------------|
          |     2      x1   74   2      x1   -.0060087   .0005179 |
          |     2      x1   74   2   _cons    39.44028   1.614003 |
          |-------------------------------------------------------|
          |     3   x1 x2   74   3      x1   -.0038515    .001586 |
          |     3   x1 x2   74   3      x2   -.0795935   .0553577 |
          |     3   x1 x2   74   3   _cons    47.88487    6.08787 |
          +-------------------------------------------------------+
        
        . 
        . rename (beta beta_se) (beta_ se_)
        
        . reshape wide beta_ se_, i(model) j(var) string
        (note: j = _cons x1 x2)
        
        Data                               long   ->   wide
        -----------------------------------------------------------------------------
        Number of obs.                        7   ->       3
        Number of variables                   7   ->      10
        j variable (3 values)               var   ->   (dropped)
        xij variables:
                                          beta_   ->   beta__cons beta_x1 beta_x2
                                            se_   ->   se__cons se_x1 se_x2
        -----------------------------------------------------------------------------
        
        . order model id N k
        
        . list, clean noobs abbreviate(12)
        
            model      id    N   k   beta__cons   se__cons     beta_x1      se_x1     beta_x2      se_x2  
                1      x2   74   2     60.15586   3.509057           .          .   -.2067688    .018544  
                2      x1   74   2     39.44028   1.614003   -.0060087   .0005179           .          .  
                3   x1 x2   74   3     47.88487    6.08787   -.0038515    .001586   -.0795935   .0553577

        Comment


        • #5
          Thank you very much for your help.
          I am trying to use the code which you suggested.
          Unfortunately, I do not get the desired result after the loop

          This is the output I get after the list command.
          I can not figure out why, I used the same code you suggested, except I added4 independent 4 variables

          Code:
          .
          
          list, noobs sepby(model)
          
            +--------------------------------------------------+
            | model   id    N   k     var       beta   beta_se |
            |--------------------------------------------------|
            |     1        39   1   _cons   .1588668   .185216 |
            |--------------------------------------------------|
            |     2        39   1   _cons   .1588668   .185216 |
            |--------------------------------------------------|
            |     3        39   1   _cons   .1588668   .185216 |
            |--------------------------------------------------|
            |     4        39   1   _cons   .1588668   .185216 |
            |--------------------------------------------------|
            |     5        39   1   _cons   .1588668   .185216 |
            |--------------------------------------------------|
            |     6        39   1   _cons   .1588668   .185216 |
            |--------------------------------------------------|
            |     7        39   1   _cons   .1588668   .185216 |
            |--------------------------------------------------|
            |     8        39   1   _cons   .1588668   .185216 |
            |--------------------------------------------------|
            |     9        39   1   _cons   .1588668   .185216 |
            |--------------------------------------------------|
            |    10        39   1   _cons   .1588668   .185216 |
            |--------------------------------------------------|
            |    11        39   1   _cons   .1588668   .185216 |
            |--------------------------------------------------|
            |    12        39   1   _cons   .1588668   .185216 |
            |--------------------------------------------------|
            |    13        39   1   _cons   .1588668   .185216 |
            |--------------------------------------------------|
            |    14        39   1   _cons   .1588668   .185216 |
            |--------------------------------------------------|
            |    15        39   1   _cons   .1588668   .185216 |
            +--------------------------------------------------+
          This is the loop I used

          Code:
          tuples x1 x2 x3 x4, dis
          
          postfile test model str40 id N k str16 var beta beta_se ///
              using "~/Downloads/results.dta", replace
          
          
          forvalues i=1/15 {
              quietly y `tuple`i''
              matrix results = r(table)
              local id `tuple`i''
              local N = e(N)
              local k = colsof(results)
              forvalues j=1/`k' {
                  local var : word `j' of `tuple`i'' _cons
                  local beta    = results[1,`j']
                  local beta_se = results[2,`j']
                  post test (`i') ("`id'") (`N') (`k') ("`var'") (`beta') (`beta_se')
              }
          }
          
          postclose test
          use "~/Downloads/results", clear
          list, noobs sepby(model)
          I would be very happy to find a solution to the problem and many thanks again for your help

          Comment


          • #6
            I found my mistake, sorry for my confusion.
            And thanks for the great support

            Comment

            Working...
            X