Announcement

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

  • Store the results of a regression in a matrix

    Hi!

    I'm trying to run a regression and separating the results by a binary variable, this is easy to do it with the use of bysort. However, in the following steps I'm trying to construct a matrix with the values of each regression so first I need to store the results of the regression with a certain name. The problem is that when I use matrix list to see the stored results I only get the results of the last regression and not of both

    sysuse auto.dta


    bysort foreign: regress price mpg weight headroom
    matrix list r(table)

    To be a little more clear I want to achieve something like this, but preferably using a loop or something that can be replicated:

    reg price mpg weight headroom if foreign == 0
    matrix res1=r(table)

    reg price mpg weight headroom if foreign == 1
    matrix res2=r(table)

    matrix list res1
    matrix list res2
    Last edited by Steven Karan; 30 Aug 2022, 12:12.

  • #2
    If or a foreach loop may be more suitable for this case. You can also try statsby. For example:

    Code:
    sysuse auto, clear
    statsby cons = _b[_cons] mpg = _b[mpg] weight = _b[weight] headroom = _b[headroom], by(foreign): reg price mpg weight headroom
    list
    Results:

    Code:
         +---------------------------------------------------------+
         |  foreign        cons         mpg     weight    headroom |
         |---------------------------------------------------------|
      1. | Domestic   -10845.59    192.2954   4.451391   -526.4644 |
      2. |  Foreign    -5283.24   -21.69082   5.121311    131.9128 |
         +---------------------------------------------------------+

    Comment


    • #3
      Thank you, I was trying to do it as in #1 because I need to store results of a particular value in a matrix. That's why I was trying to use a loop and then use the return results to build the matrix. But I think it could be useful your code, so thanks

      Comment


      • #4
        Code:
        webuse nhanes2, clear
        
        * Extract levels
        levelsof region, local(lv)
        
        * Loop
        foreach x of local lv{
            quietly reg bmi i.sex i.race age if region == `x'
            matrix res`x' = r(table)
        }
        
        mat list res1
        mat list res2
        mat list res3
        mat list res4

        Comment


        • #5
          Hi Steven. Ken's code is perfect if the 'levelsof' variable categorizes your data exactly. If you're interested in extracting a specific result from the regression results (for example the b coefficients), you might further consider something like:
          Code:
          mat result = J(4,2,.)
          mat result[1,1] = res1[1,4]
          mat result[2,1] = res1[1,3]
          ...
          mat result[1,2] = res0[1,4]
          ...
          mat result[4,2] = res0[1,1]
          mat coln result = "foreign" "domestic"
          mat rown result.........

          Comment


          • #6
            Thank you both, its been really helpful. Now, I wanted to follow up a little bit on Erics' code. I want to extract different coefficient and build a new matrix, but is there any way I can automate this that can work for different data sets? For example, following #4, now that I have the results in res1, res2, ... I know the position of the betas and the pvalues, however I would like to put these in a matrix that looks like this:
            beta p-value
            region 1
            region 2
            Is there any way to do this? I'm not sure if I'm being clear in my explanation. Thanks again for further help!

            Comment


            • #7
              Originally posted by Steven Karan View Post
              Thank you both, its been really helpful. Now, I wanted to follow up a little bit on Erics' code. I want to extract different coefficient and build a new matrix, but is there any way I can automate this that can work for different data sets? For example, following #4, now that I have the results in res1, res2, ... I know the position of the betas and the pvalues, however I would like to put these in a matrix that looks like this:
              beta p-value
              region 1
              region 2
              Is there any way to do this? I'm not sure if I'm being clear in my explanation. Thanks again for further help!
              Not Eric, but I've recently answered a question similar to this: https://www.statalist.org/forums/for...s-and-p-values

              Comment

              Working...
              X