I love the margins option but it can also be a source of frustration. For example, in perfectly well-behaved nonlinear models, such as logit or poisson with lots of dummy variables and interactions, I find myself having to use the "noestimcheck" option to get the average partial effects and standard errors. Probably there's a solution that I'm not aware of. I can live with that because it appears to produce the correct estimates when I've checked. My latest discovery about margins makes it very inefficient to do a simulation study using even logit or poisson.
This has nothing to do with a particular data set. I've tried it on several generated data sets and with different nonlinear estimation commands. I'll describe the problem first and then show Stata commands/output. If I use margins after, say, logit, and want to evaluate the effect at different settings of the covariates, it seems to work fine as long as I don't use the "post" option. But in a simulation I've always had to use the "post" option to access the estimated APEs. When I use that option to get a second APE at another setting of the covariates, I get the message "option dydx() not allowed" -- when, of course, I know that option is allowed. If I reestimate the model then the problem goes away. But that means that if I want, say, six APEs then I need to estimate the model six times. Clearly this shouldn't be necessary and it's a big hurdle for simulations. If I don't use "post" the problem doesn't arise, but then I can't (or don't know how) to dynamically retrieve the APEs.
This has become a hurdle in my recent work on nonlinear difference-in-differences estimation.
Here's a simple example with T = 2 generated panel data. Without "post" everything is fine. If I reestimate the model then the problem goes way, but that can't be intended. There must be something I don't know about margins and post. Do I have to somehow drop the old coefficient stored in _b[1.w] before computing the next marginal effect? Appreciate any help!
This has nothing to do with a particular data set. I've tried it on several generated data sets and with different nonlinear estimation commands. I'll describe the problem first and then show Stata commands/output. If I use margins after, say, logit, and want to evaluate the effect at different settings of the covariates, it seems to work fine as long as I don't use the "post" option. But in a simulation I've always had to use the "post" option to access the estimated APEs. When I use that option to get a second APE at another setting of the covariates, I get the message "option dydx() not allowed" -- when, of course, I know that option is allowed. If I reestimate the model then the problem goes away. But that means that if I want, say, six APEs then I need to estimate the model six times. Clearly this shouldn't be necessary and it's a big hurdle for simulations. If I don't use "post" the problem doesn't arise, but then I can't (or don't know how) to dynamically retrieve the APEs.
This has become a hurdle in my recent work on nonlinear difference-in-differences estimation.
Here's a simple example with T = 2 generated panel data. Without "post" everything is fine. If I reestimate the model then the problem goes way, but that can't be intended. There must be something I don't know about margins and post. Do I have to somehow drop the old coefficient stored in _b[1.w] before computing the next marginal effect? Appreciate any help!
Code:
. logit y i.w x i.w#c.x f02 Iteration 0: log likelihood = -686.40401 Iteration 1: log likelihood = -660.20325 Iteration 2: log likelihood = -660.18981 Iteration 3: log likelihood = -660.18981 Logistic regression Number of obs = 1,000 LR chi2(4) = 52.43 Prob > chi2 = 0.0000 Log likelihood = -660.18981 Pseudo R2 = 0.0382 ------------------------------------------------------------------------------ y | Coefficient Std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- 1.w | -1.06079 .3509639 -3.02 0.003 -1.748667 -.3729136 x | .0157663 .1231613 0.13 0.898 -.2256253 .2571579 | w#c.x | 1 | .326336 .2400305 1.36 0.174 -.1441151 .7967871 | f02 | 1.031331 .151108 6.83 0.000 .7351643 1.327497 _cons | -.6603075 .1491261 -4.43 0.000 -.9525892 -.3680257 ------------------------------------------------------------------------------ . margins, dydx(w) at(f02 = 1) Average marginal effects Number of obs = 1,000 Model VCE: OIM Expression: Pr(y), predict() dy/dx wrt: 1.w At: f02 = 1 ------------------------------------------------------------------------------ | Delta-method | dy/dx std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- 1.w | -.1857112 .0502812 -3.69 0.000 -.2842605 -.087162 ------------------------------------------------------------------------------ Note: dy/dx for factor levels is the discrete change from the base level. . margins, dydx(w) at(f02 = 0) Average marginal effects Number of obs = 1,000 Model VCE: OIM Expression: Pr(y), predict() dy/dx wrt: 1.w At: f02 = 0 ------------------------------------------------------------------------------ | Delta-method | dy/dx std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- 1.w | -.1438547 .033023 -4.36 0.000 -.2085786 -.0791309 ------------------------------------------------------------------------------ Note: dy/dx for factor levels is the discrete change from the base level. . margins, dydx(w) at(f02 = 1) post Average marginal effects Number of obs = 1,000 Model VCE: OIM Expression: Pr(y), predict() dy/dx wrt: 1.w At: f02 = 1 ------------------------------------------------------------------------------ | Delta-method | dy/dx std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- 1.w | -.1857112 .0502812 -3.69 0.000 -.2842605 -.087162 ------------------------------------------------------------------------------ Note: dy/dx for factor levels is the discrete change from the base level. . di _b[1.w] -.18571124 . margins, dydx(w) at(f02 = 0) post option dydx() not allowed r(198); . qui logit y i.w x i.w#c.x f02 . margins, dydx(w) at(f02 = 0) post Average marginal effects Number of obs = 1,000 Model VCE: OIM Expression: Pr(y), predict() dy/dx wrt: 1.w At: f02 = 0 ------------------------------------------------------------------------------ | Delta-method | dy/dx std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- 1.w | -.1438547 .033023 -4.36 0.000 -.2085786 -.0791309 ------------------------------------------------------------------------------ Note: dy/dx for factor levels is the discrete change from the base level.
Comment