了解随机化检查中本地命令的使用

发布于 2025-01-12 11:49:27 字数 1933 浏览 0 评论 0原文

我正在尝试运行 RCT 的 do 文件。作者使用的是 local 命令。

他们创建一个值为 1 的本地宏。基于此,他们为 var 变量创建缺失值。然而,在那之后,我不明白为什么他们要加1。此外,为什么 numvarnumvarse 用于平均值和标准差?

任何解释都会非常有帮助。

代码:

*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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫瑟鸿黎 2025-01-19 11:49:27

只需看几个命令就可以明白原理。

  local numvar=1

  foreach var in ...  {
                
    
    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'  
    
    local numvar = `numvarse' + 1   

}

numvar 初始化为 1。在循环内,numvarse 设置为比 numvar 大 1。

然后,代码将一系列变量中每个变量的结果放入观察值 numvarnumvarse 中,包括变量的名称及其平均值和各个组的 SD。

关键是要关注

  replace ... in ...

in 限定符仅在该观察中指定 replace 的位置。

在循环结束时,numvar 再次发生碰撞。

简而言之,第一个名为 go 的变量的结果出现在观察 1 和 2 中;对于观察 3 和 4 中名为 go 的第二个变量;等等。

至少,这个过程取决于

  1. 有足够的观察结果才能实现这一点。我数了 13 个变量,因此数据集中至少有 26 个观测值,这似乎是极有可能的。

  2. 据了解,写入的变量与任何其他变量对齐。

有些人可能会发现第二种甚至第一种糟糕的工作方式。他们希望将结果写入新的数据集或新的框架

该代码并不是最先进的:自 Stata 11 (2009) 以来,使用 xixtreg 已经过时,但仍然适用于许多用途。

(详细信息:如果单独有一个变量 miss_X 指示变量 X 丢失,则代码会覆盖变量 X 中的值。我认为任何远程观察者都无法解释这一点。)

The principle can be understood by looking at just a few commands.

  local numvar=1

  foreach var in ...  {
                
    
    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'  
    
    local numvar = `numvarse' + 1   

}

numvar is initialised to 1. Within the loop, numvarse is set to 1 more than numvar.

The code then puts results for each of a series of variables in observations numvar and numvarse, including the name of the variable AND its mean and SD for various groups.

The key is to focus on

  replace ... in ...

where the in qualifier specifies replace 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

  1. 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.

  2. 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 with xtreg 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文