Stata 在满足条件时创建指示器标志
我想找到一种方法来跨行创建一个指示符标志,这样一旦满足了一个标准,该标志就会在一个组中的所有情况下保持不变 在下面的样本数据中,我有一个变量Stata 在满足条件时创建指示器标志,stata,Stata,我想找到一种方法来跨行创建一个指示符标志,这样一旦满足了一个标准,该标志就会在一个组中的所有情况下保持不变 在下面的样本数据中,我有一个变量\u p,它定义了\u mar中的值在\u m级别之间比较的统计显著性。我还有一个分组变量\u g,它指示在一个组内进行比较 变量\u f_s和\u f_n表示我希望得到的最终结果 clear input _mar _m _p _g _f_s _f_n 2.99 0 0.00000 0 1 0 3.03
\u p
,它定义了\u mar
中的值在\u m
级别之间比较的统计显著性。我还有一个分组变量\u g
,它指示在一个组内进行比较
变量\u f_s
和\u 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)
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)
代码片段2-图形不起作用,行的结构不符合要求
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)
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)
快照还原1
排序
genx_f_s=(_p我会这样做:
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_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 |
+-------------------------------------------------------+