我正在尝试在 STata 中创建一个循环来创建 coefplots,它显示总体子组的多重回归系数。每个图都会使用不同的回归量。
我遇到了使用循环引用的问题。理想情况下,每个图表都应该像我所包含的图片一样。
sysuse auto, clear
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'
}
问题出在你的定义上
global list_graphing "trunk" "length"
Stata 删除最外面的引号作为分隔符,并且您的宏保持不变
trunk" "length
这会导致下游出现问题。如果您需要在全局宏内有匹配的引号,那么您的定义应该是
global list_graphing `" "trunk" "length" "'
但正如已经回答的那样,这里不需要任何引号。