如何计算 probit 和 tobit 模型的边际效应?

昨天有个小伙伴问了这样一个问题:

如何计算 probit 和 tobit 的边际效应呢?

这个问题非常简单,首先我们来看看 chatGPT 的回答:

基本正确,使用 margins 命令即可,不过它给的这个例子是运行不通的:

clear all
input x1 x2 y
1 0 0
1 1 1
0 0 0
0 1 0
end
probit y x1 x2

*> note: x1 != 1 predicts failure perfectly;
*> x1 omitted and 2 obs not used.

*> outcome = x2 > 0 predicts data perfectly
*> r(2000);

margins x1 x2, dydx(*)

实际上在之前系列课程「Stata 编程导论」的课时“Stata 编程案例合集(三)”中讲过这个问题。

使用 marginsplot 展示边际效应

在附件中有一个 airquality.dta 数据。

use airquality, clear
sum so2, detail
gen hiso2 = (so2 > r(p50)) & !missing(so2)
sum precip, meanonly
gen hiprecip = (precip > r(mean)) & !missing(precip)

probit hiso2 pop manuf i.hiprecip
sum manuf if e(sample)
margins, dydx(manuf) at(manuf = (50(50)750))
marginsplot

经常我们会需要对绘图结果进行各种修饰和调整,这个时候可能就不能使用 marginsplot 绘图了。因此我们还需要掌握如何手动计算结果并使用 twoway 直接绘制边际效应图:

* 不使用 marginsplot 如何绘制 margins plot
margins, dydx(manuf) at(manuf = (50(50)750))
ret list
* ssc install matrixtools
clear
matrix2stata r(table)
label list r_table__names
tostring r_table__names, replace force
egen var = msub(r_table__names), f(1 2 3 4 5 6 7 8 9) r(b crit df eform ll pvalue se ul z)
order var
drop r_table*
gather manuf*
spread var value
gen x = ustrregexs(0) if ustrregexm(var, "\d+")
order x
drop var
destring x, replace
replace x = x * 50

* 绘制 margins plot
gsort x
tw conn b x || rcap ll ul x, color(black) ///
title(Average Marginal Effects of manuf with 95% CIs) ///
xti("Mfg. workers, 000") yti("Effects on Pr(Hiso2)") ///
xlab(50(50)750) leg(off) ylab(, format(%6.4f))

关于这些代码的详细介绍,大家可以观看系列课程「Stata 编程导论」的课时“Stata 编程案例合集(三)”中的视频讲解学习。

×

点击这里跳转到 RStata 短书平台获取附件:如何计算 probit 和 tobit 模型的边际效应?

评论