满足条件时创建指标标记

时间:2018-09-08 17:11:58

标签: stata

我想找到一种在行之间创建指标标记的方法,这样,一旦满足条件,该标记就可以在组内的所有案例中持久存在。

在下面的示例数据中,我有一个变量_p,该变量定义了_mar级别之间的_m中的值比较的统计显着性。我还有一个分组变量_g,它指示比较是在一个组内进行的。

变量_f_s_f_n代表我想要的最终结果。

clear

input _mar _m  _p  _g  _f_s  _f_n 
2.99    0   0.00000    0   1  0       
3.03    1   0.00000    0   1  0       
3.05    2   0.00000    0   1  1       
3.06    3   0.22179    0   0  1       
3.07    4   0.18044    0   0  1       
3.07    5   0.58009    0   0  1       
3.06    6   0.40620    0   0  1       
3.06    7   0.47257    0   0  1       
3.06    8   0.91196    0   0  1       
3.05    9   0.68560    0   0  1       
2.65    0   0.00000    1   1  0       
2.70    1   0.00000    1   1  0       
2.73    2   0.00103    1   1  0       
2.75    3   0.00944    1   1  1       
2.75    4   0.64713    1   0  1       
2.76    5   0.55476    1   0  1       
2.77    6   0.32807    1   0  1       
2.78    7   0.03271    1   0  1       
2.78    8   0.00219    1   0  1       
2.79    9   0.57361    1   0  1              
end    

我想使用该标志在图形中指示统计显着性“停止”并忽略其他比较值的位置。

下面您还可以找到到目前为止我尝试过的代码:

代码段1-图形有效,线的结构符合要求

 snapshot save, label("import")
 snapshot list 

 twoway ///
 (line _mar _m if _g == 0 & _f_s==1, lcolor(orange) lpattern(solid)) ///
 (line _mar _m if _g == 0 & _f_n==1, lcolor(orange) lpattern(dash )) ///
 (scatter _mar _m if _g == 0, mcolor(orange) msymbol(o) mlabel(_mar) mlabcolor(orange) mlabsize(vsmall) mlabposition(11)) ///
  ///
 (line _mar _m if _g == 1 & _f_s==1, lcolor(blue*2) lpattern(solid)) ///
 (line _mar _m if _g == 1 & _f_n==1, lcolor(blue*2) lpattern(dash )) ///
 (scatter _mar _m if _g == 1, mcolor(blue*2) msymbol(o) mlabel(_mar) mlabcolor(blue*2) mlabsize(vsmall) mlabposition(11)) ///
, legend(off)   ///
xlabel(-1(1)9 -1 " " 0 "0 " 9 "9+" ) ///
ylabel(2.5(0.10)3.5, angle(horizontal) format(%5.2f) ) ymlabel(2.5(0.10)3.5, grid nolabel) ///      
xtitle( "Levels" ) ytitle("Adjusted First Year GPA", height(8) ) ///
name(good)

代码段2-图形起作用,线 not 不能按要求构造

snapshot restore 1 

sort _g _m
gen x_f_s = (_p <= .05) 
replace x_f_s = 0 if x_f_s ==1 & x_f_s[_n-1]==0 & x_f_s[_n+1]==0
replace x_f_s = 1 if _m == 0
gen x_f_n = x_f_s == 0
replace x_f_n = 1 if x_f_s ==1 & x_f_s[_n+1]==0

/*****  the created flags are not correct  *****/
list, sepby(_g)

 twoway ///
 (line _mar _m if _g == 0 & x_f_s==1, lcolor(orange) lpattern(solid)) ///
 (line _mar _m if _g == 0 & x_f_n==1, lcolor(orange) lpattern(dash )) ///
 (scatter _mar _m if _g == 0, mcolor(orange) msymbol(o) mlabel(_mar) mlabcolor(orange) mlabsize(vsmall) mlabposition(11)) ///
  ///
 (line _mar _m if _g == 1 & x_f_s==1, lcolor(blue*2) lpattern(solid)) ///
 (line _mar _m if _g == 1 & x_f_n==1, lcolor(blue*2) lpattern(dash )) ///
 (scatter _mar _m if _g == 1, mcolor(blue*2) msymbol(o) mlabel(_mar) mlabcolor(blue*2) mlabsize(vsmall) mlabposition(11)) ///
, legend(off)   ///
xlabel(-1(1)9 -1 " " 0 "0 " 9 "9+" ) ///
ylabel(2.5(0.10)3.5, angle(horizontal) format(%5.2f) ) ymlabel(2.5(0.10)3.5, grid nolabel) ///      
xtitle( "Levels" ) ytitle("Adjusted First Year GPA", height(8) ) ///
name(not_good)

我尝试计算的变量用x_f_sx_f_n标注。

当没有随后的统计比较非常重要时,这些标志将起作用。但是,如果在最初的“停止”之后进行重大比较,则绘图将不起作用。

还应该有第二个标志,指示“无意义”从哪里开始。这将以与第一个标志类似的方式进行。

我使用实线和虚线指示存在重要性的位置,然后停止。

最终,我想在组内创建标记以进行绘图。

1 个答案:

答案 0 :(得分:1)

这就是我要做的:

bysort _g (_m): generate x_f_s = (_p <= .05) 
bysort _g (_m): generate x_f_n = x_f_s == 0

