老师您好,想请教一下,我是用周个股数据和周市场数据m:1合并,并对证券代码和交易周份进行sort排序,然后再进行操作,然后参照您的代码进行 滞后和前期的变量也都正常计算了 但asreg后,得出的 _Nobs _R2 _adjR2 _b_WretwdosL1 _b_WretwdosL2 _b_Wretwdos _b_WretwdosF1 _b_WretwdosF2 _b_cons _fitted _residuals 这些变量都是缺失值,请问是什么原因呢?数据展示部分如下:
- * Example generated by -dataex-. To install: ssc install dataex
- clear
- input long(stkcd trdwnt) double(Wretwd Wretwdos WretwdosL1 WretwdosL2 WretwdosF1 WretwdosF2 t _Nobs _R2 _adjR2 _b_WretwdosL1 _b_cons _fitted _residuals)
- 2 201203 .058981 .028053 .042537 -.05185 .014238 .028875 3 . . . . . . .
- 2 201205 -.013924 .014238 .028053 .042537 .028875 .017839 4 . . . . . . .
- 2 201206 .007702 .028875 .014238 .028053 .017839 .054076 5 . . . . . . .
- 2 201207 -.007643 .017839 .028875 .014238 .054076 .009937 6 . . . . . . .
- 2 201208 .092426 .054076 .017839 .028875 .009937 .011982 7 . . . . . . .
- 2 201209 .014101 .009937 .054076 .017839 .011982 -.012917 8 . . . . . . .
- 2 201210 -.003476 .011982 .009937 .054076 -.012917 -.03138 9 . . . . . . .
- 2 201211 -.038372 -.012917 .011982 .009937 -.03138 -.061242 10 . . . . . . .
- 2 201212 -.018138 -.03138 -.012917 .011982 -.061242 .03839 11 . . . . . . .
- 2 201213 .019704 -.061242 -.03138 -.012917 .03839 .02839 12 . . . . . . .
- 2 201214 .012077 .03839 -.061242 -.03138 .02839 .012003 13 . . . . . . .
- 2 201215 .023866 .02839 .03839 -.061242 .012003 -.015583 14 . . . . . . .
- 2 201216 .006993 .012003 .02839 .03839 -.015583 .032494 15 . . . . . . .
- 2 201217 .039352 -.015583 .012003 .02839 .032494 -.015786 16 . . . . . . .
- 2 201218 .026726 .032494 -.015583 .012003 -.015786 -.01997 17 . . . . . . .
- 2 201219 -.031453 -.015786 .032494 -.015583 -.01997 -.005268 18 . . . . . . .
- 2 201220 -.039194 -.01997 -.015786 .032494 -.005268 .031779 19 . . . . . . .
- 2 201221 .031469 -.005268 -.01997 -.015786 .031779 -.041468 20 . . . . . . .
- 2 201222 .033898 .031779 -.005268 -.01997 -.041468 .024673 21 . . . . . . .
- 2 201223 -.025137 -.041468 .031779 -.005268 .024673 -.015239 22 . . . . . . .
- 2 201224 .0213 .024673 -.041468 .031779 -.015239 -.02567 23 . . . . . . .
- 2 201225 -.008782 -.015239 .024673 -.041468 -.02567 .013018 24 . . . . . . .
- 2 201226 -.013289 -.02567 -.015239 .024673 .013018 -.006191 25 . . . . . . .
- 2 201227 .098208 .013018 -.02567 -.015239 -.006191 -.029549 26 . . . . . . .
- 2 201228 .021762 -.006191 .013018 -.02567 -.029549 -.024075 27 . . . . . . .
- 2 201229 -.056795 -.029549 -.006191 .013018 -.024075 -.006205 28 . . . . . . .
- 2 201230 -.034409 -.024075 -.029549 -.006191 -.006205 .03673 29 . . . . . . .
- 2 201231 -.03118 -.006205 -.024075 -.029549 .03673 -.032604 30 . . . . . . .
- 2 201232 .010345 .03673 -.006205 -.024075 -.032604 -.012021 31 . . . . . . .
- 2 201233 -.020478 -.032604 .03673 -.006205 -.012021 -.039589 32 . . . . . . .
- end
复制代码
代码如下:
- use /周个股回报率文件.dta
- destring Markettype,replace
- //转字符变量为数值变量
- destring Stkcd,replace
- destring Trdwnt, replace ignore("-")
- //横向合并文件
- merge m:1 Markettype Trdwnt using /周市场回报率文件(调整).dta
- drop _merge
- sort Stkcd Trdwnt //数据按照两个变量进行排序
- //重新命名变量
- rename Stkcd stkcd //股票代码
- rename Trdwnt trdwnt //交易周份
- //转字符变量为数值变量
- destring Wretwdos Wretwd,replace
- drop if Wretwd==.
- //生成面板数据
- bys stkcd (trdwnt): gen t = _n //bys是分组命令
- xtset stkcd t
- //生成一期滞后项
- gen WretwdosL1=L1.Wretwdos //L表示滞后命令 L1滞后一期 F1提前一期
- gen WretwdosL2=L2.Wretwdos
- gen WretwdosF1=F1.Wretwdos
- gen WretwdosF2=F2.Wretwdos
- drop if WretwdosL1==.
- drop if WretwdosL2==.
- drop if WretwdosF1==.
- drop if WretwdosF2==. //剔除缺失值,缺失值是由于滞后或者提前一期数据不存在
- //计算残差ε 即个股收益未被市场所解释的部分
- bys stkcd trdwnt: asreg Wretwd WretwdosL1 WretwdosL2 Wretwdos WretwdosF1 WretwdosF2, fit
- ren _residuals e_asreg
复制代码