Announcement

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

  • Help with manually calculating marginal effects at mean for logit

    Hi Stata Users

    It seems simple but I have a question on how to manually calculate the marginal effects at mean for logit model. So basically I need to manually replicate the results of the output I obtained when I used margins, dydx (*) atmeans (in other words, I need to replicate the red colored numbers using a manual method). I tried several things but couldn't replicate it. My continuous variables are gre and gpa and the categorical variable is rank. The dataset is attached

    use binary
    qui logit admit gre gpa i.rank
    margins , dydx(*) atmeans

    The output is given below

    . qui logit admit gre gpa i.rank

    . margins , dydx(*) atmeans

    Conditional marginal effects Number of obs = 400
    Model VCE : OIM

    Expression : Pr(admit), predict()
    dy/dx w.r.t. : gre gpa 2.rank 3.rank 4.rank
    at : gre = 587.7 (mean)
    gpa = 3.3899 (mean)
    1.rank = .1525 (mean)
    2.rank = .3775 (mean)
    3.rank = .3025 (mean)
    4.rank = .1675 (mean)

    ------------------------------------------------------------------------------
    | Delta-method
    | dy/dx Std. Err. z P>|z| [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    gre | .0004743 .0002282 2.08 0.038 .0000271 .0009215
    gpa | .1684057 .0691417 2.44 0.015 .0328904 .3039211
    |
    rank |
    2 | -.164317 .0770559 -2.13 0.033 -.3153438 -.0132903
    3 | -.2979896 .0761584 -3.91 0.000 -.4472572 -.1487219
    4 | -.3319332 .0821748 -4.04 0.000 -.4929928 -.1708735
    ------------------------------------------------------------------------------
    Note: dy/dx for factor levels is the discrete change from the base level.
    Attached Files

  • #2
    What formula were you using to calculate dy/dx manually? Reflect a bit, and you'll realize we could not possibly tell what is wrong with what you are doing if you don't tell us what you did! And, if you can give us a bit of the context for *why* you are wanting to calculate this manually, that would be helpful.

    Comment


    • #3
      Hi Mike

      Thanks for the message. Sorry for not being detailed. Here are my answers

      "Why" - I am doing a class on probit and logit and wanted to show the students how to margins works (both with the direct margins code and also manually). So I was showing them how Marginal effects at the mean (MEM) and Average Marginal Effects (AME) would work for both probit and logit using the command "margins" and then I wanted to do it manually so that the students understand what happens underneath. I was also showing them how to do this for both a continuous and a categorical independent variables. I was able to replicate the results for probit for both MEM and AME for continuous and categorical case. For logit, I was able to replicate AME but not MEM. Hence my question


      Here are the codes I used for Logit. These codes did not replicate the results of margins , dydx(*) atmeans. I wanted to do this for the continuous variables (gre and gpa) and the categorical variable (rank) separately so that students understand what happens. I started with doing this for continuous variables but the code did not work. I couldn't get to the categorical variable. I tried two methods (both from the web) and in both cases couldn't replicate the results. The codes for two methods are given below

      //Marginal effects with binary logit
      //Marginal effects at mean
      //Marginal effects at mean for discrete variables and continuous variable
      s

      //Method 1
      clear
      use binary
      qui logit admit gre gpa i.rank
      margins , dydx(*) atmeans
      //Lets try to replicate the results manually
      quietly summarize gre
      quietly replace gre=r(mean)
      quietly summarize gpa
      quietly replace gpa=r(mean)
      predict xb, xb
      gen my_dfdxb= exp(xb)*(1+exp(xb))^(-2)
      display _b[gre]*my_dfdxb
      display _b[gpa]*my_dfdxb


      //Method 2
      clear
      use binary
      qui logit admit gre gpa i.rank
      replace gpa = 3.3899
      predict lw_0 if e(sample)
      qui sum gre
      scalar r = r(sd)/1000
      replace gre = gre + r
      predict lw_1 if e(sample)
      gen dydx = (lw_1-lw_0)/r
      sum dydx


      Thanks

      Comment


      • #4
        I won't comment on your Method 2 because I don't understand it. To the extent that I follow it, it appears to calculate some kind of standardized marginal effect of gre, not rank, but it does it by treating gre as a discrete variable when it is, in fact, continuous.

        But as for method 1, what you have calculated there is the marginal effect for rank treated as if it were a continuous variable, which is not the way -margins- treats it. Try this:

        Code:
        clear
        use binary 
        qui logit admit gre gpa i.rank
        margins , dydx(*) atmeans
        //Lets try to replicate the results manually
        quietly summarize gre if e(sample)
        quietly replace gre=r(mean) if e(sample)
        quietly summarize gpa if e(sample)
        quietly replace gpa=r(mean) if e(sample)
        
        forvalues r = 1/4 {
            replace rank = `r'
            predct pr`r', pr if e(sample)
            summ pr`r', meanonly
            local m`r' = r(mean)
            if `r' > 1 {
                display "Marginal effect for rank = `r': " = `m`r'' - `m`1''
            }
        }
        Notes:

        1. Not tested. I do not download attachments from strangers, so I had nothing to work with.
        2. I have also restricted some of your calculations to the estimation sample. If you have missing values on some of the variables, your calculations might have resulted in new observations being added to the estimation sample--this, too, can cause the calculations to produce results that differ from what -margins- gives you.

        In the future, if you use the -dataex- command to post example data, everyone will be able to try out code on a faithful replica of our data. If you are running version 15.1 or a fully updated version 14.2, it 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.

        Comment


        • #5
          Hello Clyde

          Thanks for the reply and the note on using dataex. I unfortunately couldn't get the code to work. There was an error while running the loop which is given below. The data doesn't contain any missing values. I have attached the dataex output as well for future reference. How should I go about for a continuous variable i.e. gre or gpa?

          . forvalues r = 1/4 {
          2. replace rank = `r'
          3. predict pr`r', pr if e(sample)
          4. summ pr`r', meanonly
          5. local m`r' = r(mean)
          6. if `r' > 1 {
          7. display "Marginal effect for rank = `r': " = `m`r'' - `m`1''
          8. }
          9. }
          (339 real changes made)
          equation sample not found

          The dataex output. Hope this is what is expected. Apologies if not.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(admit gre gpa rank)
          0 380 3.61 3
          1 660 3.67 3
          1 800    4 1
          1 640 3.19 4
          0 520 2.93 4
          1 760    3 2
          1 560 2.98 1
          0 400 3.08 2
          1 540 3.39 3
          0 700 3.92 2
          0 800    4 4
          0 440 3.22 1
          1 760    4 1
          0 700 3.08 2
          1 700    4 1
          0 480 3.44 3
          0 780 3.87 4
          0 360 2.56 3
          0 800 3.75 2
          1 540 3.81 1
          0 500 3.17 3
          1 660 3.63 2
          0 600 2.82 4
          0 680 3.19 4
          1 760 3.35 2
          1 800 3.66 1
          1 620 3.61 1
          1 520 3.74 4
          1 780 3.22 2
          0 520 3.29 1
          0 540 3.78 4
          0 760 3.35 3
          0 600  3.4 3
          1 800    4 3
          0 360 3.14 1
          0 400 3.05 2
          0 580 3.25 1
          0 520  2.9 3
          1 500 3.13 2
          1 520 2.68 3
          0 560 2.42 2
          1 580 3.32 2
          1 600 3.15 2
          0 500 3.31 3
          0 700 2.94 2
          1 460 3.45 3
          1 580 3.46 2
          0 500 2.97 4
          0 440 2.48 4
          0 400 3.35 3
          0 640 3.86 3
          0 440 3.13 4
          0 740 3.37 4
          1 680 3.27 2
          0 660 3.34 3
          1 740    4 3
          0 560 3.19 3
          0 380 2.94 3
          0 400 3.65 2
          0 600 2.82 4
          1 620 3.18 2
          0 560 3.32 4
          0 640 3.67 3
          1 680 3.85 3
          0 580    4 3
          0 600 3.59 2
          0 740 3.62 4
          0 620  3.3 1
          0 580 3.69 1
          0 800 3.73 1
          0 640    4 3
          0 300 2.92 4
          0 480 3.39 4
          0 580    4 2
          0 720 3.45 4
          0 720    4 3
          0 560 3.36 3
          1 800    4 3
          0 540 3.12 1
          1 620    4 1
          0 700  2.9 4
          0 620 3.07 2
          0 500 2.71 2
          0 380 2.91 4
          1 500  3.6 3
          0 520 2.98 2
          0 600 3.32 2
          0 600 3.48 2
          0 700 3.28 1
          1 660    4 2
          0 700 3.83 2
          1 720 3.64 1
          0 800  3.9 2
          0 580 2.93 2
          1 660 3.44 2
          0 660 3.33 2
          0 640 3.52 4
          0 480 3.57 2
          0 700 2.88 2
          0 400 3.31 3
          end


          Senal

          Comment


          • #6
            With the data example, I am able to test the code and have found two errors in it.

            First, the -if e(sample)- in the -predict- command has to precede the comma.

            Second, in the -display- command, `m`1'' should be `m1'.

            With those changes you will find the code runs correctly and produces the same results as -margins- gives, at least to within a very small rounding error.

            Code:
            qui logit admit gre gpa i.rank
            margins , dydx(*) atmeans
            //Lets try to replicate the results manually
            quietly summarize gre if e(sample)
            quietly replace gre=r(mean) if e(sample)
            quietly summarize gpa if e(sample)
            quietly replace gpa=r(mean) if e(sample)
            
            
            forvalues r = 1/4 {
                replace rank = `r'
                predict pr`r' if e(sample), pr
                summ pr`r', meanonly
                local m`r' = r(mean)
                if `r' > 1 {
                    display "Marginal effect for rank = `r': " = `m`r'' - `m1'
                }
            }

            Comment


            • #7
              How did we calculate the value with predict? We need to know the process when using predict command.
              Thank you for your help!

              Comment


              • #8
                Run -help predict- and click on the link "View compmlete PDF manual entry." Then click on Methods and Formulas in the PDF. All the details are there.

                Comment

                Working...
                X