我正在使用coefplotStata 中的包来绘制系数如何根据所采用的模型而变化。我特别想看看利息系数是如何随时间变化的。我正在垂直绘制它,因此 x 轴可以显示每个系数所指的年份。但是,我无法相应地标记 x 轴(而不是显示我感兴趣的变量的名称x1,它应该说明 1、2 和 3。我还想通过使用选项来省略图例,legend(off)但是不起作用。
这是我正在使用的代码:
reg y x1 x2 if year==1;
estimates store t1;
reg y x1 x2 if year==2;
estimates store t2;
reg y x1 x2 if year==3;
estimates store t3;
coefplot t1 t2 t3, drop(x2) vertical yline(0);
Run Code Online (Sandbox Code Playgroud)
任何建议将不胜感激。
使用两个类别(而不是时间)的无意义示例可以很容易地进行调整:
clear
set more off
sysuse auto
reg price weight rep78 if foreign
estimates store foreign
reg price weight rep78 if !foreign
estimates store not_foreign
matrix at = (1 / 2)
coefplot foreign || not_foreign, drop(rep78 _cons) vertical bycoefs
Run Code Online (Sandbox Code Playgroud)
您可以在循环中构建语法,使用local. 然后喂给coefplot. 准确地说,我的意思是类似于示例语法:
year1 || year2 || ... || yearn
Run Code Online (Sandbox Code Playgroud)
最后的命令看起来像:
coefplot `allyears', drop(<some_stuff>) vertical bycoefs
Run Code Online (Sandbox Code Playgroud)
一个涉及时间的完整示例:
clear
set more off
use http://www.stata-press.com/data/r12/nlswork.dta
forvalues i = 70/73 {
regress ln_w grade age if year == `i'
estimates store year`i'
local allyears `allyears' year`i' ||
local labels `labels' `i'
}
// check
display "`allyears'"
display `"`labels'"'
coefplot `allyears', keep(grade) vertical bycoefs bylabels(`labels')
Run Code Online (Sandbox Code Playgroud)
如果结果coefplot证明不够灵活,您可以随时尝试使用statsby和graph命令 ( help graph)。