Announcement

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

  • Storing regression coefficients in a single variable

    I have a database on bilateral trade between 35 countries, so I have 1190 obs (35 countries * 34 possible partners). One of my variables is called PARTNER, which consists of the names of the countries. I am running a multiple regression with a dummy variable for each "PARTNER", using the following code:

    encode PARTNER, gen(PARTNERCODE)

    reg logconsrat1 i.PARTNERCODE logdist Commlang Colrel Contig, r

    I now have a coefficient for each of the 34 dummy variables included in the regression, and I would like to store them in a new variable called "S", in which the coefficient is asigned to a certain observation depending on its "PARTNERCODE".

    I have tried using a loop, using the following code:

    reg logconsrat1 i.PARTNERCODE logdist Commlang Colrel Contig, r
    postfile S (PARTNERCODE) using filename, replace foreach pcode of numlist 1/35 {
    replace S = _b[i.PARTNERCODE`pcode'] if PARTNERCODE == `pcode'
    }
    postclose

    But i am obtaining an error message stating "( invalid name" after the second line.

    What should I do?

  • #2
    Code:
    reg logconsrat1 i.PARTNERCODE logdist Commlang Colrel Contig, r
    
    tempfile filename // SET ASIDE A TEMPORARY FILE TO HOLD THE COEFFICIENTS
    postfile handle S PARTNERCODE using `filename', replace // USE IT FOR A POSTFILE
    foreach pcode of numlist 1/35 {
        post handle (_b[`pcode'.PARTNERCODE]) (`pcode') // POST THE COEFFICIENTS
    }
    postclose handle // CLOSE THE POSTFILE TO MAKE IT AVAILABLE
    merge m:1 PARTNERCODE using `filename', keep(match master) nogenerate // MERGE WITH ORIGINAL DATA SET
    By the way, I would be more likely to do this with a -frame- than with a -postfile- nowadays. But the -postfile- mechanism is still sometimes useful and can do a few things that cannot be done with -frame-s, so you probably should learn how to use it effectively. Once you have mastered that, -frame-s will be a only a small leap beyond that.

    Note: This code is untested because no example data was provided. In the future, when asking for help with code, please post example data so that those who want to help you can develop and test their code under realistic conditions and increase the chance that their solution will work in your actual data. The useful way to do that is with the -dataex- command. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      I'm afraid I don't understand your code, but this example seems to do what you describe your objective to be.
      Code:
      sysuse auto, clear
      drop if missing(rep78)
      regress price weight i.rep78
      generate S  = .
      forvalues i=1/5 {
          replace S = _b[`i'.rep78] if rep78==`i'
      }
      tab S rep78
      Code:
      . sysuse auto, clear
      (1978 automobile data)
      
      . drop if missing(rep78)
      (5 observations deleted)
      
      . regress price weight i.rep78
      
            Source |       SS           df       MS      Number of obs   =        69
      -------------+----------------------------------   F(5, 63)        =      7.18
             Model |   209422879         5  41884575.9   Prob > F        =    0.0000
          Residual |   367374080        63   5831334.6   R-squared       =    0.3631
      -------------+----------------------------------   Adj R-squared   =    0.3125
             Total |   576796959        68  8482308.22   Root MSE        =    2414.8
      
      ------------------------------------------------------------------------------
             price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
      -------------+----------------------------------------------------------------
            weight |   2.440292   .4155855     5.87   0.000     1.609811    3.270773
                   |
             rep78 |
                2  |   783.9009    1911.99     0.41   0.683    -3036.906    4604.708
                3  |   1379.115    1765.47     0.78   0.438    -2148.895    4907.126
                4  |   2068.267   1802.434     1.15   0.256    -1533.609    5670.143
                5  |   3245.272    1884.18     1.72   0.090      -519.96    7010.505
                   |
             _cons |  -3000.405   2139.024    -1.40   0.166    -7274.903    1274.093
      ------------------------------------------------------------------------------
      
      . generate S  = .
      (69 missing values generated)
      
      . forvalues i=1/5 {
        2.     replace S = _b[`i'.rep78] if rep78==`i'
        3. }
      (2 real changes made)
      (8 real changes made)
      (30 real changes made)
      (18 real changes made)
      (11 real changes made)
      
      . tab S rep78
      
                 |                   Repair record 1978
               S |         1          2          3          4          5 |     Total
      -----------+-------------------------------------------------------+----------
               0 |         2          0          0          0          0 |         2 
        783.9009 |         0          8          0          0          0 |         8 
        1379.115 |         0          0         30          0          0 |        30 
        2068.267 |         0          0          0         18          0 |        18 
        3245.272 |         0          0          0          0         11 |        11 
      -----------+-------------------------------------------------------+----------
           Total |         2          8         30         18         11 |        69 
      
      .

      Comment


      • #4
        William LIsowski's solution in #3 bypasses the -postfile- mechanism altogether and just directly stores the coefficients in the original data set. That is a better solution, unless there is some other use to which the file containing just the coefficients can be put.

        Comment


        • #5
          Thank you very much for both of your kind responses. I did not have any other use to the file containing the coefficients, so I was able to use the code provided by William correctly. I will make sure to post examples of the data the next time I ask a question.

          Comment

          Working...
          X