楼主: LLieo
18927 9

[面板数据求助] forvalues使用 [推广有奖]

  • 2关注
  • 0粉丝

硕士生

5%

还不是VIP/贵宾

-

威望
0
论坛币
164 个
通用积分
1.0322
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
3030 点
帖子
93
精华
0
在线时间
138 小时
注册时间
2014-1-17
最后登录
2020-11-29

楼主
LLieo 发表于 2014-3-22 13:08:22 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
一个三维面板数据(t*c*p,t是时间,c是省市,p是分类居民消费物价指数的对数形式)
具体来说,在stata中如下存储的:
city        time        lnfood        lncigwine        lncloth
1        200301        4.646696         4.606869         4.581492
1        200302        4.632396         4.604270         4.570786
1        200303        4.596936         4.601363         4.601263
1        200304        4.593199         4.609959         4.605270
2        200301        4.646504         4.606869         4.585681
2        200302        4.623108         4.604270         4.589345
2        200303        4.590767         4.601363         4.600158
2        200304        4.590260         4.609959         4.613138
3        200301        4.642851         4.605370         4.601563
3        200302        4.636475         4.606170         4.599353
3        200303        4.601563         4.606369         4.599555
3        200304        4.586905         4.606170         4.606869


数据仅粘贴了部分

我想要计算给定时期给定商品种类的两省市之间的物价指数对数形式的差,即对第t时期第k类商品i、j两地求lnfooditk-lnfoodjtk以及其他商品类的。先将面板数据转换成截面数据,用reshape wide lnfood,i(time) j(city)命令,这只能将一类价格指数转成截面数据,不知道有没有能将所有价格指数都转成截面数据的方法。

在没有找到将所有价格指数同时转化为截面数据的情况下,我就利用以下转换出的数据进行计算(即只有食品类的不同时期不同城市的数据):

time        lnfood1        lnfood2        lnfood3        lnfood4
200301        4.6467        4.6465        4.64285        4.66363
200302        4.6324        4.62311        4.63648        4.62281
200303        4.59694        4.59077        4.60156        4.60577
200304        4.5932        4.59026        4.58691        4.58731
然后再用forvalues进行循环计算,但做到这里就不知道怎么做了。希望大家指点一下。先谢谢了,急呀!!!


二维码

扫码加我 拉你入群

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

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

关键词:forvalues forvalue values value forv

回帖推荐

jjjj6666 发表于3楼  查看完整内容

reshape wide lnfood lncigwine lncloth, i(city) j(time) I already gave you the code to compute, here is modfied ones based on your new var names

沙发
LLieo 发表于 2014-3-22 13:48:59
求解答呀!急!谢谢!

藤椅
jjjj6666 发表于 2014-3-22 21:57:36
reshape wide lnfood lncigwine lncloth, i(city) j(time)

I already gave you the code to compute, here is modfied ones based on your new var names
  1. levelsof time, local(times)
  2. reshape wide lnfood lncigwine lncloth, i(city) j(time)
  3. tempfile f1 f2
  4. save `f1'
  5. gen city2 = city
  6. drop city
  7. gen x=1
  8. save `f2'
  9. use `f1'
  10. gen city1 = city
  11. drop city
  12. foreach vm of var ln* {
  13. rename `vm' _`vm'
  14. }
  15. gen x=1
  16. joinby x using `f2'
  17. drop x
  18. * compute diference and mean for each pair of city at the same time and P
  19. qui foreach nm in lnfood lncigwine lncloth {
  20. foreach j in `times' {
  21.   gen d_`nm'`j' = _`nm'`j'-`nm'`j' if city1<city2
  22.   egen m_`nm'`j' = mean(d_`nm'`j') if d_`nm'`j'!=.
  23. }
  24. }
复制代码

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
LLieo + 1 + 1 + 1 谢谢~

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

板凳
LLieo 发表于 2014-3-25 12:30:04
jjjj6666 发表于 2014-3-22 21:57
reshape wide lnfood lncigwine lncloth, i(city) j(time)

I already gave you the code to compute, he ...
非常感谢!我星期天的时候已经好好研究过了,是算了出来的。但是一直未能向你说声谢谢!万分感谢,学到了很多东西!

报纸
LLieo 发表于 2014-3-25 12:59:30
jjjj6666 发表于 2014-3-22 21:57
reshape wide lnfood lncigwine lncloth, i(city) j(time)

I already gave you the code to compute, he ...
但是这样算出来,我看不出来时间趋势,也不好画图,而且不能简单地分辨出差值是哪两个城市的差值。我首先要求出给定某一商品k在给定时期t时i、j两地之间价格指数的差值△Qijkt的绝对值,然后求出给定商品k给定时间t,在所有两地之间的平均值|△Qkt|=mean(|△Qijkt|),然后将每组的价格差减去这个平均值qijkt=|△Qijkt|-|△Qkt|,并求出不同种类商品之间的价格波动的方差,即Var(qijt),并且这个方差是时间序列的,可用于画图及协整检验,有多少组两两城市应该就有多少列时间序列,最后求出各地Var(qijt)的均值,即Var(qt),这个应该是一列时间序列。我有尝试过将你给的程序改了又改但是也无法计算出来。也有试过在reshape wide ln*, i(time) j(province)以保证是时间序列的再去进行其他计算,但还是没有成功。能否再指点一下?

