我是 Stata 的新手,尽管我对 Python 和 R 有一些经验。我正在尝试创建一个循环来创建 coefplots,它显示总体子组的多重回归系数。每个图都会使用不同的回归量。尽管我遇到了使用循环的引用问题。理想情况下,每个图表都应该像我所包含的图片一样。
replace foreign = 3 if (rep78 == 3)
regress price mpg length if foreign == 1
estimates store mpg_foreign_1_length
regress price mpg length if foreign == 0
estimates store mpg_foreign_0_length
regress price mpg length if foreign == 3
estimates store mpg_foreign_3_length
regress price mpg trunk if foreign == 1
estimates store mpg_foreign_1_trunk
regress price mpg trunk if foreign == 0
estimates store mpg_foreign_0_trunk
regress price mpg trunk if foreign == 3
estimates store mpg_foreign_3_trunk
global list_graphing "trunk" "length"
foreach x of global list_graphing {
coefplot "mpg_foreign_3_`x' mpg_foreign_1_`x'" ||, drop(_cons)
}
foreach x of global list_graphing {
coefplot "mpg_foreign_3_`x' mpg_foreign_1_`x' mpg_foreign_0_`x'" ||, drop(_cons)
}
//invalid something: quotes do not match
foreach x of global list_graphing {
coefplot "mpg_foreign_3_`x'" "mpg_foreign_1_`x'" "mpg_foreign_0_`x'"
}
第一张图基本上显示了我正在尝试创建的内容;但是,当添加第三个变量时它不起作用。我无法用引号将它们分开,因为正如代码清楚显示的那样,这使得系数不再位于同一个图表上。任何帮助理解/解决这个问题的帮助将不胜感激。
我对
coefplot
没有太多经验,但如果你不使用引号,这似乎是有效的(事实上,我有点惊讶它有时确实可以使用引号)。
sysuse auto
replace foreign = 3 if (rep78 == 3)
regress price mpg length if foreign == 1
estimates store mpg_foreign_1_length
regress price mpg length if foreign == 0
estimates store mpg_foreign_0_length
regress price mpg length if foreign == 3
estimates store mpg_foreign_3_length
regress price mpg trunk if foreign == 1
estimates store mpg_foreign_1_trunk
regress price mpg trunk if foreign == 0
estimates store mpg_foreign_0_trunk
regress price mpg trunk if foreign == 3
estimates store mpg_foreign_3_trunk
global list_graphing trunk length
foreach x of global list_graphing {
coefplot mpg_foreign_3_`x' mpg_foreign_1_`x' ||, drop(_cons)
}
foreach x of global list_graphing {
coefplot mpg_foreign_3_`x' mpg_foreign_1_`x' mpg_foreign_0_`x' ||, drop(_cons)
}
foreach x of global list_graphing {
coefplot mpg_foreign_3_`x' mpg_foreign_1_`x' mpg_foreign_0_`x'
}