Announcement

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

  • How to compute manually margins (eyex and dydx) after a multinomial regression

    Hello,
    Can someone help me with computing the margins eyex and dydx manually, similar to what Stata does with the commands margins, eyex() and margins, dydx()?

    I am working for an institution using a platform where Stata is not available, and we do not have the option to install packages in other statistical software's.
    Therefore, I need to understand how Stata computes these margins so that I can replicate the process manually in other available applications on the platform.

    However, I am not obtaining the same results when I compute them manually compared to when I use the commands margins, eyex() and margins, dydx() on my Stata.

    Please find my data and scripts below.

    Many thanks for your help!








    . ssc install dataex, replace
    checking dataex consistency and verifying not already installed...
    all files already exist and are up to date.

    . dataex

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(income ipc) str3 prem_emission float(testclass dicho)
                  5185 .08737096837606123 "nc"  1 0
                  6900  .2751666645772861 "nc"  1 0
                  7150 .44855189232301124 "nc"  1 0
                  5700  .5503496211136313 "nc"  1 0
                  6180  .6150057483911646 "nc"  1 0
                  7160  12.38556978124845 "nc"  3 0
                  7080 1.3336560869358243 "nc"  3 0
                  6650 1.4300558710753521 "nc"  3 0
                  7940  4.936040486312466 "nc"  3 0
                 10590  2.285560283724503 "nc"  3 0
                  4810  .6379019006512485 "nc"  1 0
                  5910 .10172782199131047 "nc"  1 0
                  8610  4.725078450252779 "nc"  3 0
                  6680  .8463431101088507 "nc"  2 0
                  6860 1.0357571481480847 "nc"  2 0
                  7630  1.854448838311463 "nc"  3 0
                  3040  91.72693891176144 "nc"  3 0
                  4340  90.03352982721125 "nc"  3 0
               34254.6 .05051347388196038 "nc"  1 0
               39848.4 .15006706827114039 "nc"  1 0
                 42502  .8108797955127851 "nc"  2 0
               36233.9  .7376828109330235 "nc"  1 0
                 36897  .9346414377752771 "nc"  2 0
               40701.2 16.456397743328935 "nc"  3 0
               34643.8 2.3534043016277897 "nc"  3 0
                 41303  .6323109752132469 "nc"  1 0
               45267.6  6.465596085877682 "nc"  3 0
                 50249 42.903106538453635 "nc"  3 0
               36119.9 .43771418545513907 "nc"  1 0
               36890.6 .08594027310274638 "nc"  1 0
               47784.2  9.053379009537554 "nc"  3 0
               37337.6  .7511179583907089 "nc"  1 0
               40425.8   .928748844024827 "nc"  2 0
               39739.2 2.3423877640346658 "nc"  3 0
    28216.474203154732   89.9736677709193 "nc"  3 0
     28843.50696322484  91.76434419463205 "nc"  3 0
                 61600  .2085018513959576 "13"  1 1
                 69000  .2014138892114737 "18"  1 1
                 74000 1.2771290982254766 "45"  2 1
                     0 1.0009551098376313 "33"  2 1
                 64500 1.4021279178918629 "28"  3 1
                 66000 10.198002759116184 "149" 3 1
                 81000 2.3965958959220846 "23"  3 1
                 73000 .30595178199915696 "12"  1 1
                 76500  4.094888449590512 "12"  3 1
                 96000  37.07914298207258 "3"   3 1
                 62800 .17349063150589866 "11"  1 1
                 72500  .4684846023188834 "28"  1 1
                 85000  9.244558200225383 "35"  3 1
                 79000 1.2183171332468152 "34"  2 1
                 77500 1.5383212568171136 "50"  3 1
                 80000  3.268682223143715 "108" 3 1
                 86604 4.6343267360552405 "5"   3 1
                116570 11.645024425579592 "6"   3 1
    end
    ------------------ copy up to and including the previous line ------------------

    Listed 54 out of 54 observations

    .
    .
    .
    . // Multilogit and margins commands
    . mlogit testclass income dicho, base(1) nolog

    Multinomial logistic regression Number of obs = 54
    LR chi2(4) = 2.58
    Prob > chi2 = 0.6304
    Log likelihood = -52.547628 Pseudo R2 = 0.0240

    ------------------------------------------------------------------------------
    testclass | Coef. Std. Err. z P>|z| [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    1 | (base outcome)
    -------------+----------------------------------------------------------------
    2 |
    income | -8.88e-06 .0000214 -0.42 0.678 -.0000507 .000033
    dicho | .9468991 1.366389 0.69 0.488 -1.731175 3.624973
    _cons | -.8557658 .6622474 -1.29 0.196 -2.153747 .4422152
    -------------+----------------------------------------------------------------
    3 |
    income | .0000173 .0000173 1.00 0.316 -.0000165 .0000512
    dicho | -.3893059 1.10068 -0.35 0.724 -2.546598 1.767986
    _cons | -.2064702 .5378888 -0.38 0.701 -1.260713 .8477725
    ------------------------------------------------------------------------------

    . margins, dydx(*)

    Average marginal effects Number of obs = 54
    Model VCE : OIM

    dy/dx w.r.t. : income dicho
    1._predict : Pr(testclass==1), predict(pr outcome(1))
    2._predict : Pr(testclass==2), predict(pr outcome(2))
    3._predict : Pr(testclass==3), predict(pr outcome(3))

    ------------------------------------------------------------------------------
    | Delta-method
    | dy/dx Std. Err. z P>|z| [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    income |
    _predict |
    1 | -2.48e-06 3.42e-06 -0.72 0.469 -9.19e-06 4.23e-06
    2 | -2.32e-06 2.38e-06 -0.98 0.329 -6.98e-06 2.34e-06
    3 | 4.80e-06 3.60e-06 1.33 0.183 -2.26e-06 .0000119
    -------------+----------------------------------------------------------------
    dicho |
    _predict |
    1 | .0157943 .2235704 0.07 0.944 -.4223956 .4539842
    2 | .1445371 .1537585 0.94 0.347 -.1568241 .4458982
    3 | -.1603314 .2379593 -0.67 0.500 -.6267231 .3060602
    ------------------------------------------------------------------------------

    . margins, eyex(*)

    Average marginal effects Number of obs = 54
    Model VCE : OIM

    ey/ex w.r.t. : income dicho
    1._predict : Pr(testclass==1), predict(pr outcome(1))
    2._predict : Pr(testclass==2), predict(pr outcome(2))
    3._predict : Pr(testclass==3), predict(pr outcome(3))

    ------------------------------------------------------------------------------
    | Delta-method
    | ey/ex Std. Err. z P>|z| [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    income |
    _predict |
    1 | -.3409894 .462768 -0.74 0.461 -1.247998 .5660192
    2 | -.6924964 .7032758 -0.98 0.325 -2.070892 .6858989
    3 | .3455409 .2587482 1.34 0.182 -.1615963 .852678
    -------------+----------------------------------------------------------------
    dicho |
    _predict |
    1 | .0194882 .2456872 0.08 0.937 -.4620498 .5010262
    2 | .3351212 .3353336 1.00 0.318 -.3221205 .9923629
    3 | -.1102804 .1668992 -0.66 0.509 -.4373968 .2168359
    ------------------------------------------------------------------------------

    .
    .
    . // Computing eyex margins manually
    . quietly{

    . sum elasticity_*

    Variable | Obs Mean Std. Dev. Min Max
    -------------+---------------------------------------------------------
    elasticit~e1 | 54 0 0 0 0
    elasticit~o1 | 54 -5.90e-07 8.46e-07 -2.09e-06 0
    elasticit~e2 | 54 0 0 0 0
    elasticit~o2 | 54 .0424143 .0634159 0 .2297057
    elasticit~e3 | 54 0 0 0 0
    -------------+---------------------------------------------------------
    elasticit~o3 | 54 -.0673789 .0968364 -.213939 0

    .
    end of do-file






    Last edited by Justin Ndoutoumou; 27 Nov 2023, 11:38.

  • #2

    I fixed some errors on the previous post. I apologize.

    . ssc install dataex, replace
    checking dataex consistency and verifying not already installed...
    all files already exist and are up to date.

    . dataex

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(income ipc) str3 prem_emission float(testclass dicho)
                  5185 .08737096837606123 "nc"  1 0
                  6900  .2751666645772861 "nc"  1 0
                  7150 .44855189232301124 "nc"  1 0
                  5700  .5503496211136313 "nc"  1 0
                  6180  .6150057483911646 "nc"  1 0
                  7160  12.38556978124845 "nc"  3 0
                  7080 1.3336560869358243 "nc"  3 0
                  6650 1.4300558710753521 "nc"  3 0
                  7940  4.936040486312466 "nc"  3 0
                 10590  2.285560283724503 "nc"  3 0
                  4810  .6379019006512485 "nc"  1 0
                  5910 .10172782199131047 "nc"  1 0
                  8610  4.725078450252779 "nc"  3 0
                  6680  .8463431101088507 "nc"  2 0
                  6860 1.0357571481480847 "nc"  2 0
                  7630  1.854448838311463 "nc"  3 0
                  3040  91.72693891176144 "nc"  3 0
                  4340  90.03352982721125 "nc"  3 0
               34254.6 .05051347388196038 "nc"  1 0
               39848.4 .15006706827114039 "nc"  1 0
                 42502  .8108797955127851 "nc"  2 0
               36233.9  .7376828109330235 "nc"  1 0
                 36897  .9346414377752771 "nc"  2 0
               40701.2 16.456397743328935 "nc"  3 0
               34643.8 2.3534043016277897 "nc"  3 0
                 41303  .6323109752132469 "nc"  1 0
               45267.6  6.465596085877682 "nc"  3 0
                 50249 42.903106538453635 "nc"  3 0
               36119.9 .43771418545513907 "nc"  1 0
               36890.6 .08594027310274638 "nc"  1 0
               47784.2  9.053379009537554 "nc"  3 0
               37337.6  .7511179583907089 "nc"  1 0
               40425.8   .928748844024827 "nc"  2 0
               39739.2 2.3423877640346658 "nc"  3 0
    28216.474203154732   89.9736677709193 "nc"  3 0
     28843.50696322484  91.76434419463205 "nc"  3 0
                 61600  .2085018513959576 "13"  1 1
                 69000  .2014138892114737 "18"  1 1
                 74000 1.2771290982254766 "45"  2 1
                     0 1.0009551098376313 "33"  2 1
                 64500 1.4021279178918629 "28"  3 1
                 66000 10.198002759116184 "149" 3 1
                 81000 2.3965958959220846 "23"  3 1
                 73000 .30595178199915696 "12"  1 1
                 76500  4.094888449590512 "12"  3 1
                 96000  37.07914298207258 "3"   3 1
                 62800 .17349063150589866 "11"  1 1
                 72500  .4684846023188834 "28"  1 1
                 85000  9.244558200225383 "35"  3 1
                 79000 1.2183171332468152 "34"  2 1
                 77500 1.5383212568171136 "50"  3 1
                 80000  3.268682223143715 "108" 3 1
                 86604 4.6343267360552405 "5"   3 1
                116570 11.645024425579592 "6"   3 1
    end
    ------------------ copy up to and including the previous line ------------------

    Listed 54 out of 54 observations

    .
    .
    .
    . // Multilogit and margins commands
    . mlogit testclass income dicho, base(1) nolog

    Multinomial logistic regression Number of obs = 54
    LR chi2(4) = 2.58
    Prob > chi2 = 0.6304
    Log likelihood = -52.547628 Pseudo R2 = 0.0240

    ------------------------------------------------------------------------------
    testclass | Coef. Std. Err. z P>|z| [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    1 | (base outcome)
    -------------+----------------------------------------------------------------
    2 |
    income | -8.88e-06 .0000214 -0.42 0.678 -.0000507 .000033
    dicho | .9468991 1.366389 0.69 0.488 -1.731175 3.624973
    _cons | -.8557658 .6622474 -1.29 0.196 -2.153747 .4422152
    -------------+----------------------------------------------------------------
    3 |
    income | .0000173 .0000173 1.00 0.316 -.0000165 .0000512
    dicho | -.3893059 1.10068 -0.35 0.724 -2.546598 1.767986
    _cons | -.2064702 .5378888 -0.38 0.701 -1.260713 .8477725
    ------------------------------------------------------------------------------

    . margins, dydx(*)

    Average marginal effects Number of obs = 54
    Model VCE : OIM

    dy/dx w.r.t. : income dicho
    1._predict : Pr(testclass==1), predict(pr outcome(1))
    2._predict : Pr(testclass==2), predict(pr outcome(2))
    3._predict : Pr(testclass==3), predict(pr outcome(3))

    ------------------------------------------------------------------------------
    | Delta-method
    | dy/dx Std. Err. z P>|z| [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    income |
    _predict |
    1 | -2.48e-06 3.42e-06 -0.72 0.469 -9.19e-06 4.23e-06
    2 | -2.32e-06 2.38e-06 -0.98 0.329 -6.98e-06 2.34e-06
    3 | 4.80e-06 3.60e-06 1.33 0.183 -2.26e-06 .0000119
    -------------+----------------------------------------------------------------
    dicho |
    _predict |
    1 | .0157943 .2235704 0.07 0.944 -.4223956 .4539842
    2 | .1445371 .1537585 0.94 0.347 -.1568241 .4458982
    3 | -.1603314 .2379593 -0.67 0.500 -.6267231 .3060602
    ------------------------------------------------------------------------------

    . margins, eyex(*)

    Average marginal effects Number of obs = 54
    Model VCE : OIM

    ey/ex w.r.t. : income dicho
    1._predict : Pr(testclass==1), predict(pr outcome(1))
    2._predict : Pr(testclass==2), predict(pr outcome(2))
    3._predict : Pr(testclass==3), predict(pr outcome(3))

    ------------------------------------------------------------------------------
    | Delta-method
    | ey/ex Std. Err. z P>|z| [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    income |
    _predict |
    1 | -.3409894 .462768 -0.74 0.461 -1.247998 .5660192
    2 | -.6924964 .7032758 -0.98 0.325 -2.070892 .6858989
    3 | .3455409 .2587482 1.34 0.182 -.1615963 .852678
    -------------+----------------------------------------------------------------
    dicho |
    _predict |
    1 | .0194882 .2456872 0.08 0.937 -.4620498 .5010262
    2 | .3351212 .3353336 1.00 0.318 -.3221205 .9923629
    3 | -.1102804 .1668992 -0.66 0.509 -.4373968 .2168359
    ------------------------------------------------------------------------------

    .
    .
    . // Computing eyex margins manually
    . mat list e(b)
    . matrix a = e(b)
    . mat a1 = a[1, "1:"]
    . mat a2 = a[1, "2:"]
    . mat coeff = (a1 \ a2)'
    . mat list coeff

    coeff[3,2]
    y1 y1
    1:o.income 0 -8.881e-06
    1:o.dicho 0 .94689907
    1:o._cons 0 -.85576583

    .
    . predict double prob_1 prob_2 prob_3, pr
    . gen p=.
    . forval i = 1/3 {
    2. replace p=prob_`i'
    3. gen elasticity_income`i' = coeff[`i', 1] *income* (1 - p)
    4. gen elasticity_dicho`i' = coeff[`i', 2] *dicho* (1 - p)
    5. }

    . sum elasticity_*

    Variable | Obs Mean Std. Dev. Min Max
    -------------+---------------------------------------------------------
    elasticit~e1 | 54 0 0 0 0
    elasticit~o1 | 54 -2.14e-06 3.06e-06 -7.28e-06 0
    elasticit~e2 | 54 0 0 0 0
    elasticit~o2 | 54 .2630275 .3772945 0 .8805537
    elasticit~e3 | 54 0 0 0 0
    -------------+---------------------------------------------------------
    elasticit~o3 | 54 -.1267801 .1881919 -.6775556 0

    .
    end of do-file
    Last edited by Justin Ndoutoumou; 27 Nov 2023, 13:44.

    Comment


    • #3
      See https://www.stata.com/support/faqs/s...fects-methods/. The elasticity is related to derivative in the following way: https://en.wikipedia.org/wiki/Elasticity_of_a_function.

      Comment


      • #4
        Many thanks, Andrew. I am taking a look at this material and will give a feedback.

        Comment

        Working...
        X