第三章do文件

*-3.1 共线性问题
    *(1)共线性
    sysuse auto.dta, clear
    sum
    asdoc summarize // 该文件还有可能保存未清理的历史数据
    asdoc summarize, replace  // 解决方法之一
    asdoc summarize, save(jxufe) replace  // 解决方法之二,jxufe可自己命名

    gen weight2=weight^2
    asdoc corr price weight weight2 mpg headroom length trunk // asdoc可与很多stata命令组合。本命令查看被解释变量与解释变量的相关系数或协方差矩阵

    quietly regress price weight mpg headroom length trunk //quietly不报告结果
    outreg2 using jxufe, word replace
    quietly regress price weight weight2 mpg headroom length trunk
    outreg2 using jxufe, word // 引入二次项后一次项系数变成负数,新加入的二次项系数为正。
    
    *(2)共线性的检验
    regress price weight weight2 mpg headroom length trunk // 回归,包括二次项
    estat vif // 方差膨胀因子检验。VIF大于10时,考虑两者之间的共线性问题

    *(3)共线性的处理
    gen y=sum(x)       // 几个命令,本命令求列累积和
    egen z=sum(x)      // 求列总和
    egen avg=mean(A)   // 求变量A的均值
    egen med=median(A) // 变量A的中位数
    egen std=sd(A)     // 变量A的标准差
    egen max=max(A)    // 变量A的最大值
    egen min=min(A)    // 变量A的最小值


    *--首先对price,length和weight变量进行标准化处理
    egen weightstd=std(weight)
    egen pricestd=std(price)
    egen lengthstd=std(length)

    *--其次生成变量weight标准化后的二次项
    gen weightstd2=weightstd^2

    *--新的回归模型
    quietly regress price weight weight2 mpg headroom length trunk
    outreg2 using jxufe, word replace
    quietly regress pricestd weightstd weightstd2 mpg headroom lengthstd trunk
    outreg2 using jxufe, word 

    *--检验共线性
    estat vif // 不存在


*-3.2 异方差
    *(1)诊断
    sysuse auto.dta, clear
    quietly regress price weight mpg headroom length trunk
    rvfplot // 第一种,观察残差图,如果拟合后的残差分布不均匀,有可能存在异方差。
    estat imtest, white // 第二种,white检验,拒绝同方差原假设,则存在异方差
    estat hettest, iid  // 第三种,BP检验

    *(2)处理
    *--取对数,回归
    gen lprice=log(price)
    gen lweight=log(weight)
    gen lmpg=log(mpg)
    gen lheadroom=log(headroom)
    gen llength=log(length)
    gen ltrunk=log(trunk)

    regress lprice lweight lmpg lheadroom llength ltrunk

    estat vif // 检验共线性
    rvfplot 

    *-取对数前后回归结果对比
    quietly regress price weight mpg headroom length trunk
    rvfplot
    graph save rvf1.gph, replace
    quietly regress lprice lweight lmpg lheadroom llength ltrunk
    rvfplot
    graph save rvf2.gph, replace
    graph combine rvf1.gph rvf2.gph

    *--加入robust选项得到异方差稳健标准误
    regress lprice lweight lmpg lheadroom llength ltrunk, robust

*-3.3 自相关
    *(1)诊断
    sysuse icecream.dta, clear
    tsset time    
    gen temp100=temp/100 // 气温与冰激凌消费单位补贴,除以100使二者尽量接近
    graph twoway connect consumption temp100 time, msymbol(circle) msymbol(square) //connect画连线图,按time展示,msymbol标记,圆形方形。

    regress consumption time price income  //回归时温度变量不用除以100
    predict e1, res // 对模型的残差进行预测,并生成残差的一期滞后项,后续进行自相关检验
    gen e2=L.e1
    graph twoway (scatter e1 e2) (lfit e1 e2)  

    ac e1
    pac e1 // 自相关和偏自相关图,观察样本是否存在自相关,最主要目的,确定序列的ARMA(p,q)的具体形式
    estat bgodfrey // BG检验,也称LM检验

    predict e6, resid
    wntestq e6    // Q检验
    estat dwatson // DW检验。小样本,一阶,现已不多用

    *(2)处理
    newey consumption temp price income, lag(3) // Newdy-West估计,滞后3期。与OLS相比,系数估计值不变,变动的是各项标准误
    newey consumption temp price income, lag(6) // 滞后6期

    prais consumption temp price income, corc   // corc表CO估计即科克伦-奥克特估计,逐次迭代 
    prais consumption temp price income, nolog  // 普莱斯-温斯登估计
    reg consumption temp L.temp price income    // 修改模型设定,增加滞后项必须有理论依据
    estat bgodfrey
    estat dwatson 

*-3.4 可行的GLS
    *(1)异方差及FGLS估计
    sysuse auto.dta, clear
    list  // 非时间序列数据
    quietly regress price weight mpg headroom length trunk  // 直接使用水平值进行OLS回归
    estat hettest, iid // 异方差检验

    predict e1, resid
    gen e2=e1^2
    quietly regress price weight mpg headroom length trunk[aw=1/e2]  //直接使用水平值进行OLS回归
    estat hettest, iid // 异方差检验

    *(2)群组之间的异方差性及GLS估计

    reg y x1 x2 x3     // 首先进行OLS回归
    predict double eps, residual        // 将回归残差的值赋给变量eps,双精度
    robvar eps, by(x4) // 检验残差eps,看其是否同方差,按分组变量x4进行分类,如地区或行业等。原假设:同方差
    by state, sort: egen sd_eps=sd(eps) // sd(eps)表取eps的标准差
    generate double gw_wt=1/sd_eps^2    // 生成一个变量gw_wt为标准差sd_eps平方的倒数
    tabstat sd_eps gw_wt, by(state)     // 列表对比,按state分类
    regress y x1 x2 x3[aw=gw_wt]        // 以gw_wt作为权重,stata将每个变量乘以gw_wt,然后进行回归

    *(3)分组数据的异方差---------*
    reg y x1 x2 x3        // 简单回归,用于对比分析
    reg y x1 x2 x3[aw=x4] // x4代表人数权重,或其他的可能的权重

    *(4)序列相关与GLS估计

    prais y x1 x2 x3, nolog // nolog表示不要显示迭代的记录

    webuse idle, clear
    tsset t //设置时间变量
    summarize
    prais usr idle   // Prais-Winsten AR(1)回归估计
    prais usr idle, corc r // Cochrane-Qrcutt AR(1)估计


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值