Announcement

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

  • Storing predictions from margins

    Hi all,

    I'm trying to generate a predicted outcome holding a categorical variable (x2) constant.

    I'm doing the following:
    Code:
    reg y i.x1 i.x2
    margins i.x1, at(x2==1) predict(xb)
    What I can't figure out is how to then store that result in a variable as the -predict- command does.

    Is this doable? What am I doing wrong?

    Thanks in advance.

  • #2
    It's a little bit convoluted to do this. One approach is to save the -margins- output to a tempfile using the undocumented -saving()- option. Then -merge- that information back into the original data. This entails knowing about about how Stata names the variables in the -saving()- file. In your case it would look something like this (untested):

    Code:
    reg y i.x1 i.x2
    tempfile holding
    margins x1, at(x2 = 1) saving(`holding')
    clonevar _m1 = x1
    merge m:1 _m1 using `holding', keepusing(_margin)
    drop _m1
    There are other approaches as well. -margins- leaves behind a matrix, r(table), that you can save as a real matrix and then loop over the columns of that matrix to store the corresponding -margins- values (first row) in observations with corresponding values of x1. This requires knowing all the values of x1 and in which column they appear in r(table). This approach is, in my view, more complicated to code--but if the data set is very large it will run faster.

    Comment


    • #3
      This (your first solution) worked perfectly; thank you!

      Surprising that Stata doesn't have a more elegant way of doing this. I know that in my field (health policy), it's often necessary to calculate and use these sorts of "adjusted" margins.

      Comment


      • #4
        Yes, I calculate a lot of adjusted margins in my work, too. In part, that's why I know this workaround. That said, I find that most of the time, these adjusted margins are my final outputs, and only occasionally used as inputs to additional calculations. So having them in the output logs, but not in the data sets, is typically sufficient in my work. Still, I agree it would be nice if StataCorp made it easier to capture these results in the data set.

        Comment


        • #5
          Clyde,

          Maybe I missed it, but I did not find saving listed as a option in margins. Thanks for showing me this. The savings option might be an oversight Stata might correct in the documentation.

          Phil

          Comment


          • #6
            No, it's not an oversight in the documentation. And you didn't miss it. It's officially undocumented. I'm sure StataCorp has their reasons for this, but I don't know them.

            By the way, the thanks are not due to me. Somebody else on the Forum pointed it out to me several months back. Unfortunately, I don't remember who that was, and I can't seem to find the post.

            Comment


            • #7
              -help undocumented- has documentation (!) for commands that are undocumented. It also explains the status of those commands:

              "Undocumented commands may change their syntax or behavior in subsequent releases of Stata, though this is rare, so use them with caution."

              The same presumably applies to undocumented features and options: you can use them, but there is no guarantee that their behavior won't change and/or that they won't disappear.

              Comment


              • #8
                Actually, now there is the option generate(var). It saves margins for each of the combinations of the values of the categorical variable and levels the margins were generated for. Still undoumented though, Thanks for the tip that -help undocumented- exists!

                Comment


                • #9
                  There is also the official option called -post- which moves the estimates into e(b) and e(V), so that is yet one more way to access the results of margins.

                  Comment

                  Working...
                  X