全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 Stata专版
3771 5
2013-01-30
各位好,本人想做个累积分布函数Cumulative Distribution Function图,每个类别Cat(取值1-4)做一个图,含y1和y2的cdf.s,最后graph combine合到一起(2*2)。愁的地方是:最后的replace居然不能把local macro的值输到变量c`i'里,但是如果把r`i'换成y`i'就可以,不知何故。请各位指点。先表示感谢!

cdf.zip
大小:(13.76 KB)

 马上下载

本附件包括:

  • work.dta
  • trial.do

:附件里是.dta和.do-files
代码如下:
use work,clear
* initialize cumulative consumption shares:
forv i=1/2{
        gen c`i'=0
}
forv i=1/2{
        bys cat y`i': gen freq`i'=_N
        bys cat: egen ytot=total(y`i')
        gen r`i'=y`i'*freq`i'/ytot
        drop ytot
}
forv c=1/4{
        forv i=1/2{
                qui levelsof r`i' if cat==`c', local(r`i')
                local x=0
                foreach j of local r`i'{
                        local x=`x'+`j'
                        replace c`i'=`x' if r`i'==`j' & cat==`c'
                        di `x'
                }
        }
}
console信息截取:
(0 real changes made)
.88744252
(0 real changes made)
.90539243
(0 real changes made)
.92391871
(0 real changes made)
.94763235
(0 real changes made)
.97167534
(0 real changes made)
.99999997
粗斜部分为困扰点,通过display `x'可以看到`x'的值是正确的(都有值,且递增,(0,1]范围内),
但就是不能通过replace那个语句存到目标变量c`i'里,不知是哪里出错了,百思不得其解。


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2013-1-30 13:21:50
*样本分布函数图像的近似画法
use work,clear
g y=1
expandcl 2,cl(y) gen(c)
replace y=y1*(c==1)+y2*(c==2)
replace c=c*10+cat
bys c (y): cumul y,eq g(p)
line p y if c==11 ///
|| line p y if c==12 ///
|| line p y if c==13 ///
|| line p y if c==14 ///
|| line p y if c==21 ///
|| line p y if c==22 ///
|| line p y if c==23 ///
|| line p y if c==24
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-1-30 21:35:36
sungmoo 发表于 2013-1-30 13:21
*样本分布函数图像的近似画法
use work,clear
g y=1
衷心感谢您的回复:
原来cumul是这么用的,以前没看到option里的equal (generate equal cumulatives for tied values),英文不好啊。另学到:做cumul,bysort的次关键词要加括号,很重要。您原作的这步
*  Replace each cluster with n copies of the cluster
expandcl 2,cluster(y) generate(c)用于generate duplicates (2 copies),先y=1,之后再做替换(0,1判定识别),高啊~~
我原来是导到R里面去实现的(把y1,y2放到一个变量y下,分时间time=1和2),又学一招。

另,原图还有些细节:限制了y的范围,并标识了贫困线
改1(基于您原作):
use work,clear
global x=500000
generate y=1
*  Replace each cluster with n copies of the cluster
expandcl 2,cluster(y) generate(c)
replace y=y1*(c==1)+y2*(c==2)
replace c=c*10+cat
*  equal: generate equal cumulatives for tied values
bysort c (y): cumul y if y<=$x,equal generate(p)
forv c=1/4{
        #d ;
        twoway
        line p y if c==1*10+`c' & y<=$x,sort  ||
        line p y if c==2*10+`c' & y<=$x, sort
        title(cat=`c')
        legend(ring(0) pos(4) col(1) region(lcolor(white))
        label(1 "time 1") label(2 "time 2"))
        xline(109663)
        saving(g`c',replace)
        ;
        #d cr
}
#d;
graph combine g1.gph g2.gph g3.gph g4.gph,
title("CDFs for two-time income in four categories"
"truncated at 500,000")
note("the vertical line is the poverty line (at 109,663)")
;
#d cr
graph export cdf.pdf,replace

所意以一图一目了然:
cdf.jpg
改2:(不用expandcl,直接用cumul,eq分别做y1,y2的cumulative shares)
use work,clear
global x=500000
forv i=1/2{
        * (y`i'): the parentheses must be there!
        bysort cat (y`i'): cumul y`i' if y`i'<=$x, eq g(p`i')
}
forv c=1/4{
        #d ;
        twoway
        line p1 y1 if cat==`c' & y1<=$x,sort  ||
        line p2 y2 if cat==`c' & y2<=$x, sort
        title(cat=`c')
        legend(ring(0) pos(4) col(1) region(lcolor(white))
        label(1 "time 1") label(2 "time 2"))
        xline(109663)
        saving(g`c',replace)
        ;
        #d cr
}
#d;
graph combine g1.gph g2.gph g3.gph g4.gph,
title("CDFs for two-time income in four categories"
"truncated at 500,000")
note("the vertical line is the poverty line (at 109,663)")
;
#d cr
graph export cdf.pdf,replace
同图



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-1-31 07:35:58
当样本观测值很多且很均匀时,这种画法可以很接近真正的样本分布函数图像;反之,则需要修正(样本分布函数是阶梯状函数)。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-1-31 08:18:46
sungmoo 发表于 2013-1-31 07:35
当样本观测值很多且很均匀时,这种画法可以很接近真正的样本分布函数图像;反之,则需要修正(样本分布函数 ...
哦,那就得一级级断开画了
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-3-7 23:23:10
赞一个,支持楼主,学习一下
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群