estat icc *> Intraclass correlation *> ------------------------------------------------------------------------------ *> Level | ICC Std. err. [95% conf. interval] *> -----------------------------+------------------------------------------------ *> region | .4088542 .1636402 .1550255 .7227831 *> state|region | .9769574 .0063228 .9607063 .9865813 *> ------------------------------------------------------------------------------ ret list *> scalars: *> r(icc3) = .408854229065962 *> r(se3) = .1636402482538196 *> r(icc2) = .9769574293643243 *> r(se2) = .0063228483990126 *> r(level) = 95 *> macros: *> r(label3) : "region" *> r(label2) : "state|region" *> matrices: *> r(ci3) : 1 x 2 *> r(ci2) : 1 x 2 mat b = (r(icc3), r(se3), r(ci3) r(icc2), r(se2), r(ci2)) mat list b *> b[2,4] *> c1 c2 ll ul *> r1 .40885423 .16364025 .1550255 .7227831 *> r2 .97695743 .00632285 .96070626 .98658129
由此我们可以把这俩矩阵保存起来:
clear matlist a *> symmetric a[1,1] *> c1 *> r1 . svmat a *> number of observations will be reset to 1 *> Press any key to continue, or Break to abort *> Number of observations (_N) was 0, now 1. gen v = "gsp" genclass = _n order v class save data1, replace *> file data1.dta saved clear matlist a *> symmetric a[1,1] *> c1 *> r1 . svmat b *> number of observations will be reset to 2 *> Press any key to continue, or Break to abort *> Number of observations (_N) was 0, now 2. gen v = "gsp" order v save data2, replace *> file data2.dta saved
然后就可以循环所有的了:
capmkdir"Random_effects_parameters" capmkdir"icc" use productivity, clear foreach i of varlist hwy - unemp { di"`i'" quicap { use productivity, clear mixed `i' || region: || state:, mle mat a = r(table) estat icc mat b = (r(icc3), r(se3), r(ci3) r(icc2), r(se2), r(ci2))
clear svmat a gen v = "`i'" genclass = _n order v class save Random_effects_parameters/`i', replace
clear svmat b gen v = "`i'" order v save icc/`i', replace } }
*- 把运行结果存储为 log 文件 caplogclose log using temp.txt, replace text use productivity, clear foreach i of varlist hwy - unemp { di"变量:`i'" cap { use productivity, clear noi mixed `i' || region: || state:, mle noiestat icc } } logclose
读取 log 文件:
infix strL v 1-2000 using temp.txt, clear *> (397 observations read)
保留所需的结果所在行:
keep if index(v, "变量:") | index(v, "_cons") | index(v, "var(Residual)") | index(v, "region") *> (332 observations deleted) drop if index(v, "di") | index(v, "region: Identity") | index(v, "mixed") *> (9 observations deleted) drop if index(v[_n - 1], "变量:") *> (7 observations deleted)
把变量拿到前面:
gen newv = v ifindex(v, "变量:") *> (42 missing values generated) carryforward newv, replace *> newv: (42 real changes made) dropif v == newv *> (7 observations deleted) order newv save data1, replace *> file data1.dta saved
最后把结果分成几类进行处理:
*- 处理 _cons use data1, clear keepifsubstr(v, 1, 5) == "_cons" *> (35 observations deleted) replace v = subinstr(v, "_cons |", "", .) *> (7 real changes made) replace v = subinstr(v, " ", " ", .) *> (7 real changes made) replace v = subinstr(v, " ", " ", .) *> (7 real changes made) replace v = subinstr(v, " ", " ", .) *> (7 real changes made) replace v = subinstr(v, " ", " ", .) *> (0 real changes made) replace v = subinstr(v, " ", " ", .) *> (0 real changes made) split v, parse(" ") *> variables created as string: *> v1 v2 v3 v4 v5 v6 drop v ren (v1 v2 v3 v4 v5 v6) (b se z p lu) destring, replace *> newv: contains nonnumeric characters; no replace *> b: all characters numeric; replaced as double *> se: all characters numeric; replaced as double *> z: all characters numeric; replaced as double *> p: all characters numeric; replaced as byte *> l: all characters numeric; replaced as double *> u: all characters numeric; replaced as double save _cons, replace *> file _cons.dta saved list *> +-----------------------------------------------------------------------+ *> | newv b se z p l u | *> |-----------------------------------------------------------------------| *> 1. | 变量:hwy 9.026957 .2053362 43.96 0 8.624505 9.429409 | *> 2. | 变量:water 7.756548 .2694501 28.79 0 7.228435 8.28466 | *> 3. | 变量:other 8.905559 .2725905 32.67 0 8.371291 9.439826 | *> 4. | 变量:private 10.69631 .2329682 45.91 0 10.2397 11.15292 | *> 5. | 变量:gsp 10.65961 .2503806 42.57 0 10.16887 11.15035 | *> |-----------------------------------------------------------------------| *> 6. | 变量:emp 7.124568 .2479106 28.74 0 6.638672 7.610464 | *> 7. | 变量:unemp 6.770185 .3073489 22.03 0 6.167792 7.372578 | *> +-----------------------------------------------------------------------+
*- 处理 var(_cons) use data1, clear keepifindex(v, "var(_cons)") *> (28 observations deleted) replace v = subinstr(v, "var(_cons) |", "", .) *> (14 real changes made) replace v = subinstr(v, " ", " ", .) *> (14 real changes made) replace v = subinstr(v, " ", " ", .) *> (14 real changes made) replace v = subinstr(v, " ", " ", .) *> (14 real changes made) replace v = subinstr(v, " ", " ", .) *> (0 real changes made) replace v = subinstr(v, " ", " ", .) *> (0 real changes made) split v, parse(" ") *> variables created as string: *> v1 v2 v3 v4 drop v ren (v1 v2 v3 v4) (b selu) destring, replace *> newv: contains nonnumeric characters; no replace *> b: all characters numeric; replaced as double *> se: all characters numeric; replaced as double *> l: all characters numeric; replaced as double *> u: all characters numeric; replaced as double egen group = fill(1 2 1 2) tostring group, replace *> group was float now str1 egen newgroup = msub(group), f(1 2) r("region""state") drop group order newv newgroup save var_cons , replace *> file var_cons.dta saved list *> +----------------------------------------------------------------------+ *> | newv newgroup b se l u | *> |----------------------------------------------------------------------| *> 1. | 变量:hwy region .3048045 .1801655 .0956948 .9708547 | *> 2. | 变量:hwy state .3578027 .0811456 .2294039 .5580673 | *> 3. | 变量:water region .4871242 .3137895 .1378236 1.721694 | *> 4. | 变量:water state .8018785 .1824776 .513342 1.252594 | *> 5. | 变量:other region .5220583 .3214056 .1561982 1.744866 | *> |----------------------------------------------------------------------| *> 6. | 变量:other state .7043993 .1602948 .4509386 1.100324 | *> 7. | 变量:private region .3936212 .2308798 .1246819 1.242663 | *> 8. | 变量:private state .4530668 .1029507 .2902305 .7072637 | *> 9. | 变量:gsp region .4376123 .2697616 .1307299 1.464887 | *> 10. | 变量:gsp state .6080626 .138273 .3893907 .949535 | *> |----------------------------------------------------------------------| *> 11. | 变量:emp region .4271744 .2629881 .1278106 1.427722 | *> 12. | 变量:emp state .6054883 .1374675 .3880186 .9448415 | *> 13. | 变量:unemp region .6514024 .4014016 .1946835 2.179564 | *> 14. | 变量:unemp state .746314 .2170363 .4220684 1.319655 | *> +----------------------------------------------------------------------+
*- 处理 var(Residual) use data1, clear keepifindex(v, "var(Residual)") *> (35 observations deleted) replace v = subinstr(v, "var(Residual) |", "", .) *> (7 real changes made) replace v = subinstr(v, " ", " ", .) *> (7 real changes made) replace v = subinstr(v, " ", " ", .) *> (7 real changes made) replace v = subinstr(v, " ", " ", .) *> (7 real changes made) replace v = subinstr(v, " ", " ", .) *> (0 real changes made) replace v = subinstr(v, " ", " ", .) *> (0 real changes made) split v, parse(" ") *> variables created as string: *> v1 v2 v3 v4 drop v ren (v1 v2 v3 v4) (b selu) destring, replace *> newv: contains nonnumeric characters; no replace *> b: all characters numeric; replaced as double *> se: all characters numeric; replaced as double *> l: all characters numeric; replaced as double *> u: all characters numeric; replaced as double save var_Residual, replace *> file var_Residual.dta saved list *> +-----------------------------------------------------------+ *> | newv b se l u | *> |-----------------------------------------------------------| *> 1. | 变量:hwy .0054385 .0002775 .0049209 .0060106 | *> 2. | 变量:water .0346385 .0017676 .0313417 .0382822 | *> 3. | 变量:other .023078 .0011777 .0208814 .0255056 | *> 4. | 变量:private .0291108 .0014856 .0263401 .0321731 | *> 5. | 变量:gsp .0246633 .0012586 .0223159 .0272577 | *> |-----------------------------------------------------------| *> 6. | 变量:emp .0207226 .0010575 .0187503 .0229025 | *> 7. | 变量:unemp 3.601394 .1837828 3.258614 3.980231 | *> +-----------------------------------------------------------+
*- 处理 icc use data1, clear keepifindex(v, "region") *> (28 observations deleted) replace v = subinstr(v, " ", " ", .) *> (14 real changes made) replace v = subinstr(v, " ", " ", .) *> (14 real changes made) replace v = subinstr(v, " ", " ", .) *> (14 real changes made) replace v = subinstr(v, " ", " ", .) *> (0 real changes made) replace v = subinstr(v, " ", " ", .) *> (0 real changes made) split v, parse(" "" | ") *> variables created as string: *> v1 v2 v3 v4 v5 drop v ren (v1 v2 v3 v4 v5) (level icc selu) destring, replace *> newv: contains nonnumeric characters; no replace *> level: contains nonnumeric characters; no replace *> icc: all characters numeric; replaced as double *> se: all characters numeric; replaced as double *> l: all characters numeric; replaced as double *> u: all characters numeric; replaced as double save icc, replace *> file icc.dta saved list *> +--------------------------------------------------------------------------+ *> | newv level icc se l u | *> |--------------------------------------------------------------------------| *> 1. | 变量:hwy region .4562629 .1616785 .1895854 .7506184 | *> 2. | 变量:hwy state|region .991859 .0023559 .9856681 .9953882 | *> 3. | 变量:water region .3680183 .1643013 .1272719 .6992739 | *> 4. | 变量:water state|region .9738309 .0068837 .9563633 .9844202 | *> 5. | 变量:other region .4178018 .1647916 .1598114 .7302763 | *> |--------------------------------------------------------------------------| *> 6. | 变量:other state|region .9815307 .0051415 .9682353 .9893226 | *> 7. | 变量:private region .4494425 .158936 .1881876 .7419222 | *> 8. | 变量:private state|region .9667608 .0094065 .9424859 .9809966 | *> 9. | 变量:gsp region .4088542 .1636402 .1550255 .7227831 | *> 10. | 变量:gsp state|region .9769574 .0063228 .9607063 .9865813 | *> |--------------------------------------------------------------------------| *> 11. | 变量:emp region .4055253 .1629987 .1534632 .7196454 | *> 12. | 变量:emp state|region .9803276 .0053749 .9665091 .9885123 | *> 13. | 变量:unemp region .1303037 .070809 .0421714 .3376856 | *> 14. | 变量:unemp state|region .279593 .0638113 .172586 .419323 | *> +--------------------------------------------------------------------------+
这样我们就解决了这个问题~
最后我们再绘图展示下处理结果:
use var_cons.dta, clear replace newv = subinstr(newv, "变量:", "", .) *> (14 real changes made) sencode newv, gen(x) gsort(b) gen x1 = x - 0.2 gen x2 = x + 0.2 codebook x *> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- *> x (unlabeled) *> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- *> Type: Numeric (byte) *> Label: x *> Range: [1,7] Units: 1 *> Unique values: 7 Missing .: 0/14 *> Tabulation: Freq. Numeric Label *> 2 1 hwy *> 2 2 private *> 2 3 emp *> 2 4 gsp *> 2 5 water *> 2 6 other *> 2 7 unemp tw bar b x1 if newgroup == "region", barwidth(0.4) /// color("254 212 57") fintensity(50) || /// bar b x2 if newgroup == "state", barwidth(0.4) /// color("112 154 225") fintensity(50) || /// rcap ul x1 if newgroup == "region", color("254 212 57") || /// rcap ul x2 if newgroup == "state", color("112 154 225") /// leg(order(1 "region" 2 "state" 3 "region" 4 "state")) /// xla(1 "hwy" 2 "private" 3 "emp" 4 "gsp"/// 5 "water" 6 "other" 7 "unemp") /// ti("不同变量的估计结果对比") /// subti("数据处理:微信公众号 RStata")
评论