Announcement

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

  • Using margins, at and then saving results as a new variable

    I would like to use "margins, at X1= a constant, where X1 is the value of one explanatory variable in a nonlinear GLM regression, and THEN save the predicted values for each observation as a new variable. How can I do this?

  • #2
    Your question is a little confusing. The -margins- command does not return any observation-level results. The output of -margins- consists of statistics that are applicable to the data set as a whole, or, if the -over()- option was used, to the specified subsets of the data set as a whole. If you actually want the -margins- outputs, they are left behind in -r(table)-, which you can convert to a real matrix and then, if it is really appropriate to put those results into the data set you can use -gen- and -replace- commands accessing that matrix.

    If what you are looking for are individual-level phenomena associated with the original estimation command, -predict- or -predictnl-, not -margins-, would be used to calculate those.

    Comment


    • #3
      It is possible to obtain observation-level results from margins by generating a variable at the observation level and using this variable within margins. However, using predict is generally more efficient, unless standard errors are required or the margins command is otherwise restricted. Bringing the results into the dataset simply involves using svmat.

      Code:
      sysuse auto, clear
      regress mpg weight disp i.rep78, robust
      predict mpghat1
      gen missing= !e(sample)
      gen obsno=_n
      margins, over(obsno) post
      mat res= r(table)'
      sort missing obsno
      svmat res, names(col)
      sort obsno
      l obsno make mpghat1 b - ul, sep(0)
      Res.:

      Code:
      . margins, over(obsno) post
      
      Predictive margins                                          Number of obs = 69
      Model VCE: Robust
      
      Expression: Linear prediction, predict()
      Over:       obsno
      
      ------------------------------------------------------------------------------
                   |            Delta-method
                   |     Margin   std. err.      t    P>|t|     [95% conf. interval]
      -------------+----------------------------------------------------------------
             obsno |
                1  |   20.96651   1.140494    18.38   0.000     18.68669    23.24632
                2  |   19.31313   .4836484    39.93   0.000     18.34633    20.27993
                4  |   19.49789   .7035685    27.71   0.000     18.09147     20.9043
                5  |   15.31405   1.011286    15.14   0.000     13.29252    17.33558
                6  |   17.11926   .8161426    20.98   0.000     15.48782    18.75071
                8  |   19.31021   .7326717    26.36   0.000     17.84562     20.7748
                9  |   15.80552   1.068683    14.79   0.000     13.66925    17.94179
               10  |   18.80836   .5546172    33.91   0.000      17.6997    19.91702
               11  |   14.36975   1.109703    12.95   0.000     12.15149    16.58802
               12  |   16.47384   .9353725    17.61   0.000     14.60406    18.34362
               13  |   14.08672   .6747479    20.88   0.000     12.73791    15.43552
               14  |   26.87849   1.590968    16.89   0.000     23.69819    30.05879
               15  |   17.04283   .6907388    24.67   0.000     15.66206     18.4236
               16  |   19.96424    .609466    32.76   0.000     18.74594    21.18255
               17  |   19.66132   .6996696    28.10   0.000      18.2627    21.05994
               18  |    22.2532   .7497174    29.68   0.000     20.75453    23.75186
               19  |   18.75578   .4844277    38.72   0.000     17.78742    19.72413
               20  |    28.5388   2.071379    13.78   0.000     24.39817    32.67943
               21  |   18.12308   .9032827    20.06   0.000     16.31745    19.92872
               22  |   18.12308   .9032827    20.06   0.000     16.31745    19.92872
               23  |     16.586    .946505    17.52   0.000     14.69396    18.47804
               24  |   27.78575   .8255026    33.66   0.000     26.13559    29.43591
               25  |   22.85326   .7121539    32.09   0.000     21.42969    24.27684
               26  |   11.00148   .8955228    12.28   0.000     9.211352     12.7916
               27  |   11.75219   .8415411    13.97   0.000     10.06997     13.4344
               28  |   16.62314    .536515    30.98   0.000     15.55067    17.69562
               29  |   23.20477   .4928664    47.08   0.000     22.21954    24.18999
               30  |   15.09788   .8168794    18.48   0.000     13.46496    16.73079
               31  |    17.3113   .5374465    32.21   0.000     16.23696    18.38564
               32  |   19.13113   .4724766    40.49   0.000     18.18666     20.0756
               33  |   14.65996   .8472413    17.30   0.000     12.96635    16.35357
               34  |    21.7272   .8434188    25.76   0.000     20.04123    23.41317
               35  |   15.43917   1.020289    15.13   0.000     13.39964     17.4787
               36  |   19.37139    .501478    38.63   0.000     18.36895    20.37383
               37  |   19.43395   .4971928    39.09   0.000     18.44008    20.42782
               38  |   16.90773    .768899    21.99   0.000     15.37073    18.44474
               39  |   18.99604   .5342028    35.56   0.000     17.92818    20.06389
               40  |   23.03028   .7460992    30.87   0.000     21.53885    24.52171
               41  |   15.71325   .6826891    23.02   0.000     14.34858    17.07793
               42  |   19.15092    1.12797    16.98   0.000     16.89614     21.4057
               43  |   30.45537   2.110751    14.43   0.000     26.23604     34.6747
               44  |   25.41956   .7694823    33.03   0.000     23.88139    26.95774
               46  |   19.15093    .642033    29.83   0.000     17.86752    20.43433
               47  |   16.84518   .7781598    21.65   0.000     15.28966    18.40069
               48  |   18.96972   .7460992    25.43   0.000     17.47829    20.46115
               49  |   19.99698   .4756989    42.04   0.000     19.04607    20.94789
               50  |   20.05954   .4753126    42.20   0.000     19.10941    21.00968
               52  |   22.62855   .7350403    30.79   0.000     21.15923    24.09788
               53  |   24.33174   2.134735    11.40   0.000     20.06446    28.59902
               54  |   26.17595   .7962953    32.87   0.000     24.58418    27.76772
               55  |   22.63176   .5994461    37.75   0.000     21.43348    23.83003
               56  |   24.36919   .5183488    47.01   0.000     23.33303    25.40536
               57  |   29.07196   2.072161    14.03   0.000     24.92977    33.21415
               58  |   24.93223   .5468229    45.59   0.000     23.83914    26.02531
               59  |   22.18392   .5163079    42.97   0.000     21.15184    23.21601
               60  |   25.85748   .7691737    33.62   0.000     24.31992    27.39503
               61  |   27.85208   2.069209    13.46   0.000     23.71579    31.98837
               62  |   27.98621    .817855    34.22   0.000     26.35135    29.62108
               63  |   26.57436   .6201265    42.85   0.000     25.33475    27.81398
               65  |   27.54939   .8755743    31.46   0.000     25.79914    29.29963
               66  |   28.96961   2.077527    13.94   0.000     24.81669    33.12252
               67  |   26.98056   2.076316    12.99   0.000     22.83006    31.13105
               68  |   28.03122   2.068933    13.55   0.000     23.89548    32.16696
               69  |   25.35402   2.088802    12.14   0.000     21.17856    29.52947
               70  |   25.52651   .5625238    45.38   0.000     24.40204    26.65098
               71  |   28.98239   2.073054    13.98   0.000     24.83842    33.12637
               72  |   26.90849   .6591067    40.83   0.000     25.59095    28.22602
               73  |   26.59002    .648159    41.02   0.000     25.29436    27.88567
               74  |   22.43226   2.175831    10.31   0.000     18.08283    26.78168
      ------------------------------------------------------------------------------
      
      . 
      . mat res= r(table)'
      
      . 
      . sort missing obsno
      
      . 
      . svmat res, names(col)
      
      . 
      . sort obsno
      
      . 
      . l obsno make mpghat1 b - ul, sep(0)
      
           +--------------------------------------------------------------------------------------------------------+
           | obsno   make                 mpghat1          b         se          t     pvalue         ll         ul |
           |--------------------------------------------------------------------------------------------------------|
        1. |     1   AMC Concord         20.96651   20.96651   1.140494   18.38371   8.73e-27   18.68669   23.24632 |
        2. |     2   AMC Pacer           19.31313   19.31313   .4836484   39.93218          0   18.34633   20.27993 |
        3. |     3   AMC Spirit                 .          .          .          .          .          .          . |
        4. |     4   Buick Century       19.49789   19.49789   .7035685   27.71285   1.24e-36   18.09147    20.9043 |
        5. |     5   Buick Electra       15.31405   15.31405   1.011286   15.14315   1.67e-22   13.29252   17.33558 |
        6. |     6   Buick LeSabre       17.11926   17.11926   .8161426   20.97582   7.49e-30   15.48782   18.75071 |
        7. |     7   Buick Opel                 .          .          .          .          .          .          . |
        8. |     8   Buick Regal         19.31021   19.31021   .7326717   26.35588   2.19e-35   17.84562    20.7748 |
        9. |     9   Buick Riviera       15.80552   15.80552   1.068683   14.78972   5.29e-22   13.66925   17.94179 |
       10. |    10   Buick Skylark       18.80836   18.80836   .5546172   33.91232          0    17.6997   19.91702 |
       11. |    11   Cad. Deville        14.36975   14.36975   1.109703   12.94918   2.75e-19   12.15149   16.58802 |
       12. |    12   Cad. Eldorado       16.47384   16.47384   .9353725   17.61206   8.18e-26   14.60406   18.34362 |
       13. |    13   Cad. Seville        14.08672   14.08672   .6747479   20.87701   9.68e-30   12.73792   15.43552 |
       14. |    14   Chev. Chevette      26.87849   26.87849   1.590968   16.89442   6.96e-25   23.69819   30.05879 |
       15. |    15   Chev. Impala        17.04283   17.04283   .6907388   24.67333   9.22e-34   15.66206    18.4236 |
       16. |    16   Chev. Malibu        19.96424   19.96424    .609466   32.75694          0   18.74594   21.18255 |
       17. |    17   Chev. Monte Carlo   19.66132   19.66132   .6996696   28.10086   5.57e-37    18.2627   21.05994 |
       18. |    18   Chev. Monza          22.2532    22.2532   .7497174   29.68211   2.36e-38   20.75453   23.75186 |
       19. |    19   Chev. Nova          18.75578   18.75578   .4844278   38.71739          0   17.78742   19.72413 |
       20. |    20   Dodge Colt           28.5388    28.5388   2.071378   13.77769   1.56e-20   24.39817   32.67943 |
       21. |    21   Dodge Diplomat      18.12309   18.12309   .9032827   20.06358   8.32e-29   16.31745   19.92872 |
       22. |    22   Dodge Magnum        18.12309   18.12309   .9032827   20.06358   8.32e-29   16.31745   19.92872 |
       23. |    23   Dodge St. Regis       16.586     16.586    .946505   17.52341   1.06e-25   14.69396   18.47804 |
       24. |    24   Ford Fiesta         27.78575   27.78575   .8255026   33.65919          0   26.13559   29.43591 |
       25. |    25   Ford Mustang        22.85326   22.85326    .712154   32.09034          0   21.42969   24.27684 |
       26. |    26   Linc. Continental   11.00148   11.00148   .8955228   12.28498   2.92e-18   9.211352    12.7916 |
       27. |    27   Linc. Mark V        11.75219   11.75219   .8415411   13.96508   8.26e-21   10.06997    13.4344 |
       28. |    28   Linc. Versailles    16.62314   16.62314    .536515   30.98356          0   15.55067   17.69562 |
       29. |    29   Merc. Bobcat        23.20477   23.20477   .4928664   47.08125          0   22.21954   24.18999 |
       30. |    30   Merc. Cougar        15.09788   15.09788   .8168793   18.48238   6.59e-27   13.46496   16.73079 |
       31. |    31   Merc. Marquis        17.3113    17.3113   .5374466   32.21027          0   16.23696   18.38564 |
       32. |    32   Merc. Monarch       19.13113   19.13113   .4724766   40.49118          0   18.18666    20.0756 |
       33. |    33   Merc. XR-7          14.65996   14.65996   .8472413   17.30317   2.04e-25   12.96635   16.35357 |
       34. |    34   Merc. Zephyr         21.7272    21.7272   .8434188   25.76086   8.03e-35   20.04123   23.41317 |
       35. |    35   Olds 98             15.43917   15.43917   1.020289   15.13216   1.73e-22   13.39964    17.4787 |
       36. |    36   Olds Cutl Supr      19.37139   19.37139    .501478    38.6286          0   18.36895   20.37383 |
       37. |    37   Olds Cutlass        19.43395   19.43395   .4971927   39.08736          0   18.44008   20.42782 |
       38. |    38   Olds Delta 88       16.90773   16.90773    .768899   21.98954   5.65e-31   15.37073   18.44474 |
       39. |    39   Olds Omega          18.99604   18.99604   .5342028    35.5596          0   17.92818   20.06389 |
       40. |    40   Olds Starfire       23.03028   23.03028   .7460992   30.86758          0   21.53885   24.52171 |
       41. |    41   Olds Toronado       15.71325   15.71325   .6826891   23.01671   4.52e-32   14.34858   17.07793 |
       42. |    42   Plym. Arrow         19.15092   19.15092    1.12797   16.97821   5.41e-25   16.89614    21.4057 |
       43. |    43   Plym. Champ         30.45537   30.45537   2.110751   14.42869   1.74e-21   26.23604    34.6747 |
       44. |    44   Plym. Horizon       25.41956   25.41956   .7694823   33.03463          0   23.88139   26.95774 |
       45. |    45   Plym. Sapporo              .          .          .          .          .          .          . |
       46. |    46   Plym. Volare        19.15093   19.15093    .642033   29.82857   1.77e-38   17.86752   20.43433 |
       47. |    47   Pont. Catalina      16.84517   16.84517   .7781597   21.64745   1.34e-30   15.28966   18.40069 |
       48. |    48   Pont. Firebird      18.96972   18.96972   .7460992    25.4252   1.69e-34   17.47829   20.46115 |
       49. |    49   Pont. Grand Prix    19.99698   19.99698   .4756989   42.03706          0   19.04607   20.94789 |
       50. |    50   Pont. Le Mans       20.05954   20.05954   .4753126   42.20285          0   19.10941   21.00968 |
       51. |    51   Pont. Phoenix              .          .          .          .          .          .          . |
       52. |    52   Pont. Sunbird       22.62855   22.62855   .7350403   30.78546          0   21.15923   24.09788 |
       53. |    53   Audi 5000           24.33174   24.33174   2.134735   11.39801   7.47e-17   20.06446   28.59901 |
       54. |    54   Audi Fox            26.17595   26.17595   .7962953   32.87217          0   24.58418   27.76772 |
       55. |    55   BMW 320i            22.63176   22.63176   .5994461   37.75445          0   21.43348   23.83003 |
       56. |    56   Datsun 200          24.36919   24.36919   .5183488   47.01311          0   23.33303   25.40536 |
       57. |    57   Datsun 210          29.07196   29.07196   2.072161   14.02977   6.63e-21   24.92977   33.21415 |
       58. |    58   Datsun 510          24.93223   24.93223   .5468228    45.5947          0   23.83914   26.02531 |
       59. |    59   Datsun 810          22.18392   22.18392   .5163078   42.96646          0   21.15184   23.21601 |
       60. |    60   Fiat Strada         25.85748   25.85748   .7691737   33.61721          0   24.31992   27.39503 |
       61. |    61   Honda Accord        27.85208   27.85208   2.069209   13.46026   4.64e-20   23.71579   31.98837 |
       62. |    62   Honda Civic         27.98621   27.98621    .817855   34.21904          0   26.35135   29.62108 |
       63. |    63   Mazda GLC           26.57436   26.57436   .6201265   42.85313          0   25.33475   27.81398 |
       64. |    64   Peugeot 604                .          .          .          .          .          .          . |
       65. |    65   Renault Le Car      27.54939   27.54939   .8755744   31.46436          0   25.79914   29.29963 |
       66. |    66   Subaru               28.9696    28.9696   2.077527   13.94427   8.86e-21   24.81669   33.12252 |
       67. |    67   Toyota Celica       26.98055   26.98055   2.076316   12.99443   2.34e-19   22.83006   31.13105 |
       68. |    68   Toyota Corolla      28.03122   28.03122   2.068933   13.54863   3.42e-20   23.89548   32.16696 |
       69. |    69   Toyota Corona       25.35402   25.35402   2.088802   12.13806   4.96e-18   21.17856   29.52947 |
       70. |    70   VW Dasher           25.52651   25.52651   .5625238   45.37854          0   24.40204   26.65098 |
       71. |    71   VW Diesel           28.98239   28.98239   2.073054   13.98053   7.84e-21   24.83842   33.12637 |
       72. |    72   VW Rabbit           26.90849   26.90849   .6591067   40.82569          0   25.59095   28.22602 |
       73. |    73   VW Scirocco         26.59002   26.59002    .648159   41.02391          0   25.29436   27.88567 |
       74. |    74   Volvo 260           22.43226   22.43226   2.175832   10.30974   4.53e-15   18.08283   26.78168 |
           +--------------------------------------------------------------------------------------------------------+
      
      .

      Comment


      • #4
        One of Stata's undocumented commands is margins, generate
        Code:
        help margins_generate
        This should provide what you are hoping to obtain.

        (I've complained often enough in this forum about undocumented commands that, IMHO, should be documented so won't do so again today.)

        Comment


        • #5


          If your goal is to obtain observation-specific fitted values after setting X1 equal to a constant for all observations, then predict or predictnl is usually the more direct route than margins.

          Conceptually, margins is designed for sample- or subgroup-level summaries, whereas predict is designed for observation-level quantities. So if you want a new variable in the data set, I would first ask whether you really need margins at all.

          A common workflow is:
          1. store the original values of X1
          2. replace X1 with the constant of interest
          3. use predict (or predictnl, depending on the statistic desired)
          4. save the predictions in a new variable
          5. restore the original X1
          That said, if you specifically need delta-method standard errors attached to those observation-level quantities, Andrew Musau's margins, over(obsno) approach is the relevant one.

          So the key distinction seems to be:
          • if you want fitted values only: predict
          • if you want observation-level margins with standard errors: margins plus svmat/margins, generate

          Comment

          Working...
          X