楼主: zhidushi
9624 9

[数据管理求助] 一样的数据、代码回归结果不同? [推广有奖]

  • 0关注
  • 0粉丝

已卖:1份资源

本科生

48%

还不是VIP/贵宾

-

威望
0
论坛币
221 个
通用积分
41.5083
学术水平
5 点
热心指数
5 点
信用等级
5 点
经验
643 点
帖子
31
精华
0
在线时间
143 小时
注册时间
2011-3-26
最后登录
2026-1-7

楼主
zhidushi 发表于 2021-5-30 19:17:56 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
说明:
1.不同体现在回归系数不同,p值不同,甚至影响主要解释变量的显著性;
2.使用了sort命令,id没有重复。

请问大家这可能是什么原因造成的?

二维码

扫码加我 拉你入群

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

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

关键词:回归结果 结果不同 是什么原因 回归系数 解释变量

沙发
wdlbcj 学生认证  发表于 2021-5-30 19:28:59
请给出具体的示例,

藤椅
917968079 发表于 2021-5-31 08:55:19
把你的命令贴上来啊,什么都没有大家怎么回答啊

板凳
zhidushi 发表于 2021-5-31 17:16:26
感谢回复,初入论坛经验不足,请大家海涵~

1.以下为数据处理代码:
clear
use "E:\学术小天地\房子与幸福感\data\2017年家庭金融调查数据库\2017年家庭金融调查数据_数据集_13版\chfs2017_master_city_20191120.dta", clear
keep hhid province rural swgt_h swgt_p
sort hhid
save province_house, replace
use "E:\学术小天地\房子与幸福感\data\2017年家庭金融调查数据库\2017年家庭金融调查数据_数据集_13版\ind2017_20191202_所有.dta", clear
keep hhid a2001 a2005 a2012 a2024 a2003 a2025b
gen age = 2017 - a2005
gen age2 = age^2
drop a2005
rename a2012 edu
***初中及以下***
replace edu = 1 if edu == 1 | edu == 2 | edu == 3
***高中、中专、职高***
replace edu = 2 if edu == 4 | edu == 5
***大专***
replace edu = 3 if edu ==6
***本科、硕士、博士***
replace edu = 4 if edu == 7 | edu == 8 | edu == 9
rename a2024 marital
replace marital = 0 if marital == 1 | marital == 3 | marital == 4 | marital == 5 | marital == 6
replace marital = 1 if marital == 2 |  marital == 7
rename a2003 gender
replace gender = 0 if gender == 2
rename a2025b health
replace health = 7 if health == 1
replace health = 6 if health == 2
replace health = 2 if health == 4
replace health = 1 if health == 5
replace health = 5 if health == 7
replace health = 4 if health == 6
sort hhid
merge m:m hhid using province_house,update
drop _merge
sort hhid
save ind_house, replace
use "E:\学术小天地\房子与幸福感\data\2017年家庭金融调查数据库\2017年家庭金融调查数据_数据集_13版\hh2017_20191120_所有.dta", clear
keep hhid total_income h3514 c1001 c1004 c2004_1 c2004_2 c2004_3 c2004_4 c2004_5 c2004_6 c2008b_1 c2008b_2 c2008b_3 c2008b_4 c2008b_5 c2008b_6
rename total_income income
gen lnincome = log(income)
rename h3514 happiness
replace happiness = 7 if happiness == 1
replace happiness = 6 if happiness == 2
replace happiness = 2 if happiness == 4
replace happiness = 1 if happiness == 5
replace happiness = 5 if happiness == 7
replace happiness = 4 if happiness == 6
rename c1001 htype
gen property = 0
replace property = 1 if htype == 1
gen hsquare = c1004 if htype == 2 | htype == 3
replace hsquare = c2004_1 if c2008b_1 == 1
replace hsquare = c2004_2 if c2008b_2 == 1
replace hsquare = c2004_3 if c2008b_3 == 1
replace hsquare = c2004_4 if c2008b_4 == 1
replace hsquare = c2004_5 if c2008b_5 == 1
replace hsquare = c2004_6 if c2008b_6 == 1
drop c1004 c2004_1 c2004_2 c2004_3 c2004_4 c2004_5 c2004_6 c2008b_1 c2008b_2 c2008b_3 c2008b_4 c2008b_5 c2008b_6
sort hhid
merge m:m hhid using ind_house,update
drop _merge
save hh_house, replace
import excel "E:\学术小天地\房子与幸福感\data\2017房价数据-省-年度.xls", sheet("Sheet1") firstrow clear
keep A D
rename A provincestr
rename D hprice
gen lnhprice = log(hprice)
sort provincestr
save hprice, replace
use hh_house, clear
decode province, gen(provincestr)
sort provincestr
merge m:1 provincestr using hprice
drop _merge
drop provincestr
drop if a2001 !=1
drop a2001
egen miss = rowmiss(_all)
drop if miss != 0
drop miss
winsor2 income hsquare age, replace cuts (1 99)
sum happiness hprice income age edu marital gender health rural property hsquare
save full_data1, replace