list, sepby(_g)

     +-------------------------------------------------------+
     | _mar   _m       _p   _g   _f_s   _f_n   x_f_s   x_f_n |
     |-------------------------------------------------------|
  1. | 2.99    0        0    0      1      0       1       0 |
  2. | 3.03    1        0    0      1      0       1       0 |
  3. | 3.05    2        0    0      1      1       1       0 |
  4. | 3.06    3   .22179    0      0      1       0       1 |
  5. | 3.07    4   .18044    0      0      1       0       1 |
  6. | 3.07    5   .58009    0      0      1       0       1 |
  7. | 3.06    6    .4062    0      0      1       0       1 |
  8. | 3.06    7   .47257    0      0      1       0       1 |
  9. | 3.06    8   .91196    0      0      1       0       1 |
 10. | 3.05    9    .6856    0      0      1       0       1 |
     |-------------------------------------------------------|
 11. | 2.65    0        0    1      1      0       1       0 |
 12. |  2.7    1        0    1      1      0       1       0 |
 13. | 2.73    2   .00103    1      1      0       1       0 |
 14. | 2.75    3   .00944    1      1      1       1       0 |
 15. | 2.75    4   .64713    1      0      1       0       1 |
 16. | 2.76    5   .55476    1      0      1       0       1 |
 17. | 2.77    6   .32807    1      0      1       0       1 |
 18. | 2.78    7   .03271    1      0      1       1       0 |
 19. | 2.78    8   .00219    1      0      1       1       0 |
 20. | 2.79    9   .57361    1      0      1       0       1 |
     +-------------------------------------------------------+

这是使第一条规则的应用自动化的方法:

bysort _g (_m): generate x_f_s = (_p <= .05) 

clonevar tag = x_f_s

local i 1
while `i'== 1 {
    capture noisily {
        bysort _g (_m): assert x_f_s == 0  if _p <= .05 & (tag == 1 & tag[_n-1] == 0)
    }
    if _rc {
        bysort _g (_m): replace x_f_s = 0 if _p <= .05 & (tag == 1 & tag[_n-1] == 0)
        drop tag
        clonevar tag = x_f_s                            
    }
    else local i 0
}

drop tag

哪个为x_f_s产生所需的输出:

list

     +-----------------------------------------------+
     | _mar   _m       _p   _g   _f_s   _f_n   x_f_s |
     |-----------------------------------------------|
  1. | 2.99    0        0    0      1      0       1 |
  2. | 3.03    1        0    0      1      0       1 |
  3. | 3.05    2        0    0      1      1       1 |
  4. | 3.06    3   .22179    0      0      1       0 |
  5. | 3.07    4   .18044    0      0      1       0 |
     |-----------------------------------------------|
  6. | 3.07    5   .58009    0      0      1       0 |
  7. | 3.06    6    .4062    0      0      1       0 |
  8. | 3.06    7   .47257    0      0      1       0 |
  9. | 3.06    8   .91196    0      0      1       0 |
 10. | 3.05    9    .6856    0      0      1       0 |
     |-----------------------------------------------|
 11. | 2.65    0        0    1      1      0       1 |
 12. |  2.7    1        0    1      1      0       1 |
 13. | 2.73    2   .00103    1      1      0       1 |
 14. | 2.75    3   .00944    1      1      1       1 |
 15. | 2.75    4   .64713    1      0      1       0 |
     |-----------------------------------------------|
 16. | 2.76    5   .55476    1      0      1       0 |
 17. | 2.77    6   .32807    1      0      1       0 |
 18. | 2.78    7   .03271    1      0      1       0 |
 19. | 2.78    8   .00219    1      0      1       0 |
 20. | 2.79    9   .57361    1      0      1       0 |
     +-----------------------------------------------+

第二条规则更简单,因为您只需要在截止点之前进行替换即可:

bysort _g (_m): generate x_f_n = x_f_s == 0
bysort _g (_m): replace x_f_n = 1 if x_f_s == 1 & x_f_s[_n+1]== 0

list

     +-------------------------------------------------------+
     | _mar   _m       _p   _g   _f_s   _f_n   x_f_s   x_f_n |
     |-------------------------------------------------------|
  1. | 2.99    0        0    0      1      0       1       0 |
  2. | 3.03    1        0    0      1      0       1       0 |
  3. | 3.05    2        0    0      1      1       1       1 |
  4. | 3.06    3   .22179    0      0      1       0       1 |
  5. | 3.07    4   .18044    0      0      1       0       1 |
     |-------------------------------------------------------|
  6. | 3.07    5   .58009    0      0      1       0       1 |
  7. | 3.06    6    .4062    0      0      1       0       1 |
  8. | 3.06    7   .47257    0      0      1       0       1 |
  9. | 3.06    8   .91196    0      0      1       0       1 |
 10. | 3.05    9    .6856    0      0      1       0       1 |
     |-------------------------------------------------------|
 11. | 2.65    0        0    1      1      0       1       0 |
 12. |  2.7    1        0    1      1      0       1       0 |
 13. | 2.73    2   .00103    1      1      0       1       0 |
 14. | 2.75    3   .00944    1      1      1       1       1 |
 15. | 2.75    4   .64713    1      0      1       0       1 |
     |-------------------------------------------------------|
 16. | 2.76    5   .55476    1      0      1       0       1 |
 17. | 2.77    6   .32807    1      0      1       0       1 |
 18. | 2.78    7   .03271    1      0      1       0       1 |
 19. | 2.78    8   .00219    1      0      1       0       1 |
 20. | 2.79    9   .57361    1      0      1       0       1 |
     +-------------------------------------------------------+