Announcement

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

  • converting regression output (only the intercept _cons) into a new variable/column

    Dear readers,

    Is the following possible: converting regression output (only the intercept which is _cons) into a new variable/column?
    If so, please tell me how. I need to make about 600 regressions and need the 600 intercepts (_cons) as 1 variable/column.

    Lydia

  • #2
    There are many ways to do this depending on how you are going through your 600 regressions. But iwhat you can do is create an empty variable and then fill it in one observation at a time with _b[cons]. For example, if you are running your regressions in a forvalues loop:

    g cons = .
    forvalues i = 1/600 {
    reg y x`i'
    replace cons = _b[cons] if _n == `i'
    }

    ​There might be a faster way to do this, but this is a simple solution.

    Comment


    • #3
      Hi Lydia and Conner,

      I'm a bit confused here. First, what is the number of observations? I ask this because you have to run 600 regressions and each would yield one constant, so you would have 600 constants. If there are fewer observations (say 500) you can't store 600 constants. If there are more than 600 observations then the question arises to where each constant should be placed, because you would be relating it to the rest of the values of the other variables for the observation you place it in. Actually this question arises no matter what. I thus think that although Conner's method works perfectly for what you mentioned, it's not clear why storing the 600 constants in a variable is useful at all. I would think that it would be more useful to store them in a vector maybe, but then I'm guessing because I don't know the purpose of it.
      Alfonso Sanchez-Penalver

      Comment


      • #4
        Lidia, if you want coefficients in the data just to be able to save them as a dataset, there is a more specific approach: see help for estimates store and estimates save.
        Best, Sergiy

        Comment


        • #5
          I'd use the -post- commands. Something like this:

          cap postclose constants
          postfile constants cons, replace
          for ... {
          [regression i]
          post constant (_b[_cons])
          }
          postclose constants

          That will create a Stata data file called constants.dta in your working directory with your results in a column. You can also add more columns to the postfile and post command lines to include additional info about each regression.
          --David

          Comment

          Working...
          X