2. 执行以上命令2次,得到相同的描述性统计,如下:
(1)第一次结果:
   Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
   happiness |     18323     3.84091    .8188524          1          5
      hprice |     18323    9934.169    7090.194       4165      34117
      income |     18323    91898.81    119020.5    193.085   788576.5
         age |     18323    52.64007    16.08309         20         85
         edu |     18323    1.734214     1.03079          1          4
-------------+--------------------------------------------------------
     marital |     18323    .7960487    .4029442          0          1
      gender |     18323    .5028653    .5000054          0          1
      health |     18323    3.454947    .9993328          1          5
       rural |     18323    .2262184    .4183936          0          1
    property |     18323    .6840583    .4649025          0          1
-------------+--------------------------------------------------------
     hsquare |     18323    84.17151     64.1467         10        400

(2)第二次结果:
   Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
   happiness |     18323     3.84091    .8188524          1          5
      hprice |     18323    9934.169    7090.194       4165      34117
      income |     18323    91898.81    119020.5    193.085   788576.5
         age |     18323    52.64007    16.08309         20         85
         edu |     18323    1.734214     1.03079          1          4
-------------+--------------------------------------------------------
     marital |     18323    .7960487    .4029442          0          1
      gender |     18323    .5028653    .5000054          0          1
      health |     18323    3.454947    .9993328          1          5
       rural |     18323    .2262184    .4183936          0          1
    property |     18323    .6840583    .4649025          0          1
-------------+--------------------------------------------------------
     hsquare |     18323    84.17151     64.1467         10        400

3. 执行oprobit回归命令2次,得到不同的结果:
(1)第一次结果:
Ordered probit regression                         Number of obs   =      18323
                                                  Wald chi2(13)   =     700.97
                                                  Prob > chi2     =     0.0000
Log pseudolikelihood = -1.744e+08                 Pseudo R2       =     0.0355

------------------------------------------------------------------------------
             |               Robust
   happiness |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
    lnhprice |  -.0438804   .0241215    -1.82   0.069    -.0911576    .0033968
    lnincome |   .0449901   .0086231     5.22   0.000     .0280891     .061891
         age |  -.0413227   .0048145    -8.58   0.000     -.050759   -.0318865
        age2 |   .0005369   .0000456    11.76   0.000     .0004475    .0006264
             |
         edu |
          2  |   .0098188   .0307178     0.32   0.749    -.0503869    .0700245
          3  |  -.0010152   .0414455    -0.02   0.980    -.0822468    .0802164
          4  |   .0133474   .0405819     0.33   0.742    -.0661917    .0928864
             |
   1.marital |   .2308472   .0334034     6.91   0.000     .1653778    .2963167
    1.gender |  -.0590439   .0240239    -2.46   0.014    -.1061299    -.011958
      health |   .2214292   .0141692    15.63   0.000     .1936581    .2492002
     1.rural |   .0914092   .0338109     2.70   0.007      .025141    .1576774
  1.property |   .0562409   .0269148     2.09   0.037     .0034889    .1089929
     hsquare |   .0008728   .0002046     4.27   0.000     .0004719    .0012737
-------------+----------------------------------------------------------------
       /cut1 |    -1.8893   .2498203                     -2.378938   -1.399661
       /cut2 |  -1.143309   .2464571                     -1.626356   -.6602623
       /cut3 |   .0497612   .2476107                     -.4355468    .5350692
       /cut4 |   1.471006   .2485265                      .9839029    1.958109
