应用stata学习计量经济学原理

  • 第九章 时间序列数据回归:平稳变量
    • 本章代码

第九章 时间序列数据回归:平稳变量

本章代码

* setup
version 11.1
capture log close
set more off* dates
clear
set obs 100
generate date = tq(1961q1) + _n-1
list date in 1/5
format %tq date
list date in 1/5
tsset date
save new.dta, replace* open log
log using chap09, replace text
use okun, clear
generate date = tq(1985q2) + _n-1
list date in 1format %tq date
list date in 1tsset datelabel var u "% Unemployed"
label var g "% GDP growth"
tsline u g, lpattern(solid dash)list date u L.u D.u g L1.g L2.g L3.g in 1/5
list date u L.u D.u g L1.g L2.g L3.g in 96/98regress D.u L(0/3).g
regress D.u L(0/2).g summarize g
return listscatter g L.g, xline(`r(mean)') yline(`r(mean)')
ac g, lags(12) generate(ac_g)* approximate z scores
gen z=sqrt(e(N))*ac_g
list ac_g z in 1/12use phillips_aus, clear
generate date = tq(1987q1) + _n-1
format %tq date
tsset datetsline inf
tsline D.ureg inf D.u
predict ehat, resac ehat, lags(12) generate(rk)
list rk in 1/5* --------------------------------------------------
* Corrgram
* --------------------------------------------------
corrgram ehat, lags(5)
di "rho1 = " r(ac1) " rho2 = " r(ac2) " rho3 = " r(ac3)
drop rk ehat* LM tests for AR(1) and AR(4) alternatives
reg inf D.u
predict ehat, res
regress inf D.u L.ehat
test L.ehat
* LM test for AR(1)
quietly regress ehat D.u L.ehat
di "Observations = " e(N) " and TR2 = " e(N)*e(r2)
* LM test for AR(4)
quietly regress ehat D.u L(1/4).ehat
di "Observations = " e(N) " and TR2 = " e(N)*e(r2)
drop ehat* Using the built-in bgodfrey command to test the
* AR(1) and AR(4) alternatives
regress inf D.u
predict ehat, res
estat bgodfrey, lags(1)
estat bgodfrey, lags(4)* Replacing ehat(1) with zero and computing LM
replace ehat = 0 in 1
regress inf D.u L.ehat
test L.ehat
quietly regress ehat D.u L.ehat
di "Observations = " e(N) " and TR2 = " e(N)*e(r2)
drop ehat* Getting Stata to use 90 observations for the LM test
reg inf D.u
predict ehat, res* Using all observations for bgodfrey test
set obs 94                                   // add 3 observations to data
gsort -date                                  // moves missing observations to end
replace date = date[_n-1] - 1 if missing(date) // creates dates for missing obs
replace ehat = 0 if missing(ehat)            // puts zeros in for missing ehats
sort date                                    // re-sort data into ascending order
regress ehat D.u L(1/4).ehat
di "Observations = " e(N) " and TR2 = " e(N)*e(r2)use phillips_aus, clear
generate date = tq(1987q1) + _n-1
format %tq date
tsset datescalar B = round(4*(e(N)/100)^(2/9))
scalar list Bregress inf D.u
estimates store Wrong_SE
newey inf D.u, lag(4)
estimates store HAC_4esttab Wrong_SE HAC_4, compress se(%12.3f) b(%12.5f) gaps ///scalars(r2_a rss aic) title("Dependent Variable: inf") ///mtitles("LS" "HAC(4)")* --------------------------------------------------
* Nonlinear least squares of AR(1) regression model
* --------------------------------------------------nl (inf = {b1}*(1-{rho}) + {b2}*D.u + {rho}*L.inf - {rho}*{b2}*(L.D.u)), /// variables(inf D.u L.inf L.D.u)
* To see the coefficient legend use coeflegend option
nl (inf = {b1}*(1-{rho}) + {b2}*D.u + {rho}*L.inf - {rho}*{b2}*(L.D.u)), /// variables(inf D.u L.inf L.D.u) coeflegend
scalar delta = _b[b1:_cons]*(1-_b[rho:_cons])
scalar delta1 = - _b[rho:_cons]*_b[b2:_cons]* --------------------------------------------------
* More general model
* --------------------------------------------------regress inf L.inf D.u L.D.u
estimates store General
scalar list delta delta1testnl _b[L.D.u]=-_b[L.inf]*_b[D.u]regress inf L.inf D.u
estimates store No_LDuregress inf D.u
estimates store Original
esttab General No_LDu Original, compress se(%12.3f) b(%12.5f) ///gaps scalars(r2_a rss aic) * ARDL
regress inf L.inf L(0/1).D.u
estimates store AR1_DL1
regress inf L.inf D.u
estimates store AR1_DL0
esttab AR1_DL1 AR1_DL0, compress se(%12.3f) b(%12.5f) ///gaps scalars(r2_a rss aic) * Model selection program computes aic and sc
* To remove it from memory use:
* program drop modelsel
capture program drop modelselprogram modelselscalar aic = ln(e(rss)/e(N))+2*e(rank)/e(N) scalar sc = ln(e(rss)/e(N))+e(rank)*ln(e(N))/e(N)scalar obs = e(N)scalar list aic sc obs
endquietly regress inf L.inf L(0/1).D.u
modelsel
quietly regress inf L.inf L.D.u
modelsel* --------------------------------------------------
* Residual correlogram and graph
* --------------------------------------------------quietly regress inf L.inf D.u
predict ehat, res
corrgram ehat, lags(12)
ac ehat, lags(12)
estat bgodfrey, lags(1 2 3 4 5)
drop ehat* Table 9.4 AIC and SC Values for Phillips Curve ARDL model
* Note that regress can be abreviated to reg and quietly to quiquietly reg L(0/1).inf D.u if date>= tq(1988q3)
di "p=1  q=0"
modelsel
quietly regress L(0/2).inf D.u if date>= tq(1988q3)
di "p=2  q=0"
modelsel
quietly regress L(0/3).inf D.u if date>= tq(1988q3)
di "p=3  q=0"
modelsel
quietly regress L(0/4).inf D.u if date>= tq(1988q3)
di "p=4  q=0"
modelsel
quietly regress L(0/5).inf D.u if date>= tq(1988q3)
di "p=5  q=0"
modelsel
quietly regress L(0/6).inf D.u if date>= tq(1988q3)
di "p=6  q=0"
modelselqui reg L(0/1).inf L(0/1).D.u if date>= tq(1988q3)
di "p=1  q=1"
modelsel
qui reg L(0/2).inf L(0/1).D.u if date>= tq(1988q3)
di "p=2  q=1"
modelsel
qui reg L(0/3).inf L(0/1).D.u if date>= tq(1988q3)
di "p=3  q=1"
modelsel
qui reg L(0/4).inf L(0/1).D.u if date>= tq(1988q3)
di "p=4  q=1"
modelsel
qui reg L(0/5).inf L(0/1).D.u if date>= tq(1988q3)
di "p=5  q=1"
modelsel
qui reg L(0/6).inf L(0/1).D.u if date>= tq(1988q3)
di "p=6  q=1"
modelsel* Table 9.4 AIC and SC Values for Phillips Curve ARDL model
* Here is the entire thing again, using nested loops
forvalues q=0/1 {forvalues p=1/6 {quietly regress L(0/`p').inf L(0/`q').D.u if date >= tq(1988q3)display "p=`p'  q=`q'"modelsel}}* Using var to estimate ARDL
* Disadvantage:  No estat after the procedurevar inf in 7/91, lags(1/3) exog(L(0/1).D.u) * ARDL models
use okun, clear
generate date = tq(1985q2) + _n-1
format %tq date
tsset date* Estimate the ARDL(0,2)
* Generate the correlogram and test for autocorrelation
reg D.u L(0/2).g
predict ehat, res
ac ehat, lags(12)
drop ehat
estat bgodfrey, lags(1 2 3 4 5)* Model Selection for Okun's Law model
forvalues q=1/3 {forvalues p=0/2 {quietly regress L(0/`p').D.u L(0/`q').g if date >= tq(1986q1)display "p=`p'  q=`q'"modelsel}}reg D.u L.D.u L(0/1).g
estat bgodfrey* Figure 9.11
reg g L(1/2).g
predict ehat, res
ac ehat, lags(12)* Table 9.6
forvalues p=1/5 {qui reg L(0/`p').g if date> tq(1986q2)display "p=`p'"modelsel}* Forecasting using -arima- instead of -regress-
* which, of course, yields different predictions
arima g, ar(1/2)
tsappend, add(3)
predict ghat, y // for the point estimates
predict ghatse, mse // for the standard error of prediction* Forecasting with an AR modelreg g L(1/2).g
scalar ghat1 = _b[_cons]+_b[L1.g]*g[98]+ _b[L2.g]*g[97]
scalar ghat2 = _b[_cons]+_b[L1.g]*ghat1+ _b[L2.g]*g[98]
scalar ghat3 = _b[_cons]+_b[L1.g]*ghat2+ _b[L2.g]*ghat1
scalar list ghat1 ghat2 ghat3scalar var = e(rmse)^2
scalar se1 = sqrt(var)
scalar se2 = sqrt(var*(1+(_b[L1.g])^2))
scalar se3 = sqrt(var*((_b[L1.g]^2+_b[L2.g])^2+1+_b[L1.g]^2))
scalar list se1 se2 se3scalar f1L = ghat1 - invttail(e(df_r),.025)*se1
scalar f1U = ghat1 + invttail(e(df_r),.025)*se1scalar f2L = ghat2 - invttail(e(df_r),.025)*se2
scalar f2U = ghat2 + invttail(e(df_r),.025)*se2scalar f3L = ghat3 - invttail(e(df_r),.025)*se3
scalar f3U = ghat3 + invttail(e(df_r),.025)*se3scalar list f1L f1U f2L f2U f3L f3U* --------------------------------------------------
* Impact and Delay Multipliers from Okun's ARDL(1,1) model
* --------------------------------------------------regress D.u L.D.u L(0/1).g scalar b0 = _b[g]
scalar b1 = _b[L1.D.u]*b0+_b[L1.g]
scalar b2 = b1*_b[L1.D.u]
scalar list b0 b1 b2  * An alternative method: Exploiting variable creation
regress D.u L.D.u L(0/1).g
gen mult = _b[g] in 1
replace mult = L.mult*_b[L1.D.u]+_b[L1.g] in 2
replace mult = L.mult*_b[L1.D.u] in 3/8
list mult in 1/8
gen lag = _n-1 in 1/8
line mult lag in 1/8* --------------------------------------------------
* Exponential Smoothing
* --------------------------------------------------use okun, clear
generate date = tq(1985q2) + _n-1
format %tq date
tsset datetsappend, add(1)
tssmooth exponential sm1=g, parms(.38)
tsline sm1 g, legend(lab (1 "G") lab(2 "Ghat")) title(alpha=0.38) lpattern(solid dash)
scalar f1 = .38*g[98]+(1-.38)*sm1[98]
scalar list f1
list sm1 in 99tssmooth exponential sm2=g, parms(.8)
tsline sm2 g, legend(lab (1 "G") lab(2 "Ghat")) title(alpha=0.8) lpattern(solid dash)
scalar f2 = .8*g[98]+(1-.8)*sm2[98]
scalar list f2tssmooth exponential sm3=g
scalar f3 = r(alpha)*g[98]+(1-r(alpha))*sm3[98]
scalar list f3
list sm3 in 99program drop modelsel
drop sm1 sm2 sm3 * appendix
* Durbin Watson test
use phillips_aus, clear
generate date = tq(1987q1) + _n-1
format %tq date
tsset dateregress inf D.u
estat dwatson* Prais-Winsten FGLS estimator
prais inf D.u, twostep
estimates store _2step
prais inf D.u
estimates store Iterate
esttab _2step Iterate, compress se(%12.3f) b(%12.5f) gaps scalars(rss rho) ///mtitle("2-step" "Iterated") title("Dependent Variable: inf")* AR(1) using arima
arima inf D.u, ar(1)
log close

stata:第九章 时间序列数据回归:平稳变量相关推荐

  1. 第九章 时序数据-学习笔记+练习题

    第九章 时序数据 # 导入需要的模块 import numpy as np import pandas as pd import matplotlib.pyplot as plt %matplotli ...

  2. VLP-16用户手册 第九章 传感器数据

    第九章 传感器数据 项目用到了VLP-16激光雷达,在看官方源代码时,很多地方不是很清楚,因此查看了用户手册,顺便翻译整理出来.本文翻译自VLP-16用户手册第9章内容,本章是手册的核心,说明了数据的 ...

  3. python使用statsmodels包中的adfuller函数执行增强迪基-福勒检验(ADF检验、augmented Dickey-Fuller test)、判断时间序列数据是否平稳

    python使用statsmodels包中的adfuller函数执行增强迪基-福勒检验(ADF检验.augmented Dickey-Fuller test).判断时间序列数据是否平稳(station ...

  4. 【机器学习实战 第九章】树回归 CART算法的原理与实现 - python3

    本文来自<机器学习实战>(Peter Harrington)第九章"树回归"部分,代码使用python3.5,并在jupyter notebook环境中测试通过,推荐c ...

  5. 读书笔记《Outlier Analysis》 第九章 时间序列和多维流的异常检测

    1.引言 1.1 基本介绍 时间和流数据的异常检测场景出现在许多应用中,如传感器数据.机械故障诊断.医疗数据.网络入侵数据.新闻文本帖子和财务帖子等.在这些问题中,时间连续性假设在识别异常值方面起着至 ...

  6. 第十六章 时间序列数据分析基本概念

    16.1 相关概念 时间序列数据存在的意义即是通过过去预测未, 以下数据都属于时间序列数据: (1)遥感数据: (2)气象数据: (3)金融数据(股票,期货,外汇) (4)经济数据(GDP) 时间序列 ...

  7. 时间序列数据预测的类型

    本文主要内容是使用LSTM网络进行不同类型的时间序列预测任务,不涉及代码,仅仅就不同类型的预测任务和数据划分进行说明. 参考文章:https://machinelearningmastery.com/ ...

  8. python自相关图_Python绘制时间序列数据的时序图、自相关图和偏自相关图

    时序图.自相关图和偏相关图是判断时间序列数据是否平稳的重要依据. 本文涉及的扩展库numpy.pandas.statsmodels一般可以使用pip进行在线安装,如果安装失败,可以到http://ww ...

  9. stata F值缺失_stata面板数据回归操作之GMM

    新手面板数据回归之GMM 的 stata 操作步骤 广义矩估计( Generalized Method of Moments 即 GMM ) 原理就是回归!就是一种高级点的回归! 我也是新手,也有很多 ...

  10. python平稳性检验_时间序列预测基础教程系列(14)_如何判断时间序列数据是否是平稳的(Python)...

    时间序列预测基础教程系列(14)_如何判断时间序列数据是否是平稳的(Python) 发布时间:2019-01-10 00:02, 浏览次数:620 , 标签: Python 导读: 本文介绍了数据平稳 ...

最新文章

  1. python selenium 进入新标签页_Python 爬虫 | 用selenium实现批改网的自动翻译
  2. Python中函数的参数传递方式
  3. 四十三、深入Java中的数组流,数据流和对象流操作
  4. flink读不到kafka数据问题
  5. 上传头像mysql_上传头像 - MVC5+EF6+MySql企业网盘实战 - 爱整理
  6. 贪心算法—建立雷达(POJ 1328)
  7. elk如何同步到es 方案靠谱吗_架构设计:微服务架构如何划分?这6个标准原则让你一目了然...
  8. QT每日一练day25:触发绘画事件
  9. 水晶报表 mysql_水晶报表CrystalReport2008使用JDBC连接MySQL数据库-详细步骤
  10. 宝马与intel合作 2021年推全自动无人驾驶车
  11. python常用的十进制、16进制、字符串、字节串之间的转换
  12. 创造思维方法训练_数学思维方法训练课程:每日一题11.23
  13. 开源WebRTC 服务器介绍
  14. python爬取豆瓣读书界面的书名、作者、价格、导入数据库_python爬虫:利用正则表达式爬取豆瓣读书首页的book...
  15. 国产光刻机再次斩获新订单,业绩大幅衰退的ASML如遭雷击
  16. 欧拉回路专题 POJ - 1637网络流+混合图的欧拉回路
  17. java newline_Java BufferedWriter newLine()方法
  18. 在线工具:电脑怎么提取图片中的文字?图片如何转化为文字?
  19. 朱一龙左娜扎右热巴,王栎鑫:我想活成你的样子
  20. 吉林大学计算机伦理学,王爱民-吉林大学计算机科学与技术学院

热门文章

  1. layuiadmin开发文档
  2. 深入浅出实现Electron音量调节功能
  3. 软件在线升级系统的设计与实现
  4. 安卓 VNET 抓取 wskey 教程
  5. 2018美国计算机科学专业排名,最新出炉 2018年USNews美国大学研究生计算机科学专业排名榜单...
  6. Launcher中动态加载APK出现java.lang.SecurityException异常的解决方法(二)
  7. 中国未来5年最“吃香”的4大行业,市场需求大,堪比铁饭碗!
  8. 一款实用工具:免费短信验证码SDK
  9. Oracle表中新增字段
  10. Jenkins | 搭建你第一个Jenkins应用