了解随机化检查中本地命令的使用
我正在尝试运行 RCT 的 do 文件。作者使用的是 local
命令。
他们创建一个值为 1 的本地宏。基于此,他们为 var 变量创建缺失值。然而,在那之后,我不明白为什么他们要加1。此外,为什么 numvar
和 numvarse
用于平均值和标准差?
任何解释都会非常有帮助。
代码:
*RANDOMIZATION CHECK
local numvar=1
foreach var in T1_Client_Age T1_Client_Married T1_HH_Size T1_Client_Literate T1_Client_Education T1_muslim ///
T1_Hindu_SC_Kat T1_rest3 T1_Log_HH_Income T1_Household_Business ///
T1_Client_Wage_Salaried_2 T1_Client_Bus_PR_Employed_2 T1_Client_Housewife_2 {
replace `var'=. if miss_`var'==1
local numvarse = `numvar' + 1
replace varname="`var'" in `numvar'
*MEAN AND SD FOR CONTROL
quietly: sum `var' if Treated_All==0
replace Control=r(mean) in `numvar'
replace Control=r(sd) in `numvarse'
*MEAN AND SD FOR TREATED
quietly:sum `var' if Treated_All==1
replace Treat=r(mean) in `numvar'
replace Treat=r(sd) in `numvarse'
*MEAN AND SD FOR TREATED WITH FRIEND
quietly:sum `var' if Treatment_Peer==1
replace Treat_Peer=r(mean) in `numvar'
replace Treat_Peer=r(sd) in `numvarse'
*MEAN DIFFERENCES BETWEEN TREATED AND CONTROL
xi:reg `var' Treated_All i.sewa_center*i.baseline i.t_month , cluster(t_group)
replace Diff_Control_Treat=_b[Treated_All] in `numvar'
replace Diff_Control_Treat=_se[Treated_All] in `numvarse'
*MEAN DIFFERENCES BETWEEN TREATED ALONE AND WITH FRIEND
xi:reg `var' Treated_All Treatment_Peer i.sewa_center*i.baseline i.t_month , cluster(t_group)
replace Diff_Alone_Peer=_b[Treatment_Peer] in `numvar'
replace Diff_Alone_Peer=_se[Treatment_Peer] in `numvarse'
local numvar = `numvarse' + 1
}
I am trying to run a do-file for an RCT. The authors are using the local
command.
They create a local macro taking the value 1. Based on that, they are creating missing values for the var variable. However, after that, I cannot understand why they are adding 1 to it. Moreover, why are the numvar
and numvarse
being used for mean and sd?
Any explanation would be really helpful.
The code:
*RANDOMIZATION CHECK
local numvar=1
foreach var in T1_Client_Age T1_Client_Married T1_HH_Size T1_Client_Literate T1_Client_Education T1_muslim ///
T1_Hindu_SC_Kat T1_rest3 T1_Log_HH_Income T1_Household_Business ///
T1_Client_Wage_Salaried_2 T1_Client_Bus_PR_Employed_2 T1_Client_Housewife_2 {
replace `var'=. if miss_`var'==1
local numvarse = `numvar' + 1
replace varname="`var'" in `numvar'
*MEAN AND SD FOR CONTROL
quietly: sum `var' if Treated_All==0
replace Control=r(mean) in `numvar'
replace Control=r(sd) in `numvarse'
*MEAN AND SD FOR TREATED
quietly:sum `var' if Treated_All==1
replace Treat=r(mean) in `numvar'
replace Treat=r(sd) in `numvarse'
*MEAN AND SD FOR TREATED WITH FRIEND
quietly:sum `var' if Treatment_Peer==1
replace Treat_Peer=r(mean) in `numvar'
replace Treat_Peer=r(sd) in `numvarse'
*MEAN DIFFERENCES BETWEEN TREATED AND CONTROL
xi:reg `var' Treated_All i.sewa_center*i.baseline i.t_month , cluster(t_group)
replace Diff_Control_Treat=_b[Treated_All] in `numvar'
replace Diff_Control_Treat=_se[Treated_All] in `numvarse'
*MEAN DIFFERENCES BETWEEN TREATED ALONE AND WITH FRIEND
xi:reg `var' Treated_All Treatment_Peer i.sewa_center*i.baseline i.t_month , cluster(t_group)
replace Diff_Alone_Peer=_b[Treatment_Peer] in `numvar'
replace Diff_Alone_Peer=_se[Treatment_Peer] in `numvarse'
local numvar = `numvarse' + 1
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需看几个命令就可以明白原理。
numvar
初始化为 1。在循环内,numvarse
设置为比numvar
大 1。然后,代码将一系列变量中每个变量的结果放入观察值
numvar
和numvarse
中,包括变量的名称及其平均值和各个组的 SD。关键是要关注
in
限定符仅在该观察中指定replace
的位置。在循环结束时,
numvar
再次发生碰撞。简而言之,第一个名为 go 的变量的结果出现在观察 1 和 2 中;对于观察 3 和 4 中名为 go 的第二个变量;等等。
至少,这个过程取决于
据了解,写入的变量不与任何其他变量对齐。
有些人可能会发现第二种甚至第一种糟糕的工作方式。他们希望将结果写入新的数据集或新的
框架
。该代码并不是最先进的:自 Stata 11 (2009) 以来,使用
xi
和xtreg
已经过时,但仍然适用于许多用途。(详细信息:如果单独有一个变量
miss_
X 指示变量 X 丢失,则代码会覆盖变量 X 中的值。我认为任何远程观察者都无法解释这一点。)The principle can be understood by looking at just a few commands.
numvar
is initialised to 1. Within the loop,numvarse
is set to 1 more thannumvar
.The code then puts results for each of a series of variables in observations
numvar
andnumvarse
, including the name of the variable AND its mean and SD for various groups.The key is to focus on
where the
in
qualifier specifiesreplace
only in that observation.Towards the end of the loop
numvar
is bumped yet again.In short, results for the first variable named go in observations 1 and 2; for the second variable named go in observations 3 and 4; and so on.
Minimally, this process depends on
There being enough observations for this to be possible. I count 13 variables, so there must be at least 26 observations in the dataset, which seems overwhelmingly likely.
There being an understanding that the variables being written to are not aligned with any other variables.
Some might find #2 and even #1 poor ways of working. They would want results to be written to a new dataset or to a new
frame
.The code is not state of the art: using
xi
withxtreg
has been outdated practice since Stata 11 (2009), but still works for many purposes.(Detail: The code overwrites values in variable X with missing if separately there is a variable
miss_
X indicating that variable X is missing. I don't think any remote observers can explain that.)