通过在Stata中将变量除以IQR来创建变量

时间:2015-06-08 11:42:08

标签: stata

如何通过将其除以IQR来创建变量?我已经做了很多工作如下。

示例数据和代码如下:

use http://www.ats.ucla.edu/stat/stata/notes/hsb2, clear

foreach var of varlist read-socst {
   egen `var'75 = pctile(`var'), p(75)
   egen `var'25 = pctile(`var'), p(25)
   gen `var'q =`var'75 - `var'25
   drop `var'75 `var'25
}

gen readI = read/readq
gen sciI = science/scienceq

1 个答案:

答案 0 :(得分:2)

最简单的方法就是直接使用summarize结果:

sysuse auto, clear

quietly foreach v of var price-foreign { 
    su `v', detail 
    gen `v'q = `v' / (r(p75) - r(p25)) 
} 

如果egen路径意味着为每个原始变量创建新变量,只是为了将四分位数或IQR保持为重复常数,那么egen路径就会过度。但是,如果您想通过群组执行此操作,bysort foreign: egen mpg_upq = pctile(mpg), p(75) by foreign: egen mpg_loq = pctile(mpg), p(25) gen mpg_Q = mpg / (mpg_upq - mpg_loq) 就会出现:

            Echo '<img src="images/tick.png" id="tick' . $i .'" h1 class="hidden" style="position: absolute; top: 30px; left: 70px;"/>';

请注意,IQR可以为0,指示变量通常为0。