------------------------------------------------------------------------------
(2)第二次结果:
Ordered probit regression                         Number of obs   =      18323
                                                  Wald chi2(13)   =     735.30
                                                  Prob > chi2     =     0.0000
Log pseudolikelihood = -1.738e+08                 Pseudo R2       =     0.0362

------------------------------------------------------------------------------
             |               Robust
   happiness |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
    lnhprice |  -.0369084    .023801    -1.55   0.121    -.0835576    .0097408
    lnincome |   .0455094   .0085176     5.34   0.000     .0288151    .0622037
         age |  -.0418628   .0047311    -8.85   0.000    -.0511356     -.03259
        age2 |   .0005423   .0000448    12.11   0.000     .0004546    .0006301
             |
         edu |
          2  |   .0162418   .0303299     0.54   0.592    -.0432036    .0756873
          3  |  -.0023572   .0416904    -0.06   0.955    -.0840689    .0793544
          4  |   .0094391   .0403457     0.23   0.815    -.0696371    .0885153
             |
   1.marital |   .2280683   .0330819     6.89   0.000      .163229    .2929076
    1.gender |  -.0592456   .0240172    -2.47   0.014    -.1063184   -.0121727
      health |   .2247194    .014199    15.83   0.000     .1968898     .252549
     1.rural |   .0988372   .0337502     2.93   0.003     .0326879    .1649864
  1.property |   .0616099   .0268107     2.30   0.022     .0090618     .114158
     hsquare |   .0008722   .0002068     4.22   0.000     .0004668    .0012776
-------------+----------------------------------------------------------------
       /cut1 |  -1.817774   .2496122                     -2.307005   -1.328543
       /cut2 |  -1.075892   .2459997                     -1.558042    -.593741
       /cut3 |   .1208648   .2468216                     -.3628966    .6046261
       /cut4 |   1.545501   .2475751                      1.060263     2.03074
------------------------------------------------------------------------------

问题:两次回归结果存在差异,可能是什么原因导致的?感谢各位大佬赐教~

报纸
zhidushi 发表于 2021-5-31 18:04:19
wdlbcj 发表于 2021-5-30 19:28
请给出具体的示例,
已经附后,望赐教!

地板
zhidushi 发表于 2021-5-31 18:05:03
917968079 发表于 2021-5-31 08:55
把你的命令贴上来啊,什么都没有大家怎么回答啊
感谢指正,代码已附后,望赐教!

7
wdlbcj 学生认证  发表于 2021-5-31 21:25:46
我个人觉得可能的原因在于两份数据中的排序不同。因为这种似然函数的分析,和初试值等等都有关联。

这两个操作中都有多对多的匹配,匹配之后可能不一定能保证两边数据的排序完全相同

若有不对请指教

8
917968079 发表于 2021-6-1 18:56:01
不要用m:m,m:m你根本不知道匹配到的是什么

9
zhidushi 发表于 2021-6-2 11:50:58
感谢大家的回复!结合大家的启发和自己的进一步分析,问题已经解决,归纳起来有以下几个原因:
1. sort问题,例子如下:
(1)原始数据:   
id        content2
4        a
3        b
2        c
1        d
3        e
3        f
2        g
4        i
4        j
4        k
(2)执行sort id第一次:
id        content2
1        d
2        c
2        g
3        f
3        b
3        e
4        j
4        a
4        i
4        k
(2)执行sort id第二次:
id        content2
1        d
2        c
2        g
3        f
3        b
3        e
4        j
4        i
4        a
4        k
(3)比较两次结果可知,相同的id,content2的排序可能会不同,再加上merge命令,就会导致同一个id对应的内容具有随机性,最终导致整个样本的随机性,所以回归结果会不同;
(4)解决办法:sort id, stable,固定住content的排序;
                        或者生成唯一的id,避免id相同的情况,在本例中,楼主将household的id和个人身份识别
                        pline进行合并,生成individual的唯一id,上述问题得以解决。
命令还有很多不足之处,希望大家指正,也希望对有相同问题的小伙伴有所帮助~

10
蓝色 发表于 2021-6-2 13:15:05
应该还是前面提到的   m:m  匹配数据的问题。
而且stata的manual里面已经专门讲不要用m:m ,否则数据排序不同结果不同。

而1:1、m:1、1:m这些匹配是没有问题的

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

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-8 14:21