楼主: monstersivle
3851 5

[编程问题求助] [愁!]累积分布函数,replace无法存入local macro的值(已解决,见回帖) [推广有奖]

已卖:66份资源

博士生

7%

还不是VIP/贵宾

-

威望
0
论坛币
359 个
通用积分
240.8846
学术水平
40 点
热心指数
51 点
信用等级
22 点
经验
3520 点
帖子
215
精华
0
在线时间
187 小时
注册时间
2012-10-29
最后登录
2016-7-23

楼主
monstersivle 发表于 2013-1-30 06:06:18 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
各位好,本人想做个累积分布函数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'里,不知是哪里出错了,百思不得其解


二维码

扫码加我 拉你入群

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

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

关键词:replace 累积分布函数 place Macro Local replace cat 分布函数 local

回帖推荐

sungmoo 发表于4楼  查看完整内容

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

sungmoo 发表于2楼  查看完整内容

*样本分布函数图像的近似画法 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
不爱其亲而爱他人者,谓之悖德;不敬其亲而敬他人者,谓之悖礼。——《孝经》

沙发
sungmoo 发表于 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
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
monstersivle + 1 + 1 + 1 多谢,学习了

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

藤椅
monstersivle 发表于 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
同图



不爱其亲而爱他人者,谓之悖德;不敬其亲而敬他人者,谓之悖礼。——《孝经》

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

报纸
monstersivle 发表于 2013-1-31 08:18:46
sungmoo 发表于 2013-1-31 07:35
当样本观测值很多且很均匀时,这种画法可以很接近真正的样本分布函数图像;反之,则需要修正(样本分布函数 ...
哦,那就得一级级断开画了
不爱其亲而爱他人者,谓之悖德;不敬其亲而敬他人者,谓之悖礼。——《孝经》

地板
robert_jia 发表于 2013-3-7 23:23:10
赞一个,支持楼主,学习一下

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-31 14:08