2014-09-24 28 views
1

我想在一個邊際圖中繪製margins命令產生的邊距,但是從不同的margins估算中得出。重要的限制:這些係數在相同的最小和最大範圍內,因此是可比的。我怎麼做?Stata中的一個邊緣圖中的幾個邊距的圖形

這裏是一個代碼示例:

webuse nhanes2, clear 

tnbreg psu weight hdresult iron, iterate(5) // I am using this regression type so I stick with it here 

我知道,我可以把所有的利潤響應曲線在一個情節

margins, dydx(*) 
marginsplot, horizontal xline(0) yscale(reverse) recast(scatter) 

但事實上,我爲每個連續3個margins命令因爲我想比較一下這個迴歸因素是否會有所不同。代碼因此是

foreach var in weight hdresult iron { 
    * Procedure to get the numbers for margins right 
    quietly summarize `var ' 
    local max = r(max) 
    local step = round(r(max)/6) 

    quietly margins, at(`cvar'=(1(`step')`max')) 
    marginsplot, title("") ytitle("") 
} 

這給了我三個單獨的文件。但是當然,我需要用不同顏色的單個圖形中的所有線條。

任何建議如何做到這一點?

回答

1

基於@ RobertoFerrer的建議,使用combomarginsplot我現在騙這個包(感謝尼古拉冬):

webuse nhanes2, clear 

* Run regressions 
foreach var in weight hdresult iron { 
    * Trick: always regress on the same variable 
    gen testvar = `var' 

    * Any regression where testvar enters first - the identical variable will be omitted 
    tnbreg psu /// 
    testvar weight hdresult iron, iterate(5) 

    * Procedure to get the numbers for margins right 
    quietly summarize testvar 
    local max = r(max) 
    local step = round(r(max)/6) 

    * Margins post estimation 
    quietly margins, at(testvar=(1(`step')`max')) saving(margins_`var', replace) 

    * Drop testvar so that it can be reassigned within the loop 
    drop testvar 
} 

* Combine the margins graph information 
combomarginsplot margins_weight margins_hdresult margins_iron, labels("Weight" "HDrestul" "Iron") 

當然,它纔有意義比較係數的變量都在相同的範圍內。這個限制不是我原來的答案的一部分 - 對此抱歉。

5

使用combomarginsplot(和幫助文件):

sysuse auto, clear 

oprobit rep78 i.foreign mpg price weight 
margins foreign, at(mpg=(10(5)50)) predict(outcome(3)) saving(file1, replace) 

oprobit rep78 i.foreign mpg 
margins foreign, at(mpg=(10(5)50)) predict(outcome(3)) saving(file2, replace) 

oprobit rep78 i.foreign mpg gear 
margins foreign, at(mpg=(10(5)50)) predict(outcome(3)) saving(file3, replace) 

combomarginsplot file1 file2 file3, /// 
    labels("Full model" "Restricted model" "Gear Model") noci 

combomarginsplot是由尼古拉斯·溫特用戶編寫的命令。你可以安裝運行

ssc install combomarginsplot