Announcement

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

  • Margins: decile categories

    I am using Stata 13 and a raster (grid cell) data set combined with vector (point level) data to examine an index of ecological suitability (MEI2)

    In order to interpret the interaction between treatment and the index of ecological suitability included in the logit model I am using the following margins command:

    Code:
    margins, dydx(treat2) at(MEI2=(10(10)90))
    However, this gives me the predicted probabilities for the values of MEI2 from 10 to 90 in increments of 10 while holding the other variables at their mean.

    How can I get predicted probabilities by MEI2 deciles?

    I know how to create deciles out of the MEI
    Code:
    _pctile MEI_avg, p(10(10)90)
    is it possible to do something like:

    Code:
    margins, dydx(treat2) at(MEI2=p(10(10)90))
    The MEI2 values range from 0 to 2.65.

    Thanks in advance for any suggestions.

  • #2
    Not quite. You have to build a numlist out of the actual values of the percentiles, and then feed that to -margins-. Here's one way:

    Code:
    your regression command here
    local plist
    foreach p of numlist 10(10)90 {
        centile MEI2 if e(sample), c(`p')
        local plist `plist' `r(c_1)'
    }
    margins, dydx(treat2) at(MEI2 = (`plist'))

    Comment


    • #3
      Thanks!

      I want to double check the logic to make sure I understand. Since the MEI2 is continuous and ranges from 0 to 2.65 is this part correct?

      Code:
      ...
      foreach p of numlist 0(.265) 2.65 {
      ...
      This would start the numlist at 0 and go to 2.65 increasing in 10 equal intervals of .265.

      Comment


      • #4
        No, that isn't what I suggested it, and it isn't what you said you wanted, either. You said you wanted decile categories: that means that each category contains (to the extent possible given ties) 10% of the data. That doesn't necessarily correspond to equally spaced cutpoints in the data--in fact it rarely does.

        The 10(10)90 in the code is fed, via the loop parameter `p' into the -centile- command to tell it which percentile you want on that round of the loop. -centile- calculates the corresponding percentile and adds it to the local macro plist. So at the end of the loop, plist will contain an increasing sequence of numbers between 0 and 2.65 that define the 10th, 20th, 30h,...,90th percentiles of MEI2. Then plist is passed to the -margins- command to calculate marginal effects of treat2 at those particular values of MEI2.

        Comment


        • #5
          Perfect! That is exactly what I want. You are correct. I don't want the equal intervals I do, in fact, want the 10th, 20th,...percentiles of MEI2 defined.

          Thanks for the detailed code explanation.

          Comment


          • #6
            I received the following error: invalid numlist has too few elements

            I checked the stata error manual http://www.stata.com/manuals13/perror.pdf and there isn't much detail on error r122. Below is the output and error as well as a random sample of 50 out of the ~2.1 million observations.

            How can I modify the loop parameter to generate a plist of numbers in increasing sequence?


            ----------------------- copy starting from the next line -----------------------
            Code:
            
            
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float(id MEI2)
            29609 2.0860803
             1711         0
             2511 1.0411465
             2631 1.4109602
             3162  2.105539
             5244 2.0666802
            37852  1.665062
            38002         0
            38690 1.8957915
            38786 1.8957915
            39259  1.598454
            41056         0
            41623  1.682044
            42177 1.6482167
            42634 1.5659386
            43490         0
            14030 1.8589375
            14840 1.0672604
            44552 2.1445906
            45467 1.7689055
            16123 2.0666802
            17702  .7313325
            48153         0
            49007 2.0089083
            20995   1.32364
            51596 1.3094578
            52115 1.8406857
            53305 1.9518962
            53667         0
            54886 1.1725061
            25047  1.699157
            58316 1.7689055
            59504 1.8406857
            63148  1.716401
            66701 1.9518962
            68184 1.2263303
            68601 2.0280845
            72431         0
            72758 1.9518962
            73301 1.6149104
            73331         0
            73449         0
            73833 1.7337753
            76693 1.6314977
            76921  1.598454
            77152  1.682044
            79290 1.3814105
            79944         0
            81714         0
            82458  1.786665
            end
            ------------------ copy up to and including the previous line ------------------

            [/CODE]

            Comment


            • #7
              Well, for whatever reason, your post got cut off after the data, so I can't see the code and output as you ran them. I did run the loop (taking out the -if e(sample)- since there was no regression command run), and plist does contain the expected 9 numbers when I -display- its contents. So I don't know what went wrong here. Without seeing the code and output, here's my guess. My guess is that you ran the code without replacing the your regression command here line with an actual regression. In that case, inside the loop, the -if e(sample)- clause finds no observations to take percentiles of, so that -centile- returns nothing and plist comes out empty. Then when plist is passed to -margins-, it balks. (Though if that is what happened, I'm also surprised that -margins- didn't first complain that it couldn't find your regression estimates.

              So it would be really helpful to see the full output.

              Comment


              • #8
                Thanks for the suggestions. I do have it working this morning.

                I did, in fact, replace the your regression command here line with my logit so I had that going for me. I now know I have to run the statements as a single process instead of 3 separate (logistic, macro, margins).

                Thanks again. The margins are formatted and have made there way to the white paper.

                Comment

                Working...
                X