地板
jjjj6666 发表于 2014-3-25 22:07:16
try this, this will generate the final results, but you need to check if the results are what you need

  1. drop _all
  2. use pairwise2
  3. levelsof time, local(times)
  4. reshape wide lnfood lncigwine lncloth, i(city) j(time)
  5. tempfile f1 f2
  6. save `f1'
  7. gen city2 = city
  8. drop city
  9. gen x=1
  10. save `f2'
  11. use `f1'
  12. gen city1 = city
  13. drop city
  14. foreach vm of var ln* {
  15. rename `vm' _`vm'
  16. }
  17. gen x=1
  18. joinby x using `f2'
  19. drop x
  20. * compute diference and mean for each pair of city at the same time and P
  21. qui foreach nm in lnfood lncigwine lncloth {
  22. foreach j in `times' {
  23.   gen d_`nm'`j' = abs(_`nm'`j'-`nm'`j') if city1<city2
  24.   egen m_`nm'`j' = mean(d_`nm'`j') if d_`nm'`j'!=.
  25.   gen q_`nm'`j' = d_`nm'`j'-m_`nm'`j'
  26. }
  27. }
  28. foreach j in `times' {
  29. egen v_`j' = rowsd(q_*`j')
  30. replace v_`j' = v_`j'^2
  31. }
  32. keep if city1<city2
  33. gen city_pair = "city" + string(city1) +"_" + "city" + string(city2)
  34. keep v_* city_pair
  35. local n=_N+1
  36. set obs `n'
  37. replace city_pair="Var_qt" in l
  38. qui foreach vm of var v_* {
  39. su `vm'
  40. replace `vm' = r(mean) in l
  41. }
复制代码

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
LLieo + 1 + 1 + 1 万分感谢,相当受用!

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

7
LLieo 发表于 2014-3-26 21:15:09
jjjj6666 发表于 2014-3-25 22:07
try this, this will generate the final results, but you need to check if the results are what you ne ...
非常感谢,是可以算出来的!算出来之后我在excel中倒置一下就是时间序列的了!真的超激动,这么多天终于算出来了!不过我还想再问一个问题,我在算出每组的价格差减去所有差的平均值qijkt=|△Qijkt|-|△Qkt|后,还想计算出给定时间t给定商品k在不同地区组合之间的方差,即Var(qkt),应该有多少类商品就有多少列方差的时间序列,这个应该就是计算出q_`nm'`j' = d_`nm'`j'-m_`nm'`j'后q_`nm'`j'的方差,本来summarize中会有的,但是我把所有变量都放进去后就会算出四百多列数据,summarize出来四百多个方差我整理起来很麻烦,所以想找个方法将给定时间t给定商品k在不同地区组合之间的方差Var(qkt)计算出来列在最后一行,我在你给的代码中加了一部分(如下最后几行),但是最终没有成功,错误代码是unrecognized command:  q_lnfood200301 r(199);
所以,想问问有没有什么好方法吗?
  1. set maxvar 10000
  2. set mem 128m

  3. levelsof time, local(times)
  4. reshape wide lnfood lncigwine lncloth lnfequip lnmedical lntranscom lnentecul lnliving, i(province) j(time)
  5. tempfile f1 f2
  6. save `f1'
  7. gen province2 = province
  8. drop province
  9. gen x=1
  10. save `f2'
  11. use `f1'
  12. gen province1 = province
  13. drop province
  14. foreach vm of var ln* {
  15. rename `vm' _`vm'
  16. }
  17. gen x=1
  18. joinby x using `f2'
  19. drop x
  20. * compute diference and mean for each pair of city at the same time and P
  21. qui foreach nm in lnfood lncigwine lncloth lnfequip lnmedical lntranscom lnentecul lnliving {
  22. foreach j in `times' {
  23.   gen d_`nm'`j' = abs(_`nm'`j'-`nm'`j') if province1<province2
  24.   egen m_`nm'`j' = mean(d_`nm'`j') if d_`nm'`j'!=.
  25.   gen q_`nm'`j' = d_`nm'`j'-m_`nm'`j'
  26. }
  27. }
  28. drop ln* _ln* d_ln* m_ln*
  29. keep if province1<province2
  30. drop province1 province2
  31. qui foreach vm of var q_* {
  32. `vm'= rowsd( `vm') in l
  33. replace `vm' = `vm'^2 in l
  34. }
复制代码

8
LLieo 发表于 2014-3-26 21:25:28
jjjj6666 发表于 2014-3-25 22:07
try this, this will generate the final results, but you need to check if the results are what you ne ...
就是说,上次计算的时候是求给定时间t给定i、j两地间不同商品种类之间价格波动的方差Var(qijt),这次想求给定时间t给定商品种类k不同地区组合间价格波动的方差Var(qkt),并且也要求出不同类商品间方差的均值Var(qt),我一共有八类商品,也就是求出8个单独商品时间序列+1个均值时间序列。我用上面我添加的代码,先删去一些不需要的变量后,仅留下了需要的部分,然后想添加最后一行将方差写进去,但是没有成功。谢谢指点~~~~

9
唐娜 发表于 2017-7-9 06:52:57
jjjj6666 发表于 2014-3-22 21:57
reshape wide lnfood lncigwine lncloth, i(city) j(time)

I already gave you the code to compute, he ...
你好,请问你stata用什么学的,有什么推荐经典教材自学吗?

10
米高兄弟 在职认证  发表于 2022-4-24 09:56:34
LLieo 发表于 2014-3-26 21:15
非常感谢,是可以算出来的!算出来之后我在excel中倒置一下就是时间序列的了!真的超激动,这么多天终于算 ...
感谢大神的支持,确实这种命令很难写啊

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

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