Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down at the end of May, and its replacement, **statalist.org** is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Steve Samuels <sjsamuels@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: How to store marginal effect value after using margin command? |

Date |
Mon, 10 Jun 2013 13:35:53 -0400 |

My description of the Jacobian matrix was incomplete. If an at() or atmean option is set, there is a row for each at-value. There is a column for each coefficient from the original regression command. An entry is the term in the partial derivative for the at-value that multiplies the corresponding coefficient. If no at() option is set, the estimated marginal dydx effects are averages over the population of observation specific effects, and (I assume, but have not checked) that the Jacobian is the average of the observation-specific Jacobians. In the case of linear regression, not specifying "at" will give the same result as specifying "atmean". Steve Vũ Võ : If you've now studied the -help- for -margins-, you'll know that as with -mfx-, the marginal effects are contained in the returned matrix r(b) and, with the post option, in e(b), as Kit stated. You can extract values from these matrices, e.g.: . scalar m1 = el(r(b),1,1) But you really cannot do much with scalars; they are just numbers unconnected to the command that generated them. The real advantage of the post option is that it makes the estimated effects available as Stata system "underscore variables" (type "help system variables"). To find the names of those variables, you run a second -margins- command with the "coeflegend" option, immediately after the first. . sysuse auto, clear . gen wtk = weight/1000 . reg price rep78##c.wtk . margins, dydx(wtk rep78) post . margins, coeflegend . nlcom _b[wtk] + _b[2.rep78]/_b[3.rep78] //silly manipulation . nlcom _b[w] + _b[2.r]/_b[3.r] // abbreviate names Your original question was whether -margins- could show the partial derivatives that you had derived "by hand". The answer is "Yes". These partial derivatives are contained in r(Jacobian) and e(Jacobian): . reg price c.wtk##c.wtk . margins, dydx(wtk) at(wtk =(1(1)4)) . matrix list r(Jacobian) For a constructive use of Jacobians, see Jeff Pitblado's post at: http://www.stata.com/statalist/archive/2010-04/msg00860.html Steve > On Jun 8, 2013, at 8:20 PM, William Buchanan wrote: > > If you're using the - margins - command, the - post - option is clearly documented in the help file. Additionally, the Statalist FAQ suggests looking through the documentation as a good first step prior to querying the list. If this isn't the answer that you were looking for it may be a good idea to be more explicit about your end goal. > > HTH, > Billy > Sent from my iPhone On Jun 8, 2013, at 16:54, Vũ Võ <hoangvu@ueh.edu.vn> wrote: > Dear Sergiy, > > Thank you for your message. Probably, you misunderstand my question > nor I am clear in raising a question. > > For example: > > sysuse auto, clear > > reg price price wgt mpg c.wgt#c.wgt c.mpg#c.mpg c.wgt#c.mpg > > > Source | SS df MS Number of obs = 74 > -------------+------------------------------ F( 5, 68) = 12.84 > Model | 308384833 5 61676966.6 Prob > F = 0.0000 > Residual | 326680563 68 4804125.93 R-squared = 0.4856 > -------------+------------------------------ Adj R-squared = 0.4478 > Total | 635065396 73 8699525.97 Root MSE = 2191.8 > > ------------------------------------------------------------------------------ > price | Coef. Std. Err. t P>|t| [95% Conf. Interval] > -------------+---------------------------------------------------------------- > weight | -31.88985 9.148215 -3.49 0.001 -50.14483 -13.63487 > mpg | -3549.495 1126.464 -3.15 0.002 -5797.318 -1301.672 > | > c.weight#| > c.weight | .0034574 .0008629 4.01 0.000 .0017355 .0051792 > | > c.mpg#c.mpg | 38.74472 12.62339 3.07 0.003 13.55514 63.93431 > | > c.weight#| > c.mpg | .5421927 .1971854 2.75 0.008 .1487154 .9356701 > | > _cons | 92690.55 25520.53 3.63 0.001 41765.12 143616 > ------------------------------------------------------------------------------ > > So, I would like to calculate the marginal effects of weight and mpg > on price by using command: > margins, dydx(weight mpg) > > and get -> > > Average marginal effects Number of obs = 74 > Model VCE : OLS > > Expression : Linear prediction, predict() > dy/dx w.r.t. : weight mpg > > ------------------------------------------------------------------------------ > | Delta-method > | dy/dx Std. Err. z P>|z| [95% Conf. Interval] > -------------+---------------------------------------------------------------- > weight | .5360966 .6516553 0.82 0.411 -.7411243 1.813318 > mpg | -262.0502 103.681 -2.53 0.011 -465.2612 -58.83919 > ------------------------------------------------------------------------------ > > > My question is how to obtain or store in to scalar or whatever in > stata values of 0.5360966 and -262.0502 in the above table? > > If i use mfx , after that using matrix list e(b) as you suggested, it > will give me coefficients of the regression. To some extent, it can > say that these are marginal effets, but not what I would like to > obtain. > > Any suggestion are highly appreciated. Thanks > > Vu > > On 9 June 2013 00:07, Sergiy Radyakin <serjradyakin@gmail.com> wrote: >> type either 'return list' or 'ereturn list' (depending on the command) >> to see which results are saved after these commands. From what you >> describe you are interested in matrices r(b) and e(b) >> >> sysuse auto, clear >> generate lp=log(price) >> regress lp weight length headroom mpg >> mfx >> matrix list e(b) >> >> Best, Sergiy >> >> On Sat, Jun 8, 2013 at 4:50 PM, Vũ Võ <hoangvu@ueh.edu.vn> wrote: >>> Hi everyone, >>> >>> I am writing a code in do file to calculate the marginal effect of x >>> on y (log-linear model) >>> >>> The function form as following: >>> >>> Ln(Y) = a0 + a1*X + a2*X^2 + e ----> Y = exp(a0 + a1*X + a2*X^2) >>> >>> So, dY/dX = (a1 + 2*a2*X)*exp(a0 + a1*X + a2*X^2) eq(1) >>> >>> If I run the regression by command line as: >>> >>> reg lnY x c.x#c.x >>> >>> after that using margins command: >>> >>> margins, dydx(x). >>> >>> I will obtain the marginal effect of x on Ln(Y): (a1 + 2*a2*X). >>> >>> How can I calculate the dY/dX as in eq(1) by using the do file, not >>> calculating manually? >>> >>> >>> 2. Second question is: >>> >>> If I use command margins, predict(p) and obtain the results as following: >>> >>> Marginal effects after hetprob >>> y = Pr(y) (predict, p) >>> = .54284206 >>> ------------------------------------------------------------------------------ >>> variable | dy/dx Std. Err. z p>|z| [ 95% C.I. ] X >>> ---------+-------------------------------------------------------------------- >>> x | .3704576 .03237 11.44 0.000 .307015 .4339 .020114 >>> xhet | -.0736092 .02423 -3.04 0.002 -.121095 -.026124 .502716 >>> off1 | (offset1) 1.00516 >>> off2 | (offset2) 1.09709 >>> ------------------------------------------------------------------------------ >>> >>> The question is how to store the marginal effects value, say in this >>> case is 0.3704576, >>> >>> in the scalar in stata? >>> >>> >>> Thank you so much. >>> >>> Vo Duc Hoang Vu >>> * >>> * For searches and help try: >>> * http://www.stata.com/help.cgi?search >>> * http://www.stata.com/support/faqs/resources/statalist-faq/ >>> * http://www.ats.ucla.edu/stat/stata/ >> >> * >> * For searches and help try: >> * http://www.stata.com/help.cgi?search >> * http://www.stata.com/support/faqs/resources/statalist-faq/ >> * http://www.ats.ucla.edu/stat/stata/ > > > > -- > > Vo Duc Hoang Vu > Faculty of Development Economics > University of Economics HCMC > 1 A Hoang Dieu Street > Phu Nhuan Dist. Ho Chi Minh > Vietnam > Tel: +84 94 550 22 77 (VN); +31 624 63 29 45 (NL) > Fax: +84 8 38477948 (VN) > E-mail:hoangvu@ueh.edu.vn > http://www.voduchoangvu.net > > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/faqs/resources/statalist-faq/ > * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/

**References**:**st: How to store marginal effect value after using margin command?***From:*Vũ Võ <hoangvu@ueh.edu.vn>

**Re: st: How to store marginal effect value after using margin command?***From:*Sergiy Radyakin <serjradyakin@gmail.com>

**Re: st: How to store marginal effect value after using margin command?***From:*Vũ Võ <hoangvu@ueh.edu.vn>

**Re: st: How to store marginal effect value after using margin command?***From:*William Buchanan <william@williambuchanan.net>

- Prev by Date:
**Re: st: Interpretation of interaction term in log linear (non linear) model** - Next by Date:
**Re: st: RE: FW: Scientific Notation with svy** - Previous by thread:
**Re: st: How to store marginal effect value after using margin command?** - Next by thread:
**Re: Re: st: How to store marginal effect value after using margin command?** - Index(es):