stata - 如何从 xtlogit 模型中重现平均边际效应

标签 stata logistic-regression random-effects marginal-effects

我有兴趣从随机效应 logit 模型(使用 xtlogit 在 Stata 中运行)再现平均边际效应。我了解如何使用 Delta 方法从 logit 模型中重现平均边际效应。例如,在下面的代码中,我成功地重现了 margins 中报告的 age 的平均边际效应。

*** Stata code
* download data
webuse union, clear

* calculate delta and copy variable of interest - age
sum age
gen xdelta = r(sd)/1000
clonevar age_ = age

* run logit model and calculate average marginal effect using margins
logit union age_
margins,dydx(age_)

* calculate average marginal effect by hand - mean of xme equals result from margins above
predict p1
replace age_ = age_+xdelta
predict p2
gen xme = (p2 - p1) / xdelta
sum xme

* calculate average marginal effect at fixed value of age using margins
margins,at(age=(16))

* calculate average marginal effect by hand - mean of p3 equals result from margins above
replace age_ = 16
predict p3
sum p3

我遇到困难的地方是重现 xtlogit 模型的平均边际效应。

*** Stata code
* download data and designate panel variables
webuse union, clear
xtset idcode year

* run xtlogit model
xtlogit union age

* calculate average marginal effects - can't figure out how to reproduce these estimates :(
margins, dydx(*)
margins, at(age=(16))

非常感谢您提供有关如何重现 xtlogit 边际效应的任何帮助。谢谢!

----- 编辑以更清楚地表明我有兴趣重现 margins

报告的估计值

最佳答案

有几种方法可以做到这一点,但本质上问题归结为事实

$$\Pr(y_{it}=1\vert x_{it})=\int\Lambda(u_i + x_{it}'\beta)\cdot\varphi(0,\sigma_u^2) du_i $$

其中 $\varphi()$ 是正态密度。在您的代码中,您有效地将随机效应 $u_i$ 设置为零(这就是 predict(pu0) 所做的)。这会将 RE 设置为其平均值,这可能不是您想要的。当然,$u_i$ 不会被xtlogit, re 观察到甚至估计,所以如果你想复制predict(pr) 所做的事情,你需要整合随机effect out 使用估计的方差得到无条件概率。

在 Stata 中执行此操作的一种方法是使用用户编写的 integrate 命令来执行一维数值积分,如下所示:

webuse union, clear
xtset idcode year
xtlogit union age, nolog
margins, at(age=(16)) predict(pr)
margins, dydx(*) at(age=16) predict(pr)
capture ssc install integrate
/* phat at age 16 */
integrate, f(invlogit(x - 3.0079682 + .01929225*16)*normalden(x,0,2.477537654945496)) l(-10) u(10) vectorise quadpts(1000)
/* ME at age 16 */
integrate, f(invlogit(x - 3.0079682 + .01929225*16)*(1-invlogit(x - 3.0079682 + .01929225*16))*(.01929225)*normalden(x,0,2.477537654945496)) l(-10) u(10) vectorise quadpts(1000)

如果您使用实际系数(如 _b[_cons] 和 e(sigma_u))而不是粘贴值,您可能会获得更好的精度。

使用 Mata、Python 甚至模拟方法可能有更有效的方法来执行此操作,但我将把它留给您来解决。您还可以使用 xtmelogit 获得 RE 的后验模态估计。

关于stata - 如何从 xtlogit 模型中重现平均边际效应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64656664/

相关文章:

R 相当于 Stata 本地或全局宏

正则表达式 - 字符串帮助 - Stata

database - 导出 SPSS 变量标签

python - Python 中的多元逻辑回归显示错误

machine-learning - 为什么 keras 比 sklearn 慢很多?

sas - SAS中的单向随机效应方差分析:PROC GLM还是MIXED?

r - 在 R 中指定 3 级随机截距

r - R和Stata的合并命令比较

machine-learning - 逻辑回归分类器的 Bootstrap 聚合(装袋)

r - 使用 qqmath 或 dotplot : How to make it look fancy? 绘制来自 lmer(lme4 包)的随机效应