Code:
use https://www.stata-press.com/data/r17/margex quietly logistic outcome treatment##group age c.age#c.age treatment#c.age margins, dydx(treatment)
I have tried to manually calculate this marginal effect using the following code.
First I take the average prediction, in log-odds scale, with treatment=1. Then I take the average prediction, in log-odds scale, with treatment=0.
Then I subtract the latter from the former.
Then I convert log-odds in odds and then in probabilities.
But the result is nowhere near what I obtain with margins.
What am I doing wrong?
Code:
quietly capture drop yhat
quietly capture drop treatment_bk
quietly gen treatment_bk = treatment
quietly replace treatment = 1
quietly predict yhat, xb
quietly sum yhat
global lo1 = r(mean)
quietly capture drop yhat
quietly replace treatment = 0
quietly predict yhat, xb
quietly sum yhat
global lo0 = r(mean)
quietly capture drop yhat
global mar = (${lo1} - ${lo0})
disp exp(${mar})/(1+exp(${mar}))
quietly capture drop treatment
quietly capture drop yhat
quietly rename treatment_bk treatment
0 Response to Failing to replicate margins example 16: Average marginal effect (partial effects)
Post a Comment