You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在同一张图中绘制多组Probit回归的边际效应曲线?

Looks like you’ve got the series reversed in your plot—instead of one line per occupation, you’re getting one line per year dummy. That’s because combomarginsplot treats each year dummy from your margins, dydx(year*) output as a separate series, rather than grouping results by occupation group. Here are two straightforward fixes to get the plot you want:


Fix 1: Use coefplot for Clean, Direct Comparison

coefplot is built exactly for this scenario—it lets you plot marginal effects from multiple models (one per occupation) side by side, with each occupation as a distinct series. Here’s how to adjust your code:

* Run regressions and store marginal effects for each occupation
probit one_y_unemp year* MR* AG* if Qualifica2 == 1
margins, dydx(year*)
estimates store intern  // Save results for Interns

probit one_y_unemp year* MR* AG* if Qualifica3 == 1
margins, dydx(year*)
estimates store seniormanager  // Save for Senior Managers

probit one_y_unemp year* MR* AG* if Qualifica4 == 1
margins, dydx(year*)
estimates store whitecollar  // Save for White Collar

probit one_y_unemp year* MR* AG* if Qualifica5 == 1
margins, dydx(year*)
estimates store bluecollar  // Save for Blue Collar

probit one_y_unemp year* MR* AG* if Qualifica6 == 1
margins, dydx(year*)
estimates store juniormanager  // Save for Junior Managers

* Plot with coefplot: one line per occupation, x-axis = year dummies
coefplot intern seniormanager whitecollar bluecollar juniormanager, ///
    vertical ///
    connect(l)  // Connect points into continuous lines for each occupation
    xlabel(, angle(45))  // Rotate year labels to avoid overlap
    labels("Intern" "Senior Manager" "White Collar" "Blue Collar" "Junior Manager") ///
    xtitle("Year") ///
    ytitle("Marginal Effect of Year Dummy") ///
    legend(rows(1))  // Place legend in a single row for readability
    title("Marginal Effects by Occupation and Year")

Why this works:

  • estimates store saves the marginal effects from each margins command as a separate model object.
  • coefplot treats each stored model (occupation group) as a unique series, plotting all their year-specific marginal effects on the same x-axis. The connect(l) option turns discrete points into a smooth line for each occupation.

Fix 2: Manually Merge Data for Full Control

If you want more flexibility over the underlying data, you can export each occupation’s marginal effects, merge them into one dataset, then plot with twoway:

* First occupation group: Interns
probit one_y_unemp year* MR* AG* if Qualifica2 == 1
margins, dydx(year*)
gen occupation = "Intern"
tempfile intern
save `intern'

* Second group: Senior Managers
probit one_y_unemp year* MR* AG* if Qualifica3 == 1
margins, dydx(year*)
gen occupation = "Senior Manager"
tempfile seniormanager
save `seniormanager'

* Third group: White Collar
probit one_y_unemp year* MR* AG* if Qualifica4 == 1
margins, dydx(year*)
gen occupation = "White Collar"
tempfile whitecollar
save `whitecollar'

* Fourth group: Blue Collar
probit one_y_unemp year* MR* AG* if Qualifica5 == 1
margins, dydx(year*)
gen occupation = "Blue Collar"
tempfile bluecollar
save `bluecollar'

* Fifth group: Junior Managers
probit one_y_unemp year* MR* AG* if Qualifica6 == 1
margins, dydx(year*)
gen occupation = "Junior Manager"
tempfile juniormanager
save `juniormanager'

* Merge all datasets into one
use `intern', clear
append using `seniormanager' `whitecollar' `bluecollar' `juniormanager'

* Extract year number from margin variable names (adjust if your dummies are named differently)
gen year = real(substr(_margin, 4, .))  // Works for dummies like year2000, year2001, etc.

* Plot all occupations on one graph
twoway line _margin year if occupation=="Intern" || ///
       line _margin year if occupation=="Senior Manager" || ///
       line _margin year if occupation=="White Collar" || ///
       line _margin year if occupation=="Blue Collar" || ///
       line _margin year if occupation=="Junior Manager", ///
       legend(lab(1 "Intern") lab(2 "Senior Manager") lab(3 "White Collar") lab(4 "Blue Collar") lab(5 "Junior Manager")) ///
       xtitle("Year") ///
       ytitle("Marginal Effect") ///
       title("Marginal Effects by Occupation and Year")

* Optional: Use `by(occupation)` to create separate panels per occupation
* twoway line _margin year, by(occupation) xtitle("Year") ytitle("Marginal Effect")

Notes:

  • Adjust the substr command if your year dummies aren’t named yearYYYY (e.g., use substr(_margin, 2, .) for dummies like y2000).
  • The by(occupation) option creates separate panels for each group, which can be easier to read if lines overlap heavily.

内容的提问来源于stack exchange,提问作者Vincenzoalfano

火山引擎 最